10 template<
typename DataObject>
13 typedef boost::tokenizer<boost::char_separator<char> >
15 boost::char_separator<char> sep1(
","), sep2(
" \t",
":");
16 tokenizer tok1(_expr, sep1);
17 std::vector<std::string> split_expr(tok1.begin(), tok1.end());
18 std::vector<difference_type> ind_vec_begin(split_expr.size());
19 std::vector<difference_type> ind_vec_end(split_expr.size());
20 for(
Index i = 0; i < split_expr.size(); i++) {
21 Index rev_i = split_expr.size() - (i + 1);
22 tokenizer tok2(split_expr[i], sep2);
23 std::vector<std::string> ind_expr(tok2.begin(), tok2.end());
24 if(ind_expr.size() == 1) {
25 if(ind_expr[0][0] ==
':')
26 ind_vec_begin[rev_i] = -1;
28 ind_vec_begin[rev_i] = boost::lexical_cast<
difference_type>(ind_expr[0]);
29 ind_vec_end[rev_i] = ind_vec_begin[rev_i];
31 else if(ind_expr.size() == 3) {
32 ind_vec_begin[rev_i] = boost::lexical_cast<
difference_type>(ind_expr[0]);
36 throw std::runtime_error(std::string(
"In BaseDatumFormatter::_parse_index_expression(), invalid expression \"")
37 + _expr +
"\" passed as indices for format keyword '" + name() +
"'\n");
40 for(; ind_count.
valid(); ++ind_count) {
41 m_index_rules.push_back(std::vector<difference_type>(ind_count().rbegin(), ind_count().rend()));
48 template<
typename DataObject>
52 for(
Index i = 0; i < m_data_formatters.size(); i++)
53 if(!m_data_formatters[i]->validate(_obj))
61 template<
typename DataObject>
66 Index num_pass(1), tnum;
67 for(
Index i = 0; i < m_data_formatters.size(); i++) {
68 tnum = m_data_formatters[i]->num_passes(_obj);
71 if(num_pass == 1 || tnum == num_pass)
74 std::cerr <<
"CRITICAL ERROR: Requesting to print formatted data elements that require different number of lines.\n"
79 for(
Index np = 0; np < num_pass; np++) {
80 for(
Index i = 0; i < m_data_formatters.size(); i++) {
81 m_data_formatters[i]->inject(_obj, _stream, np);
92 template<
typename DataObject>
93 template<
typename ValueType>
97 return value_stream.
value();
103 template<
typename DataObject>
104 template<
typename ValueType>
108 return value_stream.value();
114 template<
typename DataObject>
118 return value_stream.
matrix();
124 template<
typename DataObject>
125 template<
typename ValueType,
typename IteratorType>
129 return value_stream.value();
135 template<
typename DataObject>
136 template<
typename IteratorType>
139 value_stream << FormattedIteratorPair<IteratorType>(
this, begin, end);
140 return value_stream.
matrix();
145 template<
typename DataObject>
149 _stream << std::setprecision(m_prec) << std::fixed;
150 Index num_pass(1), tnum;
151 for(
Index i = 0; i < m_data_formatters.size(); i++) {
152 tnum = m_data_formatters[i]->num_passes(_obj);
155 if(num_pass == 1 || tnum == num_pass)
158 std::cerr <<
"CRITICAL ERROR: Requesting to print formatted data elements that require different number of lines.\n"
163 std::stringstream t_ss;
165 for(
Index np = 0; np < num_pass; np++) {
167 for(
Index i = 0; i < m_data_formatters.size(); i++) {
169 t_ss.str(std::string());
170 m_data_formatters[i]->print(_obj, t_ss, np);
175 if(depad_request + 2 < m_col_sep[i])
176 _stream << std::string(m_col_sep[i] - depad_request - 2,
' ');
177 _stream << t_ss.str();
178 depad_request = m_col_sep[i] + int(t_ss.str().size()) - m_col_width[i];
181 _stream << std::endl;
189 template<
typename DataObject>
193 for(
Index i = 0; i < m_data_formatters.size(); i++) {
194 m_data_formatters[i]->to_json(_obj, json[m_data_formatters[i]->short_header(_obj)]);
202 template<
typename DataObject>
211 for(
Index i = 0; i < m_data_formatters.size(); i++) {
213 m_data_formatters[i]->to_json(_obj, tmp);
214 it = json.
find(m_data_formatters[i]->short_header(_obj));
228 template<
typename DataObject>
230 _stream << m_comment;
232 _initialize(_template_obj);
233 int header_size, twidth;
234 for(
Index i = 0; i < m_data_formatters.size(); i++) {
235 std::stringstream t_ss;
236 m_data_formatters[i]->print(_template_obj, t_ss);
237 header_size = (m_data_formatters[i]->long_header(_template_obj)).size();
240 twidth +=
max(t_ss.str().size(), header_size + m_comment.size());
242 twidth +=
max(
int(t_ss.str().size()), header_size);
243 m_col_width[i] = twidth;
244 m_col_sep[i] = twidth - int(t_ss.str().size());
246 twidth -= m_comment.size();
247 _stream << std::string(twidth - header_size,
' ') << m_data_formatters[i]->long_header(_template_obj);
249 _stream << std::endl;
256 template<
typename DataObject>
258 std::vector<std::string> col;
259 for(
Index i = 0; i < m_data_formatters.size(); i++) {
260 auto v2 = m_data_formatters[i]->
col_header(_template_obj);
261 col.insert(col.end(), v2.begin(), v2.end());
268 template<
typename DataObject>
270 for(
Index i = 0; i < m_data_formatters.size(); i++)
271 m_data_formatters[i]->init(_template_obj);
272 m_initialized =
true;
281 template<
typename DataObject,
typename DatumFormatterType>
288 auto res = this->
find(_name);
289 if(res != this->end()) {
290 res->set_home(*
this);
297 auto it = this->begin();
298 for(; it != this->end(); ++it) {
300 if(min_dist < 0 || dist < min_dist) {
307 throw std::runtime_error(
"CRITICAL ERROR: Invalid format flag \"" + _name +
"\" specified.\n"
308 +
" Did you mean \"" + res->name() +
"\"?\n");
317 template<
typename DataObject,
typename DatumFormatterType>
319 std::ostream &_stream,
321 int width,
int separation)
const {
324 std::string::size_type len(0);
325 for(
auto it = it_begin; it != it_end; ++it) {
326 if(ftype == it->
type())
327 len =
max(len, it->name().size());
329 for(
auto it = it_begin; it != it_end; ++it) {
330 if(ftype != it->
type())
332 _stream << std::string(5,
' ') << it->name() << std::string(len - it->name().size() + separation,
' ');
333 std::string::size_type wcount(0);
334 std::string::const_iterator str_end(it->description().cend());
335 for(std::string::const_iterator str_it = it->description().cbegin(); str_it != str_end; ++str_it) {
336 if(wcount >= width && isspace(*str_it)) {
337 _stream << std::endl << std::string(5 + len + separation,
' ');
345 _stream << std::endl << std::endl;
357 template<
typename DataObject,
typename DatumFormatterType>
359 const std::vector<std::string> &input)
const {
362 std::vector<std::string> format_tags, format_args;
363 for(
Index i = 0; i < input.size(); i++) {
366 for(
Index i = 0; i < format_tags.size(); i++) {
367 formatter.
push_back(*lookup(format_tags[i]), format_args[i]);
379 template<
typename DataObject,
typename DatumFormatterType>
381 const std::string &input)
const {
384 std::vector<std::string> format_tags, format_args;
387 for(
Index i = 0; i < format_tags.size(); i++) {
388 formatter.
push_back(*lookup(format_tags[i]), format_args[i]);
iterator end()
Returns iterator to end of JSON object or JSON array.
A Counter allows looping over many incrementing variables in one loop.
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
An iterator over a UniqueMap.
void split_formatter_expression(const std::string &input_expr, std::vector< std::string > &tag_names, std::vector< std::string > &sub_exprs)
const Eigen::MatrixXd & matrix()
int dl_string_dist(const std::string &a, const std::string &b)
Computes the Damerescau-Levenshtein distance – the number of edits (deletions, insertions, transpositions) to go from string 'a' to string 'b'.
EigenIndex Index
For long integer indexing:
Iterator find(Iterator begin, Iterator end, const T &value, BinaryCompare q)
Equivalent to std::find(begin, end, value), but with custom comparison.
virtual DataStream & newline()
T max(const T &A, const T &B)
iterator find(const std::string &name)
Return iterator to JSON object value with 'name'.
jsonParser & push_back(const T &value)
Puts new valued element at end of array of any type T for which 'jsonParser& to_json( const T &value...
jsonParser & put_array()
Puts new empty JSON array.