Util--  1.1
plugin_factory.hh
Go to the documentation of this file.
1 // Copyright Vladimir Prus 2004.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef BOOST_PLUGIN_FACTORY_VP_2004_08_25
7 #define BOOST_PLUGIN_FACTORY_VP_2004_08_25
8 
10 #include <utilmm/plugin/dll.hh>
12 #include <utility>
13 #include <stdexcept>
14 #include <string>
15 #include <utility>
16 
17 namespace utilmm { namespace plugin {
18 
19  namespace {
20  typedef std::map<std::string, boost::any> exported_plugins_t;
21  typedef exported_plugins_t& (*get_plugins_list_t)();
22  typedef exported_plugins_t& (get_plugins_list_np)();
23 
24  template<class BasePlugin>
25  std::pair<abstract_factory<BasePlugin>*,
26  dll_handle >
27  get_abstract_factory(const dll& d, const std::string& klass)
28  {
29  boost::shared_ptr<get_plugins_list_np> f;
30  f = d.template get<exported_plugins_t& (*)()>("boost_exported_plugins_list");
31  exported_plugins_t& e = (*f)();
32 
33  if (e.count(klass)) {
34 
35  abstract_factory<BasePlugin>** xw =
36  boost::any_cast<abstract_factory<BasePlugin>*>(&(e[klass]));
37 
38  if (!xw) {
39  throw std::logic_error("Can't cast to the right factor type\n");
40  }
41  abstract_factory<BasePlugin>* w = *xw;
42  return make_pair(w, f);
43  } else {
44  throw std::logic_error("Class of the specified name is not found");
45  }
46  }
47 
48  }
49 
50 
51 
53  void create(int****);
54  protected:
56  };
57 
58  template<class BasePlugin, class Base, class Parameters>
60  };
61 
62  template<class BasePlugin, class Base>
63  struct plugin_factory_item<BasePlugin, Base, boost::mpl::list<> > : public Base {
64  using Base::create;
65  BasePlugin* create(const std::string& name)
66  {
67  std::pair<abstract_factory<BasePlugin>*,
68  dll_handle > r = get_abstract_factory<BasePlugin>(this->m_dll, name);
69  return r.first->create(r.second);
70  }
71  };
72 
73  template<class BasePlugin, class Base, class A1>
74  struct plugin_factory_item<BasePlugin, Base, boost::mpl::list<A1> > : public Base {
75  using Base::create;
76  BasePlugin* create(const std::string& name, A1 a1)
77  {
78  std::pair<abstract_factory<BasePlugin>*,
79  dll_handle > r = get_abstract_factory<BasePlugin>(this->m_dll, name);
80  return r.first->create(r.second, a1);
81  }
82  };
83 
84  template<class BasePlugin, class Base, class A1, class A2>
85  struct plugin_factory_item<BasePlugin, Base, boost::mpl::list<A1, A2> > : public Base {
86  using Base::create;
87  BasePlugin* create(const std::string& name, A1 a1, A2 a2)
88  {
89  std::pair<abstract_factory<BasePlugin>*,
90  dll_handle > r = get_abstract_factory<BasePlugin>(this->m_dll, name);
91  return r.first->create(r.second, a1, a2);
92  }
93  };
94 
95 #if 0
96 
97  template<class BasePlugin, class Base, class Parameters>
98  struct plugin_factory_item
99  : public Base,
100  public plugin_factory_item_N<BasePlugin, Parameters>
101 
102  {
103  using Base::create;
104  using plugin_factory_item_N<BasePlugin, Parameters>::create;
105  };
106 #endif
107 
108  using namespace boost::mpl::placeholders;
109 
110  template<class BasePlugin>
111  struct plugin_factory :
112  public boost::mpl::inherit_linearly<
113  typename virtual_constructors<BasePlugin>::type,
114  plugin_factory_item<BasePlugin, _, _>,
115  empty_plugin_factory_item>::type
116  {
117  plugin_factory(const dll& d)
118  {
119  this->m_dll = d;
120  }
121  };
122 
123 }}
124 
125 #endif
Definition: undirected_graph.hh:345
boost::shared_ptr< std::map< std::string, boost::any > &()> dll_handle
Definition: virtual_constructors.hh:19
Definition: dll.hh:34
Definition: plugin_factory.hh:59
Definition: plugin_factory.hh:52
BasePlugin * create(const std::string &name, A1 a1)
Definition: plugin_factory.hh:76
BasePlugin * create(const std::string &name)
Definition: plugin_factory.hh:65
Definition: plugin_factory.hh:111
dll m_dll
Definition: plugin_factory.hh:55
Definition: auto_flag.hh:6
plugin_factory(const dll &d)
Definition: plugin_factory.hh:117
BasePlugin * create(const std::string &name, A1 a1, A2 a2)
Definition: plugin_factory.hh:87

Generated on Mon Oct 8 2018 08:38:45 for Util-- by doxygen 1.8.11
SourceForge.net Project Page