Logo ROOT  
Reference Guide
TDataType.cxx
Go to the documentation of this file.
1// @(#)root/meta:$Id$
2// Author: Rene Brun 04/02/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons . *
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 TDataType
13Basic data type descriptor (datatype information is obtained from
14CINT). This class describes the attributes of type definitions
15(typedef's). The TROOT class contains a list of all currently
16defined types (accessible via TROOT::GetListOfTypes()).
17*/
18
19#include "TDataType.h"
20#include "TInterpreter.h"
21#include "TCollection.h"
22#include "TVirtualMutex.h"
23#include "ThreadLocalStorage.h"
24#ifdef R__SOLARIS
25#include <typeinfo>
26#endif
27
29
31
32////////////////////////////////////////////////////////////////////////////////
33/// Default TDataType ctor. TDataTypes are constructed in TROOT via
34/// a call to TCling::UpdateListOfTypes().
35
36TDataType::TDataType(TypedefInfo_t *info) : TDictionary(),
37 fTypeNameIdx(-1), fTypeNameLen(0)
38{
39 fInfo = info;
40
41 if (fInfo) {
48 } else {
49 SetTitle("Builtin basic type");
50 fProperty = 0;
51 fSize = 0;
53 }
54}
55
56////////////////////////////////////////////////////////////////////////////////
57/// Constructor for basic data types, like "char", "unsigned char", etc.
58
59TDataType::TDataType(const char *typenam) : fInfo(nullptr), fProperty(kIsFundamental),
60 fTypeNameIdx(-1), fTypeNameLen(0)
61{
62 fInfo = nullptr;
63 SetName(typenam);
64 SetTitle("Builtin basic type");
65
67}
68
69////////////////////////////////////////////////////////////////////////////////
70///copy constructor
71
73 TDictionary(dt),
74 fInfo(gCling->TypedefInfo_FactoryCopy(dt.fInfo)),
75 fSize(dt.fSize),
76 fType(dt.fType),
77 fProperty(dt.fProperty),
78 fTrueName(dt.fTrueName),
79 fTypeNameIdx(dt.fTypeNameIdx), fTypeNameLen(dt.fTypeNameLen)
80{
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// assignment operator
85
87{
88 if(this!=&dt) {
92 fSize=dt.fSize;
93 fType=dt.fType;
98 }
99 return *this;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// TDataType dtor deletes adopted CINT TypedefInfo object.
104
106{
108}
109
110////////////////////////////////////////////////////////////////////////////////
111/// Return the name of the type.
112
114{
115 switch (type) {
116 case 1: return "Char_t";
117 case 2: return "Short_t";
118 case 3: return "Int_t";
119 case 4: return "Long_t";
120 case 5: return "Float_t";
121 case 6: return "Int_t";
122 case 7: return "char*";
123 case 8: return "Double_t";
124 case 9: return "Double32_t";
125 case 11: return "UChar_t";
126 case 12: return "UShort_t";
127 case 13: return "UInt_t";
128 case 14: return "ULong_t";
129 case 15: return "UInt_t";
130 case 16: return "Long64_t";
131 case 17: return "ULong64_t";
132 case 18: return "Bool_t";
133 case 19: return "Float16_t";
134 case kVoid_t: return "void";
135 case kDataTypeAliasUnsigned_t: return "UInt_t";
136 case kDataTypeAliasSignedChar_t: return "SignedChar_t";
137 case kOther_t: return "";
138 case kNoType_t: return "";
139 case kchar: return "Char_t";
140 default: return "";
141 }
142 return ""; // to silence compilers
143}
144
145////////////////////////////////////////////////////////////////////////////////
146/// Get basic type of typedef, e,g.: "class TDirectory*" -> "TDirectory".
147/// Result needs to be used or copied immediately.
148
150{
151 if (fTypeNameLen) {
153 }
154
155 if (fInfo) {
156 (const_cast<TDataType*>(this))->CheckInfo();
157 TString typeName = gInterpreter->TypeName(fTrueName.Data());
158 fTypeNameIdx = fTrueName.Index(typeName);
159 if (fTypeNameIdx == -1) {
160 Error("GetTypeName", "Cannot find type name %s in true name %s!",
161 typeName.Data(), fTrueName.Data());
162 return fName;
163 }
164 fTypeNameLen = typeName.Length();
166 } else {
167 if (fType != kOther_t) return fName.Data();
168 return fTrueName;
169 }
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// Get full type description of typedef, e,g.: "class TDirectory*".
174
175const char *TDataType::GetFullTypeName() const
176{
177 if (fInfo) {
178 (const_cast<TDataType*>(this))->CheckInfo();
179 return fTrueName;
180 } else {
181 if (fType != kOther_t) return fName;
182 return fTrueName;
183 }
184}
185
186////////////////////////////////////////////////////////////////////////////////
187/// Set type id depending on name.
188
189EDataType TDataType::GetType(const std::type_info &typeinfo)
190{
191 EDataType retType = kOther_t;
192
193 if (typeid(unsigned int) == typeinfo) {
194 retType = kUInt_t;
195 } else if (typeid(int) == typeinfo) {
196 retType = kInt_t;
197 } else if (typeid(ULong_t) == typeinfo) {
198 retType = kULong_t;
199 } else if (typeid(Long_t) == typeinfo) {
200 retType = kLong_t;
201 } else if (typeid(ULong64_t) == typeinfo) {
202 retType = kULong64_t;
203 } else if (typeid(Long64_t) == typeinfo) {
204 retType = kLong64_t;
205 } else if (typeid(unsigned short) == typeinfo) {
206 retType = kUShort_t;
207 } else if (typeid(short) == typeinfo) {
208 retType = kShort_t;
209 } else if (typeid(unsigned char) == typeinfo) {
210 retType = kUChar_t;
211 } else if (typeid(char) == typeinfo) {
212 retType = kChar_t;
213 } else if (typeid(Bool_t) == typeinfo) {
214 retType = kBool_t;
215 } else if (typeid(float) == typeinfo) {
216 retType = kFloat_t;
217 } else if (typeid(Float16_t) == typeinfo) {
218 retType = kFloat16_t;
219 } else if (typeid(double) == typeinfo) {
220 retType = kDouble_t;
221 } else if (typeid(Double32_t) == typeinfo) {
222 retType = kDouble32_t;
223 } else if (typeid(char*) == typeinfo) {
224 retType = kCharStar;
225 } else if (typeid(signed char) == typeinfo) {
227 }
228 return retType;
229}
230
231////////////////////////////////////////////////////////////////////////////////
232/// Return string containing value in buffer formatted according to
233/// the basic data type. The result needs to be used or copied immediately.
234
235const char *TDataType::AsString(void *buf) const
236{
237 TTHREAD_TLS_DECL_ARG(TString, line ,81);
238 const char *name;
239
240 if (fInfo) {
241 (const_cast<TDataType*>(this))->CheckInfo();
242 name = fTrueName;
243 } else {
244 name = fName.Data();
245 }
246
247 line[0] = 0;
248 if (!strcmp("unsigned int", name))
249 line.Form( "%u", *(unsigned int *)buf);
250 else if (!strcmp("unsigned", name))
251 line.Form( "%u", *(unsigned int *)buf);
252 else if (!strcmp("int", name))
253 line.Form( "%d", *(int *)buf);
254 else if (!strcmp("unsigned long", name))
255 line.Form( "%lu", *(ULong_t *)buf);
256 else if (!strcmp("long", name))
257 line.Form( "%ld", *(Long_t *)buf);
258 else if (!strcmp("unsigned long long", name))
259 line.Form( "%llu", *(ULong64_t *)buf);
260 else if (!strcmp("ULong64_t", name))
261 line.Form( "%llu", *(ULong64_t *)buf);
262 else if (!strcmp("long long", name))
263 line.Form( "%lld", *(Long64_t *)buf);
264 else if (!strcmp("Long64_t", name))
265 line.Form( "%lld", *(Long64_t *)buf);
266 else if (!strcmp("unsigned short", name))
267 line.Form( "%hu", *(unsigned short *)buf);
268 else if (!strcmp("short", name))
269 line.Form( "%hd", *(short *)buf);
270 else if (!strcmp("bool", name))
271 line.Form( "%s", *(Bool_t *)buf ? "true" : "false");
272 else if (!strcmp("unsigned char", name) || !strcmp("char", name) ) {
273 line = *(char*)buf;
274 } else if (!strcmp("float", name))
275 line.Form( "%g", *(float *)buf);
276 else if (!strcmp("double", name))
277 line.Form( "%g", *(double *)buf);
278 else if (!strcmp("char*", name))
279 line.Form( "%s", *(char**)buf);
280
281 return line;
282}
283
284////////////////////////////////////////////////////////////////////////////////
285/// Get property description word. For meaning of bits see EProperty.
286
288{
289 if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
290 return fProperty;
291}
292
293////////////////////////////////////////////////////////////////////////////////
294/// Set type id depending on name.
295
296void TDataType::SetType(const char *name)
297{
298 fTrueName = name;
299 fType = kOther_t;
300 fSize = 0;
301
302 if (name==nullptr) {
303 return;
304 } else if (!strcmp("unsigned int", name)) {
305 fType = kUInt_t;
306 fSize = sizeof(UInt_t);
307 } else if (!strcmp("unsigned", name)) {
308 fType = kUInt_t;
309 fSize = sizeof(UInt_t);
310 } else if (!strcmp("int", name)) {
311 fType = kInt_t;
312 fSize = sizeof(Int_t);
313 } else if (!strcmp("unsigned long", name)) {
314 fType = kULong_t;
315 fSize = sizeof(ULong_t);
316 } else if (!strcmp("long", name)) {
317 fType = kLong_t;
318 fSize = sizeof(Long_t);
319 } else if (!strcmp("unsigned long long", name) || !strcmp("ULong64_t",name)) {
321 fSize = sizeof(ULong64_t);
322 } else if (!strcmp("long long", name) || !strcmp("Long64_t",name)) {
324 fSize = sizeof(Long64_t);
325 } else if (!strcmp("unsigned short", name)) {
327 fSize = sizeof(UShort_t);
328 } else if (!strcmp("short", name)) {
329 fType = kShort_t;
330 fSize = sizeof(Short_t);
331 } else if (!strcmp("unsigned char", name)) {
332 fType = kUChar_t;
333 fSize = sizeof(UChar_t);
334 } else if (!strcmp("char", name)) {
335 fType = kChar_t;
336 fSize = sizeof(Char_t);
337 } else if (!strcmp("bool", name)) {
338 fType = kBool_t;
339 fSize = sizeof(Bool_t);
340 } else if (!strcmp("float", name)) {
341 fType = kFloat_t;
342 fSize = sizeof(Float_t);
343 } else if (!strcmp("double", name)) {
345 fSize = sizeof(Double_t);
346 } else if (!strcmp("signed char", name)) {
347 fType = kChar_t; // kDataTypeAliasSignedChar_t;
348 fSize = sizeof(Char_t);
349 } else if (!strcmp("void", name)) {
350 fType = kVoid_t;
351 fSize = 0;
352 }
353
354 if (!strcmp("Float16_t", fName.Data())) {
356 }
357 if (!strcmp("Double32_t", fName.Data())) {
359 }
360 if (!strcmp("char*",fName.Data())) {
362 }
363 // kCounter = 6, kBits = 15
364}
365
366////////////////////////////////////////////////////////////////////////////////
367/// Get size of basic typedef'ed type.
368
370{
371 if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
372 return fSize;
373}
374
375////////////////////////////////////////////////////////////////////////////////
376/// Refresh the underlying information.
377
379{
380 // This can be needed if the library defining this typedef was loaded after
381 // another library and that this other library is unloaded (in which case
382 // things can get renumbered inside CINT).
383
384 if (!fInfo) return;
385
386 // This intentionally cast the constness away so that
387 // we can call CheckInfo from const data members.
389
391 strcmp(gCling->TypedefInfo_Name(fInfo),fName.Data())!=0) {
392
393 // The fInfo is invalid or does not
394 // point to this typedef anymore, let's
395 // refresh it
396
398
399 if (!gCling->TypedefInfo_IsValid(fInfo)) return;
400
405 }
406}
407
408////////////////////////////////////////////////////////////////////////////////
409/// Create the TDataType objects for builtins.
410
412{
413 if (fgBuiltins[kChar_t] == nullptr) {
414 // Add also basic types (like a identity typedef "typedef int int")
415 fgBuiltins[kChar_t] = new TDataType("char");
416 fgBuiltins[kUChar_t] = new TDataType("unsigned char");
417 fgBuiltins[kShort_t] = new TDataType("short");
418 fgBuiltins[kUShort_t] = new TDataType("unsigned short");
419 fgBuiltins[kInt_t] = new TDataType("int");
420 fgBuiltins[kUInt_t] = new TDataType("unsigned int");
421 fgBuiltins[kLong_t] = new TDataType("long");
422 fgBuiltins[kULong_t] = new TDataType("unsigned long");
423 fgBuiltins[kLong64_t] = new TDataType("long long");
424 fgBuiltins[kULong64_t] = new TDataType("unsigned long long");
425 fgBuiltins[kFloat_t] = new TDataType("float");
426 fgBuiltins[kDouble_t] = new TDataType("double");
427 fgBuiltins[kVoid_t] = new TDataType("void");
428 fgBuiltins[kBool_t] = new TDataType("bool");
429 fgBuiltins[kCharStar] = new TDataType("char*");
430
432 fgBuiltins[kDataTypeAliasSignedChar_t] = new TDataType("signed char");
433 }
434
435 for (Int_t i = 0; i < (Int_t)kNumDataTypes; ++i) {
436 if (fgBuiltins[i]) types->Add(fgBuiltins[i]);
437 }
438}
439
440////////////////////////////////////////////////////////////////////////////////
441/// Given a EDataType type, get the TDataType* that represents it.
442
444{
445 if (type == kOther_t || type >= kNumDataTypes) return nullptr;
446 return fgBuiltins[(int)type];
447}
float Float16_t
Definition: CPyCppyy.h:53
double Double32_t
Definition: CPyCppyy.h:54
int Int_t
Definition: CPyCppyy.h:43
unsigned int UInt_t
Definition: CPyCppyy.h:44
unsigned long ULong_t
Definition: CPyCppyy.h:51
long Long_t
Definition: CPyCppyy.h:50
size_t fSize
unsigned short UShort_t
Definition: RtypesCore.h:40
unsigned char UChar_t
Definition: RtypesCore.h:38
char Char_t
Definition: RtypesCore.h:33
bool Bool_t
Definition: RtypesCore.h:63
short Short_t
Definition: RtypesCore.h:39
double Double_t
Definition: RtypesCore.h:59
float Float_t
Definition: RtypesCore.h:57
#define ClassImp(name)
Definition: Rtypes.h:364
EDataType
Definition: TDataType.h:28
@ kNoType_t
Definition: TDataType.h:33
@ kFloat_t
Definition: TDataType.h:31
@ kULong64_t
Definition: TDataType.h:32
@ kInt_t
Definition: TDataType.h:30
@ kNumDataTypes
Definition: TDataType.h:40
@ kchar
Definition: TDataType.h:31
@ kLong_t
Definition: TDataType.h:30
@ kDouble32_t
Definition: TDataType.h:31
@ kShort_t
Definition: TDataType.h:29
@ kBool_t
Definition: TDataType.h:32
@ kDataTypeAliasSignedChar_t
Definition: TDataType.h:38
@ kULong_t
Definition: TDataType.h:30
@ kLong64_t
Definition: TDataType.h:32
@ kVoid_t
Definition: TDataType.h:35
@ kUShort_t
Definition: TDataType.h:29
@ kDouble_t
Definition: TDataType.h:31
@ kCharStar
Definition: TDataType.h:34
@ kChar_t
Definition: TDataType.h:29
@ kUChar_t
Definition: TDataType.h:29
@ kDataTypeAliasUnsigned_t
Definition: TDataType.h:37
@ kUInt_t
Definition: TDataType.h:30
@ kFloat16_t
Definition: TDataType.h:33
@ kOther_t
Definition: TDataType.h:32
@ kIsFundamental
Definition: TDictionary.h:70
int type
Definition: TGX11.cxx:121
R__EXTERN TVirtualMutex * gInterpreterMutex
Definition: TInterpreter.h:44
R__EXTERN TInterpreter * gCling
Definition: TInterpreter.h:561
#define gInterpreter
Definition: TInterpreter.h:560
#define R__LOCKGUARD(mutex)
Collection abstract base class.
Definition: TCollection.h:63
virtual void Add(TObject *obj)=0
Basic data type descriptor (datatype information is obtained from CINT).
Definition: TDataType.h:44
Int_t GetType() const
Definition: TDataType.h:68
void CheckInfo()
Refresh the underlying information.
Definition: TDataType.cxx:378
virtual ~TDataType()
TDataType dtor deletes adopted CINT TypedefInfo object.
Definition: TDataType.cxx:105
TypedefInfo_t * fInfo
Definition: TDataType.h:47
static TDataType * fgBuiltins[kNumDataTypes]
Definition: TDataType.h:54
Long_t Property() const
Get property description word. For meaning of bits see EProperty.
Definition: TDataType.cxx:287
const char * GetFullTypeName() const
Get full type description of typedef, e,g.: "class TDirectory*".
Definition: TDataType.cxx:175
Int_t fTypeNameLen
Definition: TDataType.h:53
Int_t fSize
pointer to CINT typedef info
Definition: TDataType.h:48
Long_t fProperty
Definition: TDataType.h:50
const char * AsString(void *buf) const
Return string containing value in buffer formatted according to the basic data type.
Definition: TDataType.cxx:235
TString GetTypeName()
Get basic type of typedef, e,g.
Definition: TDataType.cxx:149
EDataType fType
Definition: TDataType.h:49
TDataType(const TDataType &)
copy constructor
Definition: TDataType.cxx:72
static void AddBuiltins(TCollection *types)
Create the TDataType objects for builtins.
Definition: TDataType.cxx:411
static TDataType * GetDataType(EDataType type)
Given a EDataType type, get the TDataType* that represents it.
Definition: TDataType.cxx:443
TDataType & operator=(const TDataType &)
assignment operator
Definition: TDataType.cxx:86
TString fTrueName
Definition: TDataType.h:51
void SetType(const char *name)
Set type id depending on name.
Definition: TDataType.cxx:296
Int_t Size() const
Get size of basic typedef'ed type.
Definition: TDataType.cxx:369
Int_t fTypeNameIdx
Definition: TDataType.h:52
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:167
TDictionary & operator=(const TDictionary &other)
Definition: TDictionary.cxx:62
virtual TypedefInfo_t * TypedefInfo_FactoryCopy(TypedefInfo_t *) const
Definition: TInterpreter.h:540
virtual Long_t TypedefInfo_Property(TypedefInfo_t *) const
Definition: TInterpreter.h:544
virtual Bool_t TypedefInfo_IsValid(TypedefInfo_t *) const
Definition: TInterpreter.h:542
virtual void TypedefInfo_Delete(TypedefInfo_t *) const
Definition: TInterpreter.h:537
virtual void TypedefInfo_Init(TypedefInfo_t *, const char *) const
Definition: TInterpreter.h:541
virtual const char * TypedefInfo_Name(TypedefInfo_t *) const
Definition: TInterpreter.h:547
virtual const char * TypedefInfo_Title(TypedefInfo_t *) const
Definition: TInterpreter.h:548
virtual const char * TypedefInfo_TrueName(TypedefInfo_t *) const
Definition: TInterpreter.h:546
virtual int TypedefInfo_Size(TypedefInfo_t *) const
Definition: TInterpreter.h:545
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
TString fName
Definition: TNamed.h:32
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:893
Basic string class.
Definition: TString.h:136
Ssiz_t Length() const
Definition: TString.h:410
const char * Data() const
Definition: TString.h:369
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:639
long long Long64_t
Definition: cpp_cppyy.h:13
unsigned long long ULong64_t
Definition: cpp_cppyy.h:14
TLine * line
static const std::string name("name")