Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRootStreamer.hh
Go to the documentation of this file.
1 #ifndef __JROOT__JROOTSTREAMER__
2 #define __JROOT__JROOTSTREAMER__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <sstream>
8 
9 #include "JLang/JEquation.hh"
11 #include "JLang/JEquationFacet.hh"
13 #include "JLang/JBool.hh"
14 #include "JLang/JRedirectString.hh"
15 #include "JLang/JRedirectStream.hh"
16 #include "JLang/JStreamState.hh"
18 #include "JROOT/JRootClass.hh"
20 
21 
22 /**
23  * \file
24  *
25  * ASCII I/O of objects with ROOT dictionary.
26  * \author mdejong
27  */
28 namespace JROOT {}
29 namespace JPP { using namespace JROOT; }
30 
31 namespace JROOT {
32 
33  using JLANG::JEquation;
35  using JLANG::JBool;
36  using JLANG::JStreamState;
37  using JLANG::JException;
38 
39 
40  /**
41  * Implementation for ASCII input of objects with ROOT dictionary.
42  *
43  * The method JRootReader::get is compatible with equation formatting, i.e:
44  *
45  * <pre><key>[<division><key>]<separator><value>[<white space><value>]*<end of line></pre>
46  *
47  * In this, the key corresponds to the name of a data member,
48  * which can be recursively parsed via a division character.\n
49  * \n
50  * The method JRootReader::getObject relates to simple formatting, i.e:
51  *
52  * <pre><value>[<white space><value>]*</pre>
53  *
54  * The default format for input of <tt>std::vector<T></tt> is
55  *
56  * <pre><number of elements>[<white space><value>]*</pre>
57  */
58  class JRootReader :
59  public std::istream,
60  private JStreamState
61  {
62  public:
63  /**
64  * Constructor.
65  *
66  * \param in input stream
67  * \param parameters equation parameters
68  * \param dictionary dictionary
69  */
70  JRootReader(std::istream& in,
73  std::istream(in.rdbuf()),
74  JStreamState(in, static_cast<std::istream&>(*this)),
75  dictionary(dictionary)
76  {
77  imbue(std::locale(in.getloc(), new JLANG::JEquationFacet(parameters)));
78  }
79 
80 
81  /**
82  * Get dictionary.
83  *
84  * \return dictionary
85  */
87  {
88  return dictionary;
89  }
90 
91 
92  /**
93  * Read object according equation format.
94  *
95  * \param object object
96  * \return this ROOT reader
97  */
98  template<class T>
99  JRootReader& get(T& object)
100  {
101  return this->get(JRootReadableClass(object));
102  }
103 
104 
105  /**
106  * Read ROOT class according equation format.
107  *
108  * \param cls ROOT class
109  * \return this ROOT reader
110  */
112  {
113  using namespace std;
114  using namespace JPP;
115 
116  if (cls.is_valid()) {
117 
118  const JEquationFacet& facet = use_facet<JEquationFacet>(this->getloc());
119 
120  for (JEquation equation; *this >> equation; ) {
121 
122  const JRootReadableClass abc = cls.find(equation.getKey().c_str());
123 
124  bool status = abc.is_valid();
125 
126  if (status) {
127 
128  JRedirectString redirect(*this, equation.getValue());
129 
130  if (facet.isDivision (equation.getSeparator()))
131  this->get(abc);
132  else if (facet.isSeparator(equation.getSeparator()))
133  this->getObject(abc);
134  else
135  status = false;
136 
137  status &= (!this->fail() || this->eof());
138  }
139 
140  if (!status) {
141  THROW(JException, "Error parsing: " << equation);
142  }
143  }
144  }
145 
146  return *this;
147  }
148 
149 
150  /**
151  * Read object.
152  *
153  * \param object object
154  * \return this ROOT reader
155  */
156  template<class T>
158  {
159  return this->getObject(object, JBool<JStreamAvailable<T>::has_istream>());
160  }
161 
162 
163  /**
164  * Read ROOT class.
165  *
166  * \param cls ROOT class
167  * \return this ROOT reader
168  */
170  {
171  if (cls.is_valid()) {
172 
173  JRootDictionary_t::const_iterator i = this->getDictionary().find(cls.getTypename());
174 
175  if (i != this->getDictionary().end()) {
176 
177  i->second->getObject(*this, cls.getAddress());
178 
179  } else if (cls.getClass() != NULL) {
180 
181  if (cls.getClass()->GetListOfBases() != NULL) {
182 
183  TIterator* i = cls.getClass()->GetListOfBases()->MakeIterator();
184 
185  for (const TBaseClass* p; (p = (const TBaseClass*) i->Next()) != NULL && (bool) (*this); ) {
186  this->getObject(cls.get(*p));
187  }
188  }
189 
190  if (cls.getClass()->GetListOfDataMembers() != NULL) {
191 
192  TIterator* i = cls.getClass()->GetListOfDataMembers()->MakeIterator();
193 
194  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL && (bool) (*this); ) {
195  this->getObject(cls.get(*p));
196  }
197  }
198  }
199  }
200 
201  return *this;
202  }
203 
204 
205  /**
206  * Read ROOT class.
207  *
208  * \param cls ROOT class
209  * \return this ROOT reader
210  */
212  {
213  return getObject(const_cast<const JRootReadableClass&>(cls));
214  }
215 
216  private:
217  /**
218  * Read object.
219  *
220  * \param object object
221  * \param option true
222  * \return this ROOT reader
223  */
224  template<class T>
225  JRootReader& getObject(T& object, const JBool<true>& option)
226  {
227  *this >> object;
228 
229  return *this;
230  }
231 
232 
233  /**
234  * Read object.
235  *
236  * \param object object
237  * \param option false
238  * \return this ROOT reader
239  */
240  template<class T>
241  JRootReader& getObject(T& object, const JBool<false>& option)
242  {
243  return this->getObject(JRootReadableClass(object));
244  }
245 
246 
247  /**
248  * Read object.
249  *
250  * \param object object
251  * \param option false
252  * \return this ROOT reader
253  */
254  template<class JElement_t, class JAllocator_t>
256  {
257  int n = 0;
258 
259  this->getObject(n);
260 
261  for (JElement_t element; n != 0 && this->getObject(element); --n) {
262  object.push_back(element);
263  }
264 
265  return *this;
266  }
267 
268 
270  };
271 
272 
273  /**
274  * Implementation for ASCII output of objects with ROOT dictionary.
275  *
276  * The method JRootWriter::put is compatible with equation formatting, i.e:
277  *
278  * <pre><key>[<division><key>]<separator><value>[<white space><value>]*<end of line></pre>
279  *
280  * In this, the key corresponds to the name of a data member
281  * which can be recursively parsed via a division character.\n
282  * \n
283  * The method JRootWriter::putObject relates to simple formatting, i.e:
284  *
285  * <pre><value>[<white space><value>]*</pre>
286  *
287  * The default format for output of <tt>std::vector<T></tt> is
288  *
289  * <pre><number of elements>[<white space><value>]*</pre>
290  */
291  class JRootWriter :
292  public std::ostream,
293  private JStreamState
294  {
295  public:
296  /**
297  * Constructor.
298  *
299  * \param out output stream
300  * \param parameters equation parameters
301  * \param dictionary dictionary
302  */
303  JRootWriter(std::ostream& out,
305  const JRootDictionary_t& dictionary) :
306  std::ostream(out.rdbuf()),
307  JStreamState(out, static_cast<std::ostream&>(*this)),
308  dictionary(dictionary)
309  {
310  imbue(std::locale(out.getloc(), new JLANG::JEquationFacet(parameters)));
311 
312  flags (out.flags());
313  width (out.width());
314  precision(out.precision());
315  }
316 
317 
318  /**
319  * Get dictictionary.
320  *
321  * \return dictionary
322  */
324  {
325  return dictionary;
326  }
327 
328 
329  /**
330  * Write object according equation format.
331  *
332  * \param object object
333  * \return this ROOT writer
334  */
335  template<class T>
336  JRootWriter& put(const T& object)
337  {
338  return this->put(JRootWritableClass(object));
339  }
340 
341 
342  /**
343  * Write given key and value according equation format.
344  *
345  * \param key key
346  * \param value value
347  * \return this ROOT writer
348  */
349  template<class T>
350  JRootWriter& put(const std::string& key, const T& value)
351  {
352  using namespace std;
353  using namespace JPP;
354 
355  ostringstream os;
356 
357  {
358  JRedirectStream redirect(*this, os);
359 
360  this->putObject(value);
361  }
362 
363  return this->put(JEquation(key, os.str()));
364  }
365 
366 
367  /**
368  * Write ROOT class according equation format.
369  *
370  * \param cls ROOT class
371  * \return this ROOT writer
372  */
374  {
375  return this->put("", cls, false);
376  }
377 
378 
379  /**
380  * Write ROOT class according equation format.
381  *
382  * \param cls ROOT class
383  * \return this ROOT writer
384  */
386  {
387  return this->put(const_cast<const JRootWritableClass&>(cls));
388  }
389 
390 
391  /**
392  * Write ROOT class according equation format.
393  *
394  * \param prefix prefix
395  * \param cls ROOT class
396  * \param eol end-of-line
397  * \return object output
398  */
399  JRootWriter& put(const std::string& prefix, const JRootWritableClass& cls, bool eol)
400  {
401  using namespace std;
402  using namespace JPP;
403 
404  if (cls.is_valid()) {
405 
406  const JEquationFacet& facet = use_facet<JEquationFacet>(this->getloc());
407 
408  JRootDictionary_t::const_iterator i = this->getDictionary().find(cls.getTypename());
409 
410  if (i != this->getDictionary().end()) {
411 
412  i->second->put(*this, prefix, cls.getAddress());
413 
414  } else if (eol) {
415 
416  ostringstream os;
417 
418  {
419  JRedirectStream redirect(*this, os);
420 
421  this->putObject(cls);
422  }
423 
424  this->put(JEquation(prefix, os.str()));
425 
426  } else if (cls.getClass() != NULL) {
427 
428  if (cls.getClass()->GetListOfBases() != NULL) {
429 
430  TIterator* i = cls.getClass()->GetListOfBases()->MakeIterator();
431 
432  for (const TBaseClass* p; (p = (const TBaseClass*) i->Next()) != NULL; ) {
433  this->put(prefix, cls.get(*p), false);
434  }
435  }
436 
437  if (cls.getClass()->GetListOfDataMembers() != NULL) {
438 
439  TIterator* i = cls.getClass()->GetListOfDataMembers()->MakeIterator();
440 
441  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
442  if (!JRootClass::is_static(*p)) {
443  this->put(facet.getPrefix(prefix,p->GetName()), cls.get(*p), false);
444  }
445  }
446  }
447  }
448  }
449 
450  return *this;
451  }
452 
453 
454  /**
455  * Write equation.
456  *
457  * \param equation equation
458  * \return this ROOT writer
459  */
460  virtual JRootWriter& put(const JEquation& equation)
461  {
462  *this << equation;
463 
464  return *this;
465  }
466 
467 
468  /**
469  * Write object.
470  *
471  * \param object object
472  * \return this ROOT writer
473  */
474  template<class T>
475  JRootWriter& putObject(const T& object)
476  {
477  return this->putObject(object, JBool<JStreamAvailable<T>::has_ostream>());
478  }
479 
480 
481  /**
482  * Write ROOT class.
483  *
484  * \param cls ROOT class
485  * \return object output
486  */
488  {
489  using namespace std;
490 
491  if (cls.is_valid()) {
492 
493  JRootDictionary_t::const_iterator i = this->getDictionary().find(cls.getTypename());
494 
495  if (i != this->getDictionary().end()) {
496 
497  i->second->putObject(*this, cls.getAddress());
498 
499  } else if (cls.getClass() != NULL) {
500 
501  if (cls.getClass()->GetListOfBases() != NULL) {
502 
503  TIterator* i = cls.getClass()->GetListOfBases()->MakeIterator();
504 
505  for (const TBaseClass* p; (p = (const TBaseClass*) i->Next()) != NULL; ) {
506  this->putObject(cls.get(*p));
507  }
508  }
509 
510  if (cls.getClass()->GetListOfDataMembers() != NULL) {
511 
512  TIterator* i = cls.getClass()->GetListOfDataMembers()->MakeIterator();
513 
514  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
515  if (!JRootClass::is_static(*p)) {
516  this->putObject(cls.get(*p));
517  }
518  }
519  }
520  }
521  }
522 
523  return *this;
524  }
525 
526  private:
527  /**
528  * Write object.
529  *
530  * \param object object
531  * \param option true
532  * \return this ROOT writer
533  */
534  template<class T>
535  JRootWriter& putObject(const T& object, const JBool<true>& option)
536  {
537  using namespace JPP;
538 
539  *this << white_space;
540  *this << object;
541 
542  return *this;
543  }
544 
545 
546  /**
547  * Write object.
548  *
549  * \param object object
550  * \param option false
551  * \return this ROOT writer
552  */
553  template<class T>
554  JRootWriter& putObject(const T& object, const JBool<false>& option)
555  {
556  return this->putObject(JRootWritableClass(object));
557  }
558 
559 
560  /**
561  * Write object.
562  *
563  * \param object object
564  * \param option false
565  * \return this ROOT writer
566  */
567  template<class JElement_t, class JAllocator_t>
569  {
570  this->putObject(object.size());
571 
572  for (typename std::vector<JElement_t, JAllocator_t>::const_iterator i = object.begin(); i != object.end(); ++i) {
573  this->putObject(*i);
574  }
575 
576  return *this;
577  }
578 
579 
581  };
582 
583 
584  /**
585  * Auxiliary template class to define default implementation of the ROOT streamer.
586  *
587  * This class transfers the I/O functionality to the JRootReader or JRootWriter class.\n
588  * For a custom implementation of the I/O a given class, this class should be specialised.\n
589  * The class should also be added to the dictionary in use (method JRootDictionary::add).
590  */
591  template<class T>
592  struct JRootStreamer {
593  /**
594  * Read object.
595  *
596  * \param reader ROOT reader
597  * \param object object
598  * \return ROOT reader
599  */
600  static JRootReader& getObject(JRootReader& reader, T& object)
601  {
602  return reader.getObject(object);
603  }
604 
605 
606  /**
607  * Write object.
608  *
609  * \param writer ROOT writer
610  * \param object object
611  * \return ROOT writer
612  */
613  static JRootWriter& putObject(JRootWriter& writer, const T& object)
614  {
615  return writer.putObject(object);
616  }
617 
618 
619  /**
620  * Write given key and value according equation format.
621  *
622  * \param writer ROOT writer
623  * \param key key
624  * \param value value
625  * \return ROOT writer
626  */
627  static JRootWriter& put(JRootWriter& writer, const std::string& key, const T& value)
628  {
629  return writer.put(key, value);
630  }
631  };
632 
633 
634  /**
635  * JObjectStreamer class.
636  *
637  * This class implements the JROOT::JAstractStreamer interface for the given template class.\n
638  * The I/O functionality is transferred first to the JRootStreamer class and
639  * subsequently -by default- to the JRootReader and JRootWriter class.\n
640  */
641  template<class T>
643  public JAbstractStreamer
644  {
645  public:
646  /**
647  * Stream input.
648  *
649  * \param in object reader
650  * \param address pointer to object
651  * \return object reader
652  */
653  virtual JRootReader& getObject(JRootReader& in, void* address) const
654  {
655  return JRootStreamer<T>::getObject(in, * ((T*) address));
656  }
657 
658 
659  /**
660  * Stream output.
661  *
662  * \param out object writer
663  * \param address pointer to object
664  * \return object writer
665  */
666  virtual JRootWriter& putObject(JRootWriter& out, const void* address) const
667  {
668  return JRootStreamer<T>::putObject(out, * ((const T*) address));
669  }
670 
671 
672  /**
673  * Stream output.
674  *
675  * \param out object writer
676  * \param prefix prefix
677  * \param address pointer to object
678  * \return object writer
679  */
680  virtual JRootWriter& put(JRootWriter& out, const std::string& prefix, const void* address) const
681  {
682  return JRootStreamer<T>::put(out, prefix, * ((const T*) address));
683  }
684  };
685 }
686 
687 #endif
const JRootDictionary_t & getDictionary() const
Get dictictionary.
JRootWriter & put(const std::string &key, const T &value)
Write given key and value according equation format.
General exception.
Definition: JException.hh:23
JRootWriter & putObject(const T &object, const JBool< true > &option)
Write object.
ROOT class for reading object.
Definition: JRootClass.hh:528
JRootAddressableClass find(const char *name) const
Find addressable base class or data member with given name within current class.
Definition: JRootClass.hh:472
JRootReader & getObject(T &object, const JBool< false > &option)
Read object.
Facet class to specify parsing of equations in currect locale (see class JLANG::JEquation).
Implementation for ASCII output of objects with ROOT dictionary.
static JRootReader & getObject(JRootReader &reader, T &object)
Read object.
virtual JRootWriter & put(const JEquation &equation)
Write equation.
JObjectStreamer class.
const char * getTypename() const
Get type name.
Definition: JRootClass.hh:194
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
This class can be used to temporarily exchange the states between streams.
Definition: JStreamState.hh:23
std::ostream & white_space(std::ostream &out)
Print white space.
Auxiliary template class to define default implementation of the ROOT streamer.
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Simple data structure to support I/O of equations (see class JLANG::JEquation).
virtual JRootReader & getObject(JRootReader &in, void *address) const
Stream input.
Forward declaration of writer object.
virtual JRootWriter & putObject(JRootWriter &out, const void *address) const
Stream output.
JRootWriter & putObject(const T &object)
Write object.
Test availability of stream operators.
General purpose equation class.
Definition: JEquation.hh:47
virtual JRootWriter & put(JRootWriter &out, const std::string &prefix, const void *address) const
Stream output.
Implementation for ASCII input of objects with ROOT dictionary.
Auxiliary template class for type bool.
Definition: JBool.hh:20
const JRootDictionary_t & dictionary
pointer_type getAddress() const
Get address.
Definition: JRootClass.hh:450
do set_variable OUTPUT_DIRECTORY $WORKDIR T
JRootReader & getObject(const JRootReadableClass &cls)
Read ROOT class.
JRootWriter(std::ostream &out, const JEquationParameters &parameters, const JRootDictionary_t &dictionary)
Constructor.
JRootReader(std::istream &in, const JEquationParameters &parameters, const JRootDictionary_t &dictionary)
Constructor.
static JRootWriter & putObject(JRootWriter &writer, const T &object)
Write object.
JRootReader & getObject(T &object)
Read object.
JRootReader & getObject(JRootReadableClass &cls)
Read ROOT class.
JRootWriter & put(const std::string &prefix, const JRootWritableClass &cls, bool eol)
Write ROOT class according equation format.
JRootWriter & put(JRootWritableClass &cls)
Write ROOT class according equation format.
JRootReader & getObject(std::vector< JElement_t, JAllocator_t > &object, const JBool< false > &option)
Read object.
JRootAddressableClass get(const TDataMember &data_member) const
Get addressable class of given data member.
Definition: JRootClass.hh:483
bool fail(std::istream &in)
Check for stream state.
Definition: JParser.hh:96
This file contains the basic interface for ASCII I/O of ROOT objects.
JRootWriter & putObject(const T &object, const JBool< false > &option)
Write object.
Type definition of ROOT based dictionary for ASCII I/O.
static JRootWriter & put(JRootWriter &writer, const std::string &key, const T &value)
Write given key and value according equation format.
bool is_valid() const
Check validity of this addressable class.
Definition: JRootClass.hh:461
TClass * getClass() const
Get class.
Definition: JRootClass.hh:183
alias put_queue eval echo n
Definition: qlib.csh:19
JRootReader & getObject(T &object, const JBool< true > &option)
Read object.
const JRootDictionary_t & dictionary
JRootWriter & put(const T &object)
Write object according equation format.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38
const JRootDictionary_t & getDictionary() const
Get dictionary.
JRootWriter & put(const JRootWritableClass &cls)
Write ROOT class according equation format.
JRootWriter & putObject(const JRootWritableClass &cls)
Write ROOT class.
static bool is_static(const TDataMember &data_member)
Check if data member is static.
Definition: JRootClass.hh:126
ROOT class for writing object.
Definition: JRootClass.hh:588
bool putObject(TDirectory *dir, const T &object)
Write object to ROOT directory.
JRootWriter & putObject(const std::vector< JElement_t, JAllocator_t > &object, const JBool< false > option)
Write object.