fw4spl
FactoryRegistry.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWCORE_UTIL_FACTORYREGISTRY_HPP__
8 #define __FWCORE_UTIL_FACTORYREGISTRY_HPP__
9 
10 #include "fwCore/mt/types.hpp"
11 
12 #include <functional>
13 #include <map>
14 #include <string>
15 #include <vector>
16 
17 namespace fwCore
18 {
19 namespace util
20 {
21 
26 template < typename FACTORY_SIGNATURE, typename KEY_TYPE = std::string,
27  typename FACTORY_HOLDER = std::function< FACTORY_SIGNATURE > >
29 {
30 public:
31  typedef FACTORY_SIGNATURE FactorySignatureType;
32  typedef KEY_TYPE KeyType;
33 
34  typedef FACTORY_HOLDER FactoryType;
35  typedef std::map< KeyType, FactoryType > RegistryType;
36  typedef std::vector<KeyType> KeyVectorType;
37 
39  {
40  }
41 
42  virtual ~FactoryRegistryBase()
43  {
44  }
45 
49  void addFactory(const KeyType& name, FactoryType factory)
50  {
51  // get exclusive access
52  ::fwCore::mt::WriteLock lock(m_mutex);
53  m_registry[name] = factory;
54  }
55 
59  virtual FactoryType getFactory(const KeyType& key) const
60  {
61  // get shared access
62  ::fwCore::mt::ReadLock lock(m_mutex);
63  typename RegistryType::const_iterator iter = m_registry.find(key);
64  FactoryType factory;
65  if(iter != m_registry.end())
66  {
67  factory = iter->second;
68  }
69  return factory;
70  }
71 
75  virtual KeyVectorType getFactoryKeys() const
76  {
77  ::fwCore::mt::ReadLock lock(m_mutex);
78  KeyVectorType vectKeys;
79  std::transform( m_registry.begin(), m_registry.end(),
80  std::back_inserter(vectKeys),
81  std::bind(&RegistryType::value_type::first, std::placeholders::_1) );
82  return vectKeys;
83  }
84 
85 protected:
86 
87  RegistryType m_registry;
88  mutable ::fwCore::mt::ReadWriteMutex m_mutex;
89 };
90 
94 template <typename F, typename KEY_TYPE = std::string, typename FACTORY_HOLDER = std::function< F > >
96 
97 template< typename RETURN_TYPE, typename KEY_TYPE, typename FACTORY_HOLDER >
98 class FactoryRegistry< RETURN_TYPE (), KEY_TYPE, FACTORY_HOLDER > :
99  public FactoryRegistryBase < RETURN_TYPE (), KEY_TYPE >
100 {
101 typedef RETURN_TYPE (FactorySignatureType)();
102 typedef FACTORY_HOLDER FactoryType;
103 typedef RETURN_TYPE ReturnType;
104 typedef KEY_TYPE KeyType;
105 
106 public:
107 
112  ReturnType create(const KeyType& key) const
113  {
114  FactoryType factory = this->getFactory(key);
115 
116  if(!factory)
117  {
118  ReturnType obj;
119  return obj;
120  }
121  return factory();
122  }
123 };
124 
125 template< typename RETURN_TYPE, typename ARG1_TYPE, typename KEY_TYPE, typename FACTORY_HOLDER >
126 class FactoryRegistry< RETURN_TYPE (ARG1_TYPE), KEY_TYPE, FACTORY_HOLDER > :
127  public FactoryRegistryBase < RETURN_TYPE (ARG1_TYPE), KEY_TYPE >
128 {
129 typedef RETURN_TYPE (FactorySignatureType)(ARG1_TYPE);
130 typedef FACTORY_HOLDER FactoryType;
131 typedef RETURN_TYPE ReturnType;
132 typedef ARG1_TYPE Arg1Type;
133 typedef KEY_TYPE KeyType;
134 
135 public:
136 
141  ReturnType create(const KeyType& key, Arg1Type &arg1) const
142  {
143  FactoryType factory = this->getFactory(key);
144  ReturnType obj;
145  if(factory)
146  {
147  obj = factory(arg1);
148  }
149  return obj;
150  }
151 };
152 
153 } //namespace util
154 } //namespace fwCore
155 
156 #endif /* __FWCORE_UTIL_FACTORYREGISTRY_HPP__ */
virtual KeyVectorType getFactoryKeys() const
returns the registered factory keys.
This namespace fwCore provides common foundations for FW4SPL.
Definition: BaseObject.hpp:16
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
Contains fwAtomsFilter::factory utilities.
ReturnType create(const KeyType &key, Arg1Type &arg1) const
Instantiates an object with the factory associated with the specified key, passing arg1 to the factor...
FactoryRegistryBase is a class used to store factories.
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
FactoryRegistry is a class used to store factories and create instance object with these factories...
void addFactory(const KeyType &name, FactoryType factory)
Add a factory to the registry.
ReturnType create(const KeyType &key) const
Instantiates an object with the factory associated with the specified key.
virtual FactoryType getFactory(const KeyType &key) const
returns the factory associated with the key.