CASM  1.1.0
A Clusters Approach to Statistical Mechanics
FormattedDataFile_impl.hh
Go to the documentation of this file.
1 #ifndef CASM_FormattedDataFile_impl
2 #define CASM_FormattedDataFile_impl
3 
4 #include <boost/filesystem/fstream.hpp>
5 
7 #include "casm/external/gzstream/gzstream.h"
8 
9 namespace CASM {
10 
11 template <typename DataObject>
13  FormattedDataFileOptions const &options)
14  : FormattedDataFile(options.file_path, options.json_output,
15  options.json_arrays, options.compress) {}
16 
17 template <typename DataObject>
19  bool json_output,
20  bool json_arrays,
21  bool compress)
22  : m_initialized(false),
23  m_json_output(json_output),
24  m_json_arrays(json_arrays) {
25  if (!compress) {
26  m_ostream = notstd::make_unique<fs::ofstream>(file_path);
27  } else {
28  m_ostream = notstd::make_unique<gz::ogzstream>(file_path.string().c_str());
29  }
30 }
31 
32 template <typename DataObject>
34  if (m_json_output) {
35  *m_ostream << m_json;
36  }
37 }
38 
39 template <typename DataObject>
41  DataFormatter<DataObject> const &formatter, DataObject const &object) {
42  if (!m_initialized) {
43  if (!m_json_output) {
44  // do nothing. headers will be printed... somehow
45  } else if (m_json_arrays) {
46  m_json = jsonParser::object();
47  } else {
48  m_json = jsonParser::array();
49  }
50  m_initialized = true;
51  }
52  if (!m_json_output) {
53  *m_ostream << formatter(object);
54  } else if (m_json_arrays) {
55  formatter.to_json_arrays(object, m_json);
56  } else {
57  // formatter.to_json(object, m_json); // TODO: why doesn't this work?
58  m_json.push_back(formatter(object));
59  }
60 }
61 
62 } // namespace CASM
63 
64 #endif
jsonParser & to_json_arrays(const DataObject &_obj, jsonParser &json) const
void operator()(DataFormatter< DataObject > const &formatter, DataObject const &object)
FormattedDataFile(FormattedDataFileOptions const &options)
std::unique_ptr< std::ostream > m_ostream
static jsonParser object()
Returns an empty json object.
Definition: jsonParser.hh:395
static jsonParser array()
Returns an empty json array.
Definition: jsonParser.hh:409
jsonParser & push_back(const T &value, Args &&... args)
Definition: jsonParser.hh:684
Main CASM namespace.
Definition: APICommand.hh:8