Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Plugin.h
1 /*
2  * Plugin class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2005-12, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_SYS_PLUGIN_H
22 #define ELM_SYS_PLUGIN_H
23 
24 #include <elm/macros.h>
25 #include <elm/types.h>
26 #include <elm/string.h>
27 #include <elm/data/List.h>
28 #include <elm/data/Vector.h>
29 #include <elm/util/Version.h>
30 #include <elm/sys/Path.h>
31 #include "../data/Array.h"
32 
33 namespace elm { namespace sys {
34 
35 #define ELM_PLUGIN_ID_PREFIX "@@ELM-PLUGIN-ID@@:"
36 #define ELM_PLUGIN_CONCAT(x, y) ELM_CONCAT_AUX(x, y)
37 #define ELM_PLUGIN_ID(hook, info) const char *ELM_PLUGIN_CONCAT(__plugin_id_, __LINE__)() { return ELM_PLUGIN_ID_PREFIX hook ":" info; }
38 #define ELM_PLUGIN(plugin, hook) extern "C" { \
39  ELM_PLUGIN_ID(#hook, #plugin); \
40  elm::sys::Plugin *ELM_PLUGIN_CONCAT(hook, _fun)(void) { return static_cast<elm::sys::Plugin *>(&plugin); } }
41 
42 // Plugin class
43 class Plugin {
44  t::uint32 magic;
45 public:
46  static const t::uint32 MAGIC = 0xCAFEBABE;
48 
49  class make {
50  friend class Plugin;
51  public:
52  inline make(string name, const Version& plugger_version)
53  : _name(name), _plugger_version(plugger_version) { }
54  inline make& description(cstring d) { _description = d; return *this; }
55  inline make& license(cstring l) { _license = l; return *this; }
56  inline make& version(const Version& v) { _plugin_version = v; return *this; }
57  inline make& version(int major, int minor, int release = 0) { _plugin_version = Version(major, minor, release); return *this; }
58  inline make& alias(string a) { aliases.add(a); return *this; }
59  inline make& hook(cstring h) { _hook = h; return *this; }
60  private:
61  string _name;
62  cstring _description, _license, _hook;
63  Version _plugin_version, _plugger_version;
64  Vector<string> aliases;
65  };
66 
67  Plugin(string name, const Version& plugger_version, CString hook = "", const aliases_t& aliases = aliases_t::null);
68  Plugin(const make& maker);
69  virtual ~Plugin(void);
70  inline string name(void) const { return _name; }
71  inline CString description(void) const;
72  inline CString licence(void) const;
73  inline const Version& pluginVersion(void) const;
74  inline const Version& pluggerVersion(void) const;
75  inline CString hook(void) const;
76  inline const aliases_t& aliases(void) const;
77  bool matches(const string& name) const;
78  void unplug(void);
79  inline const Path& path(void) const { return _path; }
80 
82  inline DepIter dependencies(void) const { return DepIter(deps); }
83 
84 protected:
85  virtual void startup(void);
86  virtual void cleanup(void);
87 
91 
92 private:
93  void plug(void *handle);
94  static void step(void);
95  static Plugin *get(cstring hook, const string& name);
96  void setPath(const Path& path) { _path = path; }
97 
98  friend class Plugger;
99  static Vector<Plugin *> static_plugins;
100  static bool static_done;
101  static Vector<Plugin *> unused_plugins;
102  CString _hook;
103  string _name;
104  Version per_vers;
105  void *_handle;
106  int state;
107  AllocArray<string> _aliases;
108  Path _path;
109  List<Plugin *> deps;
110 };
111 
112 // Inlines
113 inline CString Plugin::description(void) const {
114  return _description;
115 }
116 
117 inline CString Plugin::licence(void) const {
118  return _licence;
119 }
120 
121 inline const Version& Plugin::pluginVersion(void) const {
122  return _plugin_version;
123 }
124 
125 inline const Version& Plugin::pluggerVersion(void) const {
126  return per_vers;
127 }
128 
129 inline CString Plugin::hook(void) const {
130  return _hook;
131 }
132 
133 inline const Plugin::aliases_t& Plugin::aliases(void) const {
134  return _aliases;
135 }
136 
137 } } // elm::sys
138 
139 #endif // ELM_SYS_PLUGIN_H
elm::sys::Plugin::name
string name(void) const
Definition: Plugin.h:70
elm::sys::Plugin::DepIter
List< Plugin * >::Iter DepIter
Definition: Plugin.h:81
elm::sys::Plugin::hook
CString hook(void) const
Definition: Plugin.h:129
elm::sys::Path
Definition: Path.h:33
elm::sys::Plugin::make::make
make(string name, const Version &plugger_version)
Definition: Plugin.h:52
elm::sys::Plugin::licence
CString licence(void) const
Definition: Plugin.h:117
elm::sys::Plugin
Definition: Plugin.h:43
elm::sys::Plugin::pluginVersion
const Version & pluginVersion(void) const
Definition: Plugin.h:121
elm::sys::Plugin::dependencies
DepIter dependencies(void) const
Definition: Plugin.h:82
elm::List::Iter
Definition: List.h:64
elm::sys::Plugin::unplug
void unplug(void)
Definition: system_Plugin.cpp:353
elm::sys::Plugin::aliases
const aliases_t & aliases(void) const
Definition: Plugin.h:133
elm::CString
Definition: CString.h:17
elm::Version
Definition: Version.h:32
elm
Definition: adapter.h:26
elm::sys::Plugin::path
const Path & path(void) const
Definition: Plugin.h:79
elm::Vector::add
void add(const T &v)
Definition: Vector.h:101
elm::Vector< string >
elm::sys::Plugin::aliases_t
Array< string > aliases_t
Definition: Plugin.h:47
elm::sys::Plugin::make::license
make & license(cstring l)
Definition: Plugin.h:55
elm::sys::Plugin::make::description
make & description(cstring d)
Definition: Plugin.h:54
elm::sys::Plugin::MAGIC
static const t::uint32 MAGIC
Definition: Plugin.h:46
elm::sys::Plugin::_plugin_version
Version _plugin_version
Definition: Plugin.h:90
elm::sys::Plugin::startup
virtual void startup(void)
Definition: system_Plugin.cpp:380
elm::sys::Plugin::make::alias
make & alias(string a)
Definition: Plugin.h:58
elm::sys::Plugin::make::version
make & version(int major, int minor, int release=0)
Definition: Plugin.h:57
elm::t::uint32
unsigned int uint32
Definition: arch.h:31
elm::t::get
ret< T > get(const var< T > &v)
Definition: type_info.h:288
elm::sys::Plugin::make::hook
make & hook(cstring h)
Definition: Plugin.h:59
elm::AllocArray
Definition: Array.h:117
elm::sys::Plugin::_licence
CString _licence
Definition: Plugin.h:89
elm::sys::Plugger
Definition: Plugger.h:36
elm::sys::Plugin::matches
bool matches(const string &name) const
Definition: system_Plugin.cpp:472
elm::Array
Definition: Array.h:32
elm::sys::Plugin::Plugin
Plugin(string name, const Version &plugger_version, CString hook="", const aliases_t &aliases=aliases_t::null)
Definition: system_Plugin.cpp:262
elm::sys::Plugin::make::version
make & version(const Version &v)
Definition: Plugin.h:56
elm::sys::Plugin::~Plugin
virtual ~Plugin(void)
Definition: system_Plugin.cpp:307
elm::sys::Plugin::pluggerVersion
const Version & pluggerVersion(void) const
Definition: Plugin.h:125
elm::sys::Plugin::description
CString description(void) const
Definition: Plugin.h:113
elm::List
Definition: List.h:34
elm::sys::Plugin::make
Definition: Plugin.h:49
elm::Array::null
static const Array< T > null
Definition: Array.h:65
elm::sys::Plugin::cleanup
virtual void cleanup(void)
Definition: system_Plugin.cpp:388
elm::sys::Plugin::_description
CString _description
Definition: Plugin.h:88