Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JRootTypewriter.hh
Go to the documentation of this file.
1#ifndef __JROOT_TYPEWRITER__
2#define __JROOT_TYPEWRITER__
3
4#include <string>
5#include <memory>
6#include <map>
7#include <ostream>
8
9#include "TString.h"
10#include "TDictionary.h"
11#include "TVirtualCollectionProxy.h"
12#include "TStreamerInfo.h"
13#include "TStreamerElement.h"
14#include "TBaseClass.h"
15#include "TDataMember.h"
16#include "TArray.h"
17#include "TArrayC.h"
18#include "TArrayS.h"
19#include "TArrayI.h"
20#include "TArrayL.h"
21#include "TArrayF.h"
22#include "TArrayD.h"
23#include "TList.h"
24#include "TDirectory.h"
25
26#include "JROOT/JRootClass.hh"
28
29
30namespace JROOT {}
31namespace JPP { using namespace JROOT; }
32
33namespace JROOT {
34
35 using JLANG::JType;
36
37
38 /**
39 * Interface for printing of a template class.
40 */
42 public:
43 /**
44 * Virtual destructor.
45 */
47 {}
48
49
50 /**
51 * Print object.
52 *
53 * \param out output stream
54 * \param po pointer to object
55 */
56 virtual void print(std::ostream& out, const char* po) const = 0;
57 };
58
59
60 /**
61 * Implementation for printing of a template class.
62 *
63 * This class implements the JROOT::JAstractTypewriter interface for the given template class.
64 */
65 template<class T>
68 {
69 public:
70 /**
71 * Print object.
72 *
73 * \param out output stream
74 * \param po pointer to object
75 */
76 virtual void print(std::ostream& out, const char* po) const override
77 {
78 out << * ((const T*) po);
79 }
80 };
81
82
83 /**
84 * Specialisation of print method for <tt>TString</tt>.
85 *
86 * \param out output stream
87 * \param po pointer to object
88 */
89 template<>
90 void JObjectTypewriter<TString>::print(std::ostream& out, const char* po) const
91 {
92 const TString* str = (const TString*) po;
93
94 if (!str->IsNull()) {
95 out << "\"" << *str << "\"";
96 }
97 }
98
99
100 /**
101 * Specialisation of print method for <tt>std::string</tt>.
102 *
103 * \param out output stream
104 * \param po pointer to object
105 */
106 template<>
107 void JObjectTypewriter<std::string>::print(std::ostream& out, const char* po) const
108 {
109 out << "\"" << * ((const std::string*) po) << "\"";
110 }
111
112
113 /**
114 * Specialisation of print method for <tt>unsigned char</tt>.
115 *
116 * \param out output stream
117 * \param po pointer to object
118 */
119 template<>
120 void JObjectTypewriter<char>::print(std::ostream& out, const char* po) const
121 {
122 using namespace std;
123
124 out << "0x" << hex << (int) *po << dec;
125 }
126
127
128 /**
129 * Specialisation of print method for <tt>unsigned char</tt>.
130 *
131 * \param out output stream
132 * \param po pointer to object
133 */
134 template<>
135 void JObjectTypewriter<unsigned char>::print(std::ostream& out, const char* po) const
136 {
137 using namespace std;
138
139 out << "\0x" << hex << (unsigned int) *po << dec;
140 }
141
142
143 /**
144 * ROOT type writer.
145 */
147 public std::map<std::string, std::shared_ptr<JAbstractTypewriter> >
148 {
149 public:
150 /**
151 * Default constructor.
152 */
154 {
155 handler(*this);
156 }
157
158
159 /**
160 * Print object.
161 *
162 * \param out output stream
163 * \param object object
164 */
165 template<class T>
166 void operator()(std::ostream& out, const T& object) const
167 {
168 (*this)(out, JRootWritableClass(object));
169 }
170
171
172 /**
173 * Print object.
174 *
175 * \param out output stream
176 * \param object object
177 */
178 void operator()(std::ostream& out, const JRootWritableClass& object) const
179 {
180 this->print(out, object.getTypename(), object);
181 }
182
183
184 /**
185 * Print object.
186 *
187 * \param out output stream
188 * \param info pointer to streamer information
189 * \param ps pointer to data
190 */
191 void operator()(std::ostream& out, TVirtualStreamerInfo* info, const char* ps) const
192 {
193 this->print(out, info->GetClass()->GetName(), info, ps);
194 }
195
196
197 /**
198 * Print object.
199 *
200 * \param out output stream
201 * \param object object
202 */
203 void operator()(std::ostream& out, const JBranchClass& object) const
204 {
205 if (object.getClass() != NULL)
206 this->print(out, object.getName(), object.getClass()->GetStreamerInfo(), static_cast<const JRootReadableClass&>(object).getAddress());
207 else
208 this->print(out, object.getName(), object.getDictionary()->GetName(), static_cast<const JRootReadableClass&>(object).getAddress());
209 }
210
211
212 /**
213 * Addition of class.
214 *
215 * \param name data name
216 * \param type data type
217 */
218 template<class T>
219 void operator()(const char* const name, const JType<T>& type)
220 {
221 this->insert(value_type(name, new JObjectTypewriter<T>()));
222 }
223
224
225 protected:
226 /**
227 * Print object.
228 *
229 * \param out output stream
230 * \param prefix prefix
231 * \param type type
232 * \param object pointer to object
233 */
234 void print(std::ostream& out, const std::string prefix, const char* type, const char* object) const
235 {
236 using namespace std;
237
238 const_iterator i1 = this->find(type);
239
240 out << prefix;
241 out << " <" << type << "> ";
242
243 if (i1 != this->end())
244 i1->second->print(out, object);
245 else
246 out << "?";
247
248 out << endl;
249 }
250
251
252 /**
253 * Print array of objects.
254 *
255 * \param out output stream
256 * \param prefix prefix
257 * \param type type
258 * \param ps pointer to begin of array
259 * \param ns number of elements
260 * \param size size of element
261 */
262 void print(std::ostream& out, const std::string prefix, const char* type, const char* ps, const size_t ns, const size_t size) const
263 {
264 using namespace std;
265
266 const_iterator i1 = this->find(type);
267
268 out << prefix;
269 out << " <" << type << "> ";
270
271 if (i1 != this->end()) {
272
273 for (size_t i = 0; i != ns; ++i) {
274 i1->second->print(out, ps + i * size);
275 }
276
277 } else {
278
279 out << "?";
280 }
281
282 out << endl;
283 }
284
285
286 /**
287 * Print <tt>TArray</tt>.
288 *
289 * \param out output stream
290 * \param prefix prefix
291 * \param pA pointer to array
292 */
293 template<class TArray_t>
294 void print(std::ostream& out, const std::string prefix, const TArray* pA) const
295 {
296 using namespace std;
297
298 const TArray_t* p = dynamic_cast<const TArray_t*>(pA);
299
300 if (p != NULL) {
301 for (Int_t i = 0; i != p->GetSize(); ++i) {
302 out << prefix << "[" << i << "]" << ' ' << p->At(i) << endl;
303 }
304 }
305 }
306
307
308 /**
309 * Print object.
310 *
311 * \param out output stream
312 * \param prefix prefix
313 * \param object object
314 */
315 void print(std::ostream& out, const std::string prefix, const JRootWritableClass& object) const
316 {
317 using namespace std;
318
319 if (object.is_valid()) {
320
321 if (this->count(object.getTypename()) != 0) {
322
323 this->print(out, prefix, object.getTypename(), object.getAddress());
324
325 } else if (object.getClass() != NULL) {
326
327 if (object.getClass() == TArray::Class()) {
328
329 TArray* p = (TArray*) (object.getAddress());
330
331 if (p->GetSize() == 0) {
332 out << prefix << " <" << p->IsA()->GetName() << "> " << "{}" << endl;
333 } else {
334 print<TArrayC>(out, prefix, p);
335 print<TArrayS>(out, prefix, p);
336 print<TArrayI>(out, prefix, p);
337 print<TArrayL>(out, prefix, p);
338 print<TArrayF>(out, prefix, p);
339 print<TArrayD>(out, prefix, p);
340 }
341
342 } else if (object.getClass() == TList::Class()) {
343
344 int i = 0;
345
346 for (TIter next((TList*) object.getAddress()); const TObject *p1 = (const TObject*) next(); ++i) {
347 print(out, prefix + "[" + to_string(i) + "]", JRootWritableClass(p1->IsA(), (const char*) p1));
348 }
349
350 } else if (object.getClass()->GetCollectionProxy() != NULL) {
351
352 unique_ptr<TVirtualCollectionProxy> p1(object.getClass()->GetCollectionProxy()->Generate());
353
354 p1->PushProxy(const_cast<char*>(object.getAddress()));
355
356 if (p1->GetValueClass() == NULL) {
357 for (UInt_t i = 0; i != p1->Size(); ++i) {
358 this->print(out, prefix + "[" + to_string(i) + "]", TDataType::GetTypeName(p1->GetType()), (const char*) p1->At(i));
359 }
360 } else {
361 for (UInt_t i = 0; i != p1->Size(); ++i) {
362 this->print(out, prefix + "[" + to_string(i) + "]", p1->GetValueClass()->GetStreamerInfo(), (const char*) p1->At(i));
363 }
364 }
365
366 } else {
367
368 if (object.getClass()->GetListOfBases() != NULL) {
369
370 for (TIter next(object.getClass()->GetListOfBases()); const TBaseClass *p1 = (const TBaseClass*) next(); ) {
371
372 if (JRoot::is_class(p1->GetName())) {
373 this->print(out, prefix, object.get(*p1));
374 }
375 }
376 }
377
378 if (object.getClass()->GetListOfDataMembers() != NULL) {
379
380 if (!object.getClass()->InheritsFrom(TClass::GetClass<TArray>())) {
381
382 for (TIter next(object.getClass()->GetListOfDataMembers()); const TDataMember *p1 = (const TDataMember*) next(); ) {
383
384 if (JRoot::is_class(p1->GetName()) && !JRoot::is_static(*p1)) {
385
386 if (!p1->IsEnum() && p1->IsPersistent()) {
387 this->print(out, prefix + "." + p1->GetName(), object.get(*p1));
388 }
389 }
390 }
391 }
392 }
393 }
394 }
395 }
396 }
397
398
399 /**
400 * Print object.
401 *
402 * \param out output stream
403 * \param prefix prefix
404 * \param info pointer to streamer information
405 * \param ps pointer to data
406 */
407 void print(std::ostream& out, const std::string prefix, TVirtualStreamerInfo* info, const char* ps) const
408 {
409 using namespace std;
410
411 if (info != NULL) {
412
413 for (TIter next(info->GetElements()); TStreamerElement* ts = dynamic_cast<TStreamerElement*>(next()); ) {
414
415 if (this->count(ts->GetTypeName()) != 0) {
416
417 if (ts->GetArrayLength() == 0)
418 this->print(out, prefix + "." + ts->GetName(), ts->GetTypeName(), ps + ts->GetOffset());
419 else
420 this->print(out, prefix + "." + ts->GetName(), ts->GetTypeName(), ps + ts->GetOffset(), ts->GetArrayLength(), ts->GetSize() / ts->GetArrayLength());
421
422 } else if (ts->GetClass() != NULL) {
423
424 if (ts->IsaPointer()) {
425
426 char* po = NULL;
427
428 memcpy(&po, ps + ts->GetOffset(), sizeof(char*));
429
430 if (po != NULL) {
431
432 if (ts->GetClass()->InheritsFrom(TList::Class())) {
433
434 this->print(out, prefix + "." + ts->GetName(), JRootWritableClass(TList::Class(), po));
435
436 } else {
437
438 this->print(out, prefix + "." + ts->GetName(), ts->GetClass()->GetStreamerInfo(), po);
439 }
440
441 } else {
442
443 out << prefix << "." << ts->GetName() << " <" << ts->GetTypeName() << "> " << "NULL" << endl;
444 }
445
446 } else if (ts->GetClass()->InheritsFrom(TArray::Class())) {
447
448 print(out, prefix + "." + ts->GetName(), JRootWritableClass(TArray::Class(), ps + ts->GetOffset()));
449
450 } else if (ts->GetClass()->InheritsFrom(TDirectory::Class())) {
451
452 // do nothing
453
454 } else if (ts->GetClass()->GetCollectionProxy() != NULL) {
455
456 print(out, prefix + "." + ts->GetName(), JRootWritableClass(ts->GetClass(), ps + ts->GetOffset()));
457
458 } else {
459
460 if (JRoot::is_class(ts->GetName())) {
461 this->print(out, prefix + "." + ts->GetName(), ts->GetClass()->GetStreamerInfo(), ps + ts->GetOffset());
462 }
463 }
464 }
465 }
466 }
467 }
468 };
469}
470
471#endif
TPaveText * p1
Interface for printing of a template class.
virtual void print(std::ostream &out, const char *po) const =0
Print object.
virtual ~JAbstractTypewriter()
Virtual destructor.
Implementation for printing of a template class.
virtual void print(std::ostream &out, const char *po) const override
Print object.
void operator()(std::ostream &out, TVirtualStreamerInfo *info, const char *ps) const
Print object.
void print(std::ostream &out, const std::string prefix, const char *type, const char *ps, const size_t ns, const size_t size) const
Print array of objects.
void print(std::ostream &out, const std::string prefix, const char *type, const char *object) const
Print object.
void operator()(std::ostream &out, const JBranchClass &object) const
Print object.
void print(std::ostream &out, const std::string prefix, const TArray *pA) const
Print TArray.
void print(std::ostream &out, const std::string prefix, TVirtualStreamerInfo *info, const char *ps) const
Print object.
void operator()(std::ostream &out, const T &object) const
Print object.
void operator()(std::ostream &out, const JRootWritableClass &object) const
Print object.
void print(std::ostream &out, const std::string prefix, const JRootWritableClass &object) const
Print object.
JRootTypewriter()
Default constructor.
void operator()(const char *const name, const JType< T > &type)
Addition of class.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
void handler(JType_t &object)
Apply ROOT types handler to given object.
const char * getName()
Get ROOT name of given data type.
Auxiliary class for a type holder.
Definition JType.hh:19
ROOT readable class for TBranch.
ROOT class for reading into object.
ROOT class for writing from object.
static bool is_class(const char *const name)
Check name of class against internal ROOT class names.
Definition JRoot.hh:30
static bool is_static(const TDataMember &object)
Check if data member is static.
Definition JRoot.hh:109