BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
IFactory.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Util/IFactory.h
6 //! @brief Defines interface class IFactory.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifndef USER_API
16 #ifndef BORNAGAIN_BASE_UTIL_IFACTORY_H
17 #define BORNAGAIN_BASE_UTIL_IFACTORY_H
18 
19 #include <cassert>
20 #include <functional>
21 #include <iostream> // debug
22 #include <map>
23 #include <memory>
24 #include <sstream>
25 
26 //! Base class for all factories.
27 
28 template <class Key, class AbstractProduct>
29 class IFactory {
30 public:
31  //! function which will be used to create object of AbstractProduct base type
32  using CreateItemCallback = std::function<AbstractProduct*()>;
33 
34  //! map for correspondence between object identifier and object creation function
35  using CallbackMap_t = std::map<Key, CreateItemCallback>;
36 
37  //! Creates object by calling creation function corresponded to given identifier
38  AbstractProduct* createItem(const Key& item_key) const
39  {
40  auto it = m_callbacks.find(item_key);
41  assert(it != m_callbacks.end());
42  return (it->second)();
43  }
44 
45  std::unique_ptr<AbstractProduct> createItemPtr(const Key& item_key) const
46  {
47  return std::unique_ptr<AbstractProduct>{createItem(item_key)};
48  }
49 
50  //! Registers object's creation function
51  bool registerItem(const Key& item_key, CreateItemCallback CreateFn)
52  {
53  assert(m_callbacks.find(item_key) == m_callbacks.end());
54  return m_callbacks.insert(make_pair(item_key, CreateFn)).second;
55  }
56 
57  bool contains(const Key& item_key) const
58  {
59  return m_callbacks.find(item_key) != m_callbacks.end();
60  }
61 
62  //! Returns number of registered objects
63  size_t size() const { return m_callbacks.size(); }
64 
65 protected:
66  CallbackMap_t m_callbacks; //!< map of correspondence of objectsId and creation functions
67 };
68 
69 //! Returns new instance of class T.
70 //!
71 //! This templated function is used in catalogs in form of a function pointer
72 //! 'create_new<T>', with no function arguments supplied. Equivalently, we could
73 //! use a lambda function '[](){return new T;}'.
74 
75 template <class T>
77 {
78  return new T();
79 }
80 
81 #endif // BORNAGAIN_BASE_UTIL_IFACTORY_H
82 #endif // USER_API
T * create_new()
Returns new instance of class T.
Definition: IFactory.h:76
Base class for all factories.
Definition: IFactory.h:29
AbstractProduct * createItem(const Key &item_key) const
Creates object by calling creation function corresponded to given identifier.
Definition: IFactory.h:38
size_t size() const
Returns number of registered objects.
Definition: IFactory.h:63
CallbackMap_t m_callbacks
map of correspondence of objectsId and creation functions
Definition: IFactory.h:66
bool registerItem(const Key &item_key, CreateItemCallback CreateFn)
Registers object's creation function.
Definition: IFactory.h:51
bool contains(const Key &item_key) const
Definition: IFactory.h:57
std::function< AbstractProduct *()> CreateItemCallback
function which will be used to create object of AbstractProduct base type
Definition: IFactory.h:32
std::map< Key, CreateItemCallback > CallbackMap_t
map for correspondence between object identifier and object creation function
Definition: IFactory.h:35
std::unique_ptr< AbstractProduct > createItemPtr(const Key &item_key) const
Definition: IFactory.h:45