CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ClassUtils.cc
Go to the documentation of this file.
5 #include "ClassUtils.h"
6 //
7 #include <cxxabi.h>
8 // externals
9 #include "Reflex/Object.h"
10 #include "Reflex/Base.h"
11 
13  m_type( type ){
14 }
15 
17  m_type( rhs.m_type ){
18 }
19 
21 }
22 
23 void ora::RflxDeleter::operator()( void* ptr ){
24  m_type.Destruct( ptr );
25 }
26 
27 void ora::ClassUtils::loadDictionary( const std::string& className ){
28  try {
29  static std::string const prefix("LCGReflex/");
30  edmplugin::PluginCapabilities::get()->load(prefix + className);
31  } catch (...){
32  throwException( "Failure while loading dictionary for class \""+className+"\".","ClassUtils::loadDictionary" );
33  }
34 }
35 
37  void* ptr,
38  const Reflex::Type& castType ){
39  void* ret = 0;
40  if( type == castType ){
41  ret = ptr;
42  } else if( type.HasBase( castType )){
43  Reflex::Object theObj( type, ptr );
44  ret = theObj.CastObject( castType ).Address();
45  }
46  return ret;
47 }
48 
50  const Reflex::Type& baseType ){
51  bool ret = false;
52  if( type == baseType || type.HasBase( baseType )){
53  ret = true;
54  }
55  return ret;
56 }
57 
58 bool ora::ClassUtils::findBaseType( Reflex::Type& type, Reflex::Type& baseType, Reflex::OffsetFunction& func ){
59  bool found = false;
60  for ( unsigned int i=0;i<type.BaseSize() && !found; i++){
61  Reflex::Base base = type.BaseAt(i);
62  Reflex::Type bt = resolvedType( base.ToType() );
63  if( bt == baseType ){
64  func = base.OffsetFP();
65  found = true;
66  } else {
67  found = findBaseType( bt, baseType, func );
68  }
69  }
70  return found;
71 }
72 
73 std::string ora::ClassUtils::demangledName( const std::type_info& typeInfo ){
74  int status = 0;
75  std::string ret("");
76  char* realname = abi::__cxa_demangle( typeInfo.name(), 0, 0, &status);
77  if( status == 0 && realname ){
78  ret = realname;
79  free(realname);
80  }
81  return ret;
82 }
83 
84 Reflex::Type ora::ClassUtils::lookupDictionary( const std::type_info& typeInfo, bool throwFlag ){
85  Reflex::Type type = Reflex::Type::ByTypeInfo( typeInfo );
86  if( typeInfo == typeid(std::string) ){
87  // ugly, but no other way with Reflex...
88  type = Reflex::Type::ByName("std::string");
89  }
90  if( !type ){
91  loadDictionary( demangledName(typeInfo) );
92  type = Reflex::Type::ByTypeInfo( typeInfo );
93  }
94  if( !type && throwFlag ){
95  throwException( "Class \""+demangledName(typeInfo)+"\" has not been found in the dictionary.",
96  "ClassUtils::lookupDictionary" );
97  }
98  return type;
99 }
100 
101 Reflex::Type ora::ClassUtils::lookupDictionary( const std::string& className, bool throwFlag ){
102  Reflex::Type type = Reflex::Type::ByName( className );
103  if( className == "std::basic_string<char>" ){
104  // ugly, but no other way with Reflex...
105  type = Reflex::Type::ByName("std::string");
106  }
107  if( !type ){
108  loadDictionary( className );
109  type = Reflex::Type::ByName( className );
110  }
111  if( !type && throwFlag ){
112  throwException( "Class \""+className+"\" has not been found in the dictionary.",
113  "ClassUtils::lookupDictionary" );
114  }
115  return type;
116 }
117 
119  void* ptr = 0;
120  if( typ.Name(Reflex::SCOPED)=="std::string"){
121  ptr = new std::string("");
122  } else {
123  ptr = typ.Construct().Address();
124  }
125  return ptr;
126 }
127 
129  std::string name = typ.Name(Reflex::SCOPED|Reflex::FINAL);
130  return ( name == "std::string" ||
131  name == "std::basic_string<char>" );
132 }
133 
135  return ( typ.IsFundamental() || typ.IsEnum() || isTypeString( typ ) );
136 }
137 
139  Reflex::TypeTemplate templ = typ.TemplateFamily();
140  if (! templ) {
141  return false;
142  } else {
143  std::string contName = templ.Name(Reflex::SCOPED|Reflex::FINAL);
144  if( contName == "std::vector" ||
145  contName == "std::list" ||
146  contName == "std::deque" ||
147  contName == "std::stack" ||
148  contName == "std::set" ||
149  contName == "std::multiset" ||
150  contName == "__gnu_cxx::hash_set" ||
151  contName == "__gnu_cxx::hash_multiset" ||
152  contName == "std::map" ||
153  contName == "std::multimap" ||
154  contName == "__gnu_cxx::hash_map" ||
155  contName == "__gnu_cxx::hash_multimap" ||
156  contName == "ora::PVector" ||
157  contName == "ora::QueryableVector" ){
158  return true;
159  }
160  }
161  return false;
162 }
163 
165  Reflex::TypeTemplate tt = typ.TemplateFamily();
166  if (! tt) {
167  return false;
168  } else {
169  std::string contName = tt.Name(Reflex::SCOPED|Reflex::FINAL);
170  if( contName == "std::map" ||
171  contName == "std::multimap" ||
172  contName == "std::set" ||
173  contName == "std::multiset" ||
174  contName == "__gnu_cxx::hash_set" ||
175  contName == "__gnu_cxx::hash_multiset" ||
176  contName == "__gnu_cxx::hash_map" ||
177  contName == "__gnu_cxx::hash_multimap" ){
178  return true;
179  }
180  }
181  return false;
182 }
183 
185  Reflex::TypeTemplate templ = typ.TemplateFamily();
186  if (! templ) {
187  return false;
188  } else {
189  std::string contName = templ.Name(Reflex::SCOPED|Reflex::FINAL);
190  if( contName == "std::vector" ||
191  contName == "std::list" ||
192  contName == "std::deque" ||
193  contName == "std::stack" ||
194  contName == "ora::PVector" ||
195  contName == "ora::QueryableVector" ){
196  return true;
197  }
198  }
199  return false;
200 }
201 
203  Reflex::TypeTemplate tt = typ.TemplateFamily();
204  if (! tt) {
205  return false;
206  } else {
207  std::string contName = tt.Name(Reflex::SCOPED|Reflex::FINAL);
208  if( contName == "std::map" ||
209  contName == "std::multimap" ||
210  contName == "__gnu_cxx::hash_map" ||
211  contName == "__gnu_cxx::hash_multimap" ){
212  return true;
213  }
214  }
215  return false;
216 }
217 
219  Reflex::TypeTemplate tt = typ.TemplateFamily();
220  if (! tt) {
221  return false;
222  } else {
223  std::string contName = tt.Name(Reflex::SCOPED|Reflex::FINAL);
224  if( contName == "std::vector" ||
225  contName == "std::list" ||
226  contName == "std::deque" ||
227  contName == "std::stack" ||
228  contName == "std::set" ||
229  contName == "std::multiset" ||
230  contName == "__gnu_cxx::hash_set" ||
231  contName == "__gnu_cxx::hash_multiset" ||
232  contName == "ora::PVector" ||
233  contName == "ora::QueryableVector"){
234  return true;
235  }
236  }
237  return false;
238 }
239 
240 
242  Reflex::TypeTemplate templ = typ.TemplateFamily();
243  if (! templ) {
244  return false;
245  } else {
246  std::string contName = templ.Name(Reflex::SCOPED|Reflex::FINAL);
247  if( contName == "ora::Ptr" ){
248  return true;
249  }
250  }
251  return false;
252 }
253 
255  return typ.HasBase(Reflex::Type::ByTypeInfo(typeid(ora::Reference)));
256 }
257 
259  return typ.HasBase(Reflex::Type::ByTypeInfo(typeid(ora::NamedReference)));
260 }
261 
263  Reflex::TypeTemplate templ = typ.TemplateFamily();
264  if (! templ) {
265  return false;
266  } else {
267  std::string contName = templ.Name(Reflex::SCOPED|Reflex::FINAL);
268  if( contName == "ora::UniqueRef" ){
269  return true;
270  }
271  }
272  return false;
273 }
274 
276  Reflex::TypeTemplate templ = typ.TemplateFamily();
277  if (! templ) {
278  return false;
279  } else {
280  std::string contName = templ.Name(Reflex::SCOPED|Reflex::FINAL);
281  if( contName == "ora::PVector" ){
282  return true;
283  }
284  }
285  return false;
286 }
287 
289  Reflex::TypeTemplate templ = typ.TemplateFamily();
290  if (! templ) {
291  return false;
292  } else {
293  std::string contName = templ.Name(Reflex::SCOPED|Reflex::FINAL);
294  if( contName == "ora::QueryableVector" ){
295  return true;
296  }
297  }
298  return false;
299 }
300 
302  Reflex::Type resType = ClassUtils::resolvedType( typ );
303  if( isTypePrimitive( resType ) ) {
304  //if ( resType.IsFundamental() || resType.IsEnum() || isTypeString(resType) ) {
305  return false;
306  } else {
307  if( resType.IsArray() ) return false;
308  if( isTypeContainer( resType ) ) return false;
309  if( isTypeOraPointer( resType ) ) return false;
310  if( isTypeUniqueReference( resType ) ) return false;
311  if( isTypePVector( resType ) ) return false;
312  if( isTypeQueryableVector( resType ) ) return false;
313  }
314  return true;
315 }
316 
318  Reflex::Type valueType;
319  // find the iterator return type as the member value_type of the containers
320  size_t subTypeSize = typ.SubTypeSize();
321  size_t i=0;
322  while(i<subTypeSize){
323  Reflex::Type sti = typ.SubTypeAt(i);
324  if(sti.Name()=="value_type") {
325  valueType = sti;
326  break;
327  }
328  i++;
329  }
330  return valueType;
331 }
332 
334  Reflex::Type keyType;
335  // find the iterator return type as the member value_type of the containers
336  size_t subTypeSize = typ.SubTypeSize();
337  size_t i=0;
338  while(i<subTypeSize){
339  Reflex::Type sti = typ.SubTypeAt(i);
340  if(sti.Name()=="key_type") {
341  keyType = sti;
342  break;
343  }
344  i++;
345  }
346  return keyType;
347 }
348 
351  // find the iterator return type as the member value_type of the containers
352  size_t subTypeSize = typ.SubTypeSize();
353  size_t i=0;
354  while(i<subTypeSize){
355  Reflex::Type sti = typ.SubTypeAt(i);
356  if(sti.Name()=="mapped_type") {
357  dataType = sti;
358  break;
359  }
360  i++;
361  }
362  return dataType;
363 }
364 
365 Reflex::Type ora::ClassUtils::containerSubType(const Reflex::Type& typ, const std::string& subTypeName){
366  Reflex::Type subType;
367  size_t subTypeSize = typ.SubTypeSize();
368  size_t i=0;
369  while(i<subTypeSize){
370  Reflex::Type sti = typ.SubTypeAt(i);
371  if(sti.Name()==subTypeName) {
372  subType = sti;
373  break;
374  }
375  i++;
376  }
377  return resolvedType(subType);
378 }
379 
382  while(resolvedType.IsTypedef()){
383  resolvedType = resolvedType.ToType();
384  }
385  return resolvedType;
386 }
tuple base
Main Program
Definition: newFWLiteAna.py:92
type
Definition: HCALResponse.h:22
bool isTypeOraPointer(const Reflex::Type &typ)
Definition: ClassUtils.cc:241
int i
Definition: DBlmapReader.cc:9
bool isTypeNonKeyedContainer(const Reflex::Type &typ)
Definition: ClassUtils.cc:184
bool isTypeAssociativeContainer(const Reflex::Type &typ)
Definition: ClassUtils.cc:202
bool isTypeString(const Reflex::Type &typ)
Definition: ClassUtils.cc:128
void * constructObject(const Reflex::Type &typ)
Definition: ClassUtils.cc:118
bool isTypePVector(const Reflex::Type &typ)
Definition: ClassUtils.cc:275
bool isTypeOraReference(const Reflex::Type &typ)
Definition: ClassUtils.cc:254
void operator()(void *ptr)
Definition: ClassUtils.cc:23
bool findBaseType(Reflex::Type &type, Reflex::Type &baseType, Reflex::OffsetFunction &func)
Definition: ClassUtils.cc:58
bool isTypeUniqueReference(const Reflex::Type &typ)
Definition: ClassUtils.cc:262
std::string demangledName(const std::type_info &typeInfo)
Definition: ClassUtils.cc:73
Reflex::Type containerSubType(const Reflex::Type &typ, const std::string &subTypeName)
Definition: ClassUtils.cc:365
Reflex::Type containerDataType(const Reflex::Type &typ)
Definition: ClassUtils.cc:349
void loadDictionary(const std::string &className)
Definition: ClassUtils.cc:27
RflxDeleter(const Reflex::Type &type)
Definition: ClassUtils.cc:12
bool isTypeContainer(const Reflex::Type &typ)
Definition: ClassUtils.cc:138
static PluginCapabilities * get()
Reflex::Type containerKeyType(const Reflex::Type &typ)
Definition: ClassUtils.cc:333
Reflex::Type containerValueType(const Reflex::Type &typ)
Definition: ClassUtils.cc:317
Reflex::Type resolvedType(const Reflex::Type &typ)
Definition: ClassUtils.cc:380
bool isTypeKeyedContainer(const Reflex::Type &typ)
Definition: ClassUtils.cc:164
bool isTypePrimitive(const Reflex::Type &typ)
Definition: ClassUtils.cc:134
bool isTypeNamedReference(const Reflex::Type &typ)
Definition: ClassUtils.cc:258
void * upCast(const Reflex::Type &type, void *ptr, const Reflex::Type &asType)
Definition: ClassUtils.cc:36
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
void load(const std::string &iName)
tuple status
Definition: ntuplemaker.py:245
bool isTypeNonAssociativeContainer(const Reflex::Type &typ)
Definition: ClassUtils.cc:218
Reflex::Type lookupDictionary(const std::type_info &typeInfo, bool throwFlag=true)
Definition: ClassUtils.cc:84
bool isTypeObject(const Reflex::Type &typ)
Definition: ClassUtils.cc:301
std::string className(const T &t)
Definition: ClassName.h:30
bool isType(const Reflex::Type &type, const Reflex::Type &baseType)
Definition: ClassUtils.cc:49
bool isTypeQueryableVector(const Reflex::Type &typ)
Definition: ClassUtils.cc:288