CASM  1.1.0
A Clusters Approach to Statistical Mechanics
DatabaseHandler.hh
Go to the documentation of this file.
1 #ifndef CASM_DatabaseHandler
2 #define CASM_DatabaseHandler
3 
4 #include <map>
5 #include <memory>
6 #include <string>
7 #include <utility>
8 
9 namespace CASM {
10 
11 class PrimClex;
12 
13 namespace DB {
14 
15 class DatabaseBase;
16 template <typename T>
17 class Database;
18 template <typename T>
19 class ValDatabase;
20 class PropertiesDatabase;
21 
29  public:
34  DatabaseHandler(const PrimClex &_primclex);
35 
37 
38  const PrimClex &primclex() const;
39 
41  template <typename T>
43 
45  template <typename T>
46  const ValDatabase<T> &generic_db() const;
47 
49  template <typename T>
51 
53  template <typename T>
54  Database<T> &db();
55 
57  template <typename T>
58  const Database<T> &db() const;
59 
61  template <typename T>
62  const Database<T> &const_db();
63 
65  template <typename T>
66  PropertiesDatabase &db_props(std::string calc_type);
67 
69  template <typename T>
70  const PropertiesDatabase &db_props(std::string calc_type) const;
71 
73  template <typename T>
74  const PropertiesDatabase &const_db_props(std::string calc_type);
75 
77  void close();
78 
80  template <typename T>
81  void insert(std::string db_name, std::unique_ptr<DatabaseBase> &&value);
82 
84  template <typename T>
85  void insert_props(std::string db_name, std::string calc_type,
86  std::unique_ptr<PropertiesDatabase> &&value);
87 
88  // --- Access non-default database (i.e. jsonDB, lmdbDB, mongoDB) ---
89  // - This would be used to migrate from one to the other, so it it not
90  // a common use case
91 
93  template <typename T>
94  ValDatabase<T> &generic_db(std::string db_name);
95 
97  template <typename T>
98  const ValDatabase<T> &generic_db(std::string db_name) const;
99 
101  template <typename T>
102  const ValDatabase<T> &const_generic_db(std::string db_name);
103 
105  template <typename T>
106  Database<T> &db(std::string db_name);
107 
109  template <typename T>
110  const Database<T> &db(std::string db_name) const;
111 
113  template <typename T>
114  const Database<T> &const_db(std::string db_name);
115 
117  template <typename T>
118  PropertiesDatabase &db_props(std::string db_name, std::string calc_type);
119 
121  template <typename T>
122  const PropertiesDatabase &db_props(std::string db_name,
123  std::string calc_type) const;
124 
126  template <typename T>
127  const PropertiesDatabase &const_db_props(std::string db_name,
128  std::string calc_type);
129 
130  private:
131  // std::pair<traits<ValueType>::name, db_name> -> db
132  typedef std::map<std::pair<std::string, std::string>,
133  std::unique_ptr<DatabaseBase> >
135 
136  class PropDBKey {
137  public:
138  PropDBKey(std::string _config_type, std::string _db_type,
139  std::string _calc_type)
140  : m_value(_config_type, _db_type, _calc_type) {}
141 
142  std::string &config_type() { return std::get<0>(m_value); }
143  const std::string &config_type() const { return std::get<0>(m_value); }
144 
145  std::string &db_type() { return std::get<1>(m_value); }
146  const std::string &db_type() const { return std::get<1>(m_value); }
147 
148  std::string &calc_type() { return std::get<2>(m_value); }
149  const std::string &calc_type() const { return std::get<2>(m_value); }
150 
151  bool operator<(const PropDBKey &B) const { return m_value < B.m_value; }
152 
153  private:
154  std::tuple<std::string, std::string, std::string> m_value;
155  };
156 
157  // std::pair<traits<ValueType>::name, db_name, calctype> -> db
158  typedef std::map<PropDBKey, std::unique_ptr<PropertiesDatabase> >
160 
161  template <typename T>
162  map_type::iterator _find(std::string db_name) const;
163 
164  template <typename T>
165  props_map_type::iterator _find_props(std::string db_name,
166  std::string calc_type) const;
167 
168  template <typename T>
169  void _no_database_error(std::string db_name) const;
170 
171  template <typename T>
172  void _no_props_database_error(std::string db_name,
173  std::string calc_type) const;
174 
176 
177  std::string m_default_db_name;
178 
179  // std::pair<traits<ValueType>::name, db_name> -> db
180  // mutable for lazy initialization
181  mutable map_type m_db;
182 
183  // PropDBKey -> db_props
184  // mutable for lazy initialization
186 };
187 
188 } // namespace DB
189 } // namespace CASM
190 
191 #endif
const std::string & db_type() const
bool operator<(const PropDBKey &B) const
const std::string & config_type() const
PropDBKey(std::string _config_type, std::string _db_type, std::string _calc_type)
std::tuple< std::string, std::string, std::string > m_value
const std::string & calc_type() const
Provides access to all databases.
const ValDatabase< T > & const_generic_db()
Access default Database<T>
std::map< std::pair< std::string, std::string >, std::unique_ptr< DatabaseBase > > map_type
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>
const PrimClex & primclex() const
Database< T > & db()
Access default Database<T>
void close()
Close all databases.
DatabaseHandler(const PrimClex &_primclex)
void insert(std::string db_name, std::unique_ptr< DatabaseBase > &&value)
Insert a Database.
const Database< T > & const_db()
Access default Database<T>
std::map< PropDBKey, std::unique_ptr< PropertiesDatabase > > props_map_type
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
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
Main CASM namespace.
Definition: APICommand.hh:8