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

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:13 using doxygen 1.8.5