OpenMS
Factory.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Chris Bielow $
6 // $Authors: Chris Bielow $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
15 
16 #include <mutex>
17 #include <map>
18 #include <typeinfo>
19 
20 namespace OpenMS
21 {
22  class String;
23 
34  template <typename FactoryProduct>
35  class Factory :
36  public FactoryBase
37  {
38  friend class singletonsNeedNoFriends; //some versions of gcc would warn otherwise
39 
40 private:
42  typedef FactoryProduct * (*FunctionType)();
43  typedef std::map<String, FunctionType> Map;
44  typedef typename Map::const_iterator MapIterator;
46 
48  ~Factory() override{}
49 
52  {
53  }
54 
56  static Factory * instance_()
57  {
58  if (!instance_ptr_)
59  {
60  // name of this Factory
61  String myName = typeid(FactoryType).name();
62 
63  //check if an instance of this kind of Factory already registered
65  {
66  // if not registered yet ... add it
67  instance_ptr_ = new Factory();
68  // now, attention as ORDER of commands is important here:
69  // first register the Factory
71  // because this call, might use another instance of this factory, but we want the other instance to register the children with "US"
72  FactoryProduct::registerChildren();
73  }
74  else
75  {
76  // get instance of this factory from registry
78  }
79  }
80  return instance_ptr_;
81  }
82 
83 public:
84 
86  static FactoryProduct * create(const String & name)
87  {
88 
89  // unique lock (make sure we only create one instance)
90  // -> Since we may call Factory<FactoryProduct>::create for another
91  // FactoryProduct during initialization, we have to implement locking
92  // per template class specialization.
93  static std::mutex factory_create_mutex;
94  std::lock_guard<std::mutex> lock(factory_create_mutex);
95 
96  MapIterator it = instance_()->inventory_.find(name);
97  if (it != instance_()->inventory_.end())
98  {
99  return (*(it->second))();
100  }
101  else
102  {
103  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "This FactoryProduct is not registered!", name.c_str());
104  }
105  }
106 
113  static void registerProduct(const String & name, const FunctionType creator)
114  {
115  instance_()->inventory_[name] = creator;
116  }
117 
119  static bool isRegistered(const String & name)
120  {
121  if (instance_()->inventory_.find(name) != instance_()->inventory_.end())
122  {
123  return true;
124  }
125  return false;
126  }
127 
129  static std::vector<String> registeredProducts()
130  {
131  std::vector<String> list;
132  for (MapIterator it = instance_()->inventory_.begin(); it != instance_()->inventory_.end(); ++it)
133  {
134  list.push_back(it->first);
135  }
136  return list;
137  }
138 
139 private:
140 
143  };
144 
145  template <typename FactoryProduct>
147 
148 }
Invalid value exception.
Definition: Exception.h:303
Base class for Factory<T>
Definition: FactoryBase.h:23
Returns FactoryProduct* based on the name of the desired concrete FactoryProduct.
Definition: Factory.h:37
static void registerProduct(const String &name, const FunctionType creator)
register new concrete FactoryProduct
Definition: Factory.h:113
Map inventory_
Definition: Factory.h:141
static bool isRegistered(const String &name)
Returns if a factory product is registered.
Definition: Factory.h:119
static FactoryProduct * create(const String &name)
return FactoryProduct according to unique identifier name
Definition: Factory.h:86
friend class singletonsNeedNoFriends
Definition: Factory.h:38
FactoryProduct *(* FunctionType)()
Function signature of creator function.
Definition: Factory.h:42
Map::const_iterator MapIterator
Definition: Factory.h:44
static Factory * instance_ptr_
Definition: Factory.h:142
std::map< String, FunctionType > Map
Definition: Factory.h:43
static std::vector< String > registeredProducts()
Returns a list of registered products.
Definition: Factory.h:129
~Factory() override
Destructor.
Definition: Factory.h:48
Factory< FactoryProduct > FactoryType
Definition: Factory.h:45
Factory()
Constructor.
Definition: Factory.h:51
static Factory * instance_()
singleton access to Factory
Definition: Factory.h:56
static bool isRegistered(String name)
Returns if a factory is registered.
Definition: SingletonRegistry.h:81
static FactoryBase * getFactory(const String &name)
return DefaultParamHandler according to unique identifier name
Definition: SingletonRegistry.h:56
static void registerFactory(const String &name, FactoryBase *instance)
register new concrete Factory
Definition: SingletonRegistry.h:75
A more convenient string class.
Definition: String.h:34
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22