Jpp
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"
13 #include "JLang/JPrimitiveTypes.hh"
14 #include "JLang/JException.hh"
15 #include "JLang/JSingleton.hh"
17 #include "JROOT/JRootStreamer.hh"
18 
19 
20 /**
21  * \author mdejong
22  */
23 
24 namespace JROOT {}
25 namespace JPP { using namespace JROOT; }
26 
27 namespace JROOT {
28 
29  using JLANG::JType;
30  using JLANG::JNullType;
31  using JLANG::JTypeList;
32  using JLANG::JTYPELIST;
33  using JLANG::JException;
34  using JLANG::JSingleton;
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  *
55  * Added ROOT type definitions:
56  * - <tt>[U]Char_t </tt>
57  * - <tt>[U]Short_t </tt>
58  * - <tt>[U]Int_t </tt>
59  * - <tt>[U]Long64_t</tt>
60  * - <tt>[U]Float_t </tt>
61  * - <tt>[U]Double_t</tt>
62  */
65  {
66 #define VALUE_TYPE(__TYPE__) value_type(#__TYPE__, new JObjectStreamer<__TYPE__>())
67 
68  using namespace JPP;
69 
70  this->add(JType<JRemove<JPrimitiveTypes_t, long double>::typelist>());
71  this->add(JType<std::string>());
72 
73  this->insert(VALUE_TYPE(Char_t));
74  this->insert(VALUE_TYPE(UChar_t));
75  this->insert(VALUE_TYPE(Short_t));
76  this->insert(VALUE_TYPE(UShort_t));
77  this->insert(VALUE_TYPE(Int_t));
78  this->insert(VALUE_TYPE(UInt_t));
79  this->insert(VALUE_TYPE(Long64_t));
80  this->insert(VALUE_TYPE(ULong64_t));
81  this->insert(VALUE_TYPE(Float_t));
82  this->insert(VALUE_TYPE(Double_t));
83 
84 #undef VALUE_TYPE
85  }
86 
87 
88  /**
89  * Get typename for given template class.
90  *
91  * This method uses the TDictionary class to get the name of the given class.
92  *
93  * \return type name
94  */
95  template<class T>
96  static const char* getTypename()
97  {
98  const TDictionary* pDictionary = TDictionary::GetDictionary(typeid(T));
99 
100  if (pDictionary != NULL)
101  return pDictionary->GetName();
102  else
103  THROW(JException, "Data type not implemented.");
104  }
105 
106 
107  /**
108  * Add ASCII I/O for given (list of) class(es) to dictionary.
109  */
110  template<class T>
111  void add()
112  {
113  add(JType<T>());
114  }
115 
116 
117  protected:
118  /**
119  * Addition of class.
120  *
121  * \param type data type
122  */
123  template<class T>
124  void add(const JType<T>& type)
125  {
126  this->insert(value_type(getTypename<T>(), new JObjectStreamer<T>()));
127 
128  try {
129  this->insert(value_type(getTypename< std::vector<T> >(), new JObjectStreamer< std::vector<T> >()));
130  }
131  catch(const JException& error) {};
132  }
133 
134 
135  /**
136  * Addition of <tt>std::vector</tt>.
137  *
138  * \param type data type
139  */
140  template<class JElement_t, class JAllocator_t>
142  {
144 
145  this->insert(value_type(getTypename<data_type>(), new JObjectStreamer<data_type>()));
146  }
147 
148 
149  /**
150  * Recursive addition of classes.
151  *
152  * \param typelist type list
153  */
154  template<class JHead_t, class JTail_t>
155  void add(const JType< JTypeList<JHead_t, JTail_t> >& typelist)
156  {
157  add(JType<JHead_t>());
158  add(JType<JTail_t>());
159  }
160 
161 
162  /**
163  * Template specialisation to terminate addition of classes.
164  *
165  * \param type null type
166  */
167  void add(const JType<JNullType>& type)
168  {}
169  };
170 }
171 
172 
173 /**
174  * Global ROOT based dictionary for ASCII I/O.
175  */
176 #define gRootDictionary (JROOT::JRootDictionary::getInstance())
177 
178 
179 /**
180  * Add ASCII I/O for (list of) class(es) to dictionary.
181  */
182 template<class T>
183 inline void addToRootDictionary()
184 {
185  gRootDictionary.add<T>();
186 }
187 
188 
189 /**
190  * Add ASCII I/O for given object to dictionary.
191  *
192  * \param object object
193  */
194 template<class T>
195 inline void addToRootDictionary(const T& object)
196 {
197  gRootDictionary.add<T>();
198 }
199 
200 #endif
JException.hh
JROOT::JRootDictionary::add
void add(const JType< JTypeList< JHead_t, JTail_t > > &typelist)
Recursive addition of classes.
Definition: JRootDictionary.hh:155
JLANG::JType
Auxiliary class for a type holder.
Definition: JType.hh:19
JROOT
Auxiliary classes and methods for ROOT I/O.
Definition: JAbstractStreamer.hh:13
JROOT::JRootDictionary::add
void add()
Add ASCII I/O for given (list of) class(es) to dictionary.
Definition: JRootDictionary.hh:111
JROOT::JRootDictionary::add
void add(const JType< T > &type)
Addition of class.
Definition: JRootDictionary.hh:124
JPrimitiveTypes.hh
gRootDictionary
#define gRootDictionary
Global ROOT based dictionary for ASCII I/O.
Definition: JRootDictionary.hh:176
JRootStreamer.hh
JLANG::JNullType
Auxiliary class for no type definition.
Definition: JNullType.hh:19
std::vector< T >
JROOT::JRootDictionary::getTypename
static const char * getTypename()
Get typename for given template class.
Definition: JRootDictionary.hh:96
JLANG::JTYPELIST
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:377
JLANG::JSingleton::data_type
T data_type
Definition: JSingleton.hh:20
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JROOT::JRootDictionary::add
void add(const JType< JNullType > &type)
Template specialisation to terminate addition of classes.
Definition: JRootDictionary.hh:167
VALUE_TYPE
#define VALUE_TYPE(__TYPE__)
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:669
JROOT::JRootDictionary_t
Type definition of ROOT based dictionary for ASCII O.
Definition: JRootDictionary_t.hh:30
JLANG::JTypeList
Type list.
Definition: JTypeList.hh:22
JRootDictionary_t.hh
JROOT::JRootDictionary::JRootDictionary
JRootDictionary()
Default constructor.
Definition: JRootDictionary.hh:63
JROOT::JRootDictionary::add
void add(const JType< std::vector< JElement_t, JAllocator_t > > &type)
Addition of std::vector.
Definition: JRootDictionary.hh:141
JTypeList.hh
addToRootDictionary
void addToRootDictionary()
Add ASCII I/O for (list of) class(es) to dictionary.
Definition: JRootDictionary.hh:183
JLANG::JSingleton
Simple singleton class.
Definition: JSingleton.hh:18
JROOT::JObjectStreamer
JObjectStreamer class.
Definition: JRootStreamer.hh:748
JNullType.hh
JType.hh
JROOT::JRootDictionary
Default implementation of ROOT based dictionary for ASCII I/O.
Definition: JRootDictionary.hh:39
JLANG::JException
General exception.
Definition: JException.hh:40
JSingleton.hh