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
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("Float16_t", name))
277 line.Form( "%g", *(float *)buf);
278 else if (!strcmp("double", name))
279 line.Form( "%g", *(double *)buf);
280 else if (!strcmp("Double32_t", name))
281 line.Form( "%g", *(double *)buf);
282 else if (!strcmp("char*", name))
283 line.Form( "%s", *(char**)buf);
284
285 return line;
286}
287
288////////////////////////////////////////////////////////////////////////////////
289/// Get property description word. For meaning of bits see EProperty.
290
292{
293 if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
294 return fProperty;
295}
296
297////////////////////////////////////////////////////////////////////////////////
298/// Set type id depending on name.
299
300void TDataType::SetType(const char *name)
301{
302 fTrueName = name;
303 fType = kOther_t;
304 fSize = 0;
305
306 if (name==nullptr) {
307 return;
308 } else if (!strcmp("unsigned int", name)) {
309 fType = kUInt_t;
310 fSize = sizeof(UInt_t);
311 } else if (!strcmp("unsigned", name)) {
312 fType = kUInt_t;
313 fSize = sizeof(UInt_t);
314 } else if (!strcmp("int", name)) {
315 fType = kInt_t;
316 fSize = sizeof(Int_t);
317 } else if (!strcmp("unsigned long", name)) {
318 fType = kULong_t;
319 fSize = sizeof(ULong_t);
320 } else if (!strcmp("long", name)) {
321 fType = kLong_t;
322 fSize = sizeof(Long_t);
323 } else if (!strcmp("unsigned long long", name) || !strcmp("ULong64_t",name)) {
325 fSize = sizeof(ULong64_t);
326 } else if (!strcmp("long long", name) || !strcmp("Long64_t",name)) {
328 fSize = sizeof(Long64_t);
329 } else if (!strcmp("unsigned short", name)) {
331 fSize = sizeof(UShort_t);
332 } else if (!strcmp("short", name)) {
333 fType = kShort_t;
334 fSize = sizeof(Short_t);
335 } else if (!strcmp("unsigned char", name)) {
336 fType = kUChar_t;
337 fSize = sizeof(UChar_t);
338 } else if (!strcmp("char", name)) {
339 fType = kChar_t;
340 fSize = sizeof(Char_t);
341 } else if (!strcmp("bool", name)) {
342 fType = kBool_t;
343 fSize = sizeof(Bool_t);
344 } else if (!strcmp("float", name)) {
345 fType = kFloat_t;
346 fSize = sizeof(Float_t);
347 } else if (!strcmp("double", name)) {
349 fSize = sizeof(Double_t);
350 } else if (!strcmp("signed char", name)) {
351 fType = kChar_t; // kDataTypeAliasSignedChar_t;
352 fSize = sizeof(Char_t);
353 } else if (!strcmp("void", name)) {
354 fType = kVoid_t;
355 fSize = 0;
356 }
357
358 if (!strcmp("Float16_t", fName.Data())) {
359 fSize = sizeof(Float16_t);
361 }
362 if (!strcmp("Double32_t", fName.Data())) {
363 fSize = sizeof(Double32_t);
365 }
366 if (!strcmp("char*",fName.Data())) {
368 }
369 // kCounter = 6, kBits = 15
370}
371
372////////////////////////////////////////////////////////////////////////////////
373/// Get size of basic typedef'ed type.
374
376{
377 if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
378 return fSize;
379}
380
381////////////////////////////////////////////////////////////////////////////////
382/// Refresh the underlying information.
383
385{
386 // This can be needed if the library defining this typedef was loaded after
387 // another library and that this other library is unloaded (in which case
388 // things can get renumbered inside CINT).
389
390 if (!fInfo) return;
391
392 // This intentionally cast the constness away so that
393 // we can call CheckInfo from const data members.
395
397 strcmp(gCling->TypedefInfo_Name(fInfo),fName.Data())!=0) {
398
399 // The fInfo is invalid or does not
400 // point to this typedef anymore, let's
401 // refresh it
402
404
405 if (!gCling->TypedefInfo_IsValid(fInfo)) return;
406
411 }
412}
413
414////////////////////////////////////////////////////////////////////////////////
415/// Create the TDataType objects for builtins.
416
418{
419 if (fgBuiltins[kChar_t] == nullptr) {
420 // Add also basic types (like a identity typedef "typedef int int")
421 fgBuiltins[kChar_t] = new TDataType("char");
422 fgBuiltins[kUChar_t] = new TDataType("unsigned char");
423 fgBuiltins[kShort_t] = new TDataType("short");
424 fgBuiltins[kUShort_t] = new TDataType("unsigned short");
425 fgBuiltins[kInt_t] = new TDataType("int");
426 fgBuiltins[kUInt_t] = new TDataType("unsigned int");
427 fgBuiltins[kLong_t] = new TDataType("long");
428 fgBuiltins[kULong_t] = new TDataType("unsigned long");
429 fgBuiltins[kLong64_t] = new TDataType("long long");
430 fgBuiltins[kULong64_t] = new TDataType("unsigned long long");
431 fgBuiltins[kFloat_t] = new TDataType("float");
432 fgBuiltins[kDouble_t] = new TDataType("double");
433 fgBuiltins[kFloat16_t] = new TDataType("Float16_t");
434 fgBuiltins[kDouble32_t] = new TDataType("Double32_t");
435 fgBuiltins[kVoid_t] = new TDataType("void");
436 fgBuiltins[kBool_t] = new TDataType("bool");
437 fgBuiltins[kCharStar] = new TDataType("char*");
438
440 fgBuiltins[kDataTypeAliasSignedChar_t] = new TDataType("signed char");
441 }
442
443 for (Int_t i = 0; i < (Int_t)kNumDataTypes; ++i) {
444 if (fgBuiltins[i]) types->Add(fgBuiltins[i]);
445 }
446}
447
448////////////////////////////////////////////////////////////////////////////////
449/// Given a EDataType type, get the TDataType* that represents it.
450
452{
453 if (type == kOther_t || type >= kNumDataTypes) return nullptr;
454 return fgBuiltins[(int)type];
455}
dim_t fSize
float Float16_t
Definition RtypesCore.h:58
bool Bool_t
Definition RtypesCore.h:63
unsigned short UShort_t
Definition RtypesCore.h:40
int Int_t
Definition RtypesCore.h:45
double Double32_t
Definition RtypesCore.h:60
unsigned char UChar_t
Definition RtypesCore.h:38
char Char_t
Definition RtypesCore.h:37
unsigned long ULong_t
Definition RtypesCore.h:55
long Long_t
Definition RtypesCore.h:54
unsigned int UInt_t
Definition RtypesCore.h:46
float Float_t
Definition RtypesCore.h:57
short Short_t
Definition RtypesCore.h:39
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:69
unsigned long long ULong64_t
Definition RtypesCore.h:70
#define ClassImp(name)
Definition Rtypes.h:382
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
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.
virtual ~TDataType()
TDataType dtor deletes adopted CINT TypedefInfo object.
TypedefInfo_t * fInfo
Definition TDataType.h:47
static TDataType * fgBuiltins[kNumDataTypes]
Definition TDataType.h:54
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:72
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:86
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: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:1005
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
const char * Data() const
Definition TString.h:376
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
TLine * line