Jpp master_rocky-44-g75b7c4f75
the software that should make you happy
Loading...
Searching...
No Matches
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"
10#include "JROOT/JRootClass.hh"
13
14/**
15 * \file
16 *
17 * Print objects in ASCII format using ROOT dictionary.
18 * \author mdejong
19 */
20namespace JROOT {}
21namespace JPP { using namespace JROOT; }
22
23namespace JROOT {
24
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,
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,
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,
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,
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.
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.
Implementation for ASCII output of objects with ROOT dictionary.
const JRootDictionary_t & getDictionary() const
Get dictictionary.
JRootWriter & putObject(const T &object)
Write object.
JRootWriter & put(const T &object)
Write object according equation format.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
static data_type & getInstance()
Get unique instance of template class.
Definition JSingleton.hh:27
bool is_valid() const
Check validity of this addressable class.
JRootAddressableClass find(const char *const name) const
Find addressable base class or data member with given name within current class.
const char * getTypename() const
Get type name.
Type definition of ROOT based dictionary for ASCII I/O.
Auxiliary class for printing objects in ASCII format using JRootWriter.
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 void print(JRootWriter &writer, const JRootWritableClass &cls)
Write class contents to output.
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.
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 JEquationParameters & getEquationParameters()
Get equation parameters.
static void setEquationParameters(const JEquationParameters &equation)
Set equation parameters.
ROOT class for writing object.