CASM  1.1.0
A Clusters Approach to Statistical Mechanics
DatabaseHandler_impl.hh
Go to the documentation of this file.
1 #ifndef CASM_DatabaseHandler_impl
2 #define CASM_DatabaseHandler_impl
3 
4 #include <sstream>
5 
10 
11 namespace CASM {
12 namespace DB {
13 
15 template <typename T>
16 void DatabaseHandler::insert(std::string db_name,
17  std::unique_ptr<DatabaseBase> &&value) {
18  m_db.emplace(std::make_pair(traits<T>::name, db_name), std::move(value));
19 }
20 
22 template <typename T>
24  std::string db_name, std::string calc_type,
25  std::unique_ptr<PropertiesDatabase> &&value) {
26  m_db_props.emplace(PropDBKey(traits<T>::name, db_name, calc_type),
27  std::move(value));
28 }
29 
31 template <typename T>
33  return generic_db<T>(m_default_db_name);
34 }
35 
37 template <typename T>
39  return generic_db<T>(m_default_db_name);
40 }
41 
43 template <typename T>
45  return const_generic_db<T>(m_default_db_name);
46 }
47 
49 template <typename T>
51  return db<T>(m_default_db_name);
52 }
53 
55 template <typename T>
57  return db<T>(m_default_db_name);
58 }
59 
61 template <typename T>
63  return const_db<T>(m_default_db_name);
64 }
65 
67 template <typename T>
69  return db_props<T>(m_default_db_name, calc_type);
70 }
71 
73 template <typename T>
75  std::string calc_type) const {
76  return db_props<T>(m_default_db_name, calc_type);
77 }
78 
80 template <typename T>
82  std::string calc_type) {
83  return const_db_props<T>(m_default_db_name, calc_type);
84 }
85 
87 template <typename T>
89  auto res = _find<T>(db_name);
90  return static_cast<ValDatabase<T> &>(res->second->open());
91 }
92 
94 template <typename T>
95 const ValDatabase<T> &DatabaseHandler::generic_db(std::string db_name) const {
96  auto res = _find<T>(db_name);
97  return static_cast<ValDatabase<T> &>(res->second->open());
98 }
99 
101 template <typename T>
103  auto res = _find<T>(db_name);
104  return static_cast<ValDatabase<T> &>(res->second->open());
105 }
106 
108 template <typename T>
109 Database<T> &DatabaseHandler::db(std::string db_name) {
110  auto res = _find<T>(db_name);
111  return static_cast<Database<T> &>(res->second->open());
112 }
113 
115 template <typename T>
116 const Database<T> &DatabaseHandler::db(std::string db_name) const {
117  auto res = _find<T>(db_name);
118  return static_cast<Database<T> &>(res->second->open());
119 }
120 
122 template <typename T>
123 const Database<T> &DatabaseHandler::const_db(std::string db_name) {
124  auto res = _find<T>(db_name);
125  return static_cast<Database<T> &>(res->second->open());
126 }
127 
129 template <typename T>
131  std::string calc_type) {
132  auto res = _find_props<T>(db_name, calc_type);
133  return static_cast<PropertiesDatabase &>(res->second->open());
134 }
135 
137 template <typename T>
139  std::string db_name, std::string calc_type) const {
140  auto res = _find_props<T>(db_name, calc_type);
141  return static_cast<PropertiesDatabase &>(res->second->open());
142 }
143 
145 template <typename T>
147  std::string db_name, std::string calc_type) {
148  auto res = _find_props<T>(db_name, calc_type);
149  return static_cast<PropertiesDatabase &>(res->second->open());
150 }
151 
152 template <typename T>
153 DatabaseHandler::map_type::iterator DatabaseHandler::_find(
154  std::string db_name) const {
155  auto key = std::make_pair(traits<T>::name, db_name);
156  auto res = m_db.find(key);
157  if (res == m_db.end()) {
158  _no_database_error<T>(db_name);
159  }
160  return res;
161 }
162 
163 template <typename T>
164 DatabaseHandler::props_map_type::iterator DatabaseHandler::_find_props(
165  std::string db_name, std::string calc_type) const {
166  auto key = PropDBKey(traits<T>::name, db_name, calc_type);
167  auto res = m_db_props.find(key);
168  if (res == m_db_props.end()) {
169  _no_props_database_error<T>(db_name, calc_type);
170  }
171  return res;
172 }
173 
174 template <typename T>
175 void DatabaseHandler::_no_database_error(std::string db_name) const {
176  std::stringstream ss;
177  ss << "Value: " << traits<T>::name;
178  ss << " Database: " << db_name;
179  throw std::runtime_error("Requested database not found: " + ss.str());
180 }
181 
182 template <typename T>
184  std::string calc_type) const {
185  std::stringstream ss;
186  ss << "Value: " << traits<T>::name;
187  ss << " Database: " << db_name;
188  ss << " CalcType: " << calc_type;
189  throw std::runtime_error("Requested properties database not found: " +
190  ss.str());
191 }
192 
193 } // namespace DB
194 } // namespace CASM
195 
196 #endif
const ValDatabase< T > & const_generic_db()
Access default Database<T>
PropertiesDatabase & db_props(std::string calc_type)
Access default PropertiesDatabase.
map_type::iterator _find(std::string db_name) const
void insert_props(std::string db_name, std::string calc_type, std::unique_ptr< PropertiesDatabase > &&value)
Insert a PropertiesDatabase.
ValDatabase< T > & generic_db()
Access default Database<T>
Database< T > & db()
Access default Database<T>
void insert(std::string db_name, std::unique_ptr< DatabaseBase > &&value)
Insert a Database.
const Database< T > & const_db()
Access default Database<T>
const PropertiesDatabase & const_db_props(std::string calc_type)
Access default PropertiesDatabase.
void _no_props_database_error(std::string db_name, std::string calc_type) const
void _no_database_error(std::string db_name) const
props_map_type::iterator _find_props(std::string db_name, std::string calc_type) const
Generic interface for database of a particular CASM type.
Definition: Database.hh:221
Main CASM namespace.
Definition: APICommand.hh:8