Jpp test-rotations-old-533-g2bdbdb559
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
275 out << (i != 0 ? " " : "");
276
277 i1->second->print(out, ps + i * size);
278 }
279
280 } else {
281
282 out << "?";
283 }
284
285 out << endl;
286 }
287
288
289 /**
290 * Print <tt>TArray</tt>.
291 *
292 * \param out output stream
293 * \param prefix prefix
294 * \param pA pointer to array
295 */
296 template<class TArray_t>
297 void print(std::ostream& out, const std::string prefix, const TArray* pA) const
298 {
299 using namespace std;
300
301 const TArray_t* p = dynamic_cast<const TArray_t*>(pA);
302
303 if (p != NULL) {
304 for (Int_t i = 0; i != p->GetSize(); ++i) {
305 out << prefix << "[" << i << "]" << ' ' << p->At(i) << endl;
306 }
307 }
308 }
309
310
311 /**
312 * Print object.
313 *
314 * \param out output stream
315 * \param prefix prefix
316 * \param object object
317 */
318 void print(std::ostream& out, const std::string prefix, const JRootWritableClass& object) const
319 {
320 using namespace std;
321
322 if (object.is_valid()) {
323
324 if (this->count(object.getTypename()) != 0) {
325
326 this->print(out, prefix, object.getTypename(), object.getAddress());
327
328 } else if (object.getClass() != NULL) {
329
330 if (object.getClass() == TArray::Class()) {
331
332 TArray* p = (TArray*) (object.getAddress());
333
334 if (p->GetSize() == 0) {
335 out << prefix << " <" << p->IsA()->GetName() << "> " << "{}" << endl;
336 } else {
337 print<TArrayC>(out, prefix, p);
338 print<TArrayS>(out, prefix, p);
339 print<TArrayI>(out, prefix, p);
340 print<TArrayL>(out, prefix, p);
341 print<TArrayF>(out, prefix, p);
342 print<TArrayD>(out, prefix, p);
343 }
344
345 } else if (object.getClass() == TList::Class()) {
346
347 int i = 0;
348
349 for (TIter next((TList*) object.getAddress()); const TObject *p1 = (const TObject*) next(); ++i) {
350 print(out, prefix + "[" + to_string(i) + "]", JRootWritableClass(p1->IsA(), (const char*) p1));
351 }
352
353 } else if (object.getClass()->GetCollectionProxy() != NULL) {
354
355 unique_ptr<TVirtualCollectionProxy> p1(object.getClass()->GetCollectionProxy()->Generate());
356
357 p1->PushProxy(const_cast<char*>(object.getAddress()));
358
359 if (p1->GetValueClass() == NULL) {
360 for (UInt_t i = 0; i != p1->Size(); ++i) {
361 this->print(out, prefix + "[" + to_string(i) + "]", TDataType::GetTypeName(p1->GetType()), (const char*) p1->At(i));
362 }
363 } else {
364 for (UInt_t i = 0; i != p1->Size(); ++i) {
365 this->print(out, prefix + "[" + to_string(i) + "]", p1->GetValueClass()->GetStreamerInfo(), (const char*) p1->At(i));
366 }
367 }
368
369 } else {
370
371 if (object.getClass()->GetListOfBases() != NULL) {
372
373 for (TIter next(object.getClass()->GetListOfBases()); const TBaseClass *p1 = (const TBaseClass*) next(); ) {
374
375 if (JRoot::is_class(p1->GetName())) {
376 this->print(out, prefix, object.get(*p1));
377 }
378 }
379 }
380
381 if (object.getClass()->GetListOfDataMembers() != NULL) {
382
383 if (!object.getClass()->InheritsFrom(TClass::GetClass<TArray>())) {
384
385 for (TIter next(object.getClass()->GetListOfDataMembers()); const TDataMember *p1 = (const TDataMember*) next(); ) {
386
387 if (JRoot::is_class(p1->GetName()) && !JRoot::is_static(*p1)) {
388
389 if (!p1->IsEnum() && p1->IsPersistent()) {
390 this->print(out, prefix + "." + p1->GetName(), object.get(*p1));
391 }
392 }
393 }
394 }
395 }
396 }
397 }
398 }
399 }
400
401
402 /**
403 * Print object.
404 *
405 * \param out output stream
406 * \param prefix prefix
407 * \param info pointer to streamer information
408 * \param ps pointer to data
409 */
410 void print(std::ostream& out, const std::string prefix, TVirtualStreamerInfo* info, const char* ps) const
411 {
412 using namespace std;
413
414 if (info != NULL) {
415
416 for (TIter next(info->GetElements()); TStreamerElement* ts = dynamic_cast<TStreamerElement*>(next()); ) {
417
418 if (this->count(ts->GetTypeName()) != 0) {
419
420 if (ts->GetArrayLength() == 0)
421 this->print(out, prefix + "." + ts->GetName(), ts->GetTypeName(), ps + ts->GetOffset());
422 else
423 this->print(out, prefix + "." + ts->GetName(), ts->GetTypeName(), ps + ts->GetOffset(), ts->GetArrayLength(), ts->GetSize() / ts->GetArrayLength());
424
425 } else if (ts->GetClass() != NULL) {
426
427 if (ts->IsaPointer()) {
428
429 char* po = NULL;
430
431 memcpy(&po, ps + ts->GetOffset(), sizeof(char*));
432
433 if (po != NULL) {
434
435 if (ts->GetClass()->InheritsFrom(TList::Class())) {
436
437 this->print(out, prefix + "." + ts->GetName(), JRootWritableClass(TList::Class(), po));
438
439 } else {
440
441 this->print(out, prefix + "." + ts->GetName(), ts->GetClass()->GetStreamerInfo(), po);
442 }
443
444 } else {
445
446 out << prefix << "." << ts->GetName() << " <" << ts->GetTypeName() << "> " << "NULL" << endl;
447 }
448
449 } else if (ts->GetClass()->InheritsFrom(TArray::Class())) {
450
451 print(out, prefix + "." + ts->GetName(), JRootWritableClass(TArray::Class(), ps + ts->GetOffset()));
452
453 } else if (ts->GetClass()->InheritsFrom(TDirectory::Class())) {
454
455 // do nothing
456
457 } else if (ts->GetClass()->GetCollectionProxy() != NULL) {
458
459 print(out, prefix + "." + ts->GetName(), JRootWritableClass(ts->GetClass(), ps + ts->GetOffset()));
460
461 } else {
462
463 if (JRoot::is_class(ts->GetName())) {
464 this->print(out, prefix + "." + ts->GetName(), ts->GetClass()->GetStreamerInfo(), ps + ts->GetOffset());
465 }
466 }
467 }
468 }
469 }
470 }
471 };
472}
473
474#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