BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
ComponentKeyHandler.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sim/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 INode* s)
21 {
22  m_objects[tag].emplace_back(s);
23 }
24 
25 std::string ComponentKeyHandler::obj2key(const INode* s) const
26 {
27  for (const auto& it : m_objects) {
28  const std::vector<const INode*>& 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::string obj2key(const INode *s) const
std::map< std::string, std::vector< const INode * > > m_objects
void insertModel(const std::string &tag, const INode *s)
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:40