Wallaroo  0.8
class.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * wallaroo - A library for configurable creation and wiring of C++ classes.
3  * Copyright (C) 2012 Daniele Pallastrelli
4  *
5  * This file is part of wallaroo.
6  * For more information, see http://wallaroolib.sourceforge.net/
7  *
8  * Boost Software License - Version 1.0 - August 17th, 2003
9  *
10  * Permission is hereby granted, free of charge, to any person or organization
11  * obtaining a copy of the software and accompanying documentation covered by
12  * this license (the "Software") to use, reproduce, display, distribute,
13  * execute, and transmit the Software, and to prepare derivative works of the
14  * Software, and to permit third-parties to whom the Software is furnished to
15  * do so, all subject to the following:
16  *
17  * The copyright notices in the Software and this entire statement, including
18  * the above license grant, this restriction and the following disclaimer,
19  * must be included in all copies of the Software, in whole or in part, and
20  * all derivative works of the Software, unless such copies or derivative
21  * works are solely in the form of machine-executable object code generated by
22  * a source language processor.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
27  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
28  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
29  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30  * DEALINGS IN THE SOFTWARE.
31  ******************************************************************************/
32 
33 #ifndef WALLAROO_CLASS_H_
34 #define WALLAROO_CLASS_H_
35 
36 #include <string>
37 #include "detail/factory.h"
38 #include "cxx0x.h"
39 #include "part.h"
40 
41 namespace wallaroo
42 {
43 
44 // forward declarations
45 class Plugin;
46 
53 template < class P1, class P2 >
54 class Class
55 {
56  public :
57 
58  typedef cxx0x::shared_ptr< Part > Ptr;
59  typedef cxx0x::function< Ptr( const P1& p1, const P2& p2 ) > FactoryMethod;
60 
67  Ptr NewInstance( const P1& p1, const P2& p2 ) const
68  {
69  if( fm )
70  return( fm( p1, p2 ) ) ;
71  else
72  return( Ptr() ) ;
73  }
74 
77  static Class ForName( const std::string& name )
78  {
79  typename Classes::const_iterator i = Registry().find( name );
80  if ( i != Registry().end() )
81  return i -> second;
82  return Class(); // default value
83  }
84  private :
85  FactoryMethod fm;
86  typedef cxx0x::unordered_map< std::string, Class< P1, P2 > > Classes;
87  template < class T, class T1, class T2 > friend class Registration;
88  static void Register( const std::string& s, const FactoryMethod& m )
89  {
90  Registry().insert( std::make_pair( s, Class( m ) ) );
91  }
92  static Classes& Registry()
93  {
94  static Classes registry;
95  return registry;
96  }
97  Class()
98  {
99  }
100  Class( FactoryMethod m ) :
101  fm( m )
102  {
103  }
104 };
105 
112 template < class P >
113 class Class< P, void >
114 {
115  public :
116 
117  typedef cxx0x::shared_ptr< Part > Ptr;
118  typedef cxx0x::function< Ptr( const P& p ) > FactoryMethod;
119 
125  Ptr NewInstance( const P& p ) const
126  {
127  if( fm )
128  return( fm( p ) ) ;
129  else
130  return( Ptr() ) ;
131  }
132 
135  static Class ForName( const std::string& name )
136  {
137  typename Classes::const_iterator i = Registry().find( name );
138  if ( i != Registry().end() )
139  return i -> second ;
140  return Class(); // default value
141  }
142  private :
143  FactoryMethod fm;
144  typedef cxx0x::unordered_map< std::string, Class< P, void > > Classes;
145  template < class T, class T1, class T2 > friend class Registration;
146  static void Register( const std::string& s, const FactoryMethod& m )
147  {
148  Registry().insert( std::make_pair( s, Class( m ) ) );
149  }
150  static Classes& Registry()
151  {
152  static Classes registry;
153  return registry;
154  }
155  Class()
156  {
157  }
158  Class( FactoryMethod m ) :
159  fm( m )
160  {
161  }
162 };
163 
164 
171 template <>
172 class Class< void, void >
173 {
174  public :
175 
176  typedef cxx0x::shared_ptr< Part > Ptr;
177  typedef cxx0x::function< Ptr() > FactoryMethod;
178 
183  Ptr NewInstance() const
184  {
185  if( fm )
186  {
187  Ptr p = fm();
188  p -> Source( plugin ); // set the ref count to shared library
189  return p;
190  }
191  else
192  return( Ptr() );
193  }
194 
197  static Class ForName( const std::string& name )
198  {
199  Classes::const_iterator i = Registry().find( name );
200  if ( i != Registry().end() )
201  return i -> second ;
202  return Class(); // default value
203  }
204  private :
205  FactoryMethod fm;
206  cxx0x::shared_ptr< Plugin > plugin; // optional shared ptr to plugin, to release the shared library when is no more used
207  typedef cxx0x::unordered_map< std::string, Class< void, void > > Classes;
208  template < class T, class T1, class T2 > friend class Registration;
209  friend class Plugin;
210  static void Register( const std::string& s, const FactoryMethod& m )
211  {
212  Registry().insert( std::make_pair( s, Class( m ) ) );
213  }
214  static void Register( const std::string& s, const FactoryMethod& m, const cxx0x::shared_ptr< Plugin >& plugin )
215  {
216  Registry().insert( std::make_pair( s, Class( m, plugin ) ) );
217  }
218  static Classes& Registry()
219  {
220  static Classes registry;
221  return registry;
222  }
223  Class()
224  {
225  }
226  Class( FactoryMethod m ) :
227  fm( m )
228  {
229  }
230  Class( FactoryMethod m, const cxx0x::shared_ptr< Plugin >& p ) :
231  fm( m ),
232  plugin( p )
233  {
234  }
235 };
236 
237 
244 template < class T, class P1 = void, class P2 = void >
246 {
247 public:
248  Registration( const std::string& name )
249  {
251  Class< P1, P2 >::Register( name, fm );
252  }
253 };
254 
255 } // wallaroo namespace
256 
257 #endif // WALLAROO_CLASS_H_
cxx0x::shared_ptr< Part > Ptr
Definition: class.h:176
Definition: class.h:245
Ptr NewInstance(const P &p) const
Definition: class.h:125
cxx0x::function< Ptr() > FactoryMethod
Definition: class.h:177
Registration(const std::string &name)
Definition: class.h:248
Definition: factory.h:47
Ptr NewInstance() const
Definition: class.h:183
Ptr NewInstance(const P1 &p1, const P2 &p2) const
Definition: class.h:67
cxx0x::shared_ptr< Part > Ptr
Definition: class.h:58
cxx0x::function< Ptr(const P &p) > FactoryMethod
Definition: class.h:118
static Class ForName(const std::string &name)
Definition: class.h:135
static Class ForName(const std::string &name)
Definition: class.h:77
Definition: dynamic_loader.h:60
cxx0x::function< Ptr(const P1 &p1, const P2 &p2) > FactoryMethod
Definition: class.h:59
cxx0x::shared_ptr< Part > Ptr
Definition: class.h:117
Definition: class.h:54
Definition: attribute.h:45
static Class ForName(const std::string &name)
Definition: class.h:197