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 #pragma once
36 
41 
42 #include <map>
43 #include <typeinfo>
44 
45 namespace OpenMS
46 {
47  class String;
48 
59  template <typename FactoryProduct>
60  class Factory :
61  public FactoryBase
62  {
63  friend class singletonsNeedNoFriends; //some versions of gcc would warn otherwise
64 
65 private:
67  typedef FactoryProduct * (*FunctionType)();
68  typedef std::map<String, FunctionType> Map;
69  typedef typename Map::const_iterator MapIterator;
71 
73  ~Factory() override{}
74 
77  {
78  }
79 
81  static Factory * instance_()
82  {
83  if (!instance_ptr_)
84  {
85  // name of this Factory
86  String myName = typeid(FactoryType).name();
87 
88  //check if an instance of this kind of Factory already registered
90  {
91  // if not registered yet ... add it
92  instance_ptr_ = new Factory();
93  // now, attention as ORDER of commands is important here:
94  // first register the Factory
96  // because this call, might use another instance of this factory, but we want the other instance to register the children with "US"
97  FactoryProduct::registerChildren();
98  }
99  else
100  {
101  // get instance of this factory from registry
102  instance_ptr_ = static_cast<FactoryType *>(SingletonRegistry::getFactory(myName));
103  }
104  }
105  return instance_ptr_;
106  }
107 
108 public:
109 
111  static FactoryProduct * create(const String & name)
112  {
113  MapIterator it = instance_()->inventory_.find(name);
114  if (it != instance_()->inventory_.end())
115  {
116  return (*(it->second))();
117  }
118  else
119  {
120  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "This FactoryProduct is not registered!", name.c_str());
121  }
122  }
123 
130  static void registerProduct(const String & name, const FunctionType creator)
131  {
132  instance_()->inventory_[name] = creator;
133  }
134 
136  static bool isRegistered(const String & name)
137  {
138  if (instance_()->inventory_.find(name) != instance_()->inventory_.end())
139  {
140  return true;
141  }
142  return false;
143  }
144 
146  static std::vector<String> registeredProducts()
147  {
148  std::vector<String> list;
149  for (MapIterator it = instance_()->inventory_.begin(); it != instance_()->inventory_.end(); ++it)
150  {
151  list.push_back(it->first);
152  }
153  return list;
154  }
155 
156 private:
157 
160  };
161 
162  template <typename FactoryProduct>
164 
165 }
static bool isRegistered(String name)
Returns if a factory is registered.
Definition: SingletonRegistry.h:107
A more convenient string class.
Definition: String.h:57
Factory< FactoryProduct > FactoryType
Definition: Factory.h:70
static Factory * instance_()
singleton access to Factory
Definition: Factory.h:81
FactoryProduct *(* FunctionType)()
Function signature of creator function.
Definition: Factory.h:67
Returns FactoryProduct* based on the name of the desired concrete FactoryProduct. ...
Definition: Factory.h:60
static void registerProduct(const String &name, const FunctionType creator)
register new concrete FactoryProduct
Definition: Factory.h:130
static bool isRegistered(const String &name)
Returns if a factory product is registered.
Definition: Factory.h:136
static Factory * instance_ptr_
Definition: Factory.h:159
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
~Factory() override
Destructor.
Definition: Factory.h:73
static FactoryProduct * create(const String &name)
return FactoryProduct according to unique identifier name
Definition: Factory.h:111
Factory()
Constructor.
Definition: Factory.h:76
Base class for Factory<T>
Definition: FactoryBase.h:48
static FactoryBase * getFactory(const String &name)
return DefaultParamHandler according to unique identifier name
Definition: SingletonRegistry.h:82
Invalid value exception.
Definition: Exception.h:335
static std::vector< String > registeredProducts()
Returns a list of registered products.
Definition: Factory.h:146
friend class singletonsNeedNoFriends
Definition: Factory.h:63
Map inventory_
Definition: Factory.h:158
Map::const_iterator MapIterator
Definition: Factory.h:69
std::map< String, FunctionType > Map
Definition: Factory.h:68
static void registerFactory(const String &name, FactoryBase *instance)
register new concrete Factory
Definition: SingletonRegistry.h:101

OpenMS / TOPP release 2.3.0 Documentation generated on Wed Apr 18 2018 19:29:04 using doxygen 1.8.14