Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "TError.h"
21#include "TInterpreter.h"
22#include "TCollection.h"
23#include "TVirtualMutex.h"
24#include "ThreadLocalStorage.h"
25#include <limits>
26#ifdef R__SOLARIS
27#include <typeinfo>
28#endif
29
30
32
33////////////////////////////////////////////////////////////////////////////////
34/// Default TDataType ctor. TDataTypes are constructed in TROOT via
35/// a call to TCling::UpdateListOfTypes().
36
38 fTypeNameIdx(-1), fTypeNameLen(0)
39{
40 fInfo = info;
41
42 if (fInfo) {
46 // SetType sets fTrueName, fType , fSize and fAlignOf.
50 } else {
51 SetTitle("Builtin basic type");
52 fProperty = 0;
53 fSize = 0;
54 fAlignOf = 0;
56 }
57}
58
59////////////////////////////////////////////////////////////////////////////////
60/// Constructor for basic data types, like "char", "unsigned char", etc.
61
62TDataType::TDataType(const char *typenam) : fInfo(nullptr), fProperty(kIsFundamental),
63 fTypeNameIdx(-1), fTypeNameLen(0)
64{
65 fInfo = nullptr;
67 SetTitle("Builtin basic type");
68
70}
71
72////////////////////////////////////////////////////////////////////////////////
73///copy constructor
74
76 : TDictionary(dt),
77 fInfo(gCling->TypedefInfo_FactoryCopy(dt.fInfo)),
78 fSize(dt.fSize),
79 fAlignOf(dt.fAlignOf),
80 fType(dt.fType),
81 fProperty(dt.fProperty),
82 fTrueName(dt.fTrueName),
83 fTypeNameIdx(dt.fTypeNameIdx),
84 fTypeNameLen(dt.fTypeNameLen)
85{
86}
87
88////////////////////////////////////////////////////////////////////////////////
89/// assignment operator
90
92{
93 if(this!=&dt) {
97 fSize=dt.fSize;
98 fAlignOf=dt.fAlignOf;
99 fType=dt.fType;
100 fProperty=dt.fProperty;
101 fTrueName=dt.fTrueName;
102 fTypeNameIdx=dt.fTypeNameIdx;
103 fTypeNameLen=dt.fTypeNameLen;
104 }
105 return *this;
106}
107
108////////////////////////////////////////////////////////////////////////////////
109/// TDataType dtor deletes adopted CINT TypedefInfo object.
110
115
116////////////////////////////////////////////////////////////////////////////////
117/// Return the name of the type.
118
120{
121 switch (type) {
122 case 1: return "Char_t";
123 case 2: return "Short_t";
124 case 3: return "Int_t";
125 case 4: return "Long_t";
126 case 5: return "Float_t";
127 case 6: return "Int_t";
128 case 7: return "char*";
129 case 8: return "Double_t";
130 case 9: return "Double32_t";
131 case 11: return "UChar_t";
132 case 12: return "UShort_t";
133 case 13: return "UInt_t";
134 case 14: return "ULong_t";
135 case 15: return "UInt_t";
136 case 16: return "Long64_t";
137 case 17: return "ULong64_t";
138 case 18: return "Bool_t";
139 case 19: return "Float16_t";
140 case kVoid_t: return "void";
141 case kDataTypeAliasUnsigned_t: return "UInt_t";
142 case kDataTypeAliasSignedChar_t: return "SignedChar_t";
143 case kOther_t: return "";
144 case kNoType_t: return "";
145 case kchar: return "Char_t";
146 default: return "";
147 }
148 return ""; // to silence compilers
149}
150
151////////////////////////////////////////////////////////////////////////////////
152/// Get basic type of typedef, e,g.: "class TDirectory*" -> "TDirectory".
153/// Result needs to be used or copied immediately.
154
156{
157 if (fTypeNameLen) {
159 }
160
161 if (fInfo) {
162 (const_cast<TDataType*>(this))->CheckInfo();
163 TString typeName = gInterpreter->TypeName(fTrueName.Data());
164 fTypeNameIdx = fTrueName.Index(typeName);
165 if (fTypeNameIdx == -1) {
166 Error("GetTypeName", "Cannot find type name %s in true name %s!",
167 typeName.Data(), fTrueName.Data());
168 return fName;
169 }
170 fTypeNameLen = typeName.Length();
172 } else {
173 if (fType != kOther_t) return fName.Data();
174 return fTrueName;
175 }
176}
177
178////////////////////////////////////////////////////////////////////////////////
179/// Get full type description of typedef, e,g.: "class TDirectory*".
180
181const char *TDataType::GetFullTypeName() const
182{
183 if (fInfo) {
184 (const_cast<TDataType*>(this))->CheckInfo();
185 return fTrueName;
186 } else {
187 if (fType != kOther_t) return fName;
188 return fTrueName;
189 }
190}
191
192////////////////////////////////////////////////////////////////////////////////
193/// Set type id depending on name.
194
196{
198
199 if (typeid(unsigned int) == typeinfo) {
201 } else if (typeid(int) == typeinfo) {
202 retType = kInt_t;
203 } else if (typeid(ULong_t) == typeinfo) {
205 } else if (typeid(Long_t) == typeinfo) {
207 } else if (typeid(ULong64_t) == typeinfo) {
209 } else if (typeid(Long64_t) == typeinfo) {
211 } else if (typeid(unsigned short) == typeinfo) {
213 } else if (typeid(short) == typeinfo) {
215 } else if (typeid(unsigned char) == typeinfo) {
217 } else if (typeid(char) == typeinfo) {
219 } else if (typeid(Bool_t) == typeinfo) {
221 } else if (typeid(float) == typeinfo) {
223 } else if (typeid(Float16_t) == typeinfo) {
225 } else if (typeid(double) == typeinfo) {
227 } else if (typeid(Double32_t) == typeinfo) {
229 } else if (typeid(char*) == typeinfo) {
231 } else if (typeid(signed char) == typeinfo) {
233 }
234 return retType;
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Return string containing value in buffer formatted according to
239/// the basic data type. The result needs to be used or copied immediately.
240
241const char *TDataType::AsString(void *buf) const
242{
244 const char *name;
245
246 if (fInfo) {
247 (const_cast<TDataType*>(this))->CheckInfo();
248 name = fTrueName;
249 } else {
250 name = fName.Data();
251 }
252
253 line[0] = 0;
254 if (!strcmp("unsigned int", name))
255 line.Form( "%u", *(unsigned int *)buf);
256 else if (!strcmp("unsigned", name))
257 line.Form( "%u", *(unsigned int *)buf);
258 else if (!strcmp("int", name))
259 line.Form( "%d", *(int *)buf);
260 else if (!strcmp("unsigned long", name))
261 line.Form( "%lu", *(ULong_t *)buf);
262 else if (!strcmp("long", name))
263 line.Form( "%ld", *(Long_t *)buf);
264 else if (!strcmp("unsigned long long", name))
265 line.Form( "%llu", *(ULong64_t *)buf);
266 else if (!strcmp("ULong64_t", name))
267 line.Form( "%llu", *(ULong64_t *)buf);
268 else if (!strcmp("long long", name))
269 line.Form( "%lld", *(Long64_t *)buf);
270 else if (!strcmp("Long64_t", name))
271 line.Form( "%lld", *(Long64_t *)buf);
272 else if (!strcmp("unsigned short", name))
273 line.Form( "%hu", *(unsigned short *)buf);
274 else if (!strcmp("short", name))
275 line.Form( "%hd", *(short *)buf);
276 else if (!strcmp("bool", name))
277 line.Form( "%s", *(Bool_t *)buf ? "true" : "false");
278 else if (!strcmp("unsigned char", name) || !strcmp("char", name) ) {
279 line = *(char*)buf;
280 } else if (!strcmp("float", name))
281 line.Form( "%g", *(float *)buf);
282 else if (!strcmp("Float16_t", name))
283 line.Form( "%g", *(float *)buf);
284 else if (!strcmp("double", name))
285 line.Form( "%g", *(double *)buf);
286 else if (!strcmp("Double32_t", name))
287 line.Form( "%g", *(double *)buf);
288 else if (!strcmp("char*", name))
289 line.Form( "%s", *(char**)buf);
290
291 return line;
292}
293
294////////////////////////////////////////////////////////////////////////////////
295/// Get property description word. For meaning of bits see EProperty.
296
298{
299 if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
300 return fProperty;
301}
302
303////////////////////////////////////////////////////////////////////////////////
304/// Set type id depending on name.
305
306void TDataType::SetType(const char *name)
307{
308 fTrueName = name;
309 fType = kOther_t;
310 fSize = 0;
311 fAlignOf = 0;
312
313 // Assigns an alignof() result to fAlignOf after verifying it fits in unsigned int.
314 // All standard C++ alignments are small powers-of-two; the assert is a safety
315 // net against exotic future platforms.
316 auto setAlignOf = [this](std::size_t al) {
317 R__ASSERT(al <= static_cast<std::size_t>(std::numeric_limits<unsigned int>::max()) &&
318 "alignof value exceeds unsigned int range");
319 fAlignOf = static_cast<unsigned int>(al);
320 };
321
322 if (name==nullptr) {
323 return;
324 } else if (!strcmp("unsigned int", name)) {
325 fType = kUInt_t;
326 fSize = sizeof(UInt_t);
327 setAlignOf(alignof(UInt_t));
328 } else if (!strcmp("unsigned", name)) {
329 fType = kUInt_t;
330 fSize = sizeof(UInt_t);
331 setAlignOf(alignof(UInt_t));
332 } else if (!strcmp("int", name)) {
333 fType = kInt_t;
334 fSize = sizeof(Int_t);
335 setAlignOf(alignof(Int_t));
336 } else if (!strcmp("unsigned long", name)) {
337 fType = kULong_t;
338 fSize = sizeof(ULong_t);
339 setAlignOf(alignof(ULong_t));
340 } else if (!strcmp("long", name)) {
341 fType = kLong_t;
342 fSize = sizeof(Long_t);
343 setAlignOf(alignof(Long_t));
344 } else if (!strcmp("unsigned long long", name) || !strcmp("ULong64_t",name)) {
346 fSize = sizeof(ULong64_t);
347 setAlignOf(alignof(ULong64_t));
348 } else if (!strcmp("long long", name) || !strcmp("Long64_t",name)) {
350 fSize = sizeof(Long64_t);
351 setAlignOf(alignof(Long64_t));
352 } else if (!strcmp("unsigned short", name)) {
354 fSize = sizeof(UShort_t);
355 setAlignOf(alignof(UShort_t));
356 } else if (!strcmp("short", name)) {
357 fType = kShort_t;
358 fSize = sizeof(Short_t);
359 setAlignOf(alignof(Short_t));
360 } else if (!strcmp("unsigned char", name)) {
361 fType = kUChar_t;
362 fSize = sizeof(UChar_t);
363 setAlignOf(alignof(UChar_t));
364 } else if (!strcmp("char", name)) {
365 fType = kChar_t;
366 fSize = sizeof(Char_t);
367 setAlignOf(alignof(Char_t));
368 } else if (!strcmp("bool", name)) {
369 fType = kBool_t;
370 fSize = sizeof(Bool_t);
371 setAlignOf(alignof(Bool_t));
372 } else if (!strcmp("float", name)) {
373 fType = kFloat_t;
374 fSize = sizeof(Float_t);
375 setAlignOf(alignof(Float_t));
376 } else if (!strcmp("double", name)) {
378 fSize = sizeof(Double_t);
379 setAlignOf(alignof(Double_t));
380 } else if (!strcmp("signed char", name)) {
381 fType = kChar_t; // kDataTypeAliasSignedChar_t;
382 fSize = sizeof(Char_t);
383 setAlignOf(alignof(Char_t));
384 } else if (!strcmp("void", name)) {
385 fType = kVoid_t;
386 fSize = 0;
387 fAlignOf = 0;
388 }
389
390 if (!strcmp("Float16_t", fName.Data())) {
391 fSize = sizeof(Float16_t);
392 setAlignOf(alignof(Float16_t));
394 }
395 if (!strcmp("Double32_t", fName.Data())) {
396 fSize = sizeof(Double32_t);
397 setAlignOf(alignof(Double32_t));
399 }
400 if (!strcmp("char*",fName.Data())) {
402 setAlignOf(alignof(char *));
403 }
404 // kCounter = 6, kBits = 15
405}
406
407////////////////////////////////////////////////////////////////////////////////
408/// Get size of basic typedef'ed type.
409
411{
412 if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
413 return fSize;
414}
415
416////////////////////////////////////////////////////////////////////////////////
417/// Refresh the underlying information.
418
420{
421 // This can be needed if the library defining this typedef was loaded after
422 // another library and that this other library is unloaded (in which case
423 // things can get renumbered inside CINT).
424
425 if (!fInfo) return;
426
427 // This intentionally cast the constness away so that
428 // we can call CheckInfo from const data members.
430
433
434 // The fInfo is invalid or does not
435 // point to this typedef anymore, let's
436 // refresh it
437
439
440 if (!gCling->TypedefInfo_IsValid(fInfo)) return;
441
446 }
447}
448
449////////////////////////////////////////////////////////////////////////////////
450/// Create the TDataType objects for builtins.
451
453{
454 if (fgBuiltins[kChar_t] == nullptr) {
455 // Add also basic types (like a identity typedef "typedef int int")
456 fgBuiltins[kChar_t] = new TDataType("char");
457 fgBuiltins[kUChar_t] = new TDataType("unsigned char");
458 fgBuiltins[kShort_t] = new TDataType("short");
459 fgBuiltins[kUShort_t] = new TDataType("unsigned short");
460 fgBuiltins[kInt_t] = new TDataType("int");
461 fgBuiltins[kUInt_t] = new TDataType("unsigned int");
462 fgBuiltins[kLong_t] = new TDataType("long");
463 fgBuiltins[kULong_t] = new TDataType("unsigned long");
464 fgBuiltins[kLong64_t] = new TDataType("long long");
465 fgBuiltins[kULong64_t] = new TDataType("unsigned long long");
466 fgBuiltins[kFloat_t] = new TDataType("float");
467 fgBuiltins[kDouble_t] = new TDataType("double");
468 fgBuiltins[kFloat16_t] = new TDataType("Float16_t");
469 fgBuiltins[kDouble32_t] = new TDataType("Double32_t");
470 fgBuiltins[kVoid_t] = new TDataType("void");
471 fgBuiltins[kBool_t] = new TDataType("bool");
472 fgBuiltins[kCharStar] = new TDataType("char*");
473
475 fgBuiltins[kDataTypeAliasSignedChar_t] = new TDataType("signed char");
476 }
477
478 for (Int_t i = 0; i < (Int_t)kNumDataTypes; ++i) {
479 if (fgBuiltins[i]) types->Add(fgBuiltins[i]);
480 }
481}
482
483////////////////////////////////////////////////////////////////////////////////
484/// Given a EDataType type, get the TDataType* that represents it.
485
487{
488 if (type == kOther_t || type >= kNumDataTypes) return nullptr;
489 return fgBuiltins[(int)type];
490}
dim_t fSize
float Float16_t
Float 4 bytes in memory, written to disk as 3 bytes (24-bits) by default or as a 4 bytes fixed-point-...
Definition RtypesCore.h:72
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
unsigned short UShort_t
Unsigned Short integer 2 bytes (unsigned short)
Definition RtypesCore.h:54
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
double Double32_t
Double 8 bytes in memory, written to disk as a 4 bytes Float_t (32-bits) by default,...
Definition RtypesCore.h:74
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char)
Definition RtypesCore.h:52
char Char_t
Character 1 byte (char)
Definition RtypesCore.h:51
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:53
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
Definition RtypesCore.h:84
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:145
R__EXTERN TVirtualMutex * gInterpreterMutex
R__EXTERN TInterpreter * gCling
#define gInterpreter
#define R__LOCKGUARD(mutex)
Collection abstract base class.
Definition TCollection.h:65
Basic data type descriptor (datatype information is obtained from CINT).
Definition TDataType.h:44
Int_t GetType() const
Definition TDataType.h:71
void CheckInfo()
Refresh the underlying information.
virtual ~TDataType()
TDataType dtor deletes adopted CINT TypedefInfo object.
TypedefInfo_t * fInfo
!pointer to CINT typedef info
Definition TDataType.h:47
static TDataType * fgBuiltins[kNumDataTypes]
Definition TDataType.h:31
const char * GetFullTypeName() const
Get full type description of typedef, e,g.: "class TDirectory*".
Int_t fTypeNameLen
Definition TDataType.h:54
Int_t fSize
Definition TDataType.h:48
Long_t Property() const override
Get property description word. For meaning of bits see EProperty.
UInt_t fAlignOf
Definition TDataType.h:49
Long_t fProperty
Definition TDataType.h:51
const char * AsString(void *buf) const
Return string containing value in buffer formatted according to the basic data type.
TString GetTypeName()
Get basic type of typedef, e,g.: "class TDirectory*" -> "TDirectory".
EDataType fType
Definition TDataType.h:50
TDataType(const TDataType &)
copy constructor
Definition TDataType.cxx:75
static void AddBuiltins(TCollection *types)
Create the TDataType objects for builtins.
static TDataType * GetDataType(EDataType type)
Given a EDataType type, get the TDataType* that represents it.
TDataType & operator=(const TDataType &)
assignment operator
Definition TDataType.cxx:91
TString fTrueName
Definition TDataType.h:52
void SetType(const char *name)
Set type id depending on name.
Int_t Size() const
Get size of basic typedef'ed type.
Int_t fTypeNameIdx
Definition TDataType.h:53
This class defines an abstract interface that must be implemented by all classes that contain diction...
TDictionary & operator=(const TDictionary &other)
virtual TypedefInfo_t * TypedefInfo_FactoryCopy(TypedefInfo_t *) const
virtual const char * TypedefInfo_TrueName(TypedefInfo_t *) const
virtual Long_t TypedefInfo_Property(TypedefInfo_t *) const
virtual Bool_t TypedefInfo_IsValid(TypedefInfo_t *) const
virtual void TypedefInfo_Delete(TypedefInfo_t *) const
virtual const char * TypedefInfo_Name(TypedefInfo_t *) const
virtual void TypedefInfo_Init(TypedefInfo_t *, const char *) const
virtual const char * TypedefInfo_Title(TypedefInfo_t *) const
virtual int TypedefInfo_Size(TypedefInfo_t *) const
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
TString fName
Definition TNamed.h:32
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1095
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
const char * Data() const
Definition TString.h:384
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:660
TLine * line