BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ComponentKeyHandler.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Core/Export/ComponentKeyHandler.cpp
6 //! @brief Implement class ComponentKeyHandler.
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 
16 #include <algorithm>
17 #include <set>
18 #include <stdexcept>
19 
20 void ComponentKeyHandler::insertModel(const std::string& tag, const IComponent* s)
21 {
22  m_objects[tag].emplace_back(s);
23 }
24 
25 std::string ComponentKeyHandler::obj2key(const IComponent* s) const
26 {
27  for (auto it : m_objects) {
28  const std::vector<const IComponent*>& v = it.second;
29  const auto vpos = std::find(v.begin(), v.end(), s);
30  if (vpos == std::end(v))
31  continue;
32  const std::string& tag = it.first;
33  if (v.size() == 1)
34  return tag;
35  return tag + "_" + std::to_string(vpos - v.begin() + 1);
36  }
37  throw std::runtime_error("BUG: object not found in ComponentKeyHandler");
38 }
Defines class ComponentKeyHandler.
std::map< std::string, std::vector< const IComponent * > > m_objects
void insertModel(const std::string &tag, const IComponent *s)
std::string obj2key(const IComponent *s) const
Root of class hierarchy for sample and instrument model.
Definition: IComponent.h:21