BornAgain  1.19.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 reflection and scattering
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 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_SAMPLE_SAMPLEBUILDERENGINE_IREGISTRY_H
21 #define BORNAGAIN_SAMPLE_SAMPLEBUILDERENGINE_IREGISTRY_H
22 
23 #include <map>
24 #include <memory>
25 #include <stdexcept>
26 #include <string>
27 #include <vector>
28 
29 //! @class IRegistry
30 //! @ingroup tools_internal
31 //! @brief Templated object registry.
32 
33 template <class ValueType> class IRegistry {
34 public:
35  const ValueType* getItem(const std::string& key) const
36  {
37  auto it = m_data.find(key);
38  if (it == m_data.end())
39  throw std::runtime_error("Key '" + key + "' not found in registry");
40  return it->second.get();
41  }
42 
43  std::vector<std::string> keys() const
44  {
45  std::vector<std::string> result;
46  for (const auto& it : m_data)
47  result.push_back(it.first);
48  return result;
49  }
50 
51  size_t size() const { return m_data.size(); }
52 
53 protected:
54  void add(const std::string& key, ValueType* item)
55  {
56  if (m_data.find(key) != m_data.end())
57  throw std::runtime_error("Key '" + key + "' already in registry");
58  m_data[key] = std::unique_ptr<ValueType>(item);
59  }
60 
61 private:
62  std::map<std::string, std::unique_ptr<ValueType>> m_data;
63 };
64 
65 #endif // BORNAGAIN_SAMPLE_SAMPLEBUILDERENGINE_IREGISTRY_H
66 #endif // USER_API
Templated object registry.
Definition: IRegistry.h:33
size_t size() const
Definition: IRegistry.h:51
std::map< std::string, std::unique_ptr< ValueType > > m_data
Definition: IRegistry.h:62
void add(const std::string &key, ValueType *item)
Definition: IRegistry.h:54
const ValueType * getItem(const std::string &key) const
Definition: IRegistry.h:35
std::vector< std::string > keys() const
Definition: IRegistry.h:43