Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
TEnum.cxx
Go to the documentation of this file.
1// @(#)root/meta:$Id$
2// Author: Bianca-Cristina Cristescu 10/07/13
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 TEnum
13The TEnum class implements the enum type.
14*/
15
16#include <iostream>
17
18#include "TEnum.h"
19#include "TEnumConstant.h"
20#include "TInterpreter.h"
21#include "TClass.h"
22#include "TClassEdit.h"
23#include "TClassTable.h"
24#include "TProtoClass.h"
25#include "TROOT.h"
26
27#include "TListOfEnums.h"
28
30
31////////////////////////////////////////////////////////////////////////////////
32/// Constructor for TEnum class.
33/// It takes the name of the TEnum type, interpreter info and surrounding class
34/// the enum it is not globalat namespace scope.
35/// Constant List is owner if enum not on global scope (thus constants not
36/// in TROOT::GetListOfGlobals).
37
39 : fClass(cls)
40{
42 if (cls) {
44 }
45
46 // Determine fQualName
47 if (0 != strcmp("",GetTitle())){ // It comes from a protoclass
48 fQualName = std::string(GetTitle()) + "::" + GetName();
49 }
50 else if (GetClass()){ // It comes from a class/ns
51 fQualName = std::string(GetClass()->GetName()) + "::" + GetName();
52 }
53 else { // it is in the global scope
55 }
56
58}
59
60////////////////////////////////////////////////////////////////////////////////
61/// Copy constructor
62
64{
65 fClass = src.fClass;
66 fInfo = src.fInfo ? gInterpreter->ClassInfo_Factory(src.fInfo) : nullptr;
67 fQualName = src.fQualName;
68 fUnderlyingType = src.fUnderlyingType;
69
70 Bool_t isowner = src.fConstantList.IsOwner();
72 TIter next(&src.fConstantList);
73 while (auto c = (TEnumConstant *) next())
75
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// Assign operator
80
82{
83 if (this != &src) {
84 if (fInfo)
85 gInterpreter->ClassInfo_Delete(fInfo);
87
89
90 fInfo = src.fInfo ? gInterpreter->ClassInfo_Factory(src.fInfo) : nullptr;
91 fQualName = src.fQualName;
92 fUnderlyingType = src.fUnderlyingType;
93
94 Bool_t isowner = src.fConstantList.IsOwner();
96 TIter next(&src.fConstantList);
97 while (auto c = (TEnumConstant *) next())
99 }
100 return *this;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Destructor
105
107{
108 gInterpreter->ClassInfo_Delete(fInfo);
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Add a EnumConstant to the list of constants of the Enum Type.
113
118
119////////////////////////////////////////////////////////////////////////////////
120/// Return true if this enum object is pointing to a currently
121/// loaded enum. If a enum is unloaded after the TEnum
122/// is created, the TEnum will be set to be invalid.
123
125{
126 if (TestBit(kBitIsValid))
127 return true;
128
129 // Register the transaction when checking the validity of the object.
132 if (newId)
133 Update(newId);
134 return newId != nullptr;
135 }
136 return fInfo != nullptr;
137}
138
139////////////////////////////////////////////////////////////////////////////////
140/// Get property description word. For meaning of bits see EProperty.
141
143{
145}
146
147////////////////////////////////////////////////////////////////////////////////
148
150{
151 if (fInfo)
152 return gInterpreter->GetDeclId(fInfo);
153
154 return nullptr;
155}
156
157////////////////////////////////////////////////////////////////////////////////
158
160{
161 if (fInfo)
162 gInterpreter->ClassInfo_Delete(fInfo);
163 if (!id) {
165 fInfo = nullptr;
166 return;
167 }
168
169 fInfo = gInterpreter->ClassInfo_Factory(id);
170
171 if (fInfo) {
172 SetBit(kBitIsScopedEnum, gInterpreter->ClassInfo_IsScopedEnum(fInfo));
173 fUnderlyingType = gInterpreter->ClassInfo_GetUnderlyingType(fInfo);
175 } else {
177 }
178}
179
180////////////////////////////////////////////////////////////////////////////////
181
182TEnum *TEnum::GetEnum(const std::type_info &ti, ESearchAction sa)
183{
184 int errorCode = 0;
186
187 if (errorCode != 0) {
189 std::cerr << "ERROR TEnum::GetEnum - A problem occurred while demangling name.\n";
190 return nullptr;
191 }
192
196 return en;
197
198}
199
200////////////////////////////////////////////////////////////////////////////////
201/// Static function to retrieve enumerator from the ROOT's typesystem.
202/// It has no side effect, except when the load flag is true. In this case,
203/// the load of the library containing the scope of the enumerator is attempted.
204/// There are two top level code paths: the enumerator is scoped or isn't.
205/// If it is not, a lookup in the list of global enums is performed.
206/// If it is, two lookups are carried out for its scope: one in the list of
207/// classes and one in the list of protoclasses. If a scope with the desired name
208/// is found, the enum is searched. If the scope is not found, and the load flag is
209/// true, the aforementioned two steps are performed again after an autoload attempt
210/// with the name of the scope as key is tried out.
211/// If the interpreter lookup flag is false, the ListOfEnums objects are not treated
212/// as such, but rather as THashList objects. This prevents any flow of information
213/// from the interpreter into the ROOT's typesystem: a snapshot of the typesystem
214/// status is taken.
215
217{
218 // Potential optimisation: reduce number of branches using partial specialisation of
219 // helper functions.
220
221 TEnum *theEnum = nullptr;
222
223 // Wrap some gymnastic around the enum finding. The special treatment of the
224 // ListOfEnums objects is located in this routine.
225 auto findEnumInList = [](const TCollection * l, const char * enName, ESearchAction sa_local) {
226 TObject *obj;
227 if (sa_local & kInterpLookup) {
228 obj = l->FindObject(enName);
229 } else {
230 auto enumTable = dynamic_cast<const TListOfEnums *>(l);
231 obj = enumTable->GetObject(enName);
232 }
233 return static_cast<TEnum *>(obj);
234 };
235
236 // Helper routine to look fo the scope::enum in the typesystem.
237 // If autoload and interpreter lookup is allowed, TClass::GetClass is called.
238 // If not, the list of classes and the list of protoclasses is inspected.
239 auto searchEnum = [&theEnum, findEnumInList](const char * scopeName, const char * enName, ESearchAction sa_local) {
240 // Check if the scope is a class
242 auto scope = TClass::GetClass(scopeName, true);
243 TEnum *en = nullptr;
244 if (scope) en = findEnumInList(scope->GetListOfEnums(kFALSE), enName, sa_local);
245 return en;
246 }
247
248
249 if (auto tClassScope = static_cast<TClass *>(gROOT->GetListOfClasses()->FindObject(scopeName))) {
250 // If this is a class, load only if the user allowed interpreter lookup
251 // If this is a namespace and the user did not allow for interpreter lookup, load but before disable
252 // autoparsing if enabled.
254 const bool scopeIsNamespace (tClassScope->Property() & kIsNamespace);
255
256 const bool autoParseSuspended = gInterpreter->IsAutoParsingSuspended();
258
260
262 canLoadEnums=true;
263 }
264
265 auto listOfEnums = tClassScope->GetListOfEnums(canLoadEnums);
266
267 // Previous incarnation of the code re-enabled the auto parsing,
268 // before executing findEnumInList
270 }
271 // Check if the scope is still a protoclass
272 else if (auto tProtoClassscope = static_cast<TProtoClass *>((gClassTable->GetProtoNorm(scopeName)))) {
273 auto listOfEnums = tProtoClassscope->GetListOfEnums();
275 }
276 return theEnum;
277 };
278
280
281 if (strchr(lastPos,'<')) {
282 // The unqualified name has template syntax, it can't possibly be an
283 // enum.
284 return nullptr;
285 }
286
287 std::string normalizedName;
288 {
292 }
293
294 if (normalizedName != enumName) {
295 enumName = normalizedName.c_str();
297 }
298
299 // Keep the state consistent. In particular prevent change in the state of
300 // AutoLoading and AutoParsing allowance and gROOT->GetListOfClasses()
301 // and the later update/modification to the autoparsing state.
303
304 if (lastPos != enumName) {
305 // We have a scope
306 const auto enName = lastPos;
307 const auto scopeNameSize = (lastPos - enumName) / sizeof(decltype(*lastPos)) - 2;
308 std::string scopeName{enumName, scopeNameSize};
309 // Three levels of search
311 if (!theEnum && (sa & kAutoload)) {
312 const auto libsLoaded = gInterpreter->AutoLoad(scopeName.c_str());
313 // It could be an enum in a scope which is not selected
314 if (libsLoaded == 0){
315 gInterpreter->AutoLoad(enumName);
316 }
318 }
319 if (!theEnum && (sa & kALoadAndInterpLookup)) {
320 if (gDebug > 0) {
321 printf("TEnum::GetEnum: Header Parsing - The enumerator %s is not known to the typesystem: an interpreter lookup will be performed. This can imply parsing of headers. This can be avoided selecting the numerator in the linkdef/selection file.\n", enumName);
322 }
324 }
325 } else {
326 // We don't have any scope: this is a global enum
327 theEnum = findEnumInList(gROOT->GetListOfEnums(), enumName, kNone);
328 if (!theEnum && (sa & kAutoload)) {
329 gInterpreter->AutoLoad(enumName);
330 theEnum = findEnumInList(gROOT->GetListOfEnums(), enumName, kAutoload);
331 }
332 if (!theEnum && (sa & kALoadAndInterpLookup)) {
333 if (gDebug > 0) {
334 printf("TEnum::GetEnum: Header Parsing - The enumerator %s is not known to the typesystem: an interpreter lookup will be performed. This can imply parsing of headers. This can be avoided selecting the numerator in the linkdef/selection file.\n", enumName);
335 }
337 }
338 }
339
340 return theEnum;
341}
Cppyy::TCppType_t fClass
#define c(i)
Definition RSha256.hxx:101
long Long_t
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define ClassImp(name)
Definition Rtypes.h:374
R__EXTERN TClassTable * gClassTable
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
@ kIsScopedEnum
Definition TDictionary.h:91
@ kIsEnum
Definition TDictionary.h:68
@ kIsNamespace
Definition TDictionary.h:95
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
char name[80]
Definition TGX11.cxx:110
#define gInterpreter
Int_t gDebug
Definition TROOT.cxx:597
#define gROOT
Definition TROOT.h:406
#define R__WRITE_LOCKGUARD(mutex)
#define R__READ_LOCKGUARD(mutex)
#define free
Definition civetweb.c:1539
static TProtoClass * GetProtoNorm(const char *cname)
Given the class normalized name returns the TClassProto object for the class.
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:3069
Collection abstract base class.
Definition TCollection.h:65
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
This class defines an abstract interface that must be implemented by all classes that contain diction...
Bool_t UpdateInterpreterStateMarker()
the Cling ID of the transaction that last updated the object
TDictionary & operator=(const TDictionary &other)
const void * DeclId_t
The TEnumConstant class implements the constants of the enum type.
The TEnum class implements the enum type.
Definition TEnum.h:33
TEnum & operator=(const TEnum &)
Assign operator.
Definition TEnum.cxx:81
THashList fConstantList
Definition TEnum.h:36
TClass * GetClass() const
Definition TEnum.h:63
void AddConstant(TEnumConstant *constant)
Add a EnumConstant to the list of constants of the Enum Type.
Definition TEnum.cxx:114
ClassInfo_t * fInfo
Definition TEnum.h:37
Long_t Property() const override
Get property description word. For meaning of bits see EProperty.
Definition TEnum.cxx:142
@ kBitIsValid
The TEnum object was read from file (assumed valid)
Definition TEnum.h:44
@ kBitIsScopedEnum
The enum is an enum class.
Definition TEnum.h:43
TClass * fClass
Interpreter information, owned by TEnum.
Definition TEnum.h:38
static TEnum * GetEnum(const std::type_info &ti, ESearchAction sa=kALoadAndInterpLookup)
Definition TEnum.cxx:182
void Update(DeclId_t id)
Definition TEnum.cxx:159
Bool_t IsValid()
Return true if this enum object is pointing to a currently loaded enum.
Definition TEnum.cxx:124
EDataType fUnderlyingType
Definition TEnum.h:40
DeclId_t GetDeclId() const
Definition TEnum.cxx:149
std::string fQualName
Owning class.
Definition TEnum.h:39
ESearchAction
Definition TEnum.h:49
@ kALoadAndInterpLookup
Definition TEnum.h:52
@ kNone
Definition TEnum.h:49
@ kAutoload
Definition TEnum.h:50
@ kInterpLookup
Definition TEnum.h:51
TEnum()
Definition TEnum.h:55
virtual ~TEnum()
Destructor.
Definition TEnum.cxx:106
void Clear(Option_t *option="") override
Remove all objects from the list.
A collection of TEnum objects designed for fast access given a DeclId_t and for keep track of TEnum t...
void Add(TObject *obj) override
Definition TList.h:81
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
TString fName
Definition TNamed.h:32
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
Mother of all ROOT objects.
Definition TObject.h:41
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:199
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:420
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:798
void ResetBit(UInt_t f)
Definition TObject.h:198
Persistent version of a TClass.
Definition TProtoClass.h:38
R__EXTERN TVirtualRWMutex * gCoreMutex
const char * GetUnqualifiedName(const char *name)
Return the start of the unqualified name include in 'original'.
char * DemangleName(const char *mangled_name, int &errorCode)
Definition TClassEdit.h:208
void GetNormalizedName(std::string &norm_name, std::string_view name)
Return the normalized name.
TLine l
Definition textangle.C:4