#include "TDataType.h"
#include "TInterpreter.h"
#include "TCollection.h"
#ifdef R__SOLARIS
#include <typeinfo>
#endif
ClassImp(TDataType)
TDataType* TDataType::fgBuiltins[kNumDataTypes] = {0};
TDataType::TDataType(TypedefInfo_t *info) : TDictionary()
{
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");
fProperty = 0;
fSize = 0;
fType = kNoType_t;
}
}
TDataType::TDataType(const char *typenam) : fInfo(0), fProperty(kIsFundamental)
{
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)
{
}
TDataType& TDataType::operator=(const TDataType& dt)
{
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()
{
gCint->TypedefInfo_Delete(fInfo);
}
const char *TDataType::GetTypeName(EDataType 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 kVoid_t: return "void";
case kDataTypeAliasUnsigned_t: return "UInt_t";
case kOther_t: return "";
case kNoType_t: return "";
case kchar: return "Char_t";
default: return "";
}
return "";
}
const char *TDataType::GetTypeName() const
{
if (fInfo) {
(const_cast<TDataType*>(this))->CheckInfo();
return gInterpreter->TypeName(fTrueName.Data());
} else {
return fName.Data();
}
}
const char *TDataType::GetFullTypeName() const
{
if (fInfo) {
(const_cast<TDataType*>(this))->CheckInfo();
return fTrueName;
} else {
return fName.Data();
}
}
EDataType TDataType::GetType(const type_info &typeinfo)
{
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
{
static TString 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))
line.Form( "%u", *(unsigned int *)buf);
else if (!strcmp("unsigned", name))
line.Form( "%u", *(unsigned int *)buf);
else if (!strcmp("int", name))
line.Form( "%d", *(int *)buf);
else if (!strcmp("unsigned long", name))
line.Form( "%lu", *(unsigned long *)buf);
else if (!strcmp("long", name))
line.Form( "%ld", *(long *)buf);
else if (!strcmp("unsigned long long", name))
line.Form( "%llu", *(ULong64_t *)buf);
else if (!strcmp("long long", name))
line.Form( "%lld", *(Long64_t *)buf);
else if (!strcmp("unsigned short", name))
line.Form( "%hu", *(unsigned short *)buf);
else if (!strcmp("short", name))
line.Form( "%hd", *(short *)buf);
else if (!strcmp("bool", name))
line.Form( "%s", *(bool *)buf ? "true" : "false");
else if (!strcmp("unsigned char", name) || !strcmp("char", name) ) {
line = (char*)buf;
} else if (!strcmp("float", name))
line.Form( "%g", *(float *)buf);
else if (!strcmp("double", name))
line.Form( "%g", *(double *)buf);
else if (!strcmp("char*", name))
line.Form( "%s", *(char**)buf);
return line;
}
Long_t TDataType::Property() const
{
if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
return fProperty;
}
void TDataType::SetType(const char *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;
}
}
Int_t TDataType::Size() const
{
if (fInfo) (const_cast<TDataType*>(this))->CheckInfo();
return fSize;
}
void TDataType::CheckInfo()
{
if (!fInfo) return;
if (!gCint->TypedefInfo_IsValid(fInfo) ||
strcmp(gCint->TypedefInfo_Name(fInfo),fName.Data())!=0) {
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);
}
}
void TDataType::AddBuiltins(TCollection* types)
{
if (fgBuiltins[kChar_t] == 0) {
fgBuiltins[kChar_t] = new TDataType("char");
fgBuiltins[kUChar_t] = new TDataType("unsigned char");
fgBuiltins[kShort_t] = new TDataType("short");
fgBuiltins[kUShort_t] = new TDataType("unsigned short");
fgBuiltins[kInt_t] = new TDataType("int");
fgBuiltins[kUInt_t] = new TDataType("unsigned int");
fgBuiltins[kLong_t] = new TDataType("long");
fgBuiltins[kULong_t] = new TDataType("unsigned long");
fgBuiltins[kLong64_t] = new TDataType("long long");
fgBuiltins[kULong64_t] = new TDataType("unsigned long long");
fgBuiltins[kFloat_t] = new TDataType("float");
fgBuiltins[kDouble_t] = new TDataType("double");
fgBuiltins[kVoid_t] = new TDataType("void");
fgBuiltins[kBool_t] = new TDataType("bool");
fgBuiltins[kCharStar] = new TDataType("char*");
fgBuiltins[kDataTypeAliasUnsigned_t] = new TDataType("unsigned");
}
for (Int_t i = 0; i < (Int_t)kNumDataTypes; ++i) {
if (fgBuiltins[i]) types->Add(fgBuiltins[i]);
}
}
TDataType* TDataType::GetDataType(EDataType type)
{
if (type == kOther_t) return 0;
return fgBuiltins[(int)type];
}