BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
itemcatalogue.cpp
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/model/itemcatalogue.cpp
6 //! @brief Implements 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 
16 #include "mvvm/model/sessionitem.h"
17 #include "mvvm/utils/ifactory.h"
18 #include <stdexcept>
19 
20 using namespace ModelView;
21 
24  struct TypeAndLabel {
25  std::string item_type;
26  std::string item_label;
27  };
28 
29  std::vector<TypeAndLabel> m_info;
30 };
31 
32 ItemCatalogue::ItemCatalogue() : p_impl(std::make_unique<ItemCatalogueImpl>()) {}
33 
35 {
36  p_impl = std::make_unique<ItemCatalogueImpl>(*other.p_impl);
37 }
38 
40 {
41  if (this != &other) {
42  ItemCatalogue tmp(other);
43  std::swap(this->p_impl, tmp.p_impl);
44  }
45  return *this;
46 }
47 
48 void ItemCatalogue::registerItem(const std::string& modelType, item_factory_func_t func,
49  const std::string& label)
50 {
51  p_impl->factory.add(modelType, func);
52  p_impl->m_info.push_back({modelType, label});
53 }
54 
56 
57 bool ItemCatalogue::contains(const std::string& modelType) const
58 {
59  return p_impl->factory.contains(modelType);
60 }
61 
62 std::unique_ptr<SessionItem> ItemCatalogue::create(const std::string& modelType) const
63 {
64  return p_impl->factory.create(modelType);
65 }
66 
67 std::vector<std::string> ItemCatalogue::modelTypes() const
68 {
69  std::vector<std::string> result;
70  for (const auto& x : p_impl->m_info)
71  result.push_back(x.item_type);
72  return result;
73 }
74 
75 std::vector<std::string> ItemCatalogue::labels() const
76 {
77  std::vector<std::string> result;
78  for (const auto& x : p_impl->m_info)
79  result.push_back(x.item_label);
80  return result;
81 }
82 
84 {
85  return static_cast<int>(p_impl->factory.size());
86 }
87 
88 //! Adds content of other catalogue to this.
89 
91 {
92  size_t index(0);
93  for (auto it : other.p_impl->factory) {
94  if (contains(it.first))
95  throw std::runtime_error(
96  "ItemCatalogue::add() -> Catalogue contains duplicated records");
97 
98  registerItem(it.first, it.second, other.p_impl->m_info[index].item_label);
99  ++index;
100  }
101 }
void swap(OutputDataIterator< TValue, TContainer > &left, OutputDataIterator< TValue, TContainer > &right)
make Swappable
Base for factories.
Definition: ifactory.h:28
Catalogue for item constructions.
Definition: itemcatalogue.h:30
std::vector< std::string > modelTypes() const
void registerItem(const std::string &label={})
Definition: itemcatalogue.h:60
std::unique_ptr< ItemCatalogueImpl > p_impl
Definition: itemcatalogue.h:56
ItemCatalogue & operator=(const ItemCatalogue &other)
std::vector< std::string > labels() const
void merge(const ItemCatalogue &other)
Adds content of other catalogue to this.
std::unique_ptr< SessionItem > create(const std::string &modelType) const
bool contains(const std::string &modelType) const
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
std::function< std::unique_ptr< SessionItem >()> item_factory_func_t
Definition for item factory funciton.
Definition: filesystem.h:81
Defines class CLASS?
IFactory< std::string, SessionItem > factory