Jpp
 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 <map>
6 #include <vector>
7 #include <typeinfo>
8 
9 #include <TDictionary.h>
10 
11 #include "JLang/JType.hh"
12 #include "JLang/JNullType.hh"
13 #include "JLang/JTypeList.hh"
14 #include "JLang/JPrimitiveTypes.hh"
15 #include "JLang/JSharedPointer.hh"
16 #include "JLang/JException.hh"
17 #include "JLang/JSingleton.hh"
19 #include "JROOT/JRootStreamer.hh"
20 
21 
22 /**
23  * \author mdejong
24  */
25 
26 namespace JROOT {}
27 namespace JPP { using namespace JROOT; }
28 
29 namespace JROOT {
30 
31  using JLANG::JType;
32  using JLANG::JNullType;
33  using JLANG::JTypeList;
34  using JLANG::JTYPELIST;
36  using JLANG::JException;
37  using JLANG::JSingleton;
38 
39  /**
40  * ROOT dictionary.
41  *
42  * This class can be used for ASCII I/O of ROOT objects.
43  */
45  public JRootDictionary_t,
46  public JSingleton<JRootDictionary>
47  {
48  public:
49  /**
50  * Default constructor.
51  *
52  * This constructor builds a default dictionary which includes all primitive data types and <tt>std::vector</tt> thereof.
53  *
54  * Removed types:
55  * - <tt>long double</tt> (not supported by ROOT TDictionary)
56  *
57  * Added ROOT type definitions:
58  * - <tt>std::string</tt>
59  * - <tt>[U]Char_t </tt>
60  * - <tt>[U]Short_t </tt>
61  * - <tt>[U]Int_t </tt>
62  * - <tt>[U]Long64_t</tt>
63  * - <tt>[U]Float_t </tt>
64  * - <tt>[U]Double_t</tt>
65  */
68  {
69 #define VALUE_TYPE(__TYPE__) value_type(#__TYPE__, new JObjectStreamer<__TYPE__>())
70 
71  using namespace JPP;
72 
73  this->add(JType<JRemove<JPrimitiveTypes_t, long double>::typelist>());
74  this->add(JType<std::string>());
75 
76  this->insert(VALUE_TYPE(Char_t));
77  this->insert(VALUE_TYPE(UChar_t));
78  this->insert(VALUE_TYPE(Short_t));
79  this->insert(VALUE_TYPE(UShort_t));
80  this->insert(VALUE_TYPE(Int_t));
81  this->insert(VALUE_TYPE(UInt_t));
82  this->insert(VALUE_TYPE(Long64_t));
83  this->insert(VALUE_TYPE(ULong64_t));
84  this->insert(VALUE_TYPE(Float_t));
85  this->insert(VALUE_TYPE(Double_t));
86 
87 #undef VALUE_TYPE
88  }
89 
90 
91  /**
92  * Get typename for given template class.
93  *
94  * This method uses the TDictionary class to get the name of the given class.
95  *
96  * \return type name
97  */
98  template<class T>
99  static const char* getTypename()
100  {
101  const TDictionary* pDictionary = TDictionary::GetDictionary(typeid(T));
102 
103  if (pDictionary != NULL)
104  return pDictionary->GetName();
105  else
106  throw JException("Error at getTypename(): data type not implemented.");
107  }
108 
109 
110  /**
111  * Add ASCII I/O for given (list of) class(es) to dictionary.
112  */
113  template<class T>
114  void add()
115  {
116  add(JType<T>());
117  }
118 
119 
120  protected:
121  /**
122  * Addition of class.
123  *
124  * \param type data type
125  */
126  template<class T>
127  void add(const JType<T>& type)
128  {
129  this->insert(value_type(getTypename<T>(), new JObjectStreamer<T>()));
130 
131  try {
132  this->insert(value_type(getTypename< std::vector<T> >(), new JObjectStreamer< std::vector<T> >()));
133  }
134  catch(const JException& error) {};
135  }
136 
137 
138  /**
139  * Addition of <tt>std::vector</tt>.
140  *
141  * \param type data type
142  */
143  template<class JElement_t, class JAllocator_t>
145  {
147 
148  this->insert(value_type(getTypename<data_type>(), new JObjectStreamer<data_type>()));
149  }
150 
151 
152  /**
153  * Recursive addition of classes.
154  *
155  * \param typelist type list
156  */
157  template<class JHead_t, class JTail_t>
158  void add(const JType< JTypeList<JHead_t, JTail_t> >& typelist)
159  {
160  add(JType<JHead_t>());
161  add(JType<JTail_t>());
162  }
163 
164 
165  /**
166  * Template specialisation to terminate addition of classes.
167  *
168  * \param type null type
169  */
170  void add(const JType<JNullType>& type)
171  {}
172  };
173 }
174 
175 
176 /**
177  * Global ROOT streamer for ASCII I/O.
178  */
179 #define gRootDictionary (JROOT::JRootDictionary::getInstance())
180 
181 
182 /**
183  * Add ASCII I/O for (list of) class(es) to dictionary.
184  */
185 template<class T>
186 inline void addToRootDictionary()
187 {
188  gRootDictionary.add<T>();
189 }
190 
191 
192 /**
193  * Add ASCII I/O for given object to dictionary.
194  *
195  * \param object object
196  */
197 template<class T>
198 inline void addToRootDictionary(const T& object)
199 {
200  gRootDictionary.add<T>();
201 }
202 
203 #endif
void addToRootDictionary()
Add ASCII I/O for (list of) class(es) to dictionary.
General exception.
Definition: JException.hh:40
Exceptions.
Simple singleton class.
Definition: JSingleton.hh:18
#define gRootDictionary
Global ROOT streamer for ASCII I/O.
JObjectStreamer class.
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.
The template JSharedPointer class can be used to share a pointer to an object.
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:377
Auxiliary class for no type definition.
Definition: JNullType.hh:19
#define VALUE_TYPE(__TYPE__)
This file contains the basic interface for ASCII I/O of ROOT objects.
ROOT dictionary.
void add(const JType< T > &type)
Addition of class.
static const char * getTypename()
Get typename for given template class.