Jpp  15.0.1-rc.1-highqe
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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::size_t</tt>
54  * - <tt>std::string</tt>
55  *
56  * Added ROOT type definitions:
57  * - <tt>[U]Char_t </tt>
58  * - <tt>[U]Short_t </tt>
59  * - <tt>[U]Int_t </tt>
60  * - <tt>[U]Long64_t</tt>
61  * - <tt>[U]Float_t </tt>
62  * - <tt>[U]Double_t</tt>
63  */
66  {
67 #define VALUE_TYPE(__TYPE__) value_type(#__TYPE__, new JObjectStreamer<__TYPE__>())
68 
69  using namespace JPP;
70 
71  this->add(JType<JRemove<JPrimitiveTypes_t, long double>::typelist>());
72 
73  this->add(JType<std::string>());
74 
75  this->insert(VALUE_TYPE(size_t));
76  this->insert(VALUE_TYPE(std::size_t));
77 
78  this->insert(VALUE_TYPE(Char_t));
79  this->insert(VALUE_TYPE(UChar_t));
80  this->insert(VALUE_TYPE(Short_t));
81  this->insert(VALUE_TYPE(UShort_t));
82  this->insert(VALUE_TYPE(Int_t));
83  this->insert(VALUE_TYPE(UInt_t));
84  this->insert(VALUE_TYPE(Long64_t));
85  this->insert(VALUE_TYPE(ULong64_t));
86  this->insert(VALUE_TYPE(Float_t));
87  this->insert(VALUE_TYPE(Double_t));
88 
89 #undef VALUE_TYPE
90  }
91 
92 
93  /**
94  * Get typename for given template class.
95  *
96  * This method uses the TDictionary class to get the name of the given class.
97  *
98  * \return type name
99  */
100  template<class T>
101  static const char* getTypename()
102  {
103  const TDictionary* pDictionary = TDictionary::GetDictionary(typeid(T));
104 
105  if (pDictionary != NULL)
106  return pDictionary->GetName();
107  else
108  THROW(JException, "Data type not implemented.");
109  }
110 
111 
112  /**
113  * Add ASCII I/O for given (list of) class(es) to dictionary.
114  */
115  template<class T>
116  void add()
117  {
118  add(JType<T>());
119  }
120 
121 
122  protected:
123  /**
124  * Addition of class.
125  *
126  * \param type data type
127  */
128  template<class T>
129  void add(const JType<T>& type)
130  {
131  this->insert(value_type(getTypename<T>(), new JObjectStreamer<T>()));
132 
133  try {
134  this->insert(value_type(getTypename< std::vector<T> >(), new JObjectStreamer< std::vector<T> >()));
135  }
136  catch(const JException& error) {};
137  }
138 
139 
140  /**
141  * Addition of <tt>std::vector</tt>.
142  *
143  * \param type data type
144  */
145  template<class JElement_t, class JAllocator_t>
147  {
149 
150  this->insert(value_type(getTypename<data_type>(), new JObjectStreamer<data_type>()));
151  }
152 
153 
154  /**
155  * Recursive addition of classes.
156  *
157  * \param typelist type list
158  */
159  template<class JHead_t, class JTail_t>
160  void add(const JType< JTypeList<JHead_t, JTail_t> >& typelist)
161  {
162  add(JType<JHead_t>());
163  add(JType<JTail_t>());
164  }
165 
166 
167  /**
168  * Template specialisation to terminate addition of classes.
169  *
170  * \param type null type
171  */
172  void add(const JType<JNullType>& type)
173  {}
174  };
175 }
176 
177 
178 /**
179  * Global ROOT based dictionary for ASCII I/O.
180  */
181 #define gRootDictionary (JROOT::JRootDictionary::getInstance())
182 
183 
184 /**
185  * Add ASCII I/O for (list of) class(es) to dictionary.
186  */
187 template<class T>
188 inline void addToRootDictionary()
189 {
190  gRootDictionary.add<T>();
191 }
192 
193 
194 /**
195  * Add ASCII I/O for given object to dictionary.
196  *
197  * \param object object
198  */
199 template<class T>
200 inline void addToRootDictionary(const T& object)
201 {
202  gRootDictionary.add<T>();
203 }
204 
205 #endif
void addToRootDictionary()
Add ASCII I/O for (list of) class(es) to dictionary.
General exception.
Definition: JException.hh:23
Exceptions.
Simple singleton class.
Definition: JSingleton.hh:18
#define gRootDictionary
Global ROOT based dictionary for ASCII I/O.
JObjectStreamer class.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
void add()
Add ASCII I/O for given (list of) class(es) to dictionary.
Auxiliary class for a type holder.
Definition: JType.hh:19
JRootDictionary()
Default constructor.
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.
Type list.
Definition: JTypeList.hh:22
void add(const JType< std::vector< JElement_t, JAllocator_t > > &type)
Addition of std::vector.
ASCII I/O of objects with ROOT dictionary.
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:351
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Auxiliary class for no type definition.
Definition: JNullType.hh:19
#define VALUE_TYPE(__TYPE__)
Type list of primitive data types.
Type definition of ROOT based dictionary for ASCII I/O.
Default implementation of ROOT based dictionary for ASCII I/O.
void add(const JType< T > &type)
Addition of class.
static const char * getTypename()
Get typename for given template class.