OpenMS
SingletonRegistry.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: $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
13 
14 #include <map>
15 
16 namespace OpenMS
17 {
18  class String;
19  class FactoryBase;
20 
28  class OPENMS_DLLAPI SingletonRegistry
29  {
30  friend class singletonsNeedNoFriends; //some versions of gcc would warn otherwise
31 
32 private:
34  typedef std::map<String, FactoryBase *> Map;
35  typedef Map::const_iterator MapIterator;
36 
38  virtual ~SingletonRegistry(){}
39 
42 
45  {
46  if (!singletonRegistryInstance_)
47  {
48  singletonRegistryInstance_ = new SingletonRegistry();
49  }
50  return singletonRegistryInstance_;
51  }
52 
53 public:
54 
56  static FactoryBase * getFactory(const String & name)
57  {
58  MapIterator it = instance_()->inventory_.find(name);
59  if (it != instance_()->inventory_.end())
60  {
61  return it->second;
62  }
63  else
64  {
65  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "This Factory is not registered with SingletonRegistry!", name.c_str());
66  }
67  }
68 
75  static void registerFactory(const String & name, FactoryBase * instance)
76  {
77  instance_()->inventory_[name] = instance;
78  }
79 
81  static bool isRegistered(String name)
82  {
83  if (instance_()->inventory_.find(name) != instance_()->inventory_.end())
84  {
85  return true;
86  }
87  return false;
88  }
89 
90 private:
91 
94  };
95 
96 
97 }
Invalid value exception.
Definition: Exception.h:303
Base class for Factory<T>
Definition: FactoryBase.h:23
Holds pointers to unique instance of a singleton factory.
Definition: SingletonRegistry.h:29
Map inventory_
Definition: SingletonRegistry.h:92
static SingletonRegistry * singletonRegistryInstance_
Definition: SingletonRegistry.h:93
std::map< String, FactoryBase * > Map
Function signature of creator function.
Definition: SingletonRegistry.h:34
static SingletonRegistry * instance_()
singleton access to SingletonRegistry
Definition: SingletonRegistry.h:44
static bool isRegistered(String name)
Returns if a factory is registered.
Definition: SingletonRegistry.h:81
Map::const_iterator MapIterator
Definition: SingletonRegistry.h:35
SingletonRegistry()
C'Tor.
Definition: SingletonRegistry.h:41
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
virtual ~SingletonRegistry()
destructor
Definition: SingletonRegistry.h:38
A more convenient string class.
Definition: String.h:34
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22