Jpp master_rocky-44-g75b7c4f75
the software that should make you happy
Loading...
Searching...
No Matches
JRootDictionary.hh
Go to the documentation of this file.
1#ifndef __JROOT__JROOTDICTIONARY__
2#define __JROOT__JROOTDICTIONARY__
3
4#include <string>
5#include <vector>
6#include <typeinfo>
7
8#include "TDictionary.h"
9
10#include "JLang/JType.hh"
11#include "JLang/JNullType.hh"
12#include "JLang/JTypeList.hh"
14#include "JLang/JException.hh"
15#include "JLang/JSingleton.hh"
18
19
20/**
21 * \author mdejong
22 */
23
24namespace JROOT {}
25namespace JPP { using namespace JROOT; }
26
27namespace JROOT {
28
29 using JLANG::JType;
30 using JLANG::JNullType;
31 using JLANG::JTypeList;
32 using JLANG::JTYPELIST;
35
36 /**
37 * Default implementation of ROOT based dictionary for ASCII I/O.
38 */
40 public JRootDictionary_t,
41 public JSingleton<JRootDictionary>
42 {
43 public:
44 /**
45 * Default constructor.
46 *
47 * This constructor builds a default dictionary which includes all primitive data types and <tt>std::vector</tt> thereof.
48 *
49 * Removed types:
50 * - <tt>long double</tt> (not supported by ROOT TDictionary)
51 *
52 * Added STL type definitions:
53 * - <tt>std::string</tt>
54 * - <tt>size_t</tt>
55 * - <tt>std::size_t</tt>
56 *
57 * Added ROOT type definitions:
58 * - <tt>[U]Char_t </tt>
59 * - <tt>[U]Short_t </tt>
60 * - <tt>[U]Int_t </tt>
61 * - <tt>[U]Long64_t</tt>
62 * - <tt>[U]Float_t </tt>
63 * - <tt>[U]Double_t</tt>
64 */
67 {
68#define VALUE_TYPE(__TYPE__) value_type(#__TYPE__, new JObjectStreamer<__TYPE__>())
69
70 using namespace JPP;
71
73
74 this->add(JType<std::string>());
75
76 this->insert(VALUE_TYPE(size_t));
77 this->insert(VALUE_TYPE(std::size_t));
78
79 this->insert(VALUE_TYPE(Char_t));
80 this->insert(VALUE_TYPE(UChar_t));
81 this->insert(VALUE_TYPE(Short_t));
82 this->insert(VALUE_TYPE(UShort_t));
83 this->insert(VALUE_TYPE(Int_t));
84 this->insert(VALUE_TYPE(UInt_t));
85 this->insert(VALUE_TYPE(Long64_t));
86 this->insert(VALUE_TYPE(ULong64_t));
87 this->insert(VALUE_TYPE(Float_t));
88 this->insert(VALUE_TYPE(Double_t));
89
90#undef VALUE_TYPE
91 }
92
93
94 /**
95 * Get typename for given template class.
96 *
97 * This method uses the TDictionary class to get the name of the given class.
98 *
99 * \return type name
100 */
101 template<class T>
102 static const char* getTypename()
103 {
104 const TDictionary* pDictionary = TDictionary::GetDictionary(typeid(T));
105
106 if (pDictionary != NULL)
107 return pDictionary->GetName();
108 else
109 THROW(JException, "Data type not implemented.");
110 }
111
112
113 /**
114 * Add ASCII I/O for given (list of) class(es) to dictionary.
115 */
116 template<class T>
117 void add()
118 {
119 add(JType<T>());
120 }
121
122
123 protected:
124 /**
125 * Addition of class.
126 *
127 * \param type data type
128 */
129 template<class T>
130 void add(const JType<T>& type)
131 {
132 this->insert(value_type(getTypename<T>(), new JObjectStreamer<T>()));
133
134 try {
135 this->insert(value_type(getTypename< std::vector<T> >(), new JObjectStreamer< std::vector<T> >()));
136 }
137 catch(const JException& error) {};
138 }
139
140
141 /**
142 * Addition of <tt>std::vector</tt>.
143 *
144 * \param type data type
145 */
146 template<class JElement_t, class JAllocator_t>
148 {
150
151 this->insert(value_type(getTypename<data_type>(), new JObjectStreamer<data_type>()));
152 }
153
154
155 /**
156 * Recursive addition of classes.
157 *
158 * \param typelist type list
159 */
160 template<class JHead_t, class JTail_t>
161 void add(const JType< JTypeList<JHead_t, JTail_t> >& typelist)
162 {
165 }
166
167
168 /**
169 * Template specialisation to terminate addition of classes.
170 *
171 * \param type null type
172 */
173 void add(const JType<JNullType>& type)
174 {}
175 };
176}
177
178
179/**
180 * Global ROOT based dictionary for ASCII I/O.
181 */
182#define gRootDictionary (JROOT::JRootDictionary::getInstance())
183
184
185/**
186 * Add ASCII I/O for (list of) class(es) to dictionary.
187 */
188template<class T>
190{
191 gRootDictionary.add<T>();
192}
193
194
195/**
196 * Add ASCII I/O for given object to dictionary.
197 *
198 * \param object object
199 */
200template<class T>
201inline void addToRootDictionary(const T& object)
202{
203 gRootDictionary.add<T>();
204}
205
206#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Type list of primitive data types.
#define VALUE_TYPE(__TYPE__)
#define gRootDictionary
Global ROOT based dictionary for ASCII I/O.
void addToRootDictionary()
Add ASCII I/O for (list of) class(es) to dictionary.
ASCII I/O of objects with ROOT dictionary.
General exception.
Definition JException.hh:24
JObjectStreamer class.
Default implementation of ROOT based dictionary for ASCII I/O.
void add(const JType< T > &type)
Addition of class.
void add()
Add ASCII I/O for given (list of) class(es) to dictionary.
void add(const JType< std::vector< JElement_t, JAllocator_t > > &type)
Addition of std::vector.
void add(const JType< JTypeList< JHead_t, JTail_t > > &typelist)
Recursive addition of classes.
void add(const JType< JNullType > &type)
Template specialisation to terminate addition of classes.
static const char * getTypename()
Get typename for given template class.
JRootDictionary()
Default constructor.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
Auxiliary class for no type definition.
Definition JNullType.hh:19
Removal of data type from type list.
Definition JTypeList.hh:114
Simple singleton class.
Definition JSingleton.hh:18
Auxiliary class for recursive type list generation.
Definition JTypeList.hh:351
Type list.
Definition JTypeList.hh:23
Auxiliary class for a type holder.
Definition JType.hh:19
Type definition of ROOT based dictionary for ASCII I/O.