aboutsummaryrefslogtreecommitdiff
path: root/packages/noncvs/windows/extra/Gem/dev/plugins/PluginFactory.h
blob: 87ea7cf325dbbd73239b76ed28ef067232084846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef _INCLUDE__GEM_PLUGINS_PLUGINFACTORY_H_
#define _INCLUDE__GEM_PLUGINS_PLUGINFACTORY_H_

#include "Gem/ExportDef.h"


#include <map>
#include <vector>
#include <string>

#include <typeinfo>
#include <iostream>

namespace gem {

  class GEM_EXTERN BasePluginFactory {
  public:
    int doLoadPlugins(std::string basename, std::string path);
  protected:
    BasePluginFactory();
    virtual ~BasePluginFactory(void);

    std::vector<std::string>get(void);
    void*get(std::string);
    void set(std::string, void*);

  private:
    class Pimpl;
    Pimpl*m_pimpl;
  };

  template<class Class>
    class GEM_EXPORT PluginFactory : public BasePluginFactory {
  public:

    /** 
     * constructor function type (without arguments)
     */
    typedef Class*(ctor_t)(void);

    /**
     * register a a constructor associated with a given ID
     */
    static void registerClass(std::string id, ctor_t*c);
    /**
     * get an instance of class constructed by the constructor associated with the given ID
     */
    static Class*getInstance(std::string id);

    /**
     * get a list of all IDs currently registered with this factory
     */
    static std::vector<std::string>getIDs(void);

    /**
     * load more plugins
     */
    static int loadPlugins(std::string basename, std::string path=std::string(""));

  private:
    static PluginFactory<Class>*s_factory;
    static PluginFactory<Class>*getPluginFactory();

    void doRegisterClass(std::string id, ctor_t*c);
    Class*doGetInstance(std::string id);
    std::vector<std::string>doGetIDs(void);
  };


  namespace PluginFactoryRegistrar {
    /**
     * creates a new ChildClass and returns it as a (pointer to) an instance of BaseClass
     */
    template<class ChildClass, class BaseClass>
      static BaseClass* allocator(void);

    /**
     * registers a ChildClass with a certain ID in the BaseClass factory
     *
     * example:
     *  static gem::PluginFactoryRegistrar<Child, Base, std::string > basefac_childreg("childID"); // register Child as 'childID'
     *  Base*instance=gem::PluginFactory<Base>::getInstance("childID"); // returns an instance of Child
     */
    template<class ChildClass, class BaseClass>
      struct registrar {
        registrar(std::string ID);
      };

    /**
     * registers a dummy constructor with a default ID
     */
    template<class BaseClass>
      struct dummy {
	dummy(void);
      };
  };
  
/* include the actual implementation */
#include "PluginFactoryTimple.h"


}; // namespace gem


#endif /* _INCLUDE__GEM_PLUGINS_PLUGINFACTORY_H_ */