ROOT  6.06/09
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
13 Basic data type descriptor (datatype information is obtained from
14 CINT). This class describes the attributes of type definitions
15 (typedef's). The TROOT class contains a list of all currently
16 defined 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 
30 TDataType* TDataType::fgBuiltins[kNumDataTypes] = {0};
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 /// Default TDataType ctor. TDataTypes are constructed in TROOT via
34 /// a call to TCling::UpdateListOfTypes().
35 
36 TDataType::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;
52  fType = kNoType_t;
53  }
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Constructor for basic data types, like "char", "unsigned char", etc.
58 
59 TDataType::TDataType(const char *typenam) : fInfo(0), fProperty(kIsFundamental),
60  fTypeNameIdx(-1), fTypeNameLen(0)
61 {
62  fInfo = 0;
63  SetName(typenam);
64  SetTitle("Builtin basic type");
65 
66  SetType(fName.Data());
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 
175 const 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 
189 EDataType TDataType::GetType(const type_info &typeinfo)
190 {
191  EDataType retType = kOther_t;
192 
193  if (!strcmp(typeid(unsigned int).name(), typeinfo.name())) {
194  retType = kUInt_t;
195  } else if (!strcmp(typeid(int).name(), typeinfo.name())) {
196  retType = kInt_t;
197  } else if (!strcmp(typeid(ULong_t).name(), typeinfo.name())) {
198  retType = kULong_t;
199  } else if (!strcmp(typeid(Long_t).name(), typeinfo.name())) {
200  retType = kLong_t;
201  } else if (!strcmp(typeid(ULong64_t).name(), typeinfo.name())) {
202  retType = kULong64_t;
203  } else if (!strcmp(typeid(Long64_t).name(), typeinfo.name())) {
204  retType = kLong64_t;
205  } else if (!strcmp(typeid(unsigned short).name(), typeinfo.name())) {
206  retType = kUShort_t;
207  } else if (!strcmp(typeid(short).name(), typeinfo.name())) {
208  retType = kShort_t;
209  } else if (!strcmp(typeid(unsigned char).name(), typeinfo.name())) {
210  retType = kUChar_t;
211  } else if (!strcmp(typeid(char).name(), typeinfo.name())) {
212  retType = kChar_t;
213  } else if (!strcmp(typeid(Bool_t).name(), typeinfo.name())) {
214  retType = kBool_t;
215  } else if (!strcmp(typeid(float).name(), typeinfo.name())) {
216  retType = kFloat_t;
217  } else if (!strcmp(typeid(Float16_t).name(), typeinfo.name())) {
218  retType = kFloat16_t;
219  } else if (!strcmp(typeid(double).name(), typeinfo.name())) {
220  retType = kDouble_t;
221  } else if (!strcmp(typeid(Double32_t).name(), typeinfo.name())) {
222  retType = kDouble32_t;
223  } else if (!strcmp(typeid(char*).name(), typeinfo.name())) {
224  retType = kCharStar;
225  } else if (!strcmp(typeid(signed char).name(), typeinfo.name())) {
226  retType = kDataTypeAliasSignedChar_t;
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 
235 const 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 
296 void TDataType::SetType(const char *name)
297 {
298  fTrueName = name;
299  fType = kOther_t;
300  fSize = 0;
301 
302  if (name==0) {
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)) {
320  fType = kULong64_t;
321  fSize = sizeof(ULong64_t);
322  } else if (!strcmp("long long", name) || !strcmp("Long64_t",name)) {
323  fType = kLong64_t;
324  fSize = sizeof(Long64_t);
325  } else if (!strcmp("unsigned short", name)) {
326  fType = kUShort_t;
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)) {
344  fType = kDouble_t;
345  fSize = sizeof(Double_t);
346  } else if (!strcmp("signed char", name)) {
347  fType = kChar_t; // kDataTypeAliasSignedChar_t;
348  fSize = sizeof(Char_t);
349  }
350 
351  if (!strcmp("Float16_t", fName.Data())) {
352  fType = kFloat16_t;
353  }
354  if (!strcmp("Double32_t", fName.Data())) {
355  fType = kDouble32_t;
356  }
357  if (!strcmp("char*",fName.Data())) {
358  fType = kCharStar;
359  }
360  // kCounter = 6, kBits = 15
361 }
362 
363 ////////////////////////////////////////////////////////////////////////////////
364 /// Get size of basic typedef'ed type.
365 
367 {
368  if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
369  return fSize;
370 }
371 
372 ////////////////////////////////////////////////////////////////////////////////
373 /// Refresh the underlying information.
374 
376 {
377  // This can be needed if the library defining this typedef was loaded after
378  // another library and that this other library is unloaded (in which case
379  // things can get renumbered inside CINT).
380 
381  if (!fInfo) return;
382 
383  // This intentionally cast the constness away so that
384  // we can call CheckInfo from const data members.
386 
388  strcmp(gCling->TypedefInfo_Name(fInfo),fName.Data())!=0) {
389 
390  // The fInfo is invalid or does not
391  // point to this typedef anymore, let's
392  // refresh it
393 
395 
396  if (!gCling->TypedefInfo_IsValid(fInfo)) return;
397 
402  }
403 }
404 
405 ////////////////////////////////////////////////////////////////////////////////
406 /// Create the TDataType objects for builtins.
407 
409 {
410  if (fgBuiltins[kChar_t] == 0) {
411  // Add also basic types (like a identity typedef "typedef int int")
412  fgBuiltins[kChar_t] = new TDataType("char");
413  fgBuiltins[kUChar_t] = new TDataType("unsigned char");
414  fgBuiltins[kShort_t] = new TDataType("short");
415  fgBuiltins[kUShort_t] = new TDataType("unsigned short");
416  fgBuiltins[kInt_t] = new TDataType("int");
417  fgBuiltins[kUInt_t] = new TDataType("unsigned int");
418  fgBuiltins[kLong_t] = new TDataType("long");
419  fgBuiltins[kULong_t] = new TDataType("unsigned long");
420  fgBuiltins[kLong64_t] = new TDataType("long long");
421  fgBuiltins[kULong64_t] = new TDataType("unsigned long long");
422  fgBuiltins[kFloat_t] = new TDataType("float");
423  fgBuiltins[kDouble_t] = new TDataType("double");
424  fgBuiltins[kVoid_t] = new TDataType("void");
425  fgBuiltins[kBool_t] = new TDataType("bool");
426  fgBuiltins[kCharStar] = new TDataType("char*");
427 
428  fgBuiltins[kDataTypeAliasUnsigned_t] = new TDataType("unsigned");
429  fgBuiltins[kDataTypeAliasSignedChar_t] = new TDataType("signed char");
430  }
431 
432  for (Int_t i = 0; i < (Int_t)kNumDataTypes; ++i) {
433  if (fgBuiltins[i]) types->Add(fgBuiltins[i]);
434  }
435 }
436 
437 ////////////////////////////////////////////////////////////////////////////////
438 /// Given a EDataType type, get the TDataType* that represents it.
439 
441 {
442  if (type == kOther_t) return 0;
443  return fgBuiltins[(int)type];
444 }
virtual void TypedefInfo_Init(TypedefInfo_t *, const char *) const
Definition: TInterpreter.h:483
virtual Long_t TypedefInfo_Property(TypedefInfo_t *) const
Definition: TInterpreter.h:486
static TDataType * GetDataType(EDataType type)
Given a EDataType type, get the TDataType* that represents it.
Definition: TDataType.cxx:440
TString GetTypeName()
Get basic type of typedef, e,g.
Definition: TDataType.cxx:149
long long Long64_t
Definition: RtypesCore.h:69
ClassImp(TDataType) TDataType *TDataType
Definition: TDataType.cxx:28
Int_t GetType() const
Definition: TDataType.h:70
Ssiz_t Length() const
Definition: TString.h:390
TLine * line
float Float_t
Definition: RtypesCore.h:53
virtual const char * TypedefInfo_Title(TypedefInfo_t *) const
Definition: TInterpreter.h:490
virtual Bool_t TypedefInfo_IsValid(TypedefInfo_t *) const
Definition: TInterpreter.h:484
R__EXTERN TVirtualMutex * gInterpreterMutex
Definition: TInterpreter.h:46
TDictionary & operator=(const TDictionary &other)
Definition: TDictionary.cxx:62
unsigned short UShort_t
Definition: RtypesCore.h:36
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
const char * GetFullTypeName() const
Get full type description of typedef, e,g.: "class TDirectory*".
Definition: TDataType.cxx:175
void SetType(const char *name)
Set type id depending on name.
Definition: TDataType.cxx:296
virtual const char * TypedefInfo_Name(TypedefInfo_t *) const
Definition: TInterpreter.h:489
Basic string class.
Definition: TString.h:137
TypedefInfo_t * fInfo
Definition: TDataType.h:49
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
#define gInterpreter
Definition: TInterpreter.h:502
Definition: drr.cxx:518
Long_t fProperty
Definition: TDataType.h:52
const char * Data() const
Definition: TString.h:349
float Float16_t
Definition: RtypesCore.h:54
UChar_t mod R__LOCKGUARD2(gSrvAuthenticateMutex)
static TDataType * fgBuiltins[kNumDataTypes]
Definition: TDataType.h:56
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
double Double32_t
Definition: RtypesCore.h:56
Long_t Property() const
Get property description word. For meaning of bits see EProperty.
Definition: TDataType.cxx:287
virtual ~TDataType()
TDataType dtor deletes adopted CINT TypedefInfo object.
Definition: TDataType.cxx:105
Int_t fSize
pointer to CINT typedef info
Definition: TDataType.h:50
TDataType(const TDataType &)
copy constructor
Definition: TDataType.cxx:72
virtual int TypedefInfo_Size(TypedefInfo_t *) const
Definition: TInterpreter.h:487
Basic data type descriptor (datatype information is obtained from CINT).
Definition: TDataType.h:46
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:162
Collection abstract base class.
Definition: TCollection.h:48
unsigned int UInt_t
Definition: RtypesCore.h:42
short Short_t
Definition: RtypesCore.h:35
TString fName
Definition: TNamed.h:36
Int_t fTypeNameLen
Definition: TDataType.h:55
PyObject * fType
long Long_t
Definition: RtypesCore.h:50
double Double_t
Definition: RtypesCore.h:55
TString fTrueName
Definition: TDataType.h:53
int type
Definition: TGX11.cxx:120
unsigned long long ULong64_t
Definition: RtypesCore.h:70
unsigned long ULong_t
Definition: RtypesCore.h:51
EDataType
Definition: TDataType.h:30
virtual void Add(TObject *obj)=0
static void AddBuiltins(TCollection *types)
Create the TDataType objects for builtins.
Definition: TDataType.cxx:408
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual TypedefInfo_t * TypedefInfo_FactoryCopy(TypedefInfo_t *) const
Definition: TInterpreter.h:482
Int_t Size() const
Get size of basic typedef'ed type.
Definition: TDataType.cxx:366
char Char_t
Definition: RtypesCore.h:29
TDataType & operator=(const TDataType &)
assignment operator
Definition: TDataType.cxx:86
virtual void TypedefInfo_Delete(TypedefInfo_t *) const
Definition: TInterpreter.h:479
void CheckInfo()
Refresh the underlying information.
Definition: TDataType.cxx:375
unsigned char UChar_t
Definition: RtypesCore.h:34
R__EXTERN TInterpreter * gCling
Definition: TInterpreter.h:504
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:582
EDataType fType
Definition: TDataType.h:51
virtual const char * TypedefInfo_TrueName(TypedefInfo_t *) const
Definition: TInterpreter.h:488
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
const char * AsString(void *buf) const
Return string containing value in buffer formatted according to the basic data type.
Definition: TDataType.cxx:235
Int_t fTypeNameIdx
Definition: TDataType.h:54