1 #ifndef CASM_DataFormatter_impl
2 #define CASM_DataFormatter_impl
3 #include <boost/lexical_cast.hpp>
4 #include <boost/tokenizer.hpp>
14 template <
typename DataObject>
16 const std::string &_expr) {
18 std::vector<difference_type> ind_begin(bounds.first.rbegin(),
20 std::vector<difference_type> ind_end(bounds.second.rbegin(),
21 bounds.second.rend());
24 ind_begin, ind_end, std::vector<difference_type>(ind_begin.size(), 1));
26 for (; ind_count.
valid(); ++ind_count) {
27 m_index_rules.push_back(
28 std::vector<difference_type>(ind_count().rbegin(), ind_count().rend()));
34 template <
typename DataObject>
37 for (
Index i = 0; i < m_data_formatters.size(); i++)
38 if (!m_data_formatters[i]->
validate(_obj))
return false;
45 template <
typename DataObject>
50 Index num_pass(1), tnum;
51 for (
Index i = 0; i < m_data_formatters.size(); i++) {
52 tnum = m_data_formatters[i]->num_passes(_obj);
53 if (tnum == 1)
continue;
54 if (num_pass == 1 || tnum == num_pass)
57 std::cerr <<
"CRITICAL ERROR: Requesting to print formatted data "
58 "elements that require different number of lines.\n"
63 for (
Index np = 0; np < num_pass; np++) {
64 for (
Index i = 0; i < m_data_formatters.size(); i++) {
65 m_data_formatters[i]->inject(_obj, _stream, np);
76 template <
typename DataObject>
77 template <
typename ValueType>
82 return value_stream.
value();
88 template <
typename DataObject>
89 template <
typename ValueType>
94 return value_stream.value();
100 template <
typename DataObject>
105 return value_stream.
matrix();
111 template <
typename DataObject>
112 template <
typename ValueType,
typename IteratorType>
114 IteratorType begin, IteratorType end)
const {
117 return value_stream.value();
123 template <
typename DataObject>
124 template <
typename IteratorType>
126 IteratorType begin, IteratorType end)
const {
128 value_stream << FormattedIteratorPair<IteratorType>(
this, begin, end);
129 return value_stream.
matrix();
134 template <
typename DataObject>
136 std::ostream &_stream)
const {
138 _stream << std::setprecision(m_prec) << std::fixed;
139 Index num_pass(1), tnum;
140 for (
Index i = 0; i < m_data_formatters.size(); i++) {
141 tnum = m_data_formatters[i]->num_passes(_obj);
142 if (tnum == 1)
continue;
143 if (num_pass == 1 || tnum == num_pass)
146 std::cerr <<
"CRITICAL ERROR: Requesting to print formatted data "
147 "elements that require different number of lines.\n"
152 std::stringstream t_ss;
154 for (
Index np = 0; np < num_pass; np++) {
156 for (
Index i = 0; i < m_data_formatters.size(); i++) {
158 t_ss.str(std::string());
159 m_data_formatters[i]->print(_obj, t_ss, np);
164 if (depad_request + 2 < m_col_sep[i])
165 _stream << std::string(m_col_sep[i] - depad_request - 2,
' ');
166 _stream << t_ss.str();
167 depad_request = m_col_sep[i] + int(t_ss.str().size()) - m_col_width[i];
169 _stream << std::endl;
177 template <
typename DataObject>
181 for (
Index i = 0; i < m_data_formatters.size(); i++) {
182 m_data_formatters[i]->to_json(
183 _obj, json[m_data_formatters[i]->short_header(_obj)]);
191 template <
typename DataObject>
199 for (
Index i = 0; i < m_data_formatters.size(); i++) {
201 m_data_formatters[i]->to_json(_obj, tmp);
202 it = json.
find(m_data_formatters[i]->short_header(_obj));
215 template <
typename DataObject>
217 std::ostream &_stream)
const {
218 _stream << m_comment;
222 int header_size, twidth;
223 for (
Index i = 0; i < m_data_formatters.size(); i++) {
224 std::stringstream t_ss;
225 m_data_formatters[i]->print(_template_obj, t_ss);
226 header_size = (m_data_formatters[i]->long_header(_template_obj)).size();
229 twidth +=
max(t_ss.str().size(), header_size + m_comment.size());
231 twidth +=
max(
int(t_ss.str().size()), header_size);
232 m_col_width[i] = twidth;
233 m_col_sep[i] = twidth - int(t_ss.str().size());
234 if (i == 0) twidth -= m_comment.size();
235 _stream << std::string(twidth - header_size,
' ')
236 << m_data_formatters[i]->long_header(_template_obj);
238 _stream << std::endl;
245 template <
typename DataObject>
248 std::vector<std::string> col;
249 for (
Index i = 0; i < m_data_formatters.size(); i++) {
251 col.insert(col.end(), v2.begin(), v2.end());
258 template <
typename DataObject>
261 if (!m_initialized) {
262 m_initialized =
true;
263 for (
Index i = 0; i < m_data_formatters.size(); i++)
265 m_data_formatters[i]->init(_template_obj) && m_initialized;
267 return m_initialized;
274 template <
typename DataObject,
typename DatumFormatterType>
280 auto res = this->
find(_name);
281 if (res != this->end()) {
282 res->set_home(*
this);
288 auto it = this->begin();
289 for (; it != this->end(); ++it) {
291 if (min_dist < 0 || dist < min_dist) {
299 throw std::runtime_error(
300 "CRITICAL ERROR: Invalid format flag \"" + _name +
"\" specified.\n" +
301 " Did you mean \"" + res->name() +
"\"?\n");
308 template <
typename DataObject,
typename DatumFormatterType>
311 int separation)
const {
314 for (
auto it = it_begin; it != it_end; ++it) {
315 if (ftype == it->type()) len =
max(len, it->name().size());
317 for (
auto it = it_begin; it != it_end; ++it) {
318 if (ftype != it->type())
continue;
319 _stream << std::string(5,
' ') << it->name()
320 << std::string(len - it->name().size() + separation,
' ');
322 std::string::const_iterator str_end(it->description().cend());
323 for (std::string::const_iterator str_it = it->description().cbegin();
324 str_it != str_end; ++str_it) {
325 if (wcount >= width && isspace(*str_it)) {
326 _stream << std::endl << std::string(5 + len + separation,
' ');
333 _stream << std::endl << std::endl;
344 template <
typename DataObject,
typename DatumFormatterType>
347 const std::vector<std::string> &input)
const {
349 std::vector<std::string> format_tags, format_args;
350 for (
Index i = 0; i < input.size(); i++) {
353 for (
Index i = 0; i < format_tags.size(); i++) {
354 formatter.
push_back(*lookup(format_tags[i]), format_args[i]);
367 template <
typename DataObject,
typename DatumFormatterType>
370 const std::string &input)
const {
372 std::vector<std::string> format_tags, format_args;
375 for (
Index i = 0; i < format_tags.size(); i++) {
376 formatter.
push_back(*lookup(format_tags[i]), format_args[i]);
382 template <
typename DataObject,
typename DatumFormatterType>
385 std::initializer_list<std::string> input)
const {
386 return parse(std::vector<std::string>(input));
A Counter allows looping over many incrementing variables in one loop.
virtual DataStream & newline()
const Eigen::MatrixXd & matrix()
iterator end()
Returns iterator to end of JSON object or JSON array.
iterator find(const std::string &name)
Return iterator to JSON object value with 'name'.
jsonParser & put_array()
Puts new empty JSON array.
An iterator over a UniqueMap.
jsonParser & push_back(const T &value, Args &&... args)
Validator validate(OccupationDoFSpecs const &occ_specs, const Structure &prim)
std::pair< std::vector< long >, std::vector< long > > index_expression_to_bounds(const std::string &_expr)
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
void split_formatter_expression(const std::string &input_expr, std::vector< std::string > &tag_names, std::vector< std::string > &sub_exprs)
int dl_string_dist(const std::string &a, const std::string &b)
Computes the Damerescau-Levenshtein distance – the number of edits (deletions, insertions,...
Iterator find(Iterator begin, Iterator end, const T &value, BinaryCompare q)
Equivalent to std::find(begin, end, value), but with custom comparison.
INDEX_TYPE Index
For long integer indexing:
void parse(InputParser< ConfigEnumOptions > &parser, std::string method_name, PrimClex const &primclex, DataFormatterDictionary< Configuration > const &dict)
T max(const T &A, const T &B)