Util--  1.1
factory.hh
Go to the documentation of this file.
1 /* -*- C++ -*-
2  * $Id: factory.hh 957 2005-03-07 16:01:20Z sjoyeux $
3  */
4 #ifndef UTILMM_TYPES_FACTORY_HEADER
5 # define UTILMM_TYPES_FACTORY_HEADER
6 #include "utilmm/config/config.h"
7 
8 #include "boost/utility.hpp"
9 
12 
15 
16 namespace utilmm {
17 
42  template< class AbstractProduct, typename IdentifierType, typename Result,
43  typename ProductCreator,
44  template<typename, class, typename> class FactoryErrorPolicy,
45  class OrderId >
46  class factory
47  :public FactoryErrorPolicy<IdentifierType, AbstractProduct, Result>,
48  public boost::noncopyable {
49  private:
50  typedef typename arg_traits<IdentifierType>::type id_param;
51  typedef typename arg_traits<ProductCreator>::type creator_param;
52 
53  typedef std::map<IdentifierType, ProductCreator, OrderId> factory_db;
54 
55  factory_db associations;
56 
57  public:
70  bool add(id_param id, creator_param creator) {
71  typename factory_db::value_type to_add(id, creator);
72 
73  return associations.insert(to_add).second;
74  }
84  bool remove(id_param id) {
85  return associations.erase(id)==1;
86  }
87 
100  bool make_alias(id_param from, id_param to) {
101  typename factory_db::iterator i = associations.find(to);
102 
103  return associations.end()==i || add(from, i->second);
104  }
105 
116  Result create(id_param id) {
117  typename factory_db::iterator i = associations.find(id);
118 
119  if( associations.end()==i )
120  return this->on_unknown_id(id);
121  else
122  return (i->second)();
123  }
124 
133  bool check_entry(id_param id) const {
134  return associations.find(id)!=associations.end();
135  }
136 
148  ProductCreator const &get_creator(id_param id) const {
149  typename factory_db::const_iterator i = associations.find(id);
150 
151  if( associations.end()==i )
152  return this->on_unknown_type(id);
153  else
154  return i->second;
155  }
156 
157  private:
158  factory() {}
159  ~factory() {}
160 
161  template<class Ty>
162  friend class singleton::wrapper;
163  }; // class utilmm::factory<>
164 
165 } // namespace utilmm
166 
167 # define IN_UTILMM_TYPES_FACTORY_HEADER
168 #include "utilmm/types/bits/factory.tcc"
169 # undef IN_UTILMM_TYPES_FACTORY_HEADER
170 #endif // UTILMM_TYPES_FACTORY_HEADER
171 
Forward declaration of utilmm::factory.
Generic Factory implementation.
Definition: factory.hh:46
bool add(id_param id, creator_param creator)
Add a new production method.
Definition: factory.hh:70
bool check_entry(id_param id) const
Check for entry.
Definition: factory.hh:133
ProductCreator const & get_creator(id_param id) const
Creation method access.
Definition: factory.hh:148
Forward declaration of utilmm::singleton::wrapper.
Wrapper for singleton instances.
Definition: wrapper.hh:31
Definition: auto_flag.hh:6
Definition of utilmm::arg_traits class.
computed_type type
argument type
Definition: arg_traits.hh:38
bool make_alias(id_param from, id_param to)
Alias creation.
Definition: factory.hh:100
Result create(id_param id)
Creation method.
Definition: factory.hh:116
Definition of utilmm::factory_toolbox::error.

Generated on Wed Oct 17 2018 09:34:19 for Util-- by doxygen 1.8.13
SourceForge.net Project Page