Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
Factory.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2017.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Chris Bielow $
32 // $Authors: Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_CONCEPT_FACTORY_H
36 #define OPENMS_CONCEPT_FACTORY_H
37 
42 
43 #include <map>
44 #include <typeinfo>
45 
46 namespace OpenMS
47 {
48  class String;
49 
60  template <typename FactoryProduct>
61  class Factory :
62  public FactoryBase
63  {
64  friend class singletonsNeedNoFriends; //some versions of gcc would warn otherwise
65 
66 private:
68  typedef FactoryProduct * (*FunctionType)();
69  typedef std::map<String, FunctionType> Map;
70  typedef typename Map::const_iterator MapIterator;
72 
74  virtual ~Factory(){}
75 
78  {
79  }
80 
82  static Factory * instance_()
83  {
84  if (!instance_ptr_)
85  {
86  // name of this Factory
87  String myName = typeid(FactoryType).name();
88 
89  //check if an instance of this kind of Factory already registered
91  {
92  // if not registered yet ... add it
93  instance_ptr_ = new Factory();
94  // now, attention as ORDER of commands is important here:
95  // first register the Factory
97  // because this call, might use another instance of this factory, but we want the other instance to register the children with "US"
98  FactoryProduct::registerChildren();
99  }
100  else
101  {
102  // get instance of this factory from registry
103  instance_ptr_ = static_cast<FactoryType *>(SingletonRegistry::getFactory(myName));
104  }
105  }
106  return instance_ptr_;
107  }
108 
109 public:
110 
112  static FactoryProduct * create(const String & name)
113  {
114  MapIterator it = instance_()->inventory_.find(name);
115  if (it != instance_()->inventory_.end())
116  {
117  return (*(it->second))();
118  }
119  else
120  {
121  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "This FactoryProduct is not registered!", name.c_str());
122  }
123  }
124 
131  static void registerProduct(const String & name, const FunctionType creator)
132  {
133  instance_()->inventory_[name] = creator;
134  }
135 
137  static bool isRegistered(const String & name)
138  {
139  if (instance_()->inventory_.find(name) != instance_()->inventory_.end())
140  {
141  return true;
142  }
143  return false;
144  }
145 
147  static std::vector<String> registeredProducts()
148  {
149  std::vector<String> list;
150  for (MapIterator it = instance_()->inventory_.begin(); it != instance_()->inventory_.end(); ++it)
151  {
152  list.push_back(it->first);
153  }
154  return list;
155  }
156 
157 private:
158 
161  };
162 
163  template <typename FactoryProduct>
165 
166 }
167 #endif //OPENMS_CONCEPT_FACTORY_H
static bool isRegistered(String name)
Returns if a factory is registered.
Definition: SingletonRegistry.h:108
A more convenient string class.
Definition: String.h:57
Factory< FactoryProduct > FactoryType
Definition: Factory.h:71
static Factory * instance_()
singleton access to Factory
Definition: Factory.h:82
FactoryProduct *(* FunctionType)()
Function signature of creator function.
Definition: Factory.h:68
Returns FactoryProduct* based on the name of the desired concrete FactoryProduct. ...
Definition: Factory.h:61
static void registerProduct(const String &name, const FunctionType creator)
register new concrete FactoryProduct
Definition: Factory.h:131
static bool isRegistered(const String &name)
Returns if a factory product is registered.
Definition: Factory.h:137
static Factory * instance_ptr_
Definition: Factory.h:160
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
static FactoryProduct * create(const String &name)
return FactoryProduct according to unique identifier name
Definition: Factory.h:112
Factory()
Constructor.
Definition: Factory.h:77
virtual ~Factory()
Destructor.
Definition: Factory.h:74
Base class for Factory<T>
Definition: FactoryBase.h:49
static FactoryBase * getFactory(const String &name)
return DefaultParamHandler according to unique identifier name
Definition: SingletonRegistry.h:83
Invalid value exception.
Definition: Exception.h:336
static std::vector< String > registeredProducts()
Returns a list of registered products.
Definition: Factory.h:147
friend class singletonsNeedNoFriends
Definition: Factory.h:64
Map inventory_
Definition: Factory.h:159
Map::const_iterator MapIterator
Definition: Factory.h:70
std::map< String, FunctionType > Map
Definition: Factory.h:69
static void registerFactory(const String &name, FactoryBase *instance)
register new concrete Factory
Definition: SingletonRegistry.h:102

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:00 using doxygen 1.8.13