BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
IRegistry.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sample/ComponentBuilder/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_COMPONENTBUILDER_IREGISTRY_H
21 #define BORNAGAIN_SAMPLE_COMPONENTBUILDER_IREGISTRY_H
22 
23 #include "Base/Util/Assert.h"
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 //! @class IRegistry
30 //! @brief Templated object registry.
31 
32 template <class ValueType>
33 class IRegistry {
34 public:
35  const ValueType* getItem(const std::string& key) const
36  {
37  auto it = m_data.find(key);
38  ASSERT(it != m_data.end());
39  return it->second.get();
40  }
41 
42  std::vector<std::string> keys() const
43  {
44  std::vector<std::string> result;
45  for (const auto& it : m_data)
46  result.push_back(it.first);
47  return result;
48  }
49 
50  size_t size() const { return m_data.size(); }
51 
52 protected:
53  void add(const std::string& key, ValueType* item)
54  {
55  ASSERT(m_data.find(key) == m_data.end());
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_SAMPLE_COMPONENTBUILDER_IREGISTRY_H
64 #endif // USER_API
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:45
Templated object registry.
Definition: IRegistry.h:33
size_t size() const
Definition: IRegistry.h:50
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:53
const ValueType * getItem(const std::string &key) const
Definition: IRegistry.h:35
std::vector< std::string > keys() const
Definition: IRegistry.h:42