CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Database_impl.hh
Go to the documentation of this file.
1 #ifndef CASM_Database_impl
2 #define CASM_Database_impl
3 
8 #include "casm/clex/PrimClex.hh"
10 
11 namespace CASM {
12 namespace DB {
13 
15 template <typename ValueType>
16 std::pair<typename ValDatabase<ValueType>::iterator, bool>
17 ValDatabase<ValueType>::set_alias(const std::string &name_or_alias,
18  const std::string &alias) {
19  auto it = find(name_or_alias);
20 
21  // if name_or_alias not valid
22  if (it == end()) {
23  // - return (end, false)
24  return std::make_pair(it, false);
25  }
26  // if name_or_alias valid, but alias already used
27  else if (m_alias_to_name.find(alias) != m_alias_to_name.end()) {
28  // - return (it, false)
29  return std::make_pair(it, false);
30  }
31 
32  // if name_or_alias valid, and alias available
33  std::string name;
34 
35  // if name_or_alias is name
36  if (m_alias_to_name.find(name_or_alias) == m_alias_to_name.end()) {
37  name = name_or_alias;
38 
39  auto old_alias_it = m_name_to_alias.find(name);
40  // if there is an old_alias, erase it
41  if (old_alias_it != m_name_to_alias.end()) {
42  m_alias_to_name.erase(old_alias_it->second);
43  }
44  }
45  // if name_or_alias is old_alias
46  else {
47  // std::string old_alias = name_or_alias;
48  name = m_alias_to_name[name_or_alias];
49  m_alias_to_name.erase(name_or_alias);
50  }
51 
52  // update data
53  m_name_to_alias[name] = alias;
54  m_alias_to_name[alias] = name;
55 
56  return std::make_pair(it, true);
57 }
58 
64 template <typename ValueType>
66  const std::string &name_or_alias) const {
67  auto it = m_alias_to_name.find(name_or_alias);
68  if (it == m_alias_to_name.end()) {
69  return name_or_alias;
70  }
71  return it->second;
72 }
73 
82 template <typename ValueType>
84  const std::string &name_or_alias) const {
85  auto it = m_alias_to_name.find(name_or_alias);
86 
87  // if name_or_alias is an alias
88  if (it != m_alias_to_name.end()) {
89  return name_or_alias;
90  }
91 
92  auto name_it = m_name_to_alias.find(name_or_alias);
93 
94  // name has no known alias
95  // - could be because name is valid, but no alias is set
96  // - or because name is not valid
97  if (name_it == m_name_to_alias.end()) {
98  return std::string("");
99  }
100 
101  // name has known alias
102  return name_it->second;
103 }
104 
105 template <typename ValueType>
107  if (!primclex().has_dir()) {
108  return;
109  }
110  fs::path p = primclex().dir().template aliases<ValueType>();
111  if (fs::exists(p)) {
112  jsonParser json(p);
113  from_json(m_name_to_alias, json);
114  for (const auto &val : m_name_to_alias) {
115  m_alias_to_name[val.second] = val.first;
116  }
117  }
118 }
119 
120 template <typename ValueType>
122  if (!primclex().has_dir()) {
123  throw std::runtime_error(
124  "Error in write_aliases: CASM project has no root directory.");
125  }
126  fs::path p = primclex().dir().template aliases<ValueType>();
127  fs::create_directories(p.parent_path());
128 
129  SafeOfstream file;
130  file.open(p);
131  jsonParser json(m_name_to_alias);
132  json.print(file.ofstream());
133  file.close();
134 }
135 
136 } // namespace DB
137 } // namespace CASM
138 
139 #endif
std::string name(const std::string &name_or_alias) const
std::string alias(const std::string &name_or_alias) const
Get alias from name_or_alias.
std::pair< iterator, bool > set_alias(const std::string &name_or_alias, const std::string &alias)
For setting an alias.
Write to a temporary file to ensure a good write, then rename.
Definition: SafeOfstream.hh:31
void close()
Closes stream, and if not a failed write, removes "file" and renames "file.tmp" to "file".
Definition: SafeOfstream.hh:84
void open(fs::path name, std::string tmp_ext="tmp")
Opens "file.tmp" for writing, with intended final target "file".
Definition: SafeOfstream.hh:65
fs::ofstream & ofstream()
Access underlying stream.
Definition: SafeOfstream.hh:80
void print(std::ostream &stream, unsigned int indent=2, unsigned int prec=12) const
Print json to stream.
Definition: jsonParser.cc:188
jsonParser const & from_json(ImportSettings &_set, jsonParser const &_json)
Definition: Import.cc:18
Main CASM namespace.
Definition: APICommand.hh:8
GenericDatumFormatter< std::string, DataObject > alias()
GenericDatumFormatter< std::string, DataObject > name()
Iterator find(Iterator begin, Iterator end, const T &value, BinaryCompare q)
Equivalent to std::find(begin, end, value), but with custom comparison.
Definition: algorithm.hh:16
PrimClex * primclex
Definition: settings.cc:135