Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
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
11
12#include "JROOT/JRootClass.hh"
15
16
17/**
18 * \author mdejong
19 */
20namespace JROOT {}
21namespace JPP { using namespace JROOT; }
22
23namespace 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) {
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
ASCII I/O of objects with ROOT dictionary.
Simple data structure to support I/O of equations (see class JLANG::JEquation).
This class can be used to temporarily redirect an input stream to an input string.
Default implementation of ROOT based dictionary for ASCII I/O.
Implementation for ASCII input of objects with ROOT dictionary.
JRootReader & getObject(T &object)
Read object.
Implementation for ASCII output of objects with ROOT dictionary.
JRootWriter & putObject(const T &object)
Write object.
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
std::vector< TDataMember * > getListOfDataMembers()
Get list of ROOT data members for given class.
static data_type & getInstance()
Get unique instance of template class.
Definition JSingleton.hh:27
Auxiliary class to read objects with ROOT dictionary from ASCII formatted file.
JASCIIFileReader(const char *const file_name, const JRootDictionary &dictionary=JRootDictionary::getInstance())
Constructor.
JASCIIFileReader(const JRootDictionary &dictionary=JRootDictionary::getInstance())
Constructor.
JASCIIFileReader & operator>>(T &object)
Read object from this file.
std::vector< JROOT::JRootReadableClass > columns
column count -> data member
Auxiliary class to write object with ROOT dictionary from ASCII formatted file.
std::vector< JROOT::JRootWritableClass > columns
column count -> data member
JASCIIFileWriter(const JRootDictionary &dictionary=JRootDictionary::getInstance())
Constructor.
JASCIIFileWriter & operator<<(const T &object)
Write object to this file.
JASCIIFileWriter(const char *const file_name, const JRootDictionary &dictionary=JRootDictionary::getInstance())
Constructor.
JRootAddressableClass find(const char *const name) const
Find addressable base class or data member with given name within current class.
JRootAddressableClass get(const TDataMember &object) const
Get addressable class of given data member.
ROOT class for reading into object.
ROOT class for writing from object.