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 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/utils/ifactory.h
6 //! @brief Defines class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifndef BORNAGAIN_MVVM_MODEL_MVVM_UTILS_IFACTORY_H
16 #define BORNAGAIN_MVVM_MODEL_MVVM_UTILS_IFACTORY_H
17 
18 #include <functional>
19 #include <map>
20 #include <memory>
21 #include <sstream>
22 #include <stdexcept>
23 
24 namespace ModelView {
25 
26 //! Base for factories.
27 
28 template <class Key, class Value> class IFactory {
29 public:
30  using function_t = std::function<std::unique_ptr<Value>()>;
31  using map_t = std::map<Key, function_t>;
32 
33  bool contains(const Key& item_key) const { return m_data.find(item_key) != m_data.end(); }
34 
35  std::unique_ptr<Value> create(const Key& item_key) const
36  {
37  auto it = m_data.find(item_key);
38  if (it == m_data.end()) {
39  std::ostringstream message;
40  message << "IFactory::createItem() -> Error. Unknown item key '" << item_key << "'";
41  throw std::runtime_error(message.str());
42  }
43  return it->second();
44  }
45 
46  bool add(const Key& key, function_t func)
47  {
48  if (m_data.find(key) != m_data.end()) {
49  std::ostringstream message;
50  message << "IFactory::createItem() -> Already registered item key '" << key << "'";
51  throw std::runtime_error(message.str());
52  }
53  return m_data.insert(make_pair(key, func)).second;
54  }
55 
56  size_t size() const { return m_data.size(); }
57 
58  typename map_t::iterator begin() { return m_data.begin(); }
59  typename map_t::iterator end() { return m_data.end(); }
60 
61 private:
63 };
64 
65 } // namespace ModelView
66 
67 #endif // BORNAGAIN_MVVM_MODEL_MVVM_UTILS_IFACTORY_H
Base for factories.
Definition: ifactory.h:28
std::function< std::unique_ptr< Value >()> function_t
Definition: ifactory.h:30
bool contains(const Key &item_key) const
Definition: ifactory.h:33
size_t size() const
Definition: ifactory.h:56
map_t::iterator end()
Definition: ifactory.h:59
map_t::iterator begin()
Definition: ifactory.h:58
std::map< Key, function_t > map_t
Definition: ifactory.h:31
bool add(const Key &key, function_t func)
Definition: ifactory.h:46
std::unique_ptr< Value > create(const Key &item_key) const
Definition: ifactory.h:35
materialitems.h Collection of materials to populate MaterialModel.