Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JRootComparator.hh
Go to the documentation of this file.
1#ifndef __JROOT_COMPARATOR__
2#define __JROOT_COMPARATOR__
3
4#include <string>
5#include <memory>
6#include <map>
7
8#include "TString.h"
9#include "TDictionary.h"
10#include "TVirtualCollectionProxy.h"
11#include "TBaseClass.h"
12#include "TDataMember.h"
13
14#include "JROOT/JRootClass.hh"
16
17#include "Jeep/JMessage.hh"
18
19
20namespace JROOT {}
21namespace JPP { using namespace JROOT; }
22
23namespace JROOT {
24
25 using JLANG::JType;
26
27
28 /**
29 * Interface for comparison of a template class.
30 */
32 public:
33 /**
34 * Virtual destructor.
35 */
37 {}
38
39
40 /**
41 * Compare objects.
42 *
43 * \param first pointer to first object
44 * \param second pointer to second object
45 * \return true if objects are equal; else false
46 */
47 virtual bool equals(const void* first, const void* second) const = 0;
48 };
49
50
51 /**
52 * Implementation for comparison of a template class.
53 *
54 * This class implements the JROOT::JAstractComparator interface for the given template class using the operator <tt>==</tt>.
55 */
56 template<class T>
59 {
60 public:
61 /**
62 * Compare objects.
63 *
64 * \param first pointer to first object
65 * \param second pointer to second object
66 * \return true if objects are equal; else false
67 */
68 virtual bool equals(const void* first, const void* second) const override
69 {
70 return (* ((const T*) first) == * ((const T*) second));
71 }
72 };
73
74
75 /**
76 * ROOT comparator.
77 */
79 public std::map<std::string, std::shared_ptr<JAbstractComparator> >
80 {
81 public:
82 /**
83 * Default constructor.
84 */
86 {
87 handler(*this);
88 }
89
90
91 /**
92 * Compare objects.
93 *
94 * \param first first object
95 * \param second second object
96 * \return true if objects are equal; else false
97 */
98 template<class T>
99 bool operator()(const T& first, const T& second) const
100 {
101 using namespace std;
102
103 DEBUG("compare: " << JRootWritableClass(first).getTypename() << ' ' << JRootWritableClass(second).getTypename() << endl << endl);
104
105 return this->compare(JRootWritableClass(first), JRootWritableClass(second));
106 }
107
108
109 /**
110 * Addition of class.
111 *
112 * \param name data name
113 * \param type data type
114 */
115 template<class T>
116 void operator()(const char* const name, const JType<T>& type)
117 {
118 this->insert(value_type(name, new JObjectComparator<T>()));
119 }
120
121
122 int debug = 0; //!< debug level
123
124 protected:
125 /**
126 * Compare objects.
127 *
128 * \param first first object
129 * \param second second object
130 * \return true if objects are equal; else false
131 */
132 bool compare(const JRootWritableClass& first, const JRootWritableClass& second) const
133 {
134 using namespace std;
135
136 if (first .is_valid() &&
137 second.is_valid()) {
138
139 const_iterator i1 = this->find(first .getTypename());
140 const_iterator i2 = this->find(second.getTypename());
141
142 if (i1 != this->end() &&
143 i2 != this->end() &&
144 i1 == i2) {
145
146 if (!i1->second->equals(first.getAddress(), second.getAddress())) {
147 return false;
148 }
149
150 } else if (first .getClass() != NULL &&
151 second.getClass() != NULL) {
152
153 if (first .getClass()->GetCollectionType() != ROOT::ESTLType::kNotSTL && first .getClass()->GetCollectionProxy() != NULL &&
154 second.getClass()->GetCollectionType() != ROOT::ESTLType::kNotSTL && second.getClass()->GetCollectionProxy() != NULL) {
155
156 std::unique_ptr<TVirtualCollectionProxy> p1(first .getClass()->GetCollectionProxy()->Generate());
157 std::unique_ptr<TVirtualCollectionProxy> p2(second.getClass()->GetCollectionProxy()->Generate());
158
159 p1->PushProxy(const_cast<char*>(first .getAddress()));
160 p2->PushProxy(const_cast<char*>(second.getAddress()));
161
162 TDictionary* d1 = TDictionary::GetDictionary(p1->GetType() == EDataType::kNoType_t ? p1->GetValueClass()->GetName() : TDataType::GetTypeName(p1->GetType()));
163 TDictionary* d2 = TDictionary::GetDictionary(p2->GetType() == EDataType::kNoType_t ? p2->GetValueClass()->GetName() : TDataType::GetTypeName(p2->GetType()));
164
165 DEBUG("proxy: " << p1->GetCollectionClass()->GetName() << " " << d1->GetName() << "(" << p1->Size() << ");" << endl);
166 DEBUG("proxy: " << p2->GetCollectionClass()->GetName() << " " << d2->GetName() << "(" << p2->Size() << ");" << endl);
167
168 if (p1->Size() != p2->Size()) {
169 return false;
170 }
171
172 for (UInt_t i = 0; (i != p1->Size() &&
173 i != p2->Size()); ++i) {
174
175 DEBUG("index: " << d1->GetName() << "[" << i << "] " << endl);
176 //DEBUG("index: " << d2->GetName() << "[" << i << "] " << endl);
177
178 const JRootWritableClass w1(d1, (char*) p1->At(i));
179 const JRootWritableClass w2(d2, (char*) p2->At(i));
180
181 if (!this->compare(w1, w2)) {
182 return false;
183 }
184 }
185
186 } else {
187
188 if (first .getClass()->GetListOfBases() != NULL &&
189 second.getClass()->GetListOfBases() != NULL) {
190
191 std::unique_ptr<TIterator> i1(first .getClass()->GetListOfBases()->MakeIterator());
192 std::unique_ptr<TIterator> i2(second.getClass()->GetListOfBases()->MakeIterator());
193
194 for (const TBaseClass *p1, *p2; ((p1 = (const TBaseClass*) i1->Next()) != NULL &&
195 (p2 = (const TBaseClass*) i2->Next()) != NULL); ) {
196
197 if (JRoot::is_class(p1->GetName()) &&
198 JRoot::is_class(p2->GetName())) {
199
200 DEBUG("base: " << p1->GetName() << endl);
201 //DEBUG("base: " << p2->GetName() << endl);
202
203 if (!this->compare(first.get(*p1), second.get(*p2))) {
204 return false;
205 }
206 }
207 }
208 }
209
210 if (first .getClass()->GetListOfDataMembers() != NULL &&
211 second.getClass()->GetListOfDataMembers() != NULL) {
212
213 std::unique_ptr<TIterator> i1(first .getClass()->GetListOfDataMembers()->MakeIterator());
214 std::unique_ptr<TIterator> i2(second.getClass()->GetListOfDataMembers()->MakeIterator());
215
216 for (const TDataMember *p1, *p2; ((p1 = (const TDataMember*) i1->Next()) != NULL &&
217 (p2 = (const TDataMember*) i2->Next()) != NULL); ) {
218
219
220 if (JRoot::is_class(p1->GetName()) &&
221 JRoot::is_class(p2->GetName())) {
222
223 DEBUG("member: " << p1->GetTrueTypeName() << " " << p1->GetName() << ";" << endl);
224 //DEBUG("member: " << p2->GetTrueTypeName() << " " << p2->GetName() << ";" << endl);
225
226 if (!this->compare(first.get(*p1), second.get(*p2))) {
227 return false;
228 }
229 }
230 }
231 }
232 }
233 }
234 }
235
236 return true;
237 }
238 };
239}
240
241#endif
TPaveText * p1
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
Interface for comparison of a template class.
virtual bool equals(const void *first, const void *second) const =0
Compare objects.
virtual ~JAbstractComparator()
Virtual destructor.
Implementation for comparison of a template class.
virtual bool equals(const void *first, const void *second) const override
Compare objects.
bool operator()(const T &first, const T &second) const
Compare objects.
bool compare(const JRootWritableClass &first, const JRootWritableClass &second) const
Compare objects.
void operator()(const char *const name, const JType< T > &type)
Addition of class.
JRootComparator()
Default constructor.
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
bool is_valid() const
Check validity of this addressable class.
pointer_type getAddress() const
Get address.
JRootAddressableClass get(const TDataMember &object) const
Get addressable class of given data member.
const char * getTypename() const
Get type name.
TClass * getClass() const
Get class.
ROOT class for writing from object.
static bool is_class(const char *const name)
Check name of class against internal ROOT class names.
Definition JRoot.hh:30