BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IRegistry.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Sample/SampleBuilderEngine/IRegistry.h
6 //! @brief Defines templated registry for ICloneable objects
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_CORE_SAMPLEBUILDERENGINE_IREGISTRY_H
16 #define BORNAGAIN_CORE_SAMPLEBUILDERENGINE_IREGISTRY_H
17 
18 #include "Base/Types/Exceptions.h"
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 //! @class IRegistry
25 //! @ingroup tools_internal
26 //! @brief Templated object registry.
27 
28 template <class ValueType> class IRegistry
29 {
30 public:
31  const ValueType* getItem(const std::string& key) const
32  {
33  auto it = m_data.find(key);
34  if (it == m_data.end())
36  "IRegistry::createItem() -> Error. Not existing item key '" + key + "'");
37  return it->second.get();
38  }
39 
40  std::vector<std::string> keys() const
41  {
42  std::vector<std::string> result;
43  for (auto it = m_data.begin(); it != m_data.end(); ++it)
44  result.push_back(it->first);
45  return result;
46  }
47 
48  size_t size() const { return m_data.size(); }
49 
50 protected:
51  void add(const std::string& key, ValueType* item)
52  {
53  if (m_data.find(key) != m_data.end())
55  "IRegistry::createItem() -> Error. Already existing item with key '" + key + "'");
56  m_data[key] = std::unique_ptr<ValueType>(item);
57  }
58 
59 private:
60  std::map<std::string, std::unique_ptr<ValueType>> m_data;
61 };
62 
63 #endif // BORNAGAIN_CORE_SAMPLEBUILDERENGINE_IREGISTRY_H
Defines many exception classes in namespace Exceptionss.
Templated object registry.
Definition: IRegistry.h:29
size_t size() const
Definition: IRegistry.h:48
std::map< std::string, std::unique_ptr< ValueType > > m_data
Definition: IRegistry.h:60
void add(const std::string &key, ValueType *item)
Definition: IRegistry.h:51
const ValueType * getItem(const std::string &key) const
Definition: IRegistry.h:31
std::vector< std::string > keys() const
Definition: IRegistry.h:40