OpenMS
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-2023.
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 <mutex>
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  ~Factory() override{}
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 
115  // unique lock (make sure we only create one instance)
116  // -> Since we may call Factory<FactoryProduct>::create for another
117  // FactoryProduct during initialization, we have to implement locking
118  // per template class specialization.
119  static std::mutex factory_create_mutex;
120  std::lock_guard<std::mutex> lock(factory_create_mutex);
121 
122  MapIterator it = instance_()->inventory_.find(name);
123  if (it != instance_()->inventory_.end())
124  {
125  return (*(it->second))();
126  }
127  else
128  {
129  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "This FactoryProduct is not registered!", name.c_str());
130  }
131  }
132 
139  static void registerProduct(const String & name, const FunctionType creator)
140  {
141  instance_()->inventory_[name] = creator;
142  }
143 
145  static bool isRegistered(const String & name)
146  {
147  if (instance_()->inventory_.find(name) != instance_()->inventory_.end())
148  {
149  return true;
150  }
151  return false;
152  }
153 
155  static std::vector<String> registeredProducts()
156  {
157  std::vector<String> list;
158  for (MapIterator it = instance_()->inventory_.begin(); it != instance_()->inventory_.end(); ++it)
159  {
160  list.push_back(it->first);
161  }
162  return list;
163  }
164 
165 private:
166 
169  };
170 
171  template <typename FactoryProduct>
173 
174 }
Invalid value exception.
Definition: Exception.h:329
Base class for Factory<T>
Definition: FactoryBase.h:49
Returns FactoryProduct* based on the name of the desired concrete FactoryProduct.
Definition: Factory.h:63
static void registerProduct(const String &name, const FunctionType creator)
register new concrete FactoryProduct
Definition: Factory.h:139
Map inventory_
Definition: Factory.h:167
static bool isRegistered(const String &name)
Returns if a factory product is registered.
Definition: Factory.h:145
static FactoryProduct * create(const String &name)
return FactoryProduct according to unique identifier name
Definition: Factory.h:112
friend class singletonsNeedNoFriends
Definition: Factory.h:64
FactoryProduct *(* FunctionType)()
Function signature of creator function.
Definition: Factory.h:68
Map::const_iterator MapIterator
Definition: Factory.h:70
static Factory * instance_ptr_
Definition: Factory.h:168
std::map< String, FunctionType > Map
Definition: Factory.h:69
static std::vector< String > registeredProducts()
Returns a list of registered products.
Definition: Factory.h:155
~Factory() override
Destructor.
Definition: Factory.h:74
Factory< FactoryProduct > FactoryType
Definition: Factory.h:71
Factory()
Constructor.
Definition: Factory.h:77
static Factory * instance_()
singleton access to Factory
Definition: Factory.h:82
static bool isRegistered(String name)
Returns if a factory is registered.
Definition: SingletonRegistry.h:107
static FactoryBase * getFactory(const String &name)
return DefaultParamHandler according to unique identifier name
Definition: SingletonRegistry.h:82
static void registerFactory(const String &name, FactoryBase *instance)
register new concrete Factory
Definition: SingletonRegistry.h:101
A more convenient string class.
Definition: String.h:60
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48