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 "TInterpreter.h"
21#include "TCollection.h"
22#include "TVirtualMutex.h"
23#include "ThreadLocalStorage.h"
24#ifdef R__SOLARIS
25#include <typeinfo>
26#endif
27
28
30
31////////////////////////////////////////////////////////////////////////////////
32/// Default TDataType ctor. TDataTypes are constructed in TROOT via
33/// a call to TCling::UpdateListOfTypes().
34
36 fTypeNameIdx(-1), fTypeNameLen(0)
37{
38 fInfo = info;
39
40 if (fInfo) {
47 } else {
48 SetTitle("Builtin basic type");
49 fProperty = 0;
50 fSize = 0;
52 }
53}
54
55////////////////////////////////////////////////////////////////////////////////
56/// Constructor for basic data types, like "char", "unsigned char", etc.
57
58TDataType::TDataType(const char *typenam) : fInfo(nullptr), fProperty(kIsFundamental),
59 fTypeNameIdx(-1), fTypeNameLen(0)
60{
61 fInfo = nullptr;
63 SetTitle("Builtin basic type");
64
66}
67
68////////////////////////////////////////////////////////////////////////////////
69///copy constructor
70
73 fInfo(gCling->TypedefInfo_FactoryCopy(dt.fInfo)),
74 fSize(dt.fSize),
75 fType(dt.fType),
76 fProperty(dt.fProperty),
77 fTrueName(dt.fTrueName),
78 fTypeNameIdx(dt.fTypeNameIdx), fTypeNameLen(dt.fTypeNameLen)
79{
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/// assignment operator
84
86{
87 if(this!=&dt) {
91 fSize=dt.fSize;
92 fType=dt.fType;
93 fProperty=dt.fProperty;
94 fTrueName=dt.fTrueName;
95 fTypeNameIdx=dt.fTypeNameIdx;
96 fTypeNameLen=dt.fTypeNameLen;
97 }
98 return *this;
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// TDataType dtor deletes adopted CINT TypedefInfo object.
103
108
109////////////////////////////////////////////////////////////////////////////////
110/// Return the name of the type.
111
113{
114 switch (type) {
115 case 1: return "Char_t";
116 case 2: return "Short_t";
117 case 3: return "Int_t";
118 case 4: return "Long_t";
119 case 5: return "Float_t";
120 case 6: return "Int_t";
121 case 7: return "char*";
122 case 8: return "Double_t";
123 case 9: return "Double32_t";
124 case 11: return "UChar_t";
125 case 12: return "UShort_t";
126 case 13: return "UInt_t";
127 case 14: return "ULong_t";
128 case 15: return "UInt_t";
129 case 16: return "Long64_t";
130 case 17: return "ULong64_t";
131 case 18: return "Bool_t";
132 case 19: return "Float16_t";
133 case kVoid_t: return "void";
134 case kDataTypeAliasUnsigned_t: return "UInt_t";
135 case kDataTypeAliasSignedChar_t: return "SignedChar_t";
136 case kOther_t: return "";
137 case kNoType_t: return "";
138 case kchar: return "Char_t";
139 default: return "";
140 }
141 return ""; // to silence compilers
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Get basic type of typedef, e,g.: "class TDirectory*" -> "TDirectory".
146/// Result needs to be used or copied immediately.
147
149{
150 if (fTypeNameLen) {
152 }
153
154 if (fInfo) {
155 (const_cast<TDataType*>(this))->CheckInfo();
156 TString typeName = gInterpreter->TypeName(fTrueName.Data());
157 fTypeNameIdx = fTrueName.Index(typeName);
158 if (fTypeNameIdx == -1) {
159 Error("GetTypeName", "Cannot find type name %s in true name %s!",
160 typeName.Data(), fTrueName.Data());
161 return fName;
162 }
163 fTypeNameLen = typeName.Length();
165 } else {
166 if (fType != kOther_t) return fName.Data();
167 return fTrueName;
168 }
169}
170
171////////////////////////////////////////////////////////////////////////////////
172/// Get full type description of typedef, e,g.: "class TDirectory*".
173
174const char *TDataType::GetFullTypeName() const
175{
176 if (fInfo) {
177 (const_cast<TDataType*>(this))->CheckInfo();
178 return fTrueName;
179 } else {
180 if (fType != kOther_t) return fName;
181 return fTrueName;
182 }
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Set type id depending on name.
187
189{
191
192 if (typeid(unsigned int) == typeinfo) {
194 } else if (typeid(int) == typeinfo) {
195 retType = kInt_t;
196 } else if (typeid(ULong_t) == typeinfo) {
198 } else if (typeid(Long_t) == typeinfo) {
200 } else if (typeid(ULong64_t) == typeinfo) {
202 } else if (typeid(Long64_t) == typeinfo) {
204 } else if (typeid(unsigned short) == typeinfo) {
206 } else if (typeid(short) == typeinfo) {
208 } else if (typeid(unsigned char) == typeinfo) {
210 } else if (typeid(char) == typeinfo) {
212 } else if (typeid(Bool_t) == typeinfo) {
214 } else if (typeid(float) == typeinfo) {
216 } else if (typeid(Float16_t) == typeinfo) {
218 } else if (typeid(double) == typeinfo) {
220 } else if (typeid(Double32_t) == typeinfo) {
222 } else if (typeid(char*) == typeinfo) {
224 } else if (typeid(signed char) == typeinfo) {
226 }
227 return retType;
228}
229
230////////////////////////////////////////////////////////////////////////////////
231/// Return string containing value in buffer formatted according to
232/// the basic data type. The result needs to be used or copied immediately.
233
234const char *TDataType::AsString(void *buf) const
235{
237 const char *name;
238
239 if (fInfo) {
240 (const_cast<TDataType*>(this))->CheckInfo();
241 name = fTrueName;
242 } else {
243 name = fName.Data();
244 }
245
246 line[0] = 0;
247 if (!strcmp("unsigned int", name))
248 line.Form( "%u", *(unsigned int *)buf);
249 else if (!strcmp("unsigned", name))
250 line.Form( "%u", *(unsigned int *)buf);
251 else if (!strcmp("int", name))
252 line.Form( "%d", *(int *)buf);
253 else if (!strcmp("unsigned long", name))
254 line.Form( "%lu", *(ULong_t *)buf);
255 else if (!strcmp("long", name))
256 line.Form( "%ld", *(Long_t *)buf);
257 else if (!strcmp("unsigned long long", name))
258 line.Form( "%llu", *(ULong64_t *)buf);
259 else if (!strcmp("ULong64_t", name))
260 line.Form( "%llu", *(ULong64_t *)buf);
261 else if (!strcmp("long long", name))
262 line.Form( "%lld", *(Long64_t *)buf);
263 else if (!strcmp("Long64_t", name))
264 line.Form( "%lld", *(Long64_t *)buf);
265 else if (!strcmp("unsigned short", name))
266 line.Form( "%hu", *(unsigned short *)buf);
267 else if (!strcmp("short", name))
268 line.Form( "%hd", *(short *)buf);
269 else if (!strcmp("bool", name))
270 line.Form( "%s", *(Bool_t *)buf ? "true" : "false");
271 else if (!strcmp("unsigned char", name) || !strcmp("char", name) ) {
272 line = *(char*)buf;
273 } else if (!strcmp("float", name))
274 line.Form( "%g", *(float *)buf);
275 else if (!strcmp("Float16_t", name))
276 line.Form( "%g", *(float *)buf);
277 else if (!strcmp("double", name))
278 line.Form( "%g", *(double *)buf);
279 else if (!strcmp("Double32_t", name))
280 line.Form( "%g", *(double *)buf);
281 else if (!strcmp("char*", name))
282 line.Form( "%s", *(char**)buf);
283
284 return line;
285}
286
287////////////////////////////////////////////////////////////////////////////////
288/// Get property description word. For meaning of bits see EProperty.
289
291{
292 if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
293 return fProperty;
294}
295
296////////////////////////////////////////////////////////////////////////////////
297/// Set type id depending on name.
298
299void TDataType::SetType(const char *name)
300{
301 fTrueName = name;
302 fType = kOther_t;
303 fSize = 0;
304
305 if (name==nullptr) {
306 return;
307 } else if (!strcmp("unsigned int", name)) {
308 fType = kUInt_t;
309 fSize = sizeof(UInt_t);
310 } else if (!strcmp("unsigned", name)) {
311 fType = kUInt_t;
312 fSize = sizeof(UInt_t);
313 } else if (!strcmp("int", name)) {
314 fType = kInt_t;
315 fSize = sizeof(Int_t);
316 } else if (!strcmp("unsigned long", name)) {
317 fType = kULong_t;
318 fSize = sizeof(ULong_t);
319 } else if (!strcmp("long", name)) {
320 fType = kLong_t;
321 fSize = sizeof(Long_t);
322 } else if (!strcmp("unsigned long long", name) || !strcmp("ULong64_t",name)) {
324 fSize = sizeof(ULong64_t);
325 } else if (!strcmp("long long", name) || !strcmp("Long64_t",name)) {
327 fSize = sizeof(Long64_t);
328 } else if (!strcmp("unsigned short", name)) {
330 fSize = sizeof(UShort_t);
331 } else if (!strcmp("short", name)) {
332 fType = kShort_t;
333 fSize = sizeof(Short_t);
334 } else if (!strcmp("unsigned char", name)) {
335 fType = kUChar_t;
336 fSize = sizeof(UChar_t);
337 } else if (!strcmp("char", name)) {
338 fType = kChar_t;
339 fSize = sizeof(Char_t);
340 } else if (!strcmp("bool", name)) {
341 fType = kBool_t;
342 fSize = sizeof(Bool_t);
343 } else if (!strcmp("float", name)) {
344 fType = kFloat_t;
345 fSize = sizeof(Float_t);
346 } else if (!strcmp("double", name)) {
348 fSize = sizeof(Double_t);
349 } else if (!strcmp("signed char", name)) {
350 fType = kChar_t; // kDataTypeAliasSignedChar_t;
351 fSize = sizeof(Char_t);
352 } else if (!strcmp("void", name)) {
353 fType = kVoid_t;
354 fSize = 0;
355 }
356
357 if (!strcmp("Float16_t", fName.Data())) {
358 fSize = sizeof(Float16_t);
360 }
361 if (!strcmp("Double32_t", fName.Data())) {
362 fSize = sizeof(Double32_t);
364 }
365 if (!strcmp("char*",fName.Data())) {
367 }
368 // kCounter = 6, kBits = 15
369}
370
371////////////////////////////////////////////////////////////////////////////////
372/// Get size of basic typedef'ed type.
373
375{
376 if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
377 return fSize;
378}
379
380////////////////////////////////////////////////////////////////////////////////
381/// Refresh the underlying information.
382
384{
385 // This can be needed if the library defining this typedef was loaded after
386 // another library and that this other library is unloaded (in which case
387 // things can get renumbered inside CINT).
388
389 if (!fInfo) return;
390
391 // This intentionally cast the constness away so that
392 // we can call CheckInfo from const data members.
394
397
398 // The fInfo is invalid or does not
399 // point to this typedef anymore, let's
400 // refresh it
401
403
404 if (!gCling->TypedefInfo_IsValid(fInfo)) return;
405
410 }
411}
412
413////////////////////////////////////////////////////////////////////////////////
414/// Create the TDataType objects for builtins.
415
417{
418 if (fgBuiltins[kChar_t] == nullptr) {
419 // Add also basic types (like a identity typedef "typedef int int")
420 fgBuiltins[kChar_t] = new TDataType("char");
421 fgBuiltins[kUChar_t] = new TDataType("unsigned char");
422 fgBuiltins[kShort_t] = new TDataType("short");
423 fgBuiltins[kUShort_t] = new TDataType("unsigned short");
424 fgBuiltins[kInt_t] = new TDataType("int");
425 fgBuiltins[kUInt_t] = new TDataType("unsigned int");
426 fgBuiltins[kLong_t] = new TDataType("long");
427 fgBuiltins[kULong_t] = new TDataType("unsigned long");
428 fgBuiltins[kLong64_t] = new TDataType("long long");
429 fgBuiltins[kULong64_t] = new TDataType("unsigned long long");
430 fgBuiltins[kFloat_t] = new TDataType("float");
431 fgBuiltins[kDouble_t] = new TDataType("double");
432 fgBuiltins[kFloat16_t] = new TDataType("Float16_t");
433 fgBuiltins[kDouble32_t] = new TDataType("Double32_t");
434 fgBuiltins[kVoid_t] = new TDataType("void");
435 fgBuiltins[kBool_t] = new TDataType("bool");
436 fgBuiltins[kCharStar] = new TDataType("char*");
437
439 fgBuiltins[kDataTypeAliasSignedChar_t] = new TDataType("signed char");
440 }
441
442 for (Int_t i = 0; i < (Int_t)kNumDataTypes; ++i) {
443 if (fgBuiltins[i]) types->Add(fgBuiltins[i]);
444 }
445}
446
447////////////////////////////////////////////////////////////////////////////////
448/// Given a EDataType type, get the TDataType* that represents it.
449
451{
452 if (type == kOther_t || type >= kNumDataTypes) return nullptr;
453 return fgBuiltins[(int)type];
454}
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
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:110
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:68
void CheckInfo()
Refresh the underlying information.
virtual ~TDataType()
TDataType dtor deletes adopted CINT TypedefInfo object.
TypedefInfo_t * fInfo
Definition TDataType.h:47
static TDataType * fgBuiltins[kNumDataTypes]
Definition TDataType.h:29
const char * GetFullTypeName() const
Get full type description of typedef, e,g.: "class TDirectory*".
Int_t fTypeNameLen
Definition TDataType.h:53
Int_t fSize
pointer to CINT typedef info
Definition TDataType.h:48
Long_t Property() const override
Get property description word. For meaning of bits see EProperty.
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.
TString GetTypeName()
Get basic type of typedef, e,g.: "class TDirectory*" -> "TDirectory".
EDataType fType
Definition TDataType.h:49
TDataType(const TDataType &)
copy constructor
Definition TDataType.cxx:71
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:85
TString fTrueName
Definition TDataType.h:51
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:52
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:1071
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:659
TLine * line