ROOT logo
// @(#)root/meta:$Id: TDataType.cxx 26606 2008-12-02 20:36:09Z pcanal $
// Author: Rene Brun   04/02/95

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //                                                                      //
// Basic data type descriptor (datatype information is obtained from    //
// CINT). This class describes the attributes of type definitions       //
// (typedef's). The TROOT class contains a list of all currently        //
// defined types (accessible via TROOT::GetListOfTypes()).              //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TDataType.h"
#include "TInterpreter.h"
#ifdef R__SOLARIS
#include <typeinfo>
#endif

ClassImp(TDataType)

//______________________________________________________________________________
TDataType::TDataType(TypedefInfo_t *info) : TDictionary()
{
   // Default TDataType ctor. TDataTypes are constructed in TROOT via
   // a call to TCint::UpdateListOfTypes().

   fInfo = info;

   if (fInfo) {
      SetName(gCint->TypedefInfo_Name(fInfo));
      SetTitle(gCint->TypedefInfo_Title(fInfo));
      SetType(gCint->TypedefInfo_TrueName(fInfo));
      fProperty = gCint->TypedefInfo_Property(fInfo);
      fSize = gCint->TypedefInfo_Size(fInfo);
   } else {
      SetTitle("Builtin basic type");
   }
}

//______________________________________________________________________________
TDataType::TDataType(const char *typenam) : fInfo(0), fProperty(kIsFundamental)
{
   // Constructor for basic data types, like "char", "unsigned char", etc.

   fInfo = 0;
   SetName(typenam);
   SetTitle("Builtin basic type");

   SetType(fName.Data());
}

//______________________________________________________________________________
TDataType::TDataType(const TDataType& dt) :
  TDictionary(dt),
  fInfo(gCint->TypedefInfo_FactoryCopy(dt.fInfo)),
  fSize(dt.fSize),
  fType(dt.fType),
  fProperty(dt.fProperty),
  fTrueName(dt.fTrueName)
{ 
   //copy constructor
}

//______________________________________________________________________________
TDataType& TDataType::operator=(const TDataType& dt)
{
   //assignement operator
   if(this!=&dt) {
      TDictionary::operator=(dt);
      gCint->TypedefInfo_Delete(fInfo);
      fInfo=gCint->TypedefInfo_FactoryCopy(dt.fInfo);
      fSize=dt.fSize;
      fType=dt.fType;
      fProperty=dt.fProperty;
      fTrueName=dt.fTrueName;
   } 
   return *this;
}

//______________________________________________________________________________
TDataType::~TDataType()
{
   // TDataType dtor deletes adopted CINT TypedefInfo object.

   gCint->TypedefInfo_Delete(fInfo);
}

//______________________________________________________________________________
const char *TDataType::GetTypeName(EDataType type)
{
   // Return the name of the type.

   switch (type) {
      case  1: return "Char_t";
      case  2: return "Short_t";
      case  3: return "Int_t";
      case  4: return "Long_t";
      case  5: return "Float_t";
      case  6: return "Int_t";
      case  7: return "char*";
      case  8: return "Double_t";
      case  9: return "Double32_t";
      case 11: return "UChar_t";
      case 12: return "UShort_t";
      case 13: return "UInt_t";
      case 14: return "ULong_t";
      case 15: return "UInt_t";
      case 16: return "Long64_t";
      case 17: return "ULong64_t";
      case 18: return "Bool_t";
      case 19: return "Float16_t";
      case kOther_t:  return "";
      case kNoType_t: return "";
      case kchar:     return "Char_t";
   }
   return "";
}

//______________________________________________________________________________
const char *TDataType::GetTypeName() const
{
   // Get basic type of typedef, e,g.: "class TDirectory*" -> "TDirectory".
   // Result needs to be used or copied immediately.

   if (fInfo) {
      (const_cast<TDataType*>(this))->CheckInfo();
      return gInterpreter->TypeName(fTrueName.Data());
   } else {
      return fName.Data();
   }
}

//______________________________________________________________________________
const char *TDataType::GetFullTypeName() const
{
   // Get full type description of typedef, e,g.: "class TDirectory*".

   if (fInfo) {
      (const_cast<TDataType*>(this))->CheckInfo();
      return fTrueName;
   } else {
      return fName.Data();
   }
}

//______________________________________________________________________________
EDataType TDataType::GetType(const type_info &typeinfo)
{
   // Set type id depending on name.

   EDataType retType = kOther_t;

   if (!strcmp(typeid(unsigned int).name(), typeinfo.name())) {
      retType = kUInt_t;
   } else if (!strcmp(typeid(int).name(), typeinfo.name())) {
      retType = kInt_t;
   } else if (!strcmp(typeid(unsigned long).name(), typeinfo.name())) {
      retType = kULong_t;
   } else if (!strcmp(typeid(long).name(), typeinfo.name())) {
      retType = kLong_t;
   } else if (!strcmp(typeid(ULong64_t).name(), typeinfo.name())) {
      retType = kULong64_t;
   } else if (!strcmp(typeid(Long64_t).name(), typeinfo.name())) {
      retType = kLong64_t;
   } else if (!strcmp(typeid(unsigned short).name(), typeinfo.name())) {
      retType = kUShort_t;
   } else if (!strcmp(typeid(short).name(), typeinfo.name())) {
      retType = kShort_t;
   } else if (!strcmp(typeid(unsigned char).name(), typeinfo.name())) {
      retType = kUChar_t;
   } else if (!strcmp(typeid(char).name(), typeinfo.name())) {
      retType = kChar_t;
   } else if (!strcmp(typeid(bool).name(), typeinfo.name())) {
      retType = kBool_t;
   } else if (!strcmp(typeid(float).name(), typeinfo.name())) {
      retType = kFloat_t;
   } else if (!strcmp(typeid(Float16_t).name(), typeinfo.name())) {
      retType = kFloat16_t;
   } else if (!strcmp(typeid(double).name(), typeinfo.name())) {
      retType = kDouble_t;
   } else if (!strcmp(typeid(Double32_t).name(), typeinfo.name())) {
      retType = kDouble32_t;
   } else if (!strcmp(typeid(char*).name(), typeinfo.name())) {
      retType = kCharStar;
   }
   return retType;
}

//______________________________________________________________________________
const char *TDataType::AsString(void *buf) const
{
   // Return string containing value in buffer formatted according to
   // the basic data type. The result needs to be used or copied immediately.

   static char line[81];
   const char *name;

   if (fInfo) {
      (const_cast<TDataType*>(this))->CheckInfo();
      name = fTrueName;
   } else {
      name = fName.Data();
   }

   line[0] = 0;
   if (!strcmp("unsigned int", name))
      sprintf(line, "%u", *(unsigned int *)buf);
   else if (!strcmp("unsigned", name))
      sprintf(line, "%u", *(unsigned int *)buf);
   else if (!strcmp("int", name))
      sprintf(line, "%d", *(int *)buf);
   else if (!strcmp("unsigned long", name))
      sprintf(line, "%lu", *(unsigned long *)buf);
   else if (!strcmp("long", name))
      sprintf(line, "%ld", *(long *)buf);
   else if (!strcmp("unsigned long long", name))
      sprintf(line, "%llu", *(ULong64_t *)buf);
   else if (!strcmp("long long", name))
      sprintf(line, "%lld", *(Long64_t *)buf);
   else if (!strcmp("unsigned short", name))
      sprintf(line, "%hu", *(unsigned short *)buf);
   else if (!strcmp("short", name))
      sprintf(line, "%hd", *(short *)buf);
   else if (!strcmp("unsigned char", name))
      strncpy(line, (char *)buf,80);
   else if (!strcmp("bool", name))
      sprintf(line, "%s", *(bool *)buf ? "true" : "false");
   else if (!strcmp("char", name))
      strncpy(line, (char *)buf,80);
   else if (!strcmp("float", name))
      sprintf(line, "%g", *(float *)buf);
   else if (!strcmp("double", name))
      sprintf(line, "%g", *(double *)buf);
   else if (!strcmp("char*", name))
      sprintf(line, "%s", *(char**)buf);

   return line;
}

//______________________________________________________________________________
Long_t TDataType::Property() const
{
   // Get property description word. For meaning of bits see EProperty.

   if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
   return fProperty;
}

//______________________________________________________________________________
void TDataType::SetType(const char *name)
{
   // Set type id depending on name.

   fTrueName = name;
   fType = kOther_t;
   fSize = 0;

   if (!strcmp("unsigned int", name)) {
      fType = kUInt_t;
      fSize = sizeof(UInt_t);
   } else if (!strcmp("unsigned", name)) {
      fType = kUInt_t;
      fSize = sizeof(UInt_t);
   } else if (!strcmp("int", name)) {
      fType = kInt_t;
      fSize = sizeof(Int_t);
   } else if (!strcmp("unsigned long", name)) {
      fType = kULong_t;
      fSize = sizeof(ULong_t);
   } else if (!strcmp("long", name)) {
      fType = kLong_t;
      fSize = sizeof(Long_t);
   } else if (!strcmp("unsigned long long", name)) {
      fType = kULong64_t;
      fSize = sizeof(ULong64_t);
   } else if (!strcmp("long long", name)) {
      fType = kLong64_t;
      fSize = sizeof(Long64_t);
   } else if (!strcmp("unsigned short", name)) {
      fType = kUShort_t;
      fSize = sizeof(UShort_t);
   } else if (!strcmp("short", name)) {
      fType = kShort_t;
      fSize = sizeof(Short_t);
   } else if (!strcmp("unsigned char", name)) {
      fType = kUChar_t;
      fSize = sizeof(UChar_t);
   } else if (!strcmp("char", name)) {
      fType = kChar_t;
      fSize = sizeof(Char_t);
   } else if (!strcmp("bool", name)) {
      fType = kBool_t;
      fSize = sizeof(Bool_t);
   } else if (!strcmp("float", name)) {
      fType = kFloat_t;
      fSize = sizeof(Float_t);
   } else if (!strcmp("double", name)) {
      fType = kDouble_t;
      fSize = sizeof(Double_t);
   }

   if (!strcmp("Float16_t", fName.Data())) {
      fType = kFloat16_t;
   }
   if (!strcmp("Double32_t", fName.Data())) {
      fType = kDouble32_t;
   }
   if (!strcmp("char*",fName.Data())) {
      fType = kCharStar;
   }
   // kCounter =  6, kBits     = 15
}

//______________________________________________________________________________
Int_t TDataType::Size() const
{
   // Get size of basic typedef'ed type.

   if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
   return fSize;
}

//______________________________________________________________________________
void TDataType::CheckInfo()
{
   // Refresh the underlying information.

   // This can be needed if the library defining this typedef was loaded after
   // another library and that this other library is unloaded (in which case
   // things can get renumbered inside CINT).

   if (!fInfo) return;

   // This intentionally cast the constness away so that
   // we can call CheckInfo from const data members.

   if (!gCint->TypedefInfo_IsValid(fInfo) ||
       strcmp(gCint->TypedefInfo_Name(fInfo),fName.Data())!=0) {

      // The fInfo is invalid or does not
      // point to this typedef anymore, let's
      // refresh it

      gCint->TypedefInfo_Init(fInfo, fName.Data());

      if (!gCint->TypedefInfo_IsValid(fInfo)) return;

      SetTitle(gCint->TypedefInfo_Title(fInfo));
      SetType(gCint->TypedefInfo_TrueName(fInfo));
      fProperty = gCint->TypedefInfo_Property(fInfo);
      fSize = gCint->TypedefInfo_Size(fInfo);
   }
}
 TDataType.cxx:1
 TDataType.cxx:2
 TDataType.cxx:3
 TDataType.cxx:4
 TDataType.cxx:5
 TDataType.cxx:6
 TDataType.cxx:7
 TDataType.cxx:8
 TDataType.cxx:9
 TDataType.cxx:10
 TDataType.cxx:11
 TDataType.cxx:12
 TDataType.cxx:13
 TDataType.cxx:14
 TDataType.cxx:15
 TDataType.cxx:16
 TDataType.cxx:17
 TDataType.cxx:18
 TDataType.cxx:19
 TDataType.cxx:20
 TDataType.cxx:21
 TDataType.cxx:22
 TDataType.cxx:23
 TDataType.cxx:24
 TDataType.cxx:25
 TDataType.cxx:26
 TDataType.cxx:27
 TDataType.cxx:28
 TDataType.cxx:29
 TDataType.cxx:30
 TDataType.cxx:31
 TDataType.cxx:32
 TDataType.cxx:33
 TDataType.cxx:34
 TDataType.cxx:35
 TDataType.cxx:36
 TDataType.cxx:37
 TDataType.cxx:38
 TDataType.cxx:39
 TDataType.cxx:40
 TDataType.cxx:41
 TDataType.cxx:42
 TDataType.cxx:43
 TDataType.cxx:44
 TDataType.cxx:45
 TDataType.cxx:46
 TDataType.cxx:47
 TDataType.cxx:48
 TDataType.cxx:49
 TDataType.cxx:50
 TDataType.cxx:51
 TDataType.cxx:52
 TDataType.cxx:53
 TDataType.cxx:54
 TDataType.cxx:55
 TDataType.cxx:56
 TDataType.cxx:57
 TDataType.cxx:58
 TDataType.cxx:59
 TDataType.cxx:60
 TDataType.cxx:61
 TDataType.cxx:62
 TDataType.cxx:63
 TDataType.cxx:64
 TDataType.cxx:65
 TDataType.cxx:66
 TDataType.cxx:67
 TDataType.cxx:68
 TDataType.cxx:69
 TDataType.cxx:70
 TDataType.cxx:71
 TDataType.cxx:72
 TDataType.cxx:73
 TDataType.cxx:74
 TDataType.cxx:75
 TDataType.cxx:76
 TDataType.cxx:77
 TDataType.cxx:78
 TDataType.cxx:79
 TDataType.cxx:80
 TDataType.cxx:81
 TDataType.cxx:82
 TDataType.cxx:83
 TDataType.cxx:84
 TDataType.cxx:85
 TDataType.cxx:86
 TDataType.cxx:87
 TDataType.cxx:88
 TDataType.cxx:89
 TDataType.cxx:90
 TDataType.cxx:91
 TDataType.cxx:92
 TDataType.cxx:93
 TDataType.cxx:94
 TDataType.cxx:95
 TDataType.cxx:96
 TDataType.cxx:97
 TDataType.cxx:98
 TDataType.cxx:99
 TDataType.cxx:100
 TDataType.cxx:101
 TDataType.cxx:102
 TDataType.cxx:103
 TDataType.cxx:104
 TDataType.cxx:105
 TDataType.cxx:106
 TDataType.cxx:107
 TDataType.cxx:108
 TDataType.cxx:109
 TDataType.cxx:110
 TDataType.cxx:111
 TDataType.cxx:112
 TDataType.cxx:113
 TDataType.cxx:114
 TDataType.cxx:115
 TDataType.cxx:116
 TDataType.cxx:117
 TDataType.cxx:118
 TDataType.cxx:119
 TDataType.cxx:120
 TDataType.cxx:121
 TDataType.cxx:122
 TDataType.cxx:123
 TDataType.cxx:124
 TDataType.cxx:125
 TDataType.cxx:126
 TDataType.cxx:127
 TDataType.cxx:128
 TDataType.cxx:129
 TDataType.cxx:130
 TDataType.cxx:131
 TDataType.cxx:132
 TDataType.cxx:133
 TDataType.cxx:134
 TDataType.cxx:135
 TDataType.cxx:136
 TDataType.cxx:137
 TDataType.cxx:138
 TDataType.cxx:139
 TDataType.cxx:140
 TDataType.cxx:141
 TDataType.cxx:142
 TDataType.cxx:143
 TDataType.cxx:144
 TDataType.cxx:145
 TDataType.cxx:146
 TDataType.cxx:147
 TDataType.cxx:148
 TDataType.cxx:149
 TDataType.cxx:150
 TDataType.cxx:151
 TDataType.cxx:152
 TDataType.cxx:153
 TDataType.cxx:154
 TDataType.cxx:155
 TDataType.cxx:156
 TDataType.cxx:157
 TDataType.cxx:158
 TDataType.cxx:159
 TDataType.cxx:160
 TDataType.cxx:161
 TDataType.cxx:162
 TDataType.cxx:163
 TDataType.cxx:164
 TDataType.cxx:165
 TDataType.cxx:166
 TDataType.cxx:167
 TDataType.cxx:168
 TDataType.cxx:169
 TDataType.cxx:170
 TDataType.cxx:171
 TDataType.cxx:172
 TDataType.cxx:173
 TDataType.cxx:174
 TDataType.cxx:175
 TDataType.cxx:176
 TDataType.cxx:177
 TDataType.cxx:178
 TDataType.cxx:179
 TDataType.cxx:180
 TDataType.cxx:181
 TDataType.cxx:182
 TDataType.cxx:183
 TDataType.cxx:184
 TDataType.cxx:185
 TDataType.cxx:186
 TDataType.cxx:187
 TDataType.cxx:188
 TDataType.cxx:189
 TDataType.cxx:190
 TDataType.cxx:191
 TDataType.cxx:192
 TDataType.cxx:193
 TDataType.cxx:194
 TDataType.cxx:195
 TDataType.cxx:196
 TDataType.cxx:197
 TDataType.cxx:198
 TDataType.cxx:199
 TDataType.cxx:200
 TDataType.cxx:201
 TDataType.cxx:202
 TDataType.cxx:203
 TDataType.cxx:204
 TDataType.cxx:205
 TDataType.cxx:206
 TDataType.cxx:207
 TDataType.cxx:208
 TDataType.cxx:209
 TDataType.cxx:210
 TDataType.cxx:211
 TDataType.cxx:212
 TDataType.cxx:213
 TDataType.cxx:214
 TDataType.cxx:215
 TDataType.cxx:216
 TDataType.cxx:217
 TDataType.cxx:218
 TDataType.cxx:219
 TDataType.cxx:220
 TDataType.cxx:221
 TDataType.cxx:222
 TDataType.cxx:223
 TDataType.cxx:224
 TDataType.cxx:225
 TDataType.cxx:226
 TDataType.cxx:227
 TDataType.cxx:228
 TDataType.cxx:229
 TDataType.cxx:230
 TDataType.cxx:231
 TDataType.cxx:232
 TDataType.cxx:233
 TDataType.cxx:234
 TDataType.cxx:235
 TDataType.cxx:236
 TDataType.cxx:237
 TDataType.cxx:238
 TDataType.cxx:239
 TDataType.cxx:240
 TDataType.cxx:241
 TDataType.cxx:242
 TDataType.cxx:243
 TDataType.cxx:244
 TDataType.cxx:245
 TDataType.cxx:246
 TDataType.cxx:247
 TDataType.cxx:248
 TDataType.cxx:249
 TDataType.cxx:250
 TDataType.cxx:251
 TDataType.cxx:252
 TDataType.cxx:253
 TDataType.cxx:254
 TDataType.cxx:255
 TDataType.cxx:256
 TDataType.cxx:257
 TDataType.cxx:258
 TDataType.cxx:259
 TDataType.cxx:260
 TDataType.cxx:261
 TDataType.cxx:262
 TDataType.cxx:263
 TDataType.cxx:264
 TDataType.cxx:265
 TDataType.cxx:266
 TDataType.cxx:267
 TDataType.cxx:268
 TDataType.cxx:269
 TDataType.cxx:270
 TDataType.cxx:271
 TDataType.cxx:272
 TDataType.cxx:273
 TDataType.cxx:274
 TDataType.cxx:275
 TDataType.cxx:276
 TDataType.cxx:277
 TDataType.cxx:278
 TDataType.cxx:279
 TDataType.cxx:280
 TDataType.cxx:281
 TDataType.cxx:282
 TDataType.cxx:283
 TDataType.cxx:284
 TDataType.cxx:285
 TDataType.cxx:286
 TDataType.cxx:287
 TDataType.cxx:288
 TDataType.cxx:289
 TDataType.cxx:290
 TDataType.cxx:291
 TDataType.cxx:292
 TDataType.cxx:293
 TDataType.cxx:294
 TDataType.cxx:295
 TDataType.cxx:296
 TDataType.cxx:297
 TDataType.cxx:298
 TDataType.cxx:299
 TDataType.cxx:300
 TDataType.cxx:301
 TDataType.cxx:302
 TDataType.cxx:303
 TDataType.cxx:304
 TDataType.cxx:305
 TDataType.cxx:306
 TDataType.cxx:307
 TDataType.cxx:308
 TDataType.cxx:309
 TDataType.cxx:310
 TDataType.cxx:311
 TDataType.cxx:312
 TDataType.cxx:313
 TDataType.cxx:314
 TDataType.cxx:315
 TDataType.cxx:316
 TDataType.cxx:317
 TDataType.cxx:318
 TDataType.cxx:319
 TDataType.cxx:320
 TDataType.cxx:321
 TDataType.cxx:322
 TDataType.cxx:323
 TDataType.cxx:324
 TDataType.cxx:325
 TDataType.cxx:326
 TDataType.cxx:327
 TDataType.cxx:328
 TDataType.cxx:329
 TDataType.cxx:330
 TDataType.cxx:331
 TDataType.cxx:332
 TDataType.cxx:333
 TDataType.cxx:334
 TDataType.cxx:335
 TDataType.cxx:336
 TDataType.cxx:337
 TDataType.cxx:338
 TDataType.cxx:339
 TDataType.cxx:340
 TDataType.cxx:341
 TDataType.cxx:342
 TDataType.cxx:343
 TDataType.cxx:344
 TDataType.cxx:345
 TDataType.cxx:346
 TDataType.cxx:347
 TDataType.cxx:348
 TDataType.cxx:349
 TDataType.cxx:350
 TDataType.cxx:351
 TDataType.cxx:352
 TDataType.cxx:353
 TDataType.cxx:354
 TDataType.cxx:355
 TDataType.cxx:356
 TDataType.cxx:357
 TDataType.cxx:358
 TDataType.cxx:359
 TDataType.cxx:360
 TDataType.cxx:361