BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelView::ItemPool Class Reference

Provides registration of SessionItem pointers and their unique identifiers in global memory pool. More...

Public Member Functions

 ItemPool ()=default
 
 ItemPool (const ItemPool &)=delete
 
 ItemPool (ItemPool &&)=delete
 
SessionItemitem_for_key (const identifier_type &key) const
 
identifier_type key_for_item (const SessionItem *item) const
 
ItemPooloperator= (const ItemPool &)=delete
 
ItemPooloperator= (ItemPool &&)=delete
 
identifier_type register_item (SessionItem *item, identifier_type key={})
 
size_t size () const
 
void unregister_item (SessionItem *item)
 

Private Attributes

std::map< const SessionItem *, identifier_typem_item_to_key
 
std::map< identifier_type, SessionItem * > m_key_to_item
 

Detailed Description

Provides registration of SessionItem pointers and their unique identifiers in global memory pool.

Definition at line 29 of file itempool.h.

Constructor & Destructor Documentation

◆ ItemPool() [1/3]

ModelView::ItemPool::ItemPool ( )
default

◆ ItemPool() [2/3]

ModelView::ItemPool::ItemPool ( const ItemPool )
delete

◆ ItemPool() [3/3]

ModelView::ItemPool::ItemPool ( ItemPool &&  )
delete

Member Function Documentation

◆ item_for_key()

SessionItem * ItemPool::item_for_key ( const identifier_type key) const

Definition at line 72 of file itempool.cpp.

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 }
std::map< identifier_type, SessionItem * > m_key_to_item
Definition: itempool.h:47

References m_key_to_item.

◆ key_for_item()

identifier_type ItemPool::key_for_item ( const SessionItem item) const

Definition at line 63 of file itempool.cpp.

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 }
std::map< const SessionItem *, identifier_type > m_item_to_key
Definition: itempool.h:48

References m_item_to_key.

◆ operator=() [1/2]

ItemPool& ModelView::ItemPool::operator= ( const ItemPool )
delete

◆ operator=() [2/2]

ItemPool& ModelView::ItemPool::operator= ( ItemPool &&  )
delete

◆ register_item()

identifier_type ItemPool::register_item ( SessionItem item,
identifier_type  key = {} 
)

Definition at line 28 of file itempool.cpp.

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 }
static identifier_type generate()

References ModelView::UniqueIdGenerator::generate(), m_item_to_key, and m_key_to_item.

Here is the call graph for this function:

◆ size()

size_t ItemPool::size ( ) const

Definition at line 21 of file itempool.cpp.

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 }

References m_item_to_key, and m_key_to_item.

Referenced by TEST_F().

◆ unregister_item()

void ItemPool::unregister_item ( SessionItem item)

Definition at line 50 of file itempool.cpp.

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 }

References m_item_to_key, and m_key_to_item.

Member Data Documentation

◆ m_item_to_key

std::map<const SessionItem*, identifier_type> ModelView::ItemPool::m_item_to_key
private

Definition at line 48 of file itempool.h.

Referenced by key_for_item(), register_item(), size(), and unregister_item().

◆ m_key_to_item

std::map<identifier_type, SessionItem*> ModelView::ItemPool::m_key_to_item
private

Definition at line 47 of file itempool.h.

Referenced by item_for_key(), register_item(), size(), and unregister_item().


The documentation for this class was generated from the following files: