CASM  1.1.0
A Clusters Approach to Statistical Mechanics
DatumFormatterAdapter.hh
Go to the documentation of this file.
1 #ifndef CASM_DatumFormatterAdapter
2 #define CASM_DatumFormatterAdapter
3 
5 
6 namespace CASM {
7 
8 namespace adapter {
9 
10 template <typename ToType, typename FromType>
11 struct Adapter;
12 
13 } // namespace adapter
14 
17 template <typename ExternalType, typename InternalType>
18 class DatumFormatterAdapter : public BaseDatumFormatter<ExternalType> {
19  public:
20  using DataObject = ExternalType;
24 
27  DatumFormatterAdapter(const std::string &_name, const std::string &_desc,
29  BaseDatumFormatter<InternalType> const &_base_formatter)
30  : BaseDatumFormatter<ExternalType>(_name, _desc),
31  m_adapt(_adapt),
32  m_base_ptr(notstd::clone(_base_formatter)) {}
33 
37  BaseDatumFormatter<InternalType> const &_base_formatter)
38  : BaseDatumFormatter<ExternalType>(_base_formatter.name(),
39  _base_formatter.description()),
40  m_adapt(_adapt),
41  m_base_ptr(notstd::clone(_base_formatter)) {}
42 
45 
46  DatumFormatterClass type() const override { return m_base_ptr->type(); }
47 
50  const DictType &home() const { return this->home(); }
51 
54  void set_home(const DictType &home) const { this->set_home(home); }
55 
56  std::unique_ptr<DatumFormatterAdapter<ExternalType, InternalType>> clone()
57  const {
58  return std::unique_ptr<DatumFormatterAdapter<ExternalType, InternalType>>(
59  this->_clone());
60  }
61 
62  bool init(ExternalType const &_template_obj) const override {
63  return m_base_ptr->init(m_adapt(_template_obj));
64  }
65 
66  bool validate(ExternalType const &_data_obj) const override {
67  return m_base_ptr->validate(m_adapt(_data_obj));
68  }
69 
70  std::vector<std::string> col_header(
71  ExternalType const &_template_obj) const override {
72  return m_base_ptr->col_header(m_adapt(_template_obj));
73  }
74 
75  std::string long_header(ExternalType const &_template_obj) const override {
76  return m_base_ptr->long_header(m_adapt(_template_obj));
77  }
78 
79  std::string short_header(ExternalType const &_template_obj) const override {
80  return m_base_ptr->short_header(m_adapt(_template_obj));
81  }
82 
83  Index num_passes(ExternalType const &_data_obj) const override {
84  return m_base_ptr->num_passes(m_adapt(_data_obj));
85  }
86 
90  void print(ExternalType const &_data_obj, std::ostream &_stream,
91  Index pass_index = 0) const override {
92  m_base_ptr->print(m_adapt(_data_obj), _stream, pass_index);
93  }
94 
95  void inject(ExternalType const &_data_obj, DataStream &_stream,
96  Index pass_index = 0) const override {
97  m_base_ptr->inject(m_adapt(_data_obj), _stream, pass_index);
98  }
99 
100  jsonParser &to_json(ExternalType const &_data_obj,
101  jsonParser &json) const override {
102  return m_base_ptr->to_json(m_adapt(_data_obj), json);
103  }
104 
105  bool parse_args(const std::string &args) override {
106  return m_base_ptr->parse_args(args);
107  }
108 
109  private:
114  return new DatumFormatterAdapter(*this);
115  }
116 
119 };
120 
121 template <typename ExternalType, typename InternalType>
123  BaseDatumFormatter<InternalType> const &_base_formatter) {
126 }
127 
128 } // namespace CASM
129 
130 #endif
Abstract base class from which all other DatumFormatter<DataObject> classes inherit.
const std::string & name() const
Returns a name for the formatter, which becomes the tag used for parsing.
const std::string & description() const
Returns a short description of the formatter and its allowed arguments (if any). This description is ...
adapter::Adapter< InternalType, ExternalType > m_adapt
const DictType & home() const
const Access the dictionary containing this formatter, set during DictType::lookup
DatumFormatterAdapter(const std::string &_name, const std::string &_desc, adapter::Adapter< InternalType, ExternalType > _adapt, BaseDatumFormatter< InternalType > const &_base_formatter)
Index num_passes(ExternalType const &_data_obj) const override
std::unique_ptr< DatumFormatterAdapter< ExternalType, InternalType > > clone() const
std::vector< std::string > col_header(ExternalType const &_template_obj) const override
Returns a header string for each scalar produced by the formatter parsing the entries in the col_head...
DatumFormatterAdapter(adapter::Adapter< InternalType, ExternalType > _adapt, BaseDatumFormatter< InternalType > const &_base_formatter)
DatumFormatterClass type() const override
bool parse_args(const std::string &args) override
void inject(ExternalType const &_data_obj, DataStream &_stream, Index pass_index=0) const override
notstd::cloneable_ptr< BaseDatumFormatter< InternalType > > m_base_ptr
std::string long_header(ExternalType const &_template_obj) const override
Returns a long expression for each scalar produced by the formatter parsing the long_header should re...
typename BaseDatumFormatter< ExternalType >::DictType DictType
typename BaseDatumFormatter< ExternalType >::difference_type difference_type
virtual ~DatumFormatterAdapter()
Allow polymorphic deletion.
BaseDatumFormatter< ExternalType > * _clone() const override
Make an exact copy of the formatter (including any initialized members)
jsonParser & to_json(ExternalType const &_data_obj, jsonParser &json) const override
std::string short_header(ExternalType const &_template_obj) const override
Returns a short expression for the formatter parsing the short_header should allow the formatter to b...
void set_home(const DictType &home) const
Set the dictionary containing this formatter, set during DictType::lookup.
void print(ExternalType const &_data_obj, std::ostream &_stream, Index pass_index=0) const override
bool init(ExternalType const &_template_obj) const override
Perform all initialization steps using _template_obj. Returns true if initialization is successful an...
bool validate(ExternalType const &_data_obj) const override
Returns true if _data_obj has valid values for requested data.
A 'cloneable_ptr' can be used in place of 'unique_ptr'.
Main CASM namespace.
Definition: APICommand.hh:8
DatumFormatterClass
DatumFormatterAdapter< ExternalType, InternalType > make_datum_formatter_adapter(BaseDatumFormatter< InternalType > const &_base_formatter)
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Non-std smart pointer classes and functions.