CASM  1.1.0
A Clusters Approach to Statistical Mechanics
json_io_impl.hh
Go to the documentation of this file.
1 #ifndef CASM_database_json_io_impl
2 #define CASM_database_json_io_impl
3 
9 #include "casm/misc/TypeInfo.hh"
10 
11 namespace CASM {
12 namespace DB {
13 
29 template <typename DataObject>
31  const jsonParser &kwargs,
32  std::string name_key, std::string sel_key,
33  OnError on_error) {
34  std::vector<std::string> obj_names;
35  std::string sel_name;
36  kwargs.get_else(obj_names, name_key, std::vector<std::string>());
37  kwargs.get_else(sel_name, sel_key, std::string("NONE"));
38 
39  DB::Selection<DataObject> sel(db, sel_name);
40  for (const auto &name : obj_names) {
41  // validate obj_names & handle errors
42  if (!sel.db().count(name)) {
43  if (on_error == OnError::THROW || on_error == OnError::WARN) {
44  std::string msg = "Error in make_selection<" + type_name<DataObject>() +
45  ">: \"" + name + "\" is not in the database.";
46  throw std::runtime_error(msg);
47  } else if (on_error == OnError::CONTINUE) {
48  continue;
49  } else {
50  std::string msg = "Error in make_selection<" + type_name<DataObject>() +
51  ">: Unknown error";
52  throw std::runtime_error(msg);
53  }
54  }
55  sel.data()[name] = true;
56  }
57  return sel;
58 }
59 
60 } // namespace DB
61 } // namespace CASM
62 
63 #endif
map_type & data()
Definition: Selection.cc:250
Database< ObjType > & db() const
Definition: Selection.cc:212
bool get_else(T &t, const std::string &key, const T &default_value, Args &&... args) const
Definition: jsonParser.hh:775
DB::Selection< DataObject > make_selection(DB::Database< DataObject > &db, const jsonParser &kwargs, std::string name_key, std::string sel_key, OnError on_error=OnError::THROW)
Make a DB::Selection from JSON input.
Definition: json_io_impl.hh:30
Main CASM namespace.
Definition: APICommand.hh:8
OnError
Definition: enum.hh:37
GenericDatumFormatter< std::string, DataObject > name()