BornAgain  1.18.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 scattering at grazing incidence
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 #ifndef BORNAGAIN_FIT_TESTENGINE_IFACTORY_H
16 #define BORNAGAIN_FIT_TESTENGINE_IFACTORY_H
17 
18 #include <cassert>
19 #include <functional>
20 #include <iostream> // debug
21 #include <map>
22 #include <memory>
23 #include <sstream>
24 
25 //! Base class for all factories.
26 //! @ingroup tools_internal
27 
28 template <class Key, class AbstractProduct> class IFactory
29 {
30 public:
31  //! function which will be used to create object of AbstractProduct base type
32  typedef typename std::function<AbstractProduct*()> CreateItemCallback;
33 
34  //! map for correspondance between object identifier and object creation function
35  typedef std::map<Key, CreateItemCallback> CallbackMap_t;
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 #ifndef SWIG
46  std::unique_ptr<AbstractProduct> createItemPtr(const Key& item_key) const
47  {
48  return std::unique_ptr<AbstractProduct>{createItem(item_key)};
49  }
50 #endif
51 
52  //! Registers object's creation function
53  bool registerItem(const Key& item_key, CreateItemCallback CreateFn)
54  {
55  assert(m_callbacks.find(item_key) == m_callbacks.end());
56  return m_callbacks.insert(make_pair(item_key, CreateFn)).second;
57  }
58 
59  bool contains(const Key& item_key) const
60  {
61  return m_callbacks.find(item_key) != m_callbacks.end();
62  }
63 
64  //! Returns number of registered objects
65  size_t size() const { return m_callbacks.size(); }
66 
67 protected:
68  CallbackMap_t m_callbacks; //!< map of correspondence of objectsId and creation functions
69 };
70 
71 //! Returns new instance of class T.
72 //!
73 //! This templated function is used in catalogs in form of a function pointer
74 //! 'create_new<T>', with no function arguments supplied. Equivalently, we could
75 //! use a lambda function '[](){return new T;}'.
76 
77 template <class T> T* create_new()
78 {
79  return new T();
80 }
81 
82 #endif // BORNAGAIN_FIT_TESTENGINE_IFACTORY_H
T * create_new()
Returns new instance of class T.
Definition: IFactory.h:77
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
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 correspondance between object identifier and object creation function
Definition: IFactory.h:35
size_t size() const
Returns number of registered objects.
Definition: IFactory.h:65
CallbackMap_t m_callbacks
map of correspondence of objectsId and creation functions
Definition: IFactory.h:68
bool registerItem(const Key &item_key, CreateItemCallback CreateFn)
Registers object's creation function.
Definition: IFactory.h:53
bool contains(const Key &item_key) const
Definition: IFactory.h:59
std::unique_ptr< AbstractProduct > createItemPtr(const Key &item_key) const
Definition: IFactory.h:46