CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
CASM::DataFormatter< _DataObject > Singleton Reference

#include <DataFormatter.hh>

Detailed Description

template<typename _DataObject>
singleton 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 = DataFormatterDictionary<Configuration>::parse(args);

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());

Definition at line 154 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
 Output data as specified by *this of the given DataObject to json with format {"name1":x, "name2":x, ...}. More...
 
jsonParserto_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], ...}. More...
 
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)
 

Private Member Functions

void _initialize (const DataObject &_tmplt) const
 

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

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

Definition at line 160 of file DataFormatter.hh.

Constructor & Destructor Documentation

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

Definition at line 165 of file DataFormatter.hh.

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

Definition at line 171 of file DataFormatter.hh.

Member Function Documentation

template<typename DataObject >
void CASM::DataFormatter< DataObject >::_initialize ( const DataObject _tmplt) const
private

Definition at line 269 of file DataFormatter_impl.hh.

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

Definition at line 270 of file DataFormatter.hh.

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

Definition at line 180 of file DataFormatter.hh.

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 257 of file DataFormatter_impl.hh.

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

Definition at line 176 of file DataFormatter.hh.

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 115 of file DataFormatter_impl.hh.

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 137 of file DataFormatter_impl.hh.

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

Useful when formatted output can be represented as single value.

Definition at line 94 of file DataFormatter_impl.hh.

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 105 of file DataFormatter_impl.hh.

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 126 of file DataFormatter_impl.hh.

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

Output selected data from DataObject to DataStream.

Definition at line 62 of file DataFormatter_impl.hh.

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

Definition at line 185 of file DataFormatter.hh.

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

Definition at line 197 of file DataFormatter.hh.

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

Definition at line 276 of file DataFormatter.hh.

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 146 of file DataFormatter_impl.hh.

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 229 of file DataFormatter_impl.hh.

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 243 of file DataFormatter.hh.

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

Definition at line 258 of file DataFormatter.hh.

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

Definition at line 265 of file DataFormatter.hh.

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

Definition at line 281 of file DataFormatter.hh.

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

Definition at line 189 of file DataFormatter.hh.

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 190 of file DataFormatter_impl.hh.

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 203 of file DataFormatter_impl.hh.

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 49 of file DataFormatter_impl.hh.

Member Data Documentation

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

Definition at line 288 of file DataFormatter.hh.

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

Definition at line 289 of file DataFormatter.hh.

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

Definition at line 297 of file DataFormatter.hh.

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

Definition at line 287 of file DataFormatter.hh.

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

Definition at line 295 of file DataFormatter.hh.

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

Definition at line 285 of file DataFormatter.hh.

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

Definition at line 291 of file DataFormatter.hh.

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

Definition at line 293 of file DataFormatter.hh.


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