Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JRootTypesHandler.hh
Go to the documentation of this file.
1#ifndef __JROOT__JROOTTYPESHANDLER__
2#define __JROOT__JROOTTYPESHANDLER__
3
4#include <typeinfo>
5#include <string>
6#include <memory>
7#include <map>
8
9#include <TDictionary.h>
10#include <TString.h>
11
12#include "JLang/JType.hh"
13#include "JLang/JTypeList.hh"
15#include "JLang/JException.hh"
16
17
18/**
19 * \author mdejong
20 */
21
22namespace JROOT {}
23namespace JPP { using namespace JROOT; }
24
25namespace JROOT {
26
27 using JLANG::JType;
29
30
31 /**
32 * Interface for handling of a template class.
33 *
34 * The given template class should provide for the function object operator
35 * <pre>
36 * template<class T>
37 * void operator()(const char* const name, const JType<T> type);
38 * </pre>
39 */
40 template<class JType_t>
42 public:
43 /**
44 * Virtual destructor.
45 */
47 {}
48
49
50 /**
51 * Apply type with given name to object.
52 *
53 * \param object object
54 * \param name name
55 */
56 virtual void apply(JType_t& object, const char* const name) const = 0;
57 };
58
59
60 /**
61 * Implementation for handling of a template class.
62 *
63 * The given template class should provide for the function object operator
64 * <pre>
65 * template<class T>
66 * void operator()(const char* const name, const JType<T> type);
67 * </pre>
68 *
69 * This class implements the JROOT::JAstractType interface for the given template class.
70 */
71 template<class JType_t, class T>
73 public JAbstractType<JType_t>
74 {
75 public:
76 /**
77 * Apply type with given name to object.
78 *
79 * \param object object
80 * \param name name
81 */
82 virtual void apply(JType_t& object, const char* const name) const override
83 {
84 object(name, JType<T>());
85 }
86 };
87
88
89 /**
90 * Auxiliary class for handling ROOT types.
91 *
92 * Included types:
93 * - <tt>JPrimitiveTypes_t</tt>
94 *
95 * Removed types:
96 * - <tt>long double</tt> (not supported by ROOT <tt>TDictionary</tt>)
97 *
98 * Added STL types:
99 * - <tt>std::string</tt>
100 * - <tt>std::size_t</tt>
101 *
102 * Added ROOT types:
103 * - <tt>[U]Char_t</tt>
104 * - <tt>[U]Short_t</tt>
105 * - <tt>[U]Int_t</tt>
106 * - <tt>[U]Long_t</tt>
107 * - <tt>[U]Long64_t</tt>
108 * - <tt>Float_t</tt>
109 * - <tt>Double_t</tt>
110 * - <tt>LongDouble_t</tt>
111 * - <tt>TString</tt>
112 */
113 template<class JType_t>
115 public std::map<std::string, std::shared_ptr< JAbstractType<JType_t> > >
116 {
117 public:
118
120
121 /**
122 * Default constructor.
123 */
125 {
126 using namespace JPP;
127
128 for_each<JTYPELIST<JRemove<JPrimitiveTypes_t, long double>::typelist,
129 std::string,
130 std::size_t>::typelist>(*this);
131
132#define VALUE_TYPE(__TYPE__) typename map_type::value_type(#__TYPE__, new JObjectType<JType_t, __TYPE__>())
133
134 this->insert(VALUE_TYPE(Byte_t));
135 this->insert(VALUE_TYPE(Char_t));
136 this->insert(VALUE_TYPE(UChar_t));
137 this->insert(VALUE_TYPE(Short_t));
138 this->insert(VALUE_TYPE(UShort_t));
139 this->insert(VALUE_TYPE(Int_t));
140 this->insert(VALUE_TYPE(UInt_t));
141 this->insert(VALUE_TYPE(Long_t));
142 this->insert(VALUE_TYPE(ULong_t));
143 this->insert(VALUE_TYPE(Long64_t));
144 this->insert(VALUE_TYPE(ULong64_t));
145 this->insert(VALUE_TYPE(Float_t));
146 this->insert(VALUE_TYPE(Double_t));
147 this->insert(VALUE_TYPE(LongDouble_t));
148 this->insert(VALUE_TYPE(TString));
149
150#undef VALUE_TYPE
151 }
152
153
154 /**
155 * Addition of data type.
156 *
157 * This method uses <tt>TDictionary></tt> to determine name of given data type.
158 *
159 * \param type data type
160 */
161 template<class T>
162 void operator()(const JType<T>& type)
163 {
164 const TDictionary* pDictionary = TDictionary::GetDictionary(typeid(T));
165
166 if (pDictionary != NULL)
167 (*this)(pDictionary->GetName(), type);
168 else
169 THROW(JException, "ROOT TDictionary unknown data type " << typeid(T).name());
170 }
171
172
173 /**
174 * Addition of data type.
175 *
176 * \param name data name
177 * \param type data type
178 */
179 template<class T>
180 void operator()(const char* const name, const JType<T>& type)
181 {
182 this->insert(typename map_type::value_type(name, new JObjectType<JType_t, T>()));
183 }
184
185
186 /**
187 * Apply this handler to given object.
188 *
189 * \param object object
190 */
191 void apply(JType_t& object) const
192 {
193 for (typename map_type::const_iterator i = this->begin(); i != this->end(); ++i) {
194 i->second->apply(object, i->first.c_str());
195 }
196 }
197 };
198
199
200 /**
201 * Apply ROOT types handler to given object.
202 *
203 * Note that the underlying <tt>TDictionary</tt> is dynamically built in ROOT.
204 *
205 * \param object object
206 */
207 template<class JType_t>
208 inline void handler(JType_t& object)
209 {
211
212 handler.apply(object);
213 }
214}
215
216#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__)
General exception.
Definition JException.hh:24
Interface for handling of a template class.
virtual void apply(JType_t &object, const char *const name) const =0
Apply type with given name to object.
virtual ~JAbstractType()
Virtual destructor.
Implementation for handling of a template class.
virtual void apply(JType_t &object, const char *const name) const override
Apply type with given name to object.
Auxiliary class for handling ROOT types.
void operator()(const char *const name, const JType< T > &type)
Addition of data type.
void apply(JType_t &object) const
Apply this handler to given object.
JRootTypesHandler()
Default constructor.
void operator()(const JType< T > &type)
Addition of data type.
std::map< std::string, std::shared_ptr< JAbstractType< JType_t > > > map_type
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
void handler(JType_t &object)
Apply ROOT types handler to given object.
Auxiliary class for a type holder.
Definition JType.hh:19