Jpp 19.3.0-rc.1
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 "JLang/JTypeList.hh"
28
29#include "JROOT/JRootClass.hh"
30
31
32namespace JROOT {}
33namespace JPP { using namespace JROOT; }
34
35namespace JROOT {
36
37 using JLANG::JType;
38
39
40 /**
41 * Interface for printing of a template class.
42 */
44 public:
45 /**
46 * Virtual destructor.
47 */
49 {}
50
51
52 /**
53 * Print object.
54 *
55 * \param out output stream
56 * \param po pointer to object
57 */
58 virtual void print(std::ostream& out, const char* po) const = 0;
59 };
60
61
62 /**
63 * Implementation for printing of a template class.
64 *
65 * This class implements the JROOT::JAstractTypewriter interface for the given template class.
66 */
67 template<class T>
70 {
71 public:
72 /**
73 * Print object.
74 *
75 * \param out output stream
76 * \param po pointer to object
77 */
78 virtual void print(std::ostream& out, const char* po) const override
79 {
80 out << * ((const T*) po);
81 }
82 };
83
84
85 /**
86 * Specialisation of print method for <tt>TString</tt>.
87 *
88 * \param out output stream
89 * \param po pointer to object
90 */
91 template<>
92 void JObjectTypewriter<TString>::print(std::ostream& out, const char* po) const
93 {
94 const TString* str = (const TString*) po;
95
96 if (!str->IsNull()) {
97 out << "\"" << *str << "\"";
98 }
99 }
100
101
102 /**
103 * Specialisation of print method for <tt>std::string</tt>.
104 *
105 * \param out output stream
106 * \param po pointer to object
107 */
108 template<>
109 void JObjectTypewriter<std::string>::print(std::ostream& out, const char* po) const
110 {
111 out << "\"" << * ((const std::string*) po) << "\"";
112 }
113
114
115 /**
116 * Specialisation of print method for <tt>unsigned char</tt>.
117 *
118 * \param out output stream
119 * \param po pointer to object
120 */
121 template<>
122 void JObjectTypewriter<char>::print(std::ostream& out, const char* po) const
123 {
124 using namespace std;
125
126 out << "0x" << hex << (int) *po << dec;
127 }
128
129
130 /**
131 * Specialisation of print method for <tt>unsigned char</tt>.
132 *
133 * \param out output stream
134 * \param po pointer to object
135 */
136 template<>
137 void JObjectTypewriter<unsigned char>::print(std::ostream& out, const char* po) const
138 {
139 using namespace std;
140
141 out << "\0x" << hex << (unsigned int) *po << dec;
142 }
143
144
145 /**
146 * ROOT branch class.
147 */
149 public JRootReadableClass
150 {
151 /**
152 * Default constructor.
153 */
157
158
159 /**
160 * Copy constructor.
161 *
162 * \param bc branch class
163 */
165 JRootReadableClass(static_cast<const JRootReadableClass&>(bc))
166 {
167 this->name = bc.name;
168 this->type = bc.type;
169
170 if (this->type != kOther_t) {
171 this->address = new char[TDataType::GetDataType(this->type)->Size()];
172 }
173 }
174
175
176 /**
177 * Constructor.
178 *
179 * \param pb pointer to branch
180 */
181 JBranchClass(TBranch* pb) :
183 {
184 if (pb != NULL) {
185
186 this->name = pb->GetName();
187
188 TClass* cs;
189
190 if (pb->GetExpectedType(cs, this->type) == 0) {
191
192 if (cs != NULL)
193 this->dictionary = cs;
194 else
195 this->dictionary = TDictionary::GetDictionary(TDataType::GetTypeName(this->type));
196
197 if (this->type != kOther_t) {
198 this->address = new char[TDataType::GetDataType(this->type)->Size()];
199 }
200 }
201 }
202 }
203
204
205 /**
206 * Destructor.
207 */
209 {
210 if (this->type != kOther_t) {
211 if (this->address != NULL) {
212 delete [] this->address;
213 }
214 }
215 }
216
217
218 /**
219 * Get name.
220 *
221 * \return name
222 */
223 const char* const getName() const
224 {
225 return this->name.c_str();
226 }
227
228
229 /**
230 * Get type.
231 *
232 * \return type
233 */
234 EDataType getType() const
235 {
236 return this->type;
237 }
238
239
240 /**
241 * Get branch address.
242 *
243 * \return address
244 */
246 {
247 return (type == kOther_t ? (void*) (&this->address) : (void*) this->address);
248 }
249
250 private:
251 std::string name;
252 EDataType type = kNoType_t;
253 };
254
255
256 /**
257 * ROOT type writer.
258 */
260 public std::map<std::string, std::shared_ptr<JAbstractTypewriter> >
261 {
262 public:
263 /**
264 * Default constructor.
265 */
267 {
268 using namespace JPP;
269
270 for_each<JRemove<JPrimitiveTypes_t, long double>::typelist>(*this);
271
272 (*this)(JType<std::string>());
273 (*this)(JType<std::size_t>());
274
275#define VALUE_TYPE(__TYPE__) value_type(#__TYPE__, new JObjectTypewriter<__TYPE__>())
276
277 this->insert(VALUE_TYPE(Char_t));
278 this->insert(VALUE_TYPE(UChar_t));
279 this->insert(VALUE_TYPE(Short_t));
280 this->insert(VALUE_TYPE(UShort_t));
281 this->insert(VALUE_TYPE(Int_t));
282 this->insert(VALUE_TYPE(UInt_t));
283 this->insert(VALUE_TYPE(Long_t));
284 this->insert(VALUE_TYPE(ULong_t));
285 this->insert(VALUE_TYPE(Long64_t));
286 this->insert(VALUE_TYPE(ULong64_t));
287 this->insert(VALUE_TYPE(Float_t));
288 this->insert(VALUE_TYPE(Double_t));
289 this->insert(VALUE_TYPE(TString));
290
291#undef VALUE_TYPE
292 }
293
294
295 /**
296 * Print object.
297 *
298 * \param out output stream
299 * \param object object
300 */
301 template<class T>
302 void operator()(std::ostream& out, const T& object) const
303 {
304 (*this)(out, JRootWritableClass(object));
305 }
306
307
308 /**
309 * Print object.
310 *
311 * \param out output stream
312 * \param object object
313 */
314 void operator()(std::ostream& out, const JRootWritableClass& object) const
315 {
316 this->print(out, object.getTypename(), object);
317 }
318
319
320 /**
321 * Print object.
322 *
323 * \param out output stream
324 * \param info pointer to streamer information
325 * \param ps pointer to data
326 */
327 void operator()(std::ostream& out, TVirtualStreamerInfo* info, const char* ps) const
328 {
329 this->print(out, info->GetClass()->GetName(), info, ps);
330 }
331
332
333 /**
334 * Print object.
335 *
336 * \param out output stream
337 * \param object object
338 */
339 void operator()(std::ostream& out, const JBranchClass& object) const
340 {
341 if (object.getClass() != NULL)
342 this->print(out, object.getName(), object.getClass()->GetStreamerInfo(), static_cast<const JRootReadableClass&>(object).getAddress());
343 else
344 this->print(out, object.getName(), object.getDictionary()->GetName(), static_cast<const JRootReadableClass&>(object).getAddress());
345 }
346
347
348 /**
349 * Addition of class.
350 *
351 * \param type data type
352 */
353 template<class T>
354 void operator()(const JType<T>& type)
355 {
356 this->insert(value_type(JRoot::getTypename<T>(), new JObjectTypewriter<T>()));
357 }
358
359
360 protected:
361 /**
362 * Print object.
363 *
364 * \param out output stream
365 * \param prefix prefix
366 * \param type type
367 * \param object pointer to object
368 */
369 void print(std::ostream& out, const std::string prefix, const char* type, const char* object) const
370 {
371 using namespace std;
372
373 const_iterator i1 = this->find(type);
374
375 out << prefix;
376 out << " <" << type << "> ";
377
378 if (i1 != this->end())
379 i1->second->print(out, object);
380 else
381 out << "?";
382
383 out << endl;
384 }
385
386
387 /**
388 * Print array of objects.
389 *
390 * \param out output stream
391 * \param prefix prefix
392 * \param type type
393 * \param ps pointer to begin of array
394 * \param ns number of elements
395 * \param size size of element
396 */
397 void print(std::ostream& out, const std::string prefix, const char* type, const char* ps, const size_t ns, const size_t size) const
398 {
399 using namespace std;
400
401 const_iterator i1 = this->find(type);
402
403 out << prefix;
404 out << " <" << type << "> ";
405
406 if (i1 != this->end()) {
407
408 for (size_t i = 0; i != ns; ++i) {
409 i1->second->print(out, ps + i * size);
410 }
411
412 } else {
413
414 out << "?";
415 }
416
417 out << endl;
418 }
419
420
421 /**
422 * Print <tt>TArray</tt>.
423 *
424 * \param out output stream
425 * \param prefix prefix
426 * \param pA pointer to array
427 */
428 template<class TArray_t>
429 void print(std::ostream& out, const std::string prefix, const TArray* pA) const
430 {
431 using namespace std;
432
433 const TArray_t* p = dynamic_cast<const TArray_t*>(pA);
434
435 if (p != NULL) {
436 for (Int_t i = 0; i != p->GetSize(); ++i) {
437 out << prefix << "[" << i << "]" << ' ' << p->At(i) << endl;
438 }
439 }
440 }
441
442
443 /**
444 * Print object.
445 *
446 * \param out output stream
447 * \param prefix prefix
448 * \param object object
449 */
450 void print(std::ostream& out, const std::string prefix, const JRootWritableClass& object) const
451 {
452 using namespace std;
453
454 if (object.is_valid()) {
455
456 if (this->count(object.getTypename()) != 0) {
457
458 this->print(out, prefix, object.getTypename(), object.getAddress());
459
460 } else if (object.getClass() != NULL) {
461
462 if (object.getClass() == TArray::Class()) {
463
464 TArray* p = (TArray*) (object.getAddress());
465
466 if (p->GetSize() == 0) {
467 out << prefix << " <" << p->IsA()->GetName() << "> " << "{}" << endl;
468 } else {
469 print<TArrayC>(out, prefix, p);
470 print<TArrayS>(out, prefix, p);
471 print<TArrayI>(out, prefix, p);
472 print<TArrayL>(out, prefix, p);
473 print<TArrayF>(out, prefix, p);
474 print<TArrayD>(out, prefix, p);
475 }
476
477 } else if (object.getClass() == TList::Class()) {
478
479 int i = 0;
480
481 for (TIter next((TList*) object.getAddress()); const TObject *p1 = (const TObject*) next(); ++i) {
482 print(out, prefix + "[" + to_string(i) + "]", JRootWritableClass(p1->IsA(), (const char*) p1));
483 }
484
485 } else if (object.getClass()->GetCollectionProxy() != NULL) {
486
487 unique_ptr<TVirtualCollectionProxy> p1(object.getClass()->GetCollectionProxy()->Generate());
488
489 p1->PushProxy(const_cast<char*>(object.getAddress()));
490
491 if (p1->GetValueClass() == NULL) {
492 for (UInt_t i = 0; i != p1->Size(); ++i) {
493 this->print(out, prefix + "[" + to_string(i) + "]", TDataType::GetTypeName(p1->GetType()), (const char*) p1->At(i));
494 }
495 } else {
496 for (UInt_t i = 0; i != p1->Size(); ++i) {
497 this->print(out, prefix + "[" + to_string(i) + "]", p1->GetValueClass()->GetStreamerInfo(), (const char*) p1->At(i));
498 }
499 }
500
501 } else {
502
503 if (object.getClass()->GetListOfBases() != NULL) {
504
505 for (TIter next(object.getClass()->GetListOfBases()); const TBaseClass *p1 = (const TBaseClass*) next(); ) {
506
507 if (JRoot::is_class(p1->GetName())) {
508 this->print(out, prefix, object.get(*p1));
509 }
510 }
511 }
512
513 if (object.getClass()->GetListOfDataMembers() != NULL) {
514
515 if (!object.getClass()->InheritsFrom(TClass::GetClass<TArray>())) {
516
517 for (TIter next(object.getClass()->GetListOfDataMembers()); const TDataMember *p1 = (const TDataMember*) next(); ) {
518
519 if (JRoot::is_class(p1->GetName()) && !JRoot::is_static(*p1)) {
520
521 if (!p1->IsEnum() && p1->IsPersistent()) {
522 this->print(out, prefix + "." + p1->GetName(), object.get(*p1));
523 }
524 }
525 }
526 }
527 }
528 }
529 }
530 }
531 }
532
533
534 /**
535 * Print object.
536 *
537 * \param out output stream
538 * \param prefix prefix
539 * \param info pointer to streamer information
540 * \param ps pointer to data
541 */
542 void print(std::ostream& out, const std::string prefix, TVirtualStreamerInfo* info, const char* ps) const
543 {
544 using namespace std;
545
546 if (info != NULL) {
547
548 for (TIter next(info->GetElements()); TStreamerElement* ts = dynamic_cast<TStreamerElement*>(next()); ) {
549
550 if (this->count(ts->GetTypeName()) != 0) {
551
552 if (ts->GetArrayLength() == 0)
553 this->print(out, prefix + "." + ts->GetName(), ts->GetTypeName(), ps + ts->GetOffset());
554 else
555 this->print(out, prefix + "." + ts->GetName(), ts->GetTypeName(), ps + ts->GetOffset(), ts->GetArrayLength(), ts->GetSize() / ts->GetArrayLength());
556
557 } else if (ts->GetClass() != NULL) {
558
559 if (ts->IsaPointer()) {
560
561 char* po = NULL;
562
563 memcpy(&po, ps + ts->GetOffset(), sizeof(char*));
564
565 if (po != NULL) {
566
567 if (ts->GetClass()->InheritsFrom(TList::Class())) {
568
569 this->print(out, prefix + "." + ts->GetName(), JRootWritableClass(TList::Class(), po));
570
571 } else {
572
573 this->print(out, prefix + "." + ts->GetName(), ts->GetClass()->GetStreamerInfo(), po);
574 }
575
576 } else {
577
578 out << prefix << "." << ts->GetName() << " <" << ts->GetTypeName() << "> " << "NULL" << endl;
579 }
580
581 } else if (ts->GetClass()->InheritsFrom(TArray::Class())) {
582
583 print(out, prefix + "." + ts->GetName(), JRootWritableClass(TArray::Class(), ps + ts->GetOffset()));
584
585 } else if (ts->GetClass()->InheritsFrom(TDirectory::Class())) {
586
587 // do nothing
588
589 } else if (ts->GetClass()->GetCollectionProxy() != NULL) {
590
591 print(out, prefix + "." + ts->GetName(), JRootWritableClass(ts->GetClass(), ps + ts->GetOffset()));
592
593 } else {
594
595 if (JRoot::is_class(ts->GetName())) {
596 this->print(out, prefix + "." + ts->GetName(), ts->GetClass()->GetStreamerInfo(), ps + ts->GetOffset());
597 }
598 }
599 }
600 }
601 }
602 }
603 };
604}
605
606#endif
TPaveText * p1
Type list of primitive data types.
#define VALUE_TYPE(__TYPE__)
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 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.
const char * getName()
Get ROOT name of given data type.
Auxiliary class for a type holder.
Definition JType.hh:19
ROOT branch class.
JBranchClass()
Default constructor.
JBranchClass(const JBranchClass &bc)
Copy constructor.
EDataType getType() const
Get type.
JBranchClass(TBranch *pb)
Constructor.
void * getAddress()
Get branch address.
const char *const getName() const
Get name.
TDictionary * dictionary
pointer to ROOT dictionary
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
static const char * getTypename()
Get ROOT typename for given template class.
Definition JRoot.hh:138