BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IFactory.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Fit/TestEngine/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 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_FIT_TESTENGINE_IFACTORY_H
21 #define BORNAGAIN_FIT_TESTENGINE_IFACTORY_H
22 
23 #include <cassert>
24 #include <functional>
25 #include <iostream> // debug
26 #include <map>
27 #include <memory>
28 #include <sstream>
29 
30 namespace mumufit::test {
31 
32 //! Base class for all factories.
33 //! @ingroup tools_internal
34 
35 template <class Key, class AbstractProduct> class IFactory {
36 public:
37  //! function which will be used to create object of AbstractProduct base type
38  typedef typename std::function<AbstractProduct*()> CreateItemCallback;
39 
40  //! map for correspondence between object identifier and object creation function
41  typedef std::map<Key, CreateItemCallback> CallbackMap_t;
42 
43  //! Creates object by calling creation function corresponded to given identifier
44  AbstractProduct* createItem(const Key& item_key) const
45  {
46  auto it = m_callbacks.find(item_key);
47  assert(it != m_callbacks.end());
48  return (it->second)();
49  }
50 
51 #ifndef SWIG
52  std::unique_ptr<AbstractProduct> createItemPtr(const Key& item_key) const
53  {
54  return std::unique_ptr<AbstractProduct>{createItem(item_key)};
55  }
56 #endif
57 
58  //! Registers object's creation function
59  bool registerItem(const Key& item_key, CreateItemCallback CreateFn)
60  {
61  assert(m_callbacks.find(item_key) == m_callbacks.end());
62  return m_callbacks.insert(make_pair(item_key, CreateFn)).second;
63  }
64 
65  bool contains(const Key& item_key) const
66  {
67  return m_callbacks.find(item_key) != m_callbacks.end();
68  }
69 
70  //! Returns number of registered objects
71  size_t size() const { return m_callbacks.size(); }
72 
73 protected:
74  CallbackMap_t m_callbacks; //!< map of correspondence of objectsId and creation functions
75 };
76 
77 //! Returns new instance of class T.
78 //!
79 //! This templated function is used in catalogs in form of a function pointer
80 //! 'create_new<T>', with no function arguments supplied. Equivalently, we could
81 //! use a lambda function '[](){return new T;}'.
82 
83 template <class T> T* create_new()
84 {
85  return new T();
86 }
87 
88 } // namespace mumufit::test
89 
90 #endif // BORNAGAIN_FIT_TESTENGINE_IFACTORY_H
91 #endif // USER_API
Base class for all factories.
Definition: IFactory.h:35
std::map< Key, CreateItemCallback > CallbackMap_t
map for correspondence between object identifier and object creation function
Definition: IFactory.h:41
size_t size() const
Returns number of registered objects.
Definition: IFactory.h:71
std::function< AbstractProduct *()> CreateItemCallback
function which will be used to create object of AbstractProduct base type
Definition: IFactory.h:38
CallbackMap_t m_callbacks
map of correspondence of objectsId and creation functions
Definition: IFactory.h:74
bool registerItem(const Key &item_key, CreateItemCallback CreateFn)
Registers object's creation function.
Definition: IFactory.h:59
AbstractProduct * createItem(const Key &item_key) const
Creates object by calling creation function corresponded to given identifier.
Definition: IFactory.h:44
std::unique_ptr< AbstractProduct > createItemPtr(const Key &item_key) const
Definition: IFactory.h:52
bool contains(const Key &item_key) const
Definition: IFactory.h:65
T * create_new()
Returns new instance of class T.
Definition: IFactory.h:83