LBIBCell
 All Classes Functions Variables Friends Pages
Factory.hpp
1 /*
2  * Copyright (c) 2012 David-Matthias Sichau
3  * Copyright (c) 2010 Marc Kirchner
4  *
5  * This file is part of utillib.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #ifndef UTILIB_FACTORY_HPP__
27 #define UTILIB_FACTORY_HPP__
28 
29 
30 #include <exception>
31 #include <map>
32 #include <typeinfo>
33 
34 namespace UtilLib {
38 template <typename IdentifierType, typename ProductType>
40  protected:
45  static ProductType* onUnknownType(const IdentifierType& id) {
46  throw std::bad_typeid();
47  }
48 };
49 
53 template <typename IdentifierType, typename ProductType>
55  protected:
60  static ProductType* onUnknownType(const IdentifierType& id) {
61  return 0;
62  }
63 };
64 
105 template <
106  class AbstractProduct,
107  typename IdentifierType,
108  template <typename,
109  class> class FactoryErrorPolicy = ErrorPolicyThrowException,
110  typename ProductCreatorSignature = AbstractProduct* (*)()
111  >
112 class Factory : public FactoryErrorPolicy<
113  IdentifierType,
114  AbstractProduct
115  > {
116  public:
123  const IdentifierType& id,
124  ProductCreatorSignature creator) {
125  return productMap_.insert(typename ProductMap::value_type(id,
126  creator)).second;
127  }
128 
133  bool unregisterType(const IdentifierType& id) {
134  return (productMap_.erase(id) == 1);
135  }
136 
143  AbstractProduct* createObject(const IdentifierType& id) const {
144  typename ProductMap::const_iterator i = productMap_.find(id);
145  if (i != productMap_.end()) {
146  return (*i).second();
147  }
148  return this->onUnknownType(id);
149  }
150 
151  private:
155  typedef std::map<IdentifierType, ProductCreatorSignature> ProductMap;
156 
159  ProductMap productMap_;
160 };
161 } // end namespace utillib
162 
163 #endif /* UTILIB_FACTORY_HPP__ */
bool unregisterType(const IdentifierType &id)
Definition: Factory.hpp:133
static ProductType * onUnknownType(const IdentifierType &id)
Definition: Factory.hpp:45
AbstractProduct * createObject(const IdentifierType &id) const
Definition: Factory.hpp:143
bool registerType(const IdentifierType &id, ProductCreatorSignature creator)
Definition: Factory.hpp:122
static ProductType * onUnknownType(const IdentifierType &id)
Definition: Factory.hpp:60