Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Plugin.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * Plugin.h
17  * Header file for plugin class - plugins inherit from this.
18  * Copyright (C) 2005-2008 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLAD_PLUGIN_H_
22 #define INCLUDE_OLAD_PLUGIN_H_
23 
24 #include <ola/base/Macro.h>
25 #include <ola/plugin_id.h>
26 
27 #include <set>
28 #include <string>
29 #include <functional>
30 
31 namespace ola {
32 
33 class PluginAdaptor;
34 
35 /*
36  * The interface for a plugin
37  */
39  public :
40  AbstractPlugin() {}
41  virtual ~AbstractPlugin() {}
42 
43  // load the preferences for a plugin
44  virtual bool LoadPreferences() = 0;
45  // The location for preferences. This can be anything really but should
46  // indicate to the user how how the preferences were loaded.
47  virtual std::string PreferenceSource() const = 0;
48  // true if this plugin is enabled
49  virtual bool IsEnabled() const = 0;
50  // start the plugin
51  virtual bool Start() = 0;
52  // stop the plugin
53  virtual bool Stop() = 0;
54  // return the plugin_id of this plugin
55  virtual ola_plugin_id Id() const = 0;
56  // return the name of this plugin
57  virtual std::string Name() const = 0;
58  // return the description of this plugin
59  virtual std::string Description() const = 0;
60 
61  virtual void ConflictsWith(std::set<ola_plugin_id> *conflict_set) = 0;
62 
63  // used to sort plugins
64  virtual bool operator<(const AbstractPlugin &other) const = 0;
65 };
66 
67 
68 struct PluginLessThan: public std::binary_function<AbstractPlugin*,
69  AbstractPlugin*, bool> {
70  bool operator()(AbstractPlugin *x, AbstractPlugin *y) {
71  return x->Id() < y->Id();
72  }
73 };
74 
75 
76 class Plugin: public AbstractPlugin {
77  public :
78  explicit Plugin(PluginAdaptor *plugin_adaptor):
80  m_plugin_adaptor(plugin_adaptor),
81  m_preferences(NULL),
82  m_enabled(false) {
83  }
84  virtual ~Plugin() {}
85 
86  bool LoadPreferences();
87  std::string PreferenceSource() const;
88  bool IsEnabled() const;
89  virtual bool Start();
90  virtual bool Stop();
91  // return true if this plugin is enabled by default
92  virtual bool DefaultMode() const { return true; }
93  virtual ola_plugin_id Id() const = 0;
94 
95  // return the prefix used to identify this plugin
96  virtual std::string PluginPrefix() const = 0;
97 
98  // by default we don't conflict with any other plugins
99  virtual void ConflictsWith(std::set<ola_plugin_id>*) {}
100 
101  bool operator<(const AbstractPlugin &other) const {
102  return Id() < other.Id();
103  }
104 
105  protected:
106  virtual bool StartHook() { return 0; }
107  virtual bool StopHook() { return 0; }
108  virtual bool SetDefaultPreferences() { return true; }
109 
110  PluginAdaptor *m_plugin_adaptor;
111  class Preferences *m_preferences; // preferences container
112  static const char ENABLED_KEY[];
113 
114  private:
115  bool m_enabled; // are we running
116 
118 };
119 } // namespace ola
120 
121 // interface functions
122 typedef ola::AbstractPlugin* create_t(ola::PluginAdaptor *plugin_adaptor);
123 
124 #endif // INCLUDE_OLAD_PLUGIN_H_