BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MaterialKeyHandler.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Core/Export/MaterialKeyHandler.cpp
6 //! @brief Implement class MaterialKeyHandler.
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 "Base/Utils/Assert.h"
18 #include <algorithm>
19 #include <set>
20 #include <stdexcept>
21 
23 {
24  for (const auto& it : m_Mat2Unique)
25  if (*it.second == *mat) {
26  m_Mat2Unique.emplace(mat, it.second);
27  return;
28  }
29  m_Mat2Unique.emplace(mat, mat);
30 
31  const std::string key = "material_" + mat->getName();
32  if (m_Key2Mat.count(key))
33  throw std::runtime_error(
34  "Material name " + mat->getName()
35  + " used more than once, which is not supported by Python exporter");
36  m_Key2Mat.emplace(key, mat);
37 }
38 
39 const std::string& MaterialKeyHandler::mat2key(const Material* mat) const
40 {
41  const Material* unique_mat = m_Mat2Unique.at(mat);
42  for (const auto& it : m_Key2Mat)
43  if (it.second == unique_mat)
44  return it.first;
45  ASSERT(0);
46 }
47 
48 const std::map<const std::string, const Material*>& MaterialKeyHandler::materialMap() const
49 {
50  return m_Key2Mat;
51 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
Defines class MaterialKeyHandler.
Defines and implements class Material.
const std::map< const std::string, const Material * > & materialMap() const
void insertMaterial(const Material *sample)
const std::string & mat2key(const Material *sample) const
std::map< const Material *, const Material * > m_Mat2Unique
std::map< const std::string, const Material * > m_Key2Mat
A wrapper for underlying material implementation.
Definition: Material.h:29
std::string getName() const
Returns the name of material.
Definition: Material.cpp:66