Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGlobal.cxx
Go to the documentation of this file.
1 // @(#)root/meta:$Id$
2 // Author: Rene Brun 13/11/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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 TGlobal
13 Global variables class (global variables are obtained from CINT).
14 This class describes the attributes of a global variable.
15 The TROOT class contains a list of all currently defined global
16 variables (accessible via TROOT::GetListOfGlobals()).
17 */
18 
19 #include "TGlobal.h"
20 #include "TInterpreter.h"
21 #include "TList.h"
22 #include "TROOT.h"
23 
24 
25 ClassImp(TGlobal);
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Default TGlobal ctor.
29 
30 TGlobal::TGlobal(DataMemberInfo_t *info) : TDictionary(), fInfo(info)
31 {
32  if (fInfo) {
33  SetName(gCling->DataMemberInfo_Name(fInfo));
34  SetTitle(gCling->DataMemberInfo_Title(fInfo));
35  }
36 }
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Copy constructor
40 
41 TGlobal::TGlobal(const TGlobal &rhs) : TDictionary( ), fInfo(nullptr)
42 {
43  if (rhs.fInfo) {
44  fInfo = gCling->DataMemberInfo_FactoryCopy(rhs.fInfo);
45  SetName(gCling->DataMemberInfo_Name(fInfo));
46  SetTitle(gCling->DataMemberInfo_Title(fInfo));
47  }
48 }
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Assignment operator.
52 
53 TGlobal &TGlobal::operator=(const TGlobal &rhs)
54 {
55  if (this != &rhs) {
56  gCling->DataMemberInfo_Delete(fInfo);
57  if (rhs.fInfo) {
58  fInfo = gCling->DataMemberInfo_FactoryCopy(rhs.fInfo);
59  SetName(gCling->DataMemberInfo_Name(fInfo));
60  SetTitle(gCling->DataMemberInfo_Title(fInfo));
61  }
62  }
63  return *this;
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// TGlobal dtor deletes adopted CINT DataMemberInfo object.
68 
69 TGlobal::~TGlobal()
70 {
71  if (fInfo && gCling) gCling->DataMemberInfo_Delete(fInfo);
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// Return address of global.
76 
77 void *TGlobal::GetAddress() const
78 {
79  return (void *)gCling->DataMemberInfo_Offset(fInfo);
80 }
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 /// Return number of array dimensions.
84 
85 Int_t TGlobal::GetArrayDim() const
86 {
87  if (!fInfo) return 0;
88  return gCling->DataMemberInfo_ArrayDim(fInfo);
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 
93 TDictionary::DeclId_t TGlobal::GetDeclId() const
94 {
95  return gInterpreter->GetDeclId(fInfo);
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Return maximum index for array dimension "dim".
100 
101 Int_t TGlobal::GetMaxIndex(Int_t dim) const
102 {
103  if (!fInfo) return 0;
104  return gCling->DataMemberInfo_MaxIndex(fInfo,dim);
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Get type of global variable, e,g.: "class TDirectory*" -> "TDirectory".
109 /// Result needs to be used or copied immediately.
110 
111 const char *TGlobal::GetTypeName() const
112 {
113  if (!fInfo) return nullptr;
114  return gCling->TypeName(gCling->DataMemberInfo_TypeName(fInfo));
115 }
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 /// Get full type description of global variable, e,g.: "class TDirectory*".
119 
120 const char *TGlobal::GetFullTypeName() const
121 {
122  if (!fInfo) return nullptr;
123  return gCling->DataMemberInfo_TypeName(fInfo);
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// Return true if this global object is pointing to a currently
128 /// loaded global. If a global is unloaded after the TGlobal
129 /// is created, the TGlobal will be set to be invalid.
130 
131 Bool_t TGlobal::IsValid()
132 {
133  // Register the transaction when checking the validity of the object.
134  if (!fInfo && UpdateInterpreterStateMarker()) {
135  DeclId_t newId = gInterpreter->GetDataMember(0, fName);
136  if (newId) {
137  DataMemberInfo_t *info = gInterpreter->DataMemberInfo_Factory(newId, 0);
138  Update(info);
139  }
140  return newId != 0;
141  }
142  return fInfo != 0;
143 }
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// Get property description word. For meaning of bits see EProperty.
147 
148 Long_t TGlobal::Property() const
149 {
150  if (!fInfo) return 0;
151  return gCling->DataMemberInfo_Property(fInfo);
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Update the TFunction to reflect the new info.
156 ///
157 /// This can be used to implement unloading (info == 0) and then reloading
158 /// (info being the 'new' decl address).
159 
160 Bool_t TGlobal::Update(DataMemberInfo_t *info)
161 {
162  if (fInfo) gCling->DataMemberInfo_Delete(fInfo);
163  fInfo = info;
164  if (fInfo) {
165  SetName(gCling->DataMemberInfo_Name(fInfo));
166  SetTitle(gCling->DataMemberInfo_Title(fInfo));
167  }
168  return kTRUE;
169 }
170 
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Constructor
174 
175 TGlobalMappedFunction::TGlobalMappedFunction(const char *name, const char *type, GlobalFunc_t funcPtr)
176  : fFuncPtr(funcPtr)
177 {
178  SetNameTitle(name, type);
179 }
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 /// Returns list collected globals
183 /// Used to storeTGlobalMappedFunctions from other libs, before gROOT was initialized
184 
185 TList& TGlobalMappedFunction::GetEarlyRegisteredGlobals()
186 {
187  // Used to storeTGlobalMappedFunctions from other libs, before gROOT was initialized
188  static TList fEarlyRegisteredGlobals;
189  // For thread-safe setting of SetOwner(kTRUE).
190  static bool earlyRegisteredGlobalsSetOwner
191  = (fEarlyRegisteredGlobals.SetOwner(kTRUE), true);
192  (void) earlyRegisteredGlobalsSetOwner; // silence unused var
193 
194  return fEarlyRegisteredGlobals;
195 }
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// Add to GetEarlyRegisteredGlobals() if gROOT is not yet initialized; add to
199 /// gROOT->GetListOfGlobals() otherwise.
200 
201 void TGlobalMappedFunction::Add(TGlobalMappedFunction* gmf)
202 {
203  // Use "gCling" as synonym for "gROOT is initialized"
204  if (gCling) {
205  gROOT->GetListOfGlobals()->Add(gmf);
206  } else {
207  GetEarlyRegisteredGlobals().Add(gmf);
208  }
209 }