BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
itempool.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/itempool.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 
15 #include "mvvm/model/itempool.h"
17 #include <stdexcept>
18 
19 using namespace ModelView;
20 
21 size_t ItemPool::size() const
22 {
23  if (m_key_to_item.size() != m_item_to_key.size())
24  throw std::runtime_error("Error in ItemPool: array size mismatch");
25  return m_key_to_item.size();
26 }
27 
29 
30 {
31  if (m_item_to_key.find(item) != m_item_to_key.end())
32  throw std::runtime_error("ItemPool::register_item() -> Attempt to register already "
33  "registered item.");
34 
35  if (key.empty()) {
37  while (m_key_to_item.find(key) != m_key_to_item.end())
38  key = UniqueIdGenerator::generate(); // preventing improbable duplicates
39  } else {
40  if (m_key_to_item.find(key) != m_key_to_item.end())
41  throw std::runtime_error(" ItemPool::register_item() -> Attempt to reuse existing key");
42  }
43 
44  m_key_to_item.insert(std::make_pair(key, item));
45  m_item_to_key.insert(std::make_pair(item, key));
46 
47  return key;
48 }
49 
51 {
52  auto it = m_item_to_key.find(item);
53  if (it == m_item_to_key.end())
54  throw std::runtime_error("ItemPool::deregister_item() -> Attempt to deregister "
55  "non existing item.");
56  auto key = it->second;
57  m_item_to_key.erase(it);
58 
59  auto it2 = m_key_to_item.find(key);
60  m_key_to_item.erase(it2);
61 }
62 
64 {
65  const auto it = m_item_to_key.find(item);
66  if (it != m_item_to_key.end())
67  return it->second;
68 
69  return {};
70 }
71 
73 {
74  auto it = m_key_to_item.find(key);
75  if (it != m_key_to_item.end())
76  return it->second;
77 
78  return nullptr;
79 }
identifier_type register_item(SessionItem *item, identifier_type key={})
Definition: itempool.cpp:28
void unregister_item(SessionItem *item)
Definition: itempool.cpp:50
std::map< const SessionItem *, identifier_type > m_item_to_key
Definition: itempool.h:48
std::map< identifier_type, SessionItem * > m_key_to_item
Definition: itempool.h:47
size_t size() const
Definition: itempool.cpp:21
identifier_type key_for_item(const SessionItem *item) const
Definition: itempool.cpp:63
SessionItem * item_for_key(const identifier_type &key) const
Definition: itempool.cpp:72
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
static identifier_type generate()
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
std::string identifier_type
Definition: types.h:22
Defines class CLASS?