Jpp  19.1.0
the software that should make you happy
JRootPrinter.hh
Go to the documentation of this file.
1 #ifndef __JROOT__JROOTPRINTER__
2 #define __JROOT__JROOTPRINTER__
3 
4 #include <string>
5 #include <ostream>
6 
8 #include "JLang/JException.hh"
9 #include "JLang/JLangToolkit.hh"
10 #include "JROOT/JRootClass.hh"
11 #include "JROOT/JRootStreamer.hh"
12 #include "JROOT/JRootDictionary.hh"
13 
14 /**
15  * \file
16  *
17  * Print objects in ASCII format using ROOT dictionary.
18  * \author mdejong
19  */
20 namespace JROOT {}
21 namespace JPP { using namespace JROOT; }
22 
23 namespace JROOT {
24 
26  using JLANG::JParseError;
27 
28  /**
29  * Auxiliary class for printing objects in ASCII format using JRootWriter.
30  */
31  struct JRootPrinter {
32  /**
33  * Get equation parameters.
34  *
35  * \return equation parameters
36  */
38  {
39  static JEquationParameters equation("=", "\n", ".", "");
40 
41  return equation;
42  }
43 
44 
45  /**
46  * Set equation parameters.
47  *
48  * \param equation equation parameters
49  */
50  static inline void setEquationParameters(const JEquationParameters& equation)
51  {
52  getEquationParameters() = equation;
53  }
54 
55 
56  /**
57  * Write class contents to output.
58  *
59  * \param writer ROOT writer
60  * \param cls ROOT class
61  */
62  static inline void print(JRootWriter& writer, const JRootWritableClass& cls)
63  {
64  if (cls.is_valid()) {
65  if (writer.getDictionary().find(cls.getTypename()) != writer.getDictionary().end())
66  writer.putObject(cls);
67  else
68  writer.put(cls);
69  }
70  }
71 
72 
73  /**
74  * Write part of object to output using ROOT dictionary.
75  *
76  * \param out output stream
77  * \param object object
78  * \param key data member name, optionally separated by division character
79  * \param parameters equation parameters
80  * \param dictionary dictionary
81  */
82  template<class T>
83  static inline void print(std::ostream& out,
84  const T& object,
85  const std::string& key,
86  const JEquationParameters& parameters,
87  const JRootDictionary_t& dictionary = JRootDictionary::getInstance())
88  {
89  using namespace std;
90  using namespace JPP;
91 
92  JRootWritableClass cls(object);
93 
94  for (string::size_type il = 0, ir = 0; ir != string::npos && cls.is_valid(); il = ir + 1) {
95  ir = key.substr(il).find(parameters.getDefaultDivision());
96  cls = cls.find(trim(key.substr(il, ir - il)).c_str());
97  }
98 
99  if (cls.is_valid()) {
100 
101  JRootWriter writer(out, parameters, dictionary);
102 
103  print(writer, cls);
104 
105  } else {
106 
107  THROW(JParseError, "Invalid key " << key);
108  }
109  }
110 
111 
112  /**
113  * Write object to output using ROOT dictionary.
114  *
115  * \param out output stream
116  * \param object object
117  * \param parameters equation parameters
118  * \param dictionary dictionary
119  */
120  template<class T>
121  static inline void print(std::ostream& out,
122  const T& object,
123  const JEquationParameters& parameters,
124  const JRootDictionary_t& dictionary = JRootDictionary::getInstance())
125  {
126  JRootWriter writer(out, parameters, dictionary);
127  JRootWritableClass cls(object);
128 
129  print(writer, cls);
130  }
131 
132 
133  /**
134  * Write part of object to output using ROOT dictionary.
135  *
136  * \param out output stream
137  * \param object object
138  * \param key data member name, optionally separated by division character '.'
139  * \param dictionary dictionary
140  */
141  template<class T>
142  static inline void print(std::ostream& out,
143  const T& object,
144  const std::string& key,
145  const JRootDictionary_t& dictionary = JRootDictionary::getInstance())
146  {
147  print(out, object, key, JRootPrinter::getEquationParameters(), dictionary);
148  }
149 
150 
151  /**
152  * Write object to output using ROOT dictionary.
153  *
154  * \param out output stream
155  * \param object object
156  * \param dictionary dictionary
157  */
158  template<class T>
159  static inline void print(std::ostream& out,
160  const T& object,
161  const JRootDictionary_t& dictionary = JRootDictionary::getInstance())
162  {
163  print(out, object, JRootPrinter::getEquationParameters(), dictionary);
164  }
165  };
166 }
167 
168 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
ASCII I/O of objects with ROOT dictionary.
Simple data structure to support I/O of equations (see class JLANG::JEquation).
const char getDefaultDivision() const
Get default division character.
Exception for parsing value.
Definition: JException.hh:198
Implementation for ASCII output of objects with ROOT dictionary.
const JRootDictionary_t & getDictionary() const
Get dictictionary.
JRootWriter & put(const T &object)
Write object according equation format.
JRootWriter & putObject(const T &object)
Write object.
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
Definition: JSTDTypes.hh:14
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
bool is_valid() const
Check validity of this addressable class.
Definition: JRootClass.hh:471
JRootAddressableClass find(const char *const name) const
Find addressable base class or data member with given name within current class.
Definition: JRootClass.hh:483
const char * getTypename() const
Get type name.
Definition: JRootClass.hh:205
Type definition of ROOT based dictionary for ASCII I/O.
Auxiliary class for printing objects in ASCII format using JRootWriter.
Definition: JRootPrinter.hh:31
static void print(std::ostream &out, const T &object, const JEquationParameters &parameters, const JRootDictionary_t &dictionary=JRootDictionary::getInstance())
Write object to output using ROOT dictionary.
static JEquationParameters & getEquationParameters()
Get equation parameters.
Definition: JRootPrinter.hh:37
static void print(JRootWriter &writer, const JRootWritableClass &cls)
Write class contents to output.
Definition: JRootPrinter.hh:62
static void print(std::ostream &out, const T &object, const std::string &key, const JEquationParameters &parameters, const JRootDictionary_t &dictionary=JRootDictionary::getInstance())
Write part of object to output using ROOT dictionary.
Definition: JRootPrinter.hh:83
static void print(std::ostream &out, const T &object, const JRootDictionary_t &dictionary=JRootDictionary::getInstance())
Write object to output using ROOT dictionary.
static void print(std::ostream &out, const T &object, const std::string &key, const JRootDictionary_t &dictionary=JRootDictionary::getInstance())
Write part of object to output using ROOT dictionary.
static void setEquationParameters(const JEquationParameters &equation)
Set equation parameters.
Definition: JRootPrinter.hh:50
ROOT class for writing object.
Definition: JRootClass.hh:604