CASM  1.1.0
A Clusters Approach to Statistical Mechanics
CASM::DataFormatter< _DataObject > Class Template Reference

#include <DataFormatter.hh>

Detailed Description

template<typename _DataObject>
class CASM::DataFormatter< _DataObject >

Extract data from objects of 'DataObject' class.

A DataFormatter performs extraction of disparate types of data from objects of 'DataObject' class. The DataFormatter is composed of one or more 'DatumFormatters', with each DatumFormatter knowing how to access or calculate and then format a particular type of data from the 'DataObject'.

BaseDatumFormatter<DataObject> is a virtual class from which all DatumFormatters that access the particular DataObject derive.

As an example, consider a Configuration object, which has numerous attributes (name, energy, composition, etc) which can either be accessed or calculated. These attributes can be accessed or calculated and then formatted using a DataFormatter<Configuration>, which contains a number of DatumFormatter<Configuration> objects.

Example DatumFormatter<Configuration> include:

  • ConfigIO::Name (derived from DatumFormatter<Configuration>)
  • ConfigIO::Comp (derived from DatumFormatter<Configuration>)
  • ConfigIO::FormationEnergy (derived from DatumFormatter<Configuration>)

A DataFormatter<Configuration> can either be constructed explicitly with the desired set of DatumFormatter, or via a DataFormatterDictionary<Configuration> which parses a string containing descriptions of the DatumFormatter to include and optionally arguments for initializing the DatumParser. Additional DatumFormatter can also be added via 'DataFormatter::push_back' or the '<<' operator.

Explicit construction example:

DataFormatter<Configuration> formatter( ConfigIO::Name(),
ConfigIO::Comp(),
ConfigIO::FormationEnergy());

Construction via DataFormatterDictionary and a string with arguments:

std::string args = "name formation_energy comp(a) comp(c)"
DataFormatter<Configuration> formatter =
DataFormatter< DataObject > parse(const std::string &input) const
Use the vector of strings to build a DataFormatter<DataObject>

Calling DataFormatterDictionary::parse will for each name in the 'args' push back the DatumFormatters with the same name and then call BaseDatumFormatter::parse_args with the content inside the parentheses (empty string otherwise).

Adding additional DatumFormatter:

formatter.push_back(ConfigIO::Comp());
formatter << ConfigIO::Comp();

Once a DataFormatter has been constructed, it can be used to output formatted data from a single DataObject or a range of DataObject. The output can be sent to an output stream, a DataStream, or a jsonParser. For example:

jsonParser json;
// output formatted data from a single DataObject
Configuration config;
std::cout << formatter(config);
json = formatter(config);
// output formatted data from a range of DataObject
std::vector<Configuration> container = ...;
std::cout << formatter(container.begin(), container.end());
json = formatter(container.begin(), container.end());
ConfigIO::GenericConfigFormatter< jsonParser > config()
Definition: ConfigIO.cc:777

Definition at line 168 of file DataFormatter.hh.

Classes

class  FormattedIteratorPair
 Implements generic formatting member functions for ranges of data objects. More...
 
class  FormattedObject
 Implements generic formatting member functions for individual data objects. More...
 

Public Types

typedef _DataObject DataObject
 

Public Member Functions

 DataFormatter (int _sep=4, int _precision=12, std::string _comment="#")
 
template<typename... Args>
 DataFormatter (const Args &... formatters)
 
bool empty () const
 
void clear ()
 
template<typename IteratorType >
FormattedIteratorPair< IteratorType > operator() (IteratorType begin, IteratorType end) const
 
void set_indent (int _indent)
 
FormattedObject operator() (const DataObject &data_obj) const
 
bool validate (const DataObject &_obj) const
 Returns true if _obj has valid data for all portions of query. More...
 
void inject (const DataObject &_obj, DataStream &_stream) const
 Output selected data from DataObject to DataStream. More...
 
template<typename ValueType >
ValueType evaluate_as_scalar (const DataObject &_obj) const
 Useful when formatted output can be represented as single value. More...
 
template<typename ValueType >
std::vector< ValueType > evaluate_as_vector (const DataObject &_obj) const
 Useful when formatted output can be represented as std::vector. More...
 
Eigen::MatrixXd evaluate_as_matrix (const DataObject &_obj) const
 Useful when formatted output can be represented as Eigen::MatrixXd. More...
 
template<typename ValueType , typename IteratorType >
std::vector< ValueType > evaluate_as_vector (IteratorType begin, IteratorType end) const
 Useful when formatted output can be represented as std::vector. More...
 
template<typename IteratorType >
Eigen::MatrixXd evaluate_as_matrix (IteratorType begin, IteratorType end) const
 Useful when formatted output can be represented as an Eigen::MatrixXd. More...
 
void print (const DataObject &_obj, std::ostream &_stream) const
 Output data as specified by *this of the given DataObject. More...
 
jsonParserto_json (const DataObject &_obj, jsonParser &json) const
 
jsonParserto_json_arrays (const DataObject &_obj, jsonParser &json) const
 
void print_header (const DataObject &_tmplt_obj, std::ostream &_stream) const
 print the header, using _tmplt_obj to inspect array sizes, etc. More...
 
std::vector< std::string > col_header (const DataObject &_template_obj) const
 Returns all column header strings as std::vector<std::string> More...
 
void push_back (const BaseDatumFormatter< DataObject > &new_formatter, const std::string &args)
 
void push_back (const BaseDatumFormatter< DataObject > &new_formatter)
 
template<typename... Args>
void push_back (const BaseDatumFormatter< DataObject > &new_formatter, const Args &... formatters)
 
void append (const DataFormatter< DataObject > &_tail)
 
DataFormatter< DataObject > & operator<< (const BaseDatumFormatter< DataObject > &new_formatter)
 
void set_header_prefix (const std::string &_prefix)
 
bool initialize (const DataObject &_tmplt) const
 
template<typename ObjectType >
void push_back (std::unique_ptr< BaseDatumEvaluator< ObjectType >> evaluator_ptr)
 
template<typename... Args>
void push_back (std::unique_ptr< BaseDatumEvaluator< ObjectType >> evaluator_ptr, Args &&...args)
 

Private Attributes

bool m_initialized
 
std::vector< notstd::cloneable_ptr< BaseDatumFormatter< DataObject > > > m_data_formatters
 
std::vector< Indexm_col_sep
 
std::vector< Indexm_col_width
 
int m_prec
 
int m_sep
 
int m_indent
 
std::string m_comment
 

Member Typedef Documentation

◆ DataObject

template<typename _DataObject >
typedef _DataObject CASM::DataFormatter< _DataObject >::DataObject

Definition at line 177 of file DataFormatter.hh.

Constructor & Destructor Documentation

◆ DataFormatter() [1/2]

template<typename _DataObject >
CASM::DataFormatter< _DataObject >::DataFormatter ( int  _sep = 4,
int  _precision = 12,
std::string  _comment = "#" 
)
inline

Definition at line 179 of file DataFormatter.hh.

◆ DataFormatter() [2/2]

template<typename _DataObject >
template<typename... Args>
CASM::DataFormatter< _DataObject >::DataFormatter ( const Args &...  formatters)
inline

Definition at line 189 of file DataFormatter.hh.

Member Function Documentation

◆ append()

template<typename _DataObject >
void CASM::DataFormatter< _DataObject >::append ( const DataFormatter< DataObject > &  _tail)
inline

Definition at line 290 of file DataFormatter.hh.

◆ clear()

template<typename _DataObject >
void CASM::DataFormatter< _DataObject >::clear ( )
inline

Definition at line 195 of file DataFormatter.hh.

◆ col_header()

template<typename DataObject >
std::vector< std::string > CASM::DataFormatter< DataObject >::col_header ( const DataObject _template_obj) const

Returns all column header strings as std::vector<std::string>

Definition at line 246 of file DataFormatter_impl.hh.

◆ empty()

template<typename _DataObject >
bool CASM::DataFormatter< _DataObject >::empty ( ) const
inline

Definition at line 193 of file DataFormatter.hh.

◆ evaluate_as_matrix() [1/2]

template<typename DataObject >
Eigen::MatrixXd CASM::DataFormatter< DataObject >::evaluate_as_matrix ( const DataObject _obj) const

Useful when formatted output can be represented as Eigen::MatrixXd.

Definition at line 101 of file DataFormatter_impl.hh.

◆ evaluate_as_matrix() [2/2]

template<typename DataObject >
template<typename IteratorType >
Eigen::MatrixXd CASM::DataFormatter< DataObject >::evaluate_as_matrix ( IteratorType  begin,
IteratorType  end 
) const

Useful when formatted output can be represented as an Eigen::MatrixXd.

Definition at line 125 of file DataFormatter_impl.hh.

◆ evaluate_as_scalar()

template<typename DataObject >
template<typename ValueType >
template double CASM::DataFormatter< _DataObject >::evaluate_as_scalar< double > ( const DataObject _obj) const

Useful when formatted output can be represented as single value.

Definition at line 78 of file DataFormatter_impl.hh.

◆ evaluate_as_vector() [1/2]

template<typename DataObject >
template<typename ValueType >
std::vector< ValueType > CASM::DataFormatter< DataObject >::evaluate_as_vector ( const DataObject _obj) const

Useful when formatted output can be represented as std::vector.

Definition at line 90 of file DataFormatter_impl.hh.

◆ evaluate_as_vector() [2/2]

template<typename DataObject >
template<typename ValueType , typename IteratorType >
std::vector< ValueType > CASM::DataFormatter< DataObject >::evaluate_as_vector ( IteratorType  begin,
IteratorType  end 
) const

Useful when formatted output can be represented as std::vector.

Definition at line 113 of file DataFormatter_impl.hh.

◆ initialize()

template<typename DataObject >
bool CASM::DataFormatter< DataObject >::initialize ( const DataObject _tmplt) const

Definition at line 259 of file DataFormatter_impl.hh.

◆ inject()

template<typename DataObject >
void CASM::DataFormatter< DataObject >::inject ( const DataObject _obj,
DataStream _stream 
) const

Output selected data from DataObject to DataStream.

Definition at line 46 of file DataFormatter_impl.hh.

◆ operator()() [1/2]

template<typename _DataObject >
FormattedObject CASM::DataFormatter< _DataObject >::operator() ( const DataObject data_obj) const
inline

Definition at line 210 of file DataFormatter.hh.

◆ operator()() [2/2]

template<typename _DataObject >
template<typename IteratorType >
FormattedIteratorPair<IteratorType> CASM::DataFormatter< _DataObject >::operator() ( IteratorType  begin,
IteratorType  end 
) const
inline

Definition at line 198 of file DataFormatter.hh.

◆ operator<<()

template<typename _DataObject >
DataFormatter<DataObject>& CASM::DataFormatter< _DataObject >::operator<< ( const BaseDatumFormatter< DataObject > &  new_formatter)
inline

Definition at line 290 of file DataFormatter.hh.

◆ print()

template<typename DataObject >
void CASM::DataFormatter< DataObject >::print ( const DataObject _obj,
std::ostream &  _stream 
) const

Output data as specified by *this of the given DataObject.

Definition at line 135 of file DataFormatter_impl.hh.

◆ print_header()

template<typename DataObject >
void CASM::DataFormatter< DataObject >::print_header ( const DataObject _tmplt_obj,
std::ostream &  _stream 
) const

print the header, using _tmplt_obj to inspect array sizes, etc.

Definition at line 216 of file DataFormatter_impl.hh.

◆ push_back() [1/5]

template<typename _DataObject >
void CASM::DataFormatter< _DataObject >::push_back ( const BaseDatumFormatter< DataObject > &  new_formatter)
inline

Definition at line 277 of file DataFormatter.hh.

◆ push_back() [2/5]

template<typename _DataObject >
template<typename... Args>
void CASM::DataFormatter< _DataObject >::push_back ( const BaseDatumFormatter< DataObject > &  new_formatter,
const Args &...  formatters 
)
inline

Definition at line 284 of file DataFormatter.hh.

◆ push_back() [3/5]

template<typename _DataObject >
void CASM::DataFormatter< _DataObject >::push_back ( const BaseDatumFormatter< DataObject > &  new_formatter,
const std::string &  args 
)
inline

Add a particular BaseDatumFormatter to *this If the previous Formatter matches the new formatter, try to just parse the new args into it

Definition at line 261 of file DataFormatter.hh.

◆ push_back() [4/5]

template<typename _DataObject >
template<typename ObjectType >
void CASM::DataFormatter< _DataObject >::push_back ( std::unique_ptr< BaseDatumEvaluator< ObjectType >>  evaluator_ptr)

Push back BaseDatumEvaluator<DataObject>. Will be evaluated sequentially by operator().

Definition at line 512 of file SimpleFormatter.hh.

◆ push_back() [5/5]

template<typename _DataObject >
template<typename... Args>
void CASM::DataFormatter< _DataObject >::push_back ( std::unique_ptr< BaseDatumEvaluator< ObjectType >>  evaluator_ptr,
Args &&...  args 
)

Push back BaseDatumEvaluator<DataObject>. Will be evaluated sequentially by operator().

Definition at line 521 of file SimpleFormatter.hh.

◆ set_header_prefix()

template<typename _DataObject >
void CASM::DataFormatter< _DataObject >::set_header_prefix ( const std::string &  _prefix)
inline

Definition at line 300 of file DataFormatter.hh.

◆ set_indent()

template<typename _DataObject >
void CASM::DataFormatter< _DataObject >::set_indent ( int  _indent)
inline

Definition at line 203 of file DataFormatter.hh.

◆ to_json()

template<typename DataObject >
jsonParser & CASM::DataFormatter< DataObject >::to_json ( const DataObject _obj,
jsonParser json 
) const

Output data as specified by *this of the given DataObject to json with format {"name1":x, "name2":x, ...}

Definition at line 178 of file DataFormatter_impl.hh.

◆ to_json_arrays()

template<typename DataObject >
jsonParser & CASM::DataFormatter< DataObject >::to_json_arrays ( const DataObject _obj,
jsonParser json 
) const

Output data as specified by *this of the given DataObject to json with format {"name1":[..., x], "name2":[..., x], ...}

Definition at line 192 of file DataFormatter_impl.hh.

◆ validate()

template<typename DataObject >
bool CASM::DataFormatter< DataObject >::validate ( const DataObject _obj) const

Returns true if _obj has valid data for all portions of query.

Definition at line 35 of file DataFormatter_impl.hh.

Member Data Documentation

◆ m_col_sep

template<typename _DataObject >
std::vector<Index> CASM::DataFormatter< _DataObject >::m_col_sep
mutableprivate

Definition at line 309 of file DataFormatter.hh.

◆ m_col_width

template<typename _DataObject >
std::vector<Index> CASM::DataFormatter< _DataObject >::m_col_width
mutableprivate

Definition at line 310 of file DataFormatter.hh.

◆ m_comment

template<typename _DataObject >
std::string CASM::DataFormatter< _DataObject >::m_comment
private

Definition at line 318 of file DataFormatter.hh.

◆ m_data_formatters

template<typename _DataObject >
std::vector<notstd::cloneable_ptr<BaseDatumFormatter<DataObject> > > CASM::DataFormatter< _DataObject >::m_data_formatters
private

Definition at line 308 of file DataFormatter.hh.

◆ m_indent

template<typename _DataObject >
int CASM::DataFormatter< _DataObject >::m_indent
private

Definition at line 316 of file DataFormatter.hh.

◆ m_initialized

template<typename _DataObject >
bool CASM::DataFormatter< _DataObject >::m_initialized
mutableprivate

Definition at line 305 of file DataFormatter.hh.

◆ m_prec

template<typename _DataObject >
int CASM::DataFormatter< _DataObject >::m_prec
private

Definition at line 312 of file DataFormatter.hh.

◆ m_sep

template<typename _DataObject >
int CASM::DataFormatter< _DataObject >::m_sep
private

Definition at line 314 of file DataFormatter.hh.


The documentation for this class was generated from the following files: