CASM  1.1.0
A Clusters Approach to Statistical Mechanics
QueryHandler_impl.hh
Go to the documentation of this file.
1 #ifndef CASM_QueryHandler_impl
2 #define CASM_QueryHandler_impl
3 
7 #include "casm/casm_io/Log.hh"
12 
13 namespace CASM {
14 
15 template <typename DataObject>
17  : m_set(&set),
19  *m_dict = make_dictionary<DataObject>();
20 
21  // add plugins
22  load_query_plugins(set, std::inserter(*m_dict, m_dict->end()),
23  std::inserter(m_lib, m_lib.end()));
24 };
25 
26 template <typename DataObject>
28  // order of deletion matters
29  m_dict->clear();
30  m_lib.clear();
31 }
32 
33 template <typename DataObject>
35  return *m_dict;
36 }
37 
38 template <typename DataObject>
40  const {
41  return *m_dict;
42 }
43 
47 template <typename DataObject>
49  const DB::Selected<DataObject> &selection) {
50  if (m_dict->find("selected") != m_dict->end()) {
51  m_dict->erase("selected");
52  }
53  m_dict->insert(datum_formatter_alias(
54  "selected", selection,
55  "Returns true if object is specified in the input selection"));
56 }
57 
61 template <typename DataObject>
63  const DB::Selection<DataObject> &selection) {
64  set_selected(DB::Selected<DataObject>(selection));
65 }
66 
71 template <typename DataObject>
72 void QueryHandler<DataObject>::add_alias(const std::string &alias_name,
73  const std::string &alias_command) {
74  auto new_formatter =
75  datum_formatter_alias<DataObject>(alias_name, alias_command, *m_dict);
76  auto key = m_dict->key(new_formatter);
77 
78  // if not in dictionary (includes operator dictionary), add
79  if (m_dict->find(key) == m_dict->end()) {
80  m_dict->insert(new_formatter);
81  }
82  // if a user-created alias, over-write with message
83  else if (aliases().find(alias_name) != aliases().end()) {
84  err_log() << "WARNING: I already know '" << alias_name << "' as:\n"
85  << " " << _aliases()[alias_name] << "\n"
86  << " I will forget it and learn '" << alias_name
87  << "' as:\n"
88  << " " << alias_command << std::endl;
89  m_dict->insert(new_formatter);
90  }
91  // else do not add, throw error
92  else {
93  std::stringstream ss;
94  ss << "Error: Attempted to over-write standard CASM query name with user "
95  "alias.\n";
96  throw std::runtime_error(ss.str());
97  }
98 
99  // save alias
100  _aliases()[alias_name] = alias_command;
101 }
102 
104 template <typename DataFormatterDictInserter, typename RuntimeLibInserter>
105 std::pair<DataFormatterDictInserter, RuntimeLibInserter> load_query_plugins(
106  const ProjectSettings &set, DataFormatterDictInserter dict_it,
107  RuntimeLibInserter lib_it) {
108  typedef typename DataFormatterDictInserter::container_type dict_type;
109  typedef typename dict_type::DataObject DataObject;
110  typedef typename dict_type::DatumFormatterType formatter_type;
111  typedef formatter_type *formatter_type_ptr;
112  typedef formatter_type_ptr(signature)();
113 
114  const DirectoryStructure &dir = set.dir();
115 
116  if (dir.root_dir().empty()) {
117  return std::make_pair(dict_it, lib_it);
118  }
119 
120  if (fs::is_directory(dir.query_plugins<DataObject>())) {
121  // loop over custom query files *.cc
122  for (auto &entry : boost::make_iterator_range(
123  fs::directory_iterator(dir.query_plugins<DataObject>()), {})) {
124  fs::path p = entry.path();
125  std::string p_s = p.string();
126  auto p_size = p_s.size();
127 
128  if (fs::is_regular_file(p) &&
129  p_s.compare(p_size - 3, p_size, ".cc") == 0) {
130  fs::path f = p.filename();
131  std::string f_s = f.string();
132  auto f_size = f_s.size();
133 
134  std::string msg =
135  "compiling new custom query: " + f_s.substr(0, f_size - 3);
136 
137  // '-L$CASM_PREFIX/.libs' is a hack so 'make check' works
138  auto lib_ptr = log_make_shared_runtime_lib(
139  p_s.substr(0, p_size - 3),
140  set.compile_options() + " " +
141  include_path(dir.query_plugins<DataObject>()),
142  set.so_options() + " -lcasm ", msg);
143 
144  auto make_formatter = lib_ptr->template get_function<signature>(
145  "make_" + f_s.substr(0, f_size - 3) + "_formatter");
146 
147  std::unique_ptr<formatter_type> ptr(make_formatter());
148 
149  // will clone on insert
150  *dict_it++ = *ptr;
151  *lib_it++ = std::make_pair(ptr->name(), lib_ptr);
152  }
153  }
154  }
155 
156  return std::make_pair(dict_it, lib_it);
157 }
158 
159 } // namespace CASM
160 
161 #endif
Returns true if configuration is specified in given selection (default: MASTER)
Definition: Selected.hh:20
Parsing dictionary for constructing a DataFormatter<DataObject> object.
Specification of CASM project directory structure.
fs::path root_dir() const
Return casm project directory path.
fs::path query_plugins() const
Return enumerators plugin dir.
DirectoryStructure const & dir() const
Access DirectoryStructure object. Throw if not set.
std::string compile_options() const
std::string so_options() const
QueryHandler(const ProjectSettings &set)
DataFormatterDictionary< DataObject > & dict()
std::map< std::string, std::shared_ptr< RuntimeLibrary > > m_lib
Definition: QueryHandler.hh:68
_DataObject DataObject
Definition: QueryHandler.hh:26
notstd::cloneable_ptr< DataFormatterDictionary< DataObject > > m_dict
Definition: QueryHandler.hh:66
void add_alias(const std::string &alias_name, const std::string &alias_command)
Add user-defined query alias.
void set_selected(const DB::Selected< DataObject > &selection)
Set the selection to be used for the 'selected' column.
DatumFormatterAlias< DataObject > datum_formatter_alias(const std::string &_name, const std::string &_command, const DataFormatterDictionary< DataObject > &_dict, const std::string &_help="")
Make a DatumFormatterAlias.
Main CASM namespace.
Definition: APICommand.hh:8
std::shared_ptr< RuntimeLibrary > log_make_shared_runtime_lib(std::string filename_base, std::string compile_options, std::string so_options, std::string compile_msg)
Make shared_ptr<RuntimeLibrary>, logging progress and errors.
std::string include_path(const fs::path &dir)
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
std::pair< DataFormatterDictInserter, RuntimeLibInserter > load_query_plugins(const ProjectSettings &set, DataFormatterDictInserter dict_it, RuntimeLibInserter lib_it)
Load enumerator plugins from a CASM project.
Log & err_log()
Definition: Log.hh:426
Non-std smart pointer classes and functions.
cloneable_ptr< T > make_cloneable(Args &&... args)
make a cloneable_ptr<T> via T(Args... args)
DirectoryStructure const & dir
Definition: settings.cc:136
ProjectSettings & set
Definition: settings.cc:137