Jpp  16.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JASCIIFileStreamer.hh
Go to the documentation of this file.
1 #ifndef __JROOT_JASCIIFILESTREAMER__
2 #define __JROOT_JASCIIFILESTREAMER__
3 
4 #include <string>
5 #include <vector>
6 #include <fstream>
7 #include <sstream>
8 
10 #include "JLang/JRedirectString.hh"
11 
12 #include "JROOT/JRootClass.hh"
13 #include "JROOT/JRootDictionary.hh"
14 #include "JROOT/JRootStreamer.hh"
15 
16 
17 /**
18  * \author mdejong
19  */
20 namespace JROOT {}
21 namespace JPP { using namespace JROOT; }
22 
23 namespace JROOT {
24 
25 
26  /**
27  * Auxiliary class to read objects with ROOT dictionary from ASCII formatted file.
28  *
29  * The names of in the first line are one-to-one mapped to the names of the data members
30  * of the corresponding data structure using the corresponding ROOT dictionary.
31  */
32  template<class T>
34  public std::ifstream
35  {
36  using std::ifstream::operator>>;
37 
38  /**
39  * Constructor.
40  *
41  * \param dictionary dictionary
42  */
44  reader(static_cast<std::istream&>(*this), JLANG::JEquationParameters(), dictionary)
45  {}
46 
47 
48  /**
49  * Constructor.
50  *
51  * \param file_name file name
52  * \param dictionary dictionary
53  */
54  JASCIIFileReader(const char* const file_name,
55  const JRootDictionary& dictionary = JRootDictionary::getInstance()) :
56  reader(static_cast<std::istream&>(*this), JLANG::JEquationParameters(), dictionary)
57  {
58  this->open(file_name);
59  }
60 
61 
62  /**
63  * Read object from this file.
64  *
65  * \param object object
66  * \return this input stream
67  */
69  {
70  using namespace std;
71  using namespace JPP;
72 
73  if (columns.empty()) {
74 
75  JRootReadableClass cls(this->object);
76 
77  string buffer;
78 
79  if (std::getline(*this, buffer)) {
80 
81  istringstream is(buffer);
82 
83  while (is >> buffer) {
84  columns.push_back(cls.find(buffer.c_str()));
85  }
86  }
87  }
88 
89  object = value;
90 
91  reader.flags(this->flags());
92 
93  string buffer;
94 
95  if (std::getline(*this, buffer)) {
96 
97  istringstream is(buffer);
98 
99  for (size_t i = 0; i != columns.size() && is >> buffer; ++i) {
100 
101  if (columns[i].is_valid()) {
102 
103  JRedirectString redirect(reader, buffer);
104 
106  }
107  }
108 
109  object = this->object;
110  }
111 
112  return *this;
113  }
114 
115  private:
116  T object; //!< internal value
117  T value; //!< default value
118 
119  std::vector<JROOT::JRootReadableClass> columns; //!< column count -> data member
120 
122  };
123 
124 
125  /**
126  * Auxiliary class to write object with ROOT dictionary from ASCII formatted file.
127  *
128  * The names of in the first line are one-to-one mapped to the names of the data members
129  * of the corresponding data structure using the corresponding ROOT dictionary.
130  */
131  template<class T>
133  public std::ofstream
134  {
135  using std::ofstream::operator<<;
136 
137  /**
138  * Constructor.
139  *
140  * \param dictionary dictionary
141  */
143  writer(static_cast<std::ostream&>(*this), JLANG::JEquationParameters(), dictionary)
144  {}
145 
146 
147  /**
148  * Constructor.
149  *
150  * \param file_name file name
151  * \param dictionary dictionary
152  */
153  JASCIIFileWriter(const char* const file_name,
154  const JRootDictionary& dictionary = JRootDictionary::getInstance()) :
155  writer(static_cast<std::ostream&>(*this), JLANG::JEquationParameters(), dictionary)
156  {
157  this->open(file_name);
158  }
159 
160 
161  /**
162  * Write object to this file.
163  *
164  * \param object object
165  * \return this input stream
166  */
167  JASCIIFileWriter& operator<<(const T& object)
168  {
169  using namespace std;
170  using namespace JPP;
171 
172  if (columns.empty()) {
173 
174  JRootWritableClass cls(this->object);
175 
176  for (const TDataMember* p : getListOfDataMembers<T>()) {
177 
178  static_cast<ostream&>(*this) << ' ' << p->GetName();
179 
180  columns.push_back(cls.get(*p));
181  }
182 
183  static_cast<ostream&>(*this) << endl;
184  }
185 
186  this->object = object;
187 
188  writer.flags(this->flags());
189 
190  for (size_t i = 0; i != columns.size(); ++i) {
191  writer.putObject(columns[i]);
192  }
193 
194  return *this;
195  }
196 
197  private:
198  T object; //!< internal value
199 
200  std::vector<JROOT::JRootWritableClass> columns; //!< column count -> data member
201 
203  };
204 }
205 
206 #endif
JASCIIFileReader & operator>>(T &object)
Read object from this file.
ROOT class for reading object.
Definition: JRootClass.hh:529
JRootAddressableClass find(const char *name) const
Find addressable base class or data member with given name within current class.
Definition: JRootClass.hh:473
Implementation for ASCII output of objects with ROOT dictionary.
Simple data structure to support I/O of equations (see class JLANG::JEquation).
then usage $script< detector file >< detectorfile > nIf the range of floors is the first detector file is aligned to the second before the comparison nIn this
JASCIIFileWriter(const char *const file_name, const JRootDictionary &dictionary=JRootDictionary::getInstance())
Constructor.
JASCIIFileWriter & operator<<(const T &object)
Write object to this file.
is
Definition: JDAQCHSM.chsm:167
T * open(const std::string &file_name)
Open file.
Definition: JeepToolkit.hh:306
JASCIIFileWriter(const JRootDictionary &dictionary=JRootDictionary::getInstance())
Constructor.
Auxiliary class to read objects with ROOT dictionary from ASCII formatted file.
Implementation for ASCII input of objects with ROOT dictionary.
ASCII I/O of objects with ROOT dictionary.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
bool is_valid(const json &js)
Check validity of JSon data.
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
JRootReader & getObject(T &object)
Read object.
JASCIIFileReader(const JRootDictionary &dictionary=JRootDictionary::getInstance())
Constructor.
JRootAddressableClass get(const TDataMember &data_member) const
Get addressable class of given data member.
Definition: JRootClass.hh:484
Default implementation of ROOT based dictionary for ASCII I/O.
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
JASCIIFileReader(const char *const file_name, const JRootDictionary &dictionary=JRootDictionary::getInstance())
Constructor.
std::vector< JROOT::JRootWritableClass > columns
column count -&gt; data member
Auxiliary class to write object with ROOT dictionary from ASCII formatted file.
std::vector< JROOT::JRootReadableClass > columns
column count -&gt; data member
ROOT class for writing object.
Definition: JRootClass.hh:589