CASM  1.1.0
A Clusters Approach to Statistical Mechanics
SimpleFormatterDict.hh
Go to the documentation of this file.
1 namespace CASM {
2 namespace simpleformatter {
3 
4 // want to construct DataFormatter from query string:
5 //
6 // DataFormatterOptions options {
7 // ... stream? file? json? csv? sep? width? etc. ...
8 // }
9 //
10 // std::string query_string {"name1(args) name2(args) name3(args)"};
11 //
12 // std::unique_ptr<DataFormatter<DataObject>> =
13 // make_data_formatter<DataFormatter<DataObject>>(
14 // DataFormatterDict<DataObject> const &dict,
15 // DataFormatterOptions const &options,
16 // std::string query_string);
17 //
18 
19 template <typename ObjectType>
20 using EvaluatorPtrType = std::unique_ptr<BaseDatumEvaluator<ObjectType>>;
21 
22 template <typename ObjectType, typename InputType>
24  std::function<EvaluatorPtrType<ObjectType>(InputType const &)>;
25 
26 template <typename ObjectType, typename InputType>
28  public:
30  std::string _name, std::string _description,
32  : m_name(_name),
33  m_description(_description),
34  m_make_evaluator_f(_make_evaluator_f) {}
35 
36  std::string name() const { return m_name; }
37 
38  std::string description() const { return m_description; }
39 
40  std::unique_ptr<BaseDatumFormatter<ObjectType>> make(
41  InputType const &input) const {
42  return m_make_evaluator_f(input);
43  }
44 
45  private:
46  std::string m_name;
47  std::string m_description;
49 };
50 
51 std::unique_ptr<DatumEvaluatorDictEntry<DataObject>> make_dict_entry_ptr(
52  std::string _name, std::string _description,
53  MakeEvaluatorPtrFunction<ObjectType> _make_evaluator_f);
54 
55 template <typename ObjectType>
57  public:
58  typedef std::map<std::string, DatumEvaluatorEntry<ObjectType>> map_type;
59  typedef map_type::const_iterator const_iterator;
60 
62 
63  template <typename... Args>
64  DatumEvaluatorDictionary(Args &&...args) {
65  insert(std::forward<Args>(args)...);
66  };
67 
70  void insert(std::unique_ptr<DatumEvaluatorDictEntry<ObjectType>> entry_ptr) {
71  std::string name = entry_ptr->name();
72  m_evaluators.emplace(name, std::move(entry_ptr));
73  }
74 
77  template <typename... Args>
78  void insert(std::unique_ptr<DatumEvaluatorDictEntry<ObjectType>> entry_ptr,
79  Args &&...args) {
80  insert(std::move(entry_ptr));
81  insert(std::forward<Args>(args)...);
82  }
83 
85  const_iterator find(std::string const &name) const {
86  return m_evaluators.find(name);
87  }
88 
92  size_type count(std::string const &name) const {
93  return m_evaluators.count(name);
94  }
95 
96  const_iterator begin() const { return m_evaluators.begin(); }
97 
98  const_iterator end() const { return m_evaluators.end(); }
99 
100  private:
101  std::map<std::string, DatumEvaluatorDictEntry<ObjectType>> m_evaluators;
103 };
104 
105 template <typename ObjectType>
106 DatumEvaluatorDictEntry<ObjectType>::DatumEvaluatorDictEntry(
107  std::string _name, std::string _description,
108  MakeEvaluatorPtrFunction<ObjectType> _make_evaluator_ptr_f)
109  : m_name(_name),
110  m_description(_description),
111  m_make_evaluator_f(_make_evaluator_f) {}
112 
113 template <typename ObjectType>
114 std::string DatumEvaluatorDictEntry<ObjectType>::name() const {
115  return m_name;
116 }
117 
118 template <typename ObjectType>
120  return m_description;
121 }
122 
123 template <typename ObjectType>
124 std::unique_ptr<BaseDatumFormatter<ObjectType>>
125 DatumEvaluatorDictEntry<ObjectType>::make(
126  std::vector<std::string> const &args) const {
127  return m_make_evaluator_f(args);
128 }
129 
130 } // namespace simpleformatter
131 } // namespace CASM
132 
133 #endif
std::map< std::string, DatumEvaluatorEntry< ObjectType > > map_type
void insert(std::unique_ptr< DatumEvaluatorDictEntry< ObjectType >> entry_ptr, Args &&...args)
const_iterator find(std::string const &name) const
True if dictionary contains entry for.
std::map< std::string, DatumEvaluatorDictEntry< ObjectType > > m_evaluators
size_type count(std::string const &name) const
void insert(std::unique_ptr< DatumEvaluatorDictEntry< ObjectType >> entry_ptr)
std::string m_description
std::string description() const
std::unique_ptr< BaseDatumFormatter< ObjectType > > make(InputType const &input) const
std::string name() const
MakeEvaluatorPtrFunction< ObjectType, InputType > make_evaluator_f
std::string m_name
DatumEvaluatorEntry(std::string _name, std::string _description, MakeEvaluatorPtrFunction< ObjectType, InputType > _make_evaluator_ptr_f)
GenericDatumFormatter< std::string, ConfigEnumDataType > name()
std::unique_ptr< DatumEvaluatorDictEntry< DataObject > > make_dict_entry_ptr(std::string _name, std::string _description, MakeEvaluatorPtrFunction< ObjectType > _make_evaluator_f)
std::function< EvaluatorPtrType< ObjectType >(InputType const &)> MakeEvaluatorPtrFunction
std::unique_ptr< BaseDatumEvaluator< ObjectType > > EvaluatorPtrType
Main CASM namespace.
Definition: APICommand.hh:8
std::string description(const SymOp &op, const xtal::Lattice &lat, SymInfoOptions opt=SymInfoOptions())
Print SymInfo to string.
GenericDatumFormatter< std::string, DataObject > name()