CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
QueryHandler_impl.hh
Go to the documentation of this file.
4 
5 
6 namespace CASM {
7 
8  template<typename DataObject>
10  m_set(&set),
11  m_dict(make_dictionary<DataObject>()) {
12 
13  // add plugins
15  set,
16  std::inserter(m_dict, m_dict.end()),
17  std::inserter(m_lib, m_lib.end()));
18  };
19 
20  template<typename DataObject>
22  // order of deletion matters
23  m_dict.clear();
24  m_lib.clear();
25  }
26 
27  template<typename DataObject>
29  return m_dict;
30  }
31 
32  template<typename DataObject>
34  return m_dict;
35  }
36 
40  template<typename DataObject>
42  if(m_dict.find("selected") != m_dict.end()) {
43  m_dict.erase("selected");
44  }
45  m_dict.insert(
47  "selected",
48  selection,
49  "Returns true if configuration is specified in the input selection"
50  )
51  );
52  }
53 
57  template<typename DataObject>
59  typedef typename QueryTraits<DataObject>::Selected selected_type;
60  set_selected(selected_type(selection));
61  }
62 
67  template<typename DataObject>
68  void QueryHandler<DataObject>::add_alias(const std::string &alias_name, const std::string &alias_command) {
69 
70  auto new_formatter = datum_formatter_alias<DataObject>(alias_name, alias_command, m_dict);
71  auto key = m_dict.key(new_formatter);
72 
73  // if not in dictionary (includes operator dictionary), add
74  if(m_dict.find(key) == m_dict.end()) {
75  m_dict.insert(new_formatter);
76  }
77  // if a user-created alias, over-write with message
78  else if(aliases().find(alias_name) != aliases().end()) {
79  m_set->err_log() << "WARNING: I already know '" << alias_name << "' as:\n"
80  << " " << _aliases()[alias_name] << "\n"
81  << " I will forget it and learn '" << alias_name << "' as:\n"
82  << " " << alias_command << std::endl;
83  m_dict.insert(new_formatter);
84  }
85  // else do not add, throw error
86  else {
87  std::stringstream ss;
88  ss << "Error: Attempted to over-write standard CASM query name with user alias.\n";
89  throw std::runtime_error(ss.str());
90  }
91 
92  // save alias
93  _aliases()[alias_name] = alias_command;
94 
95  }
96 
98  template<typename DataFormatterDictInserter, typename RuntimeLibInserter>
99  std::pair<DataFormatterDictInserter, RuntimeLibInserter>
101  const ProjectSettings &set,
102  DataFormatterDictInserter dict_it,
103  RuntimeLibInserter lib_it) {
104 
105  typedef typename DataFormatterDictInserter::container_type dict_type;
106  typedef typename dict_type::DataObject DataObject;
107  typedef typename dict_type::DatumFormatterType formatter_type;
108  typedef formatter_type *formatter_type_ptr;
109  typedef formatter_type_ptr(signature)();
110 
111  const DirectoryStructure &dir = set.dir();
112 
113  if(dir.root_dir().empty()) {
114  return std::make_pair(dict_it, lib_it);
115  }
116 
117  if(fs::is_directory(dir.query_plugins<DataObject>())) {
118 
119  // loop over custom query files *.cc
120  for(auto &entry : boost::make_iterator_range(fs::directory_iterator(dir.query_plugins<DataObject>()), {})) {
121 
122  fs::path p = entry.path();
123  std::string p_s = p.string();
124  auto p_size = p_s.size();
125 
126  if(fs::is_regular_file(p) && p_s.compare(p_size - 3, p_size, ".cc") == 0) {
127 
128  fs::path f = p.filename();
129  std::string f_s = f.string();
130  auto f_size = f_s.size();
131 
132  std::string msg = "compiling new custom query: " + f_s.substr(0, f_size - 3);
133 
134  // '-L$CASM_PREFIX/.libs' is a hack so 'make check' works
135  auto lib_ptr = std::make_shared<RuntimeLibrary>(
136  p_s.substr(0, p_size - 3),
137  set.compile_options() + " " + include_path(dir.query_plugins<DataObject>()),
138  set.so_options() + " -lcasm ",
139  msg,
140  set);
141 
142  auto make_formatter = lib_ptr->template get_function<signature>(
143  "make_" + f_s.substr(0, f_size - 3) + "_formatter");
144 
145  std::unique_ptr<formatter_type> ptr(make_formatter());
146 
147  // will clone on insert
148  *dict_it++ = *ptr;
149  *lib_it++ = std::make_pair(ptr->name(), lib_ptr);
150  }
151  }
152  }
153 
154  return std::make_pair(dict_it, lib_it);
155  }
156 
157 }
DatumFormatterAlias< DataObject > datum_formatter_alias(const std::string &_name, const std::string &_command, const DataFormatterDictionary< DataObject > &_dict, const std::string &_help="")
Make a DatumFormatterAlias.
_DataObject DataObject
Definition: QueryHandler.hh:22
DataFormatterDictionary< DataObject > make_dictionary()
Template to can be specialized for constructing dictionaries for particular DataObject.
Specification of CASM project directory structure.
std::string include_path(const fs::path &dir)
std::string so_options() const
Get current shared library options string.
Main CASM namespace.
Definition: complete.cpp:8
QueryHandler(const ProjectSettings &set)
ProjectSettings & set
Definition: settings.cc:103
Read/modify settings of an already existing CASM project.
void set_selected(const typename QueryTraits< DataObject >::Selected &selection)
Set the selection to be used for the 'selected' column.
DataFormatterDictionary< DataObject > m_dict
Definition: QueryHandler.hh:78
std::pair< DataFormatterDictInserter, RuntimeLibInserter > load_query_plugins(const ProjectSettings &set, DataFormatterDictInserter dict_it, RuntimeLibInserter lib_it)
Load enumerator plugins from a CASM project.
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:10
std::string compile_options() const
Get current compilation options string.
DataFormatterDictionary< DataObject > & dict()
void add_alias(const std::string &alias_name, const std::string &alias_command)
Add user-defined query alias.
DirectoryStructure & dir
Definition: settings.cc:102
std::map< std::string, std::shared_ptr< RuntimeLibrary > > m_lib
Definition: QueryHandler.hh:80
const DirectoryStructure & dir() const