CASM  1.1.0
A Clusters Approach to Statistical Mechanics
ParsingDictionary.hh
Go to the documentation of this file.
1 #ifndef CASM_ParsingDictionary
2 #define CASM_ParsingDictionary
3 
4 #include "casm/misc/CASM_math.hh"
6 namespace CASM {
7 namespace ParsingDictionary_impl {
8 
11 template <typename T>
13  // performs a static_cast of value.clone().unique().release()
15  return notstd::cloneable_ptr<T>(static_cast<T *>(_t.clone().release()));
16  }
17 };
18 } // namespace ParsingDictionary_impl
19 
22 template <typename T>
23 class ParsingDictionary : public notstd::unique_cloneable_map<std::string, T> {
24  public:
26 
27  typedef typename Base::key_type key_type;
28  typedef typename Base::value_type value_type;
29  typedef typename Base::size_type size_type;
30  typedef typename Base::iterator iterator;
32 
33  using Base::begin;
34  using Base::end;
35  using Base::find;
36  using Base::insert;
37 
39  : Base(
40  [](const value_type &value) -> std::string { return value.name(); },
42 
43  std::unique_ptr<ParsingDictionary<value_type> > clone() const {
44  return notstd::make_unique<ParsingDictionary<value_type> >(*this);
45  }
46 
49  T const &lookup(const key_type &_name) const;
50 
52  bool contains(const key_type &_name) const {
53  return this->find(_name) != this->end();
54  }
55 
56  void print_help(std::ostream &_stream, std::function<bool(T const &)> filter,
57  int width = 60, int separation = 8) const;
58 };
59 
60 template <typename T>
62 
65 template <typename T>
67  const ParsingDictionary<T>::key_type &_name) const {
68  auto res = find(_name);
69 
70  if (res == end()) {
71  // If no match, try to use demerescau-levenshtein distance to make a helpful
72  // suggestion
73  auto it = begin();
74  int min_dist(-1);
75  for (; it != end(); ++it) {
76  int dist = dl_string_dist(_name, it->name());
77  if (min_dist < 0 || dist < min_dist) {
78  min_dist = dist;
79 
80  res = it;
81  }
82  }
83 
84  throw std::runtime_error(
85  "Invalid " + T::class_desc() + " \"" + _name + "\" specified.\n" +
86  " Did you mean \"" + res->name() + "\"?\n");
87  }
88  // std::cout << "Returning result of 'lookup' for " << _name << ". result is "
89  // << res->name() << std::endl;
90  return *res; //->clone();
91 }
92 
93 template <typename T>
94 void ParsingDictionary<T>::print_help(std::ostream &_stream,
95  std::function<bool(T const &)> filter,
96  int width, int separation) const {
97  const_iterator it_begin(this->cbegin()), it_end(this->cend());
99  for (auto it = it_begin; it != it_end; ++it) {
100  if (filter(*it)) len = max(len, it->name().size());
101  }
102  for (auto it = it_begin; it != it_end; ++it) {
103  if (!filter(*it)) continue;
104  _stream << std::string(5, ' ') << it->name()
105  << std::string(len - it->name().size() + separation, ' ');
106  std::string::size_type wcount(0);
107  std::string::const_iterator str_end(it->description().cend());
108  for (std::string::const_iterator str_it = it->description().cbegin();
109  str_it != str_end; ++str_it) {
110  if (wcount >= width && isspace(*str_it)) {
111  _stream << std::endl << std::string(5 + len + separation, ' ');
112  wcount = 0;
113  } else {
114  _stream << *str_it;
115  wcount++;
116  }
117  }
118  _stream << std::endl << std::endl;
119  }
120 }
121 
122 } // namespace CASM
123 #endif
Parsing dictionary for obtaining the correct MoleculeAttribute given a name.
void print_help(std::ostream &_stream, std::function< bool(T const &)> filter, int width=60, int separation=8) const
notstd::unique_cloneable_map< std::string, T > Base
std::unique_ptr< ParsingDictionary< value_type > > clone() const
Base::value_type value_type
T const & lookup(const key_type &_name) const
Equivalent to find, but set 'home' and throws error with suggestion if.
bool contains(const key_type &_name) const
True if dictionary contains entry for.
Base::const_iterator const_iterator
An iterator over a UniqueMap.
Definition: unique_map.hh:19
A 'cloneable_ptr' can be used in place of 'unique_ptr'.
std::map wrapper to enforce a 1-1 ValueType->KeyType relationship
Definition: unique_map.hh:124
MapType::size_type size_type
Definition: unique_map.hh:141
iterator find(const key_type &key)
Definition: unique_map.hh:191
std::pair< iterator, bool > insert(const value_type &value)
Insert single value.
Definition: unique_map.hh:149
ValueType value_type
Definition: unique_map.hh:126
iterator begin()
Definition: unique_map.hh:213
Main CASM namespace.
Definition: APICommand.hh:8
ParsingDictionary< T > make_parsing_dictionary()
GenericDatumFormatter< std::string, DataObject > name()
int dl_string_dist(const std::string &a, const std::string &b)
Computes the Damerescau-Levenshtein distance – the number of edits (deletions, insertions,...
Definition: CASM_math.cc:45
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
T max(const T &A, const T &B)
Definition: CASM_math.hh:95
Definition: stream_io.hh:24
Conversion Functor for inserting BasicTraits into unique_cloneable_map.
notstd::cloneable_ptr< T > operator()(const T &_t)