OpenMS  2.5.0
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-2020.
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 }
OpenMS::TOPPBase
Base class for TOPP applications.
Definition: TOPPBase.h:144
OpenMS::Factory::singletonsNeedNoFriends
friend class singletonsNeedNoFriends
Definition: Factory.h:64
OpenMS::Factory::create
static FactoryProduct * create(const String &name)
return FactoryProduct according to unique identifier name
Definition: Factory.h:112
OpenMS::Factory::registerProduct
static void registerProduct(const String &name, const FunctionType creator)
register new concrete FactoryProduct
Definition: Factory.h:139
OpenMS::Factory::isRegistered
static bool isRegistered(const String &name)
Returns if a factory product is registered.
Definition: Factory.h:145
OpenMS::Factory
Returns FactoryProduct* based on the name of the desired concrete FactoryProduct.
Definition: Factory.h:61
OpenMS::Factory::~Factory
~Factory() override
Destructor.
Definition: Factory.h:74
OpenMS::Exception::InvalidValue
Invalid value exception.
Definition: Exception.h:335
OpenMS::MzMLFile::store
void store(const String &filename, const PeakMap &map) const
Stores a map in an MzML file.
InternalCalibration.h
OpenMS::MZTrafoModel::setCoefficients
void setCoefficients(const MZTrafoModel &rhs)
Copy model coefficients from another model.
OpenMS::Factory::MapIterator
Map::const_iterator MapIterator
Definition: Factory.h:70
OpenMS::MzMLFile
File adapter for MzML files.
Definition: MzMLFile.h:55
OpenMS::String
A more convenient string class.
Definition: String.h:58
MzMLFile.h
OpenMS::MSExperiment
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:77
OpenMS::SingletonRegistry::registerFactory
static void registerFactory(const String &name, FactoryBase *instance)
register new concrete Factory
Definition: SingletonRegistry.h:101
OpenMS::IntList
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:55
OpenMS::InternalCalibration
A mass recalibration method using linear/quadratic interpolation (robust/weighted) of given reference...
Definition: InternalCalibration.h:61
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::Factory::instance_
static Factory * instance_()
singleton access to Factory
Definition: Factory.h:82
Exception.h
OpenMS::Factory::FunctionType
FactoryProduct *(* FunctionType)()
Function signature of creator function.
Definition: Factory.h:68
OpenMS::MzMLFile::load
void load(const String &filename, PeakMap &map)
Loads a map from a MzML file. Spectra and chromatograms are sorted by default (this can be disabled u...
TransformationXMLFile.h
OpenMS::Factory::registeredProducts
static std::vector< String > registeredProducts()
Returns a list of registered products.
Definition: Factory.h:155
OpenMS::InternalCalibration::applyTransformation
static void applyTransformation(std::vector< Precursor > &pcs, const MZTrafoModel &trafo)
OpenMS::Factory::instance_ptr_
static Factory * instance_ptr_
Definition: Factory.h:168
OpenMS::DataProcessing::CALIBRATION
Calibration of m/z positions.
Definition: DataProcessing.h:69
OpenMS::Factory::Factory
Factory()
Constructor.
Definition: Factory.h:77
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:73
OpenMS::Factory::inventory_
Map inventory_
Definition: Factory.h:167
OpenMS::MZTrafoModel
Create and apply models of a mass recalibration function.
Definition: MZTrafoModel.h:66
OpenMS::SingletonRegistry::isRegistered
static bool isRegistered(String name)
Returns if a factory is registered.
Definition: SingletonRegistry.h:107
String.h
OpenMS::Factory::Map
std::map< String, FunctionType > Map
Definition: Factory.h:69
OpenMS::SingletonRegistry::getFactory
static FactoryBase * getFactory(const String &name)
return DefaultParamHandler according to unique identifier name
Definition: SingletonRegistry.h:82
FactoryBase.h
OpenMS::Factory::FactoryType
Factory< FactoryProduct > FactoryType
Definition: Factory.h:71
OpenMS::FactoryBase
Base class for Factory<T>
Definition: FactoryBase.h:48
OpenMS::ProgressLogger::setLogType
void setLogType(LogType type) const
Sets the progress log that should be used. The default type is NONE!
TOPPBase.h
SingletonRegistry.h