CASM  1.1.0
A Clusters Approach to Statistical Mechanics
DataFormatterTools_impl.hh
Go to the documentation of this file.
1 #ifndef CASM_DataFormatterTools_impl
2 #define CASM_DataFormatterTools_impl
3 
4 #include <algorithm>
5 
8 
9 namespace CASM {
10 
11 template <typename ValueType, typename ArgType, typename DataObject>
13  const std::string &_args) {
14  if (_args.size() == 0) return true;
15  if (!m_arg_formatter.empty()) return false;
16 
17  std::vector<std::string> format_tags, subexprs;
18  split_formatter_expression(_args, format_tags, subexprs);
19 
20  // std::cout << "_args: " << _args << "\n";
21  // std::cout << "format_tabs: " << format_tags << "\n"
22  //<< "subexprs: '" << subexprs << "'\n";
23  for (Index i = 0; i < format_tags.size(); i++) {
24  std::string ttag(format_tags[i].size(), ' ');
25  std::transform(format_tags[i].cbegin(), format_tags[i].cend(), ttag.begin(),
26  tolower);
27 
28  char ch = ttag[0];
29  if (boost::is_any_of("-+.0123456789")(ch)) { // tag is a constant number
30  if (std::any_of(ttag.cbegin(), ttag.cend(), boost::is_any_of(".e"))) {
31  double val;
32  try {
33  val = std::stod(ttag);
34  } catch (...) {
35  throw std::runtime_error("Unable to parse '" + ttag +
36  "' from subexpression " + _args + "\n");
37  }
38  m_arg_formatter.push_back(
40  } else {
41  long val;
42  try {
43  val = std::stol(ttag);
44  } catch (...) {
45  throw std::runtime_error("Unable to parse '" + ttag +
46  "' from subexpression " + _args + "\n");
47  }
48  m_arg_formatter.push_back(
50  }
51  } else if (ttag == "false")
52  m_arg_formatter.push_back(
54  else if (ttag == "true")
55  m_arg_formatter.push_back(
57  else if ((ch == '\'' || ch == '\"') && ch == ttag.back() &&
58  ttag.size() > 1) {
59  m_arg_formatter.push_back(ConstantValueFormatter<std::string, DataObject>(
60  format_tags[i],
61  std::string((++format_tags[i].cbegin()), (--format_tags[i].cend()))));
62  } else {
63  // std::cout << "About to push_back parsed formatter!!\n";
64  // can throw, should be handled by caller
65  const BaseDatumFormatter<DataObject> &proto_format =
66  *this->home().lookup(ttag);
67 
68  m_arg_formatter.push_back(proto_format, subexprs[i]);
69  }
70  }
71  return true;
72 }
73 
74 //******************************************************************************
75 
76 template <typename ValueType, typename ArgType, typename DataObject>
78  const DataObject &_obj) const {
79  std::stringstream t_ss;
80  m_arg_formatter.print_header(_obj, t_ss);
81 
82  std::vector<std::string> format_tags, subexprs;
83  split_formatter_expression(t_ss.str(), format_tags, subexprs);
84  t_ss.str("");
85  t_ss.clear();
86  t_ss << name();
87  if (format_tags.size()) {
88  t_ss << '(';
89  for (Index i = 0; i < format_tags.size(); i++) {
90  t_ss << format_tags[i];
91  if (subexprs[i].size() > 0) t_ss << '(' << subexprs[i] << ')';
92  if (i + 1 < format_tags.size()) t_ss << ',';
93  }
94  t_ss << ')';
95  }
96  return t_ss.str();
97 }
98 
99 } // namespace CASM
100 
101 #endif
Prints a string value specified at construction. A header string can also be passed.
std::string short_header(const DataObject &_template_obj) const override
Returns a short expression for the formatter parsing the short_header should allow the formatter to b...
bool parse_args(const std::string &_args) override
Main CASM namespace.
Definition: APICommand.hh:8
void split_formatter_expression(const std::string &input_expr, std::vector< std::string > &tag_names, std::vector< std::string > &sub_exprs)
GenericDatumFormatter< std::string, DataObject > name()
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39