Logo ROOT   6.12/07
Reference Guide
TListOfTypes.cxx
Go to the documentation of this file.
1 // @(#)root/cont
2 // Author: Philippe Canal Aug 2013
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TListOfTypes
13 \ingroup Base
14 
15 A collection of TDataType designed to hold the typedef information
16 and numerical type information. The collection is populated on demand.
17 
18 Besides the built-in types (int, float) a typedef is explicitly
19 added to the collection (and thus visible via ls or Print) only if
20 it is requested explicitly.
21 */
22 
23 
24 #include "TListOfTypes.h"
25 
26 #include "TInterpreter.h"
27 #include "TDataType.h"
28 #include "TVirtualMutex.h"
29 
30 #include "TEnum.h"
31 #include "TClassTable.h"
32 #include "TROOT.h"
33 #include "TClass.h"
34 #include "TProtoClass.h"
35 #include "TListOfEnums.h"
36 
38 {
39  // Constructor
41 }
42 
44 {
45  // Specialize FindObject to do search for the
46  // typedef if its not already in the list
47 
48  return FindType(name);
49 }
50 
51 static bool NameExistsElsewhere(const char* name){
52 
53  // Is this a scope?
54  // We look into the list of classes available,
55  // the ones in the dictionaries and the protoclasses.
56  if (gROOT->GetListOfClasses()->FindObject(name) ||
58  TClassTable::GetProtoNorm(name)) return true;
59 
60  // Is this an enum?
61  TObject* theEnum = nullptr;
62  const auto lastPos = strrchr(name, ':');
63  if (lastPos != nullptr) {
64  // We have a scope
65  const auto enName = lastPos + 1;
66  const auto scopeNameSize = ((Long64_t)lastPos - (Long64_t)name) / sizeof(decltype(*lastPos)) - 1;
67 #ifdef R__WIN32
68  char *scopeName = new char[scopeNameSize + 1];
69 #else
70  char scopeName[scopeNameSize + 1]; // on the stack, +1 for the terminating character '\0'
71 #endif
72  strncpy(scopeName, name, scopeNameSize);
73  scopeName[scopeNameSize] = '\0';
74  // We have now an enum name and a scope name
75  // We look first in the classes
76  if(auto scope = dynamic_cast<TClass*>(gROOT->GetListOfClasses()->FindObject(scopeName))){
77  theEnum = ((TListOfEnums*)scope->GetListOfEnums(false))->THashList::FindObject(enName);
78  }
79  // And then if not found in the protoclasses
80  if (!theEnum){
81  if (auto scope = TClassTable::GetProtoNorm(scopeName)){
82  if (auto listOfEnums = (TListOfEnums*)scope->GetListOfEnums())
83  theEnum = listOfEnums->THashList::FindObject(enName);
84  }
85  }
86 #ifdef R__WIN32
87  delete [] scopeName;
88 #endif
89  } else { // Here we look in the global scope
90  theEnum = ((TListOfEnums*)gROOT->GetListOfEnums())->THashList::FindObject(name);
91  }
92 
93  return nullptr != theEnum;
94 
95 }
96 
98 {
99  // Look for a type, first in the hast table
100  // then in the interpreter.
101 
103 
104  TDataType *result = static_cast<TDataType*>(THashTable::FindObject(name));
105  if (!result) {
106 
107  if (NameExistsElsewhere(name)) {
108  return nullptr;
109  }
110 
111  // We perform now a lookup
112 
114 
115  TypedefInfo_t *info = gInterpreter->TypedefInfo_Factory(name);
116  if (gInterpreter->TypedefInfo_IsValid(info)) {
117  result = new TDataType(info);
118  // Double check we did not get a different spelling of an
119  // already existing typedef.
120  if (strcmp(name,result->GetName()) != 0) {
121  TDataType *alt = static_cast<TDataType*>(THashTable::FindObject(result->GetName()));
122  if (!alt)
123  const_cast<TListOfTypes*>(this)->Add(result);
124  else {
125  delete result;
126  result = alt;
127  }
128  } else {
129  const_cast<TListOfTypes*>(this)->Add(result);
130  }
131  } else {
132  gInterpreter->TypedefInfo_Delete(info);
133  }
134  }
135  return result;
136 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
#define R__COLLECTION_READ_GUARD()
Definition: TCollection.h:126
long long Long64_t
Definition: RtypesCore.h:69
static TProtoClass * GetProtoNorm(const char *cname)
Given the class normalized name returns the TClassProto object for the class.
R__EXTERN TVirtualMutex * gInterpreterMutex
Definition: TInterpreter.h:40
#define gROOT
Definition: TROOT.h:402
#define gInterpreter
Definition: TInterpreter.h:526
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashList.cxx:262
THashTable implements a hash table to store TObject&#39;s.
Definition: THashTable.h:35
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
TDataType * FindType(const char *name) const
Basic data type descriptor (datatype information is obtained from CINT).
Definition: TDataType.h:44
A collection of TDataType designed to hold the typedef information and numerical type information...
Definition: TListOfTypes.h:30
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashTable.cxx:227
void Add(TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:77
#define R__LOCKGUARD(mutex)
static void AddBuiltins(TCollection *types)
Create the TDataType objects for builtins.
Definition: TDataType.cxx:408
Mother of all ROOT objects.
Definition: TObject.h:37
A collection of TEnum objects designed for fast access given a DeclId_t and for keep track of TEnum t...
Definition: TListOfEnums.h:32
static DictFuncPtr_t GetDictNorm(const char *cname)
Given the normalized class name returns the Dictionary() function of a class (uses hash of name)...
char name[80]
Definition: TGX11.cxx:109
static bool NameExistsElsewhere(const char *name)