VICI  0.11.815
Visual Chart Interpreter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
plugin.h
Go to the documentation of this file.
1 /*
2  plugin.h
3 
4  Copyright 2012 - 2016 Brenton Ross
5 
6 This file is part of VICI.
7 
8 VICI is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 VICI is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with VICI. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
32 #ifndef CFI_PLUGIN_H
33 #define CFI_PLUGIN_H
34 
35 #include "stringy.h"
36 #include <map>
37 #include <list>
38 #include <memory>
39 #include <iostream>
40 
41 extern "C"
42 {
44 
48  void initViciPlugin();
49 
51 
55  void closeViciPlugin();
56 }
57 
59 #define VICI_PLUGIN_VERSION "0.1"
60 
61 namespace VICI
62 {
63 namespace cfi
64 {
65 
66 class PlugInLib;
67 
69 typedef std::shared_ptr<PlugInLib> PlugInLibPtr;
70 
72 
76 class PlugIn
77 {
78 private:
79  friend class PlugInMgr;
80  void setLib( PlugInLibPtr L);
81 private:
82  PlugInLibPtr lib;
83 public:
85  virtual ~PlugIn();
86 };
87 
89 
92 class AutoRunPlugIn : public PlugIn
93 {
94 public:
96  virtual void execute() = 0;
97 };
98 
100 typedef std::unique_ptr< AutoRunPlugIn > AutoRunPlugInPtr;
101 
103 
107 {
108 public:
110  virtual ~PlugInFactory(){}
111 
113 
116  virtual PlugIn * make() = 0;
117 };
118 
120 
127 template < class F >
129 {
130 public:
131  virtual F * make() = 0;
132 };
133 
134 
136 
141 template < class F, class P >
143 {
144 public:
146  virtual ~PlugInFactoryT(){}
147  virtual F * make() { return new P; }
148 };
149 
152 {
153  std::string pluginFamily;
154  std::string name;
155  std::string description;
156  std::string version;
158  bool onDemand;
159  bool autoRun;
160 };
161 
164 {
166  bool loadable;
167 
169  PlugInDetails();
170 };
171 
173 
183 {
184 public:
186  enum Mode{
190  };
191 
192 private:
193  void * libHandle;
194  VICI::Path path;
195  int usageCounter;
196  Mode mode;
197 
198  bool initLib();
199  void closeLib();
200 public:
202 
205  PlugInLib( const Path & libPath );
206 
208  ~PlugInLib();
209 
211 
217  void setMode( Mode m);
218 
220 
223  Mode getMode() { return mode; }
224 
226 
229  bool loaded();
230 
232  void startUsage();
233 
235  void endUsage();
236 
238 
241  bool inUse() { return usageCounter > 0; }
242 
244 
247  const VICI::Path & getPath() { return path; }
248 };
249 
251 
255 {
256 private:
257  // created with load(), removed when mgr destroyed
258  std::map< std::string, PlugInDetails > plugins; // owned
259 
260  // mode == autorun ==> created by load, removed by load
261  // mode == demand ==> created by getPlugin, removed when no longer needed
262  // mode == load ==> created by load, removed when mgr destroyed
263  std::map< Path, std::shared_ptr< PlugInLib > > libs; // owned
264 
265  // libs wait here while unused
266  std::list< PlugInLib * > pending; // references
267 
268  bool loadLibrary( const Path &, PlugInLib::Mode );
269  void getDetails(csr);
270 public:
272  static PlugInMgr & instance();
273 
275 
278  void load(csr prog);
279 
281  void regn( const PlugInDetails & details );
282 
284  void getDetails( std::vector< PlugInDescriptor > & plugins );
285 
287  void release( PlugInLib * );
288 
290  void shutdown();
291 
293  template < class PlugInFamily >
294  std::unique_ptr< PlugInFamily > getPlugin( csr name )
295  {
296  // make sure we know the name
297  auto P = plugins.find( name );
298  if ( P == plugins.end() ) return nullptr;
299  if ( ! P->second.loadable ) return nullptr;
300 
301  // if necessary load the library
302  auto L = libs.find(P->second.library);
303  if ( L == libs.end() || ! L->second->loaded())
304  {
305  // std::cout << "Loading library " << name << std::endl;
307  if( P->second.autoRun) mode = PlugInLib::AutoRun;
308  else if ( P->second.onDemand ) mode = PlugInLib::OnDemand;
309 
310  if ( ! loadLibrary( P->second.library, mode ) )
311  {
312  P->second.loadable = false;
313  // std::cout << "Unable to load library" << std::endl;
314  return nullptr;
315  }
316  L = libs.find(P->second.library);
317  }
318  // std::cout << "Loaded library" << std::endl;
319 
320  // get the factory from the library
322  = dynamic_cast< PlugInFamilyFactoryT<PlugInFamily> * >( P->second.factory );
323  if ( pif == 0) return nullptr;
324  // std::cout << "Got the factory" << std::endl;
325 
326  // construct the object
327  std::unique_ptr<PlugInFamily> up(pif->make() );
328  up->setLib(L->second);
329  return up;
330  }
331 };
332 
333 
334 } // namespace cfi
335 } // namespace VICI
336 
337 
338 #endif /* INCLUDE_PLUGIN_H_ */
virtual ~PlugInFactory()
Destructor.
Definition: plugin.h:110
void regn(const PlugInDetails &details)
Allow a plug-in to register itself.
Definition: plugin.cpp:197
std::unique_ptr< AutoRunPlugIn > AutoRunPlugInPtr
Unique pointer to an AutoRunPlugIn.
Definition: plugin.h:100
void initViciPlugin()
Initialise the plugin.
std::unique_ptr< PlugInFamily > getPlugin(csr name)
Get a plug-in.
Definition: plugin.h:294
Mode getMode()
Get the mode for the entire library.
Definition: plugin.h:223
bool onDemand
True if the library is loaded on demand.
Definition: plugin.h:158
virtual F * make()
Construct a plug-in object.
Definition: plugin.h:147
void closeViciPlugin()
Shutdown the plugin.
The library can be removed after initialisation.
Definition: plugin.h:187
Descriptor for plug-ins that can be used by the application.
Definition: plugin.h:151
Useful string functions.
Base class for plug ins that are run immediately that the library is loaded.
Definition: plugin.h:92
Template base class for families of plug-in factories.
Definition: plugin.h:128
PlugInFactory * factory
Construct an instance of the plug-in.
Definition: plugin.h:165
void shutdown()
Unload all the libraries prior to exiting the program.
Definition: plugin.cpp:249
std::string description
Some description for users.
Definition: plugin.h:155
virtual PlugIn * make()=0
Construct a plug-in object.
static PlugInMgr & instance()
Get a reference to the singleton object.
Definition: plugin.cpp:67
Base class for factories that create plug-in objects.
Definition: plugin.h:106
Base class for objects loaded from dynamically loaded libraries.
Definition: plugin.h:76
bool inUse()
Test if the library is in use.
Definition: plugin.h:241
Manage the handling of plug-in shared libraries.
Definition: plugin.h:254
Manipulate path strings.
Definition: stringy.h:74
virtual ~PlugInFactoryT()
Destructor.
Definition: plugin.h:146
virtual F * make()=0
Construct a plug-in object.
The library can be removed once its unused.
Definition: plugin.h:188
~PlugInLib()
Destructor.
Definition: plugin.cpp:291
Template class for factories.
Definition: plugin.h:142
bool loadable
False if the library cannot be loaded.
Definition: plugin.h:166
Mode
Control lifetime of the library.
Definition: plugin.h:186
void setMode(Mode m)
Set the lifetime for a plug-in.
Definition: plugin.cpp:310
void load(csr prog)
Load the plug-ins listed in the xini config.
Definition: plugin.cpp:123
void startUsage()
Indicate that the library is being used.
Definition: plugin.cpp:356
VICI::Path library
The shared library&#39;s path.
Definition: plugin.h:157
const VICI::Path & getPath()
Get the path to the library.
Definition: plugin.h:247
std::string name
The name as in the xini config.
Definition: plugin.h:154
PlugInDetails()
Constructor to initialise variables.
Definition: plugin.cpp:37
virtual void execute()=0
Called by the PlugInMgr to run the plug-in&#39;s code.
const std::string & csr
short cut for string constants
Definition: vici.h:80
void release(PlugInLib *)
Release a library.
Definition: plugin.cpp:221
std::shared_ptr< PlugInLib > PlugInLibPtr
Shared pointer to PlugInLib.
Definition: plugin.h:66
Details of plug-ins as provided by the loaded library.
Definition: plugin.h:163
std::string version
Should match the defined VICI_PLUGIN_VERSION.
Definition: plugin.h:156
std::string pluginFamily
The type of the plug-in.
Definition: plugin.h:153
virtual ~PlugIn()
Destructor.
Definition: plugin.cpp:49
void endUsage()
Advise that a plug-in is no longer using the library.
Definition: plugin.cpp:363
The library is only removed on program termination.
Definition: plugin.h:189
bool loaded()
Check if successfully loaded the library.
Definition: plugin.cpp:318
PlugInLib(const Path &libPath)
Constructor.
Definition: plugin.cpp:275
bool autoRun
True if its run automatically on start.
Definition: plugin.h:159
Manages an instance of a dynamically loaded shared library.
Definition: plugin.h:182