#include <iostream>
#include <cstring>
#include "THaVar.h"
#include "TMethodCall.h"
#include "TObjArray.h"
#include "TClass.h"
using namespace std;
const Int_t THaVar::kInvalidInt = -1;
const Double_t THaVar::kInvalid = 1e38;
const char* var_type_name[] = {
"kDouble", "kFloat", "kLong", "kULong", "kInt", "kUInt",
"kShort", "kUShort", "kChar", "kByte",
"kObject", "kTString", "kString", "kDoubleV", "kIntV",
"kDoubleP", "kFloatP", "kLongP", "kULongP", "kIntP", "kUIntP",
"kShortP", "kUShortP", "kCharP", "kByteP",
"kObjectP",
"kDouble2P", "kFloat2P", "kLong2P", "kULong2P", "kInt2P", "kUInt2P",
"kShort2P", "kUShort2P", "kChar2P", "kByte2P",
"kObject2P" };
THaVar::THaVar( const THaVar& rhs ) :
TNamed( rhs ), fArrayData(rhs.fArrayData), fValueD(rhs.fValueD),
fType(rhs.fType), fCount(rhs.fCount), fOffset(rhs.fOffset)
{
if( rhs.fMethod )
fMethod = new TMethodCall( *rhs.fMethod );
else
fMethod = NULL;
}
THaVar& THaVar::operator=( const THaVar& rhs )
{
if( this != &rhs ) {
TNamed::operator=(rhs);
fValueD = rhs.fValueD;
fArrayData = rhs.fArrayData;
fType = rhs.fType;
fCount = rhs.fCount;
fOffset = rhs.fOffset;
delete fMethod;
if( rhs.fMethod )
fMethod = new TMethodCall( *rhs.fMethod );
else
fMethod = NULL;
}
return *this;
}
THaVar::~THaVar()
{
delete fMethod;
}
Bool_t THaVar::HasSameSize( const THaVar& rhs ) const
{
Bool_t is_array = IsArray();
if( is_array != rhs.IsArray())
return kFALSE;
if( !is_array )
return kTRUE;
if( fCount )
return ( fCount == rhs.fCount );
if( fOffset != -1 )
return ( fObject == rhs.fObject );
return
( fArrayData.GetNdim() == rhs.fArrayData.GetNdim() &&
fArrayData.GetLen() == rhs.fArrayData.GetLen() );
}
Bool_t THaVar::HasSameSize( const THaVar* rhs ) const
{
if( !rhs )
return kFALSE;
return HasSameSize( *rhs );
}
const char* THaVar::GetTypeName( VarType itype )
{
static const char* const type[] = {
"Double_t", "Float_t", "Long64_t", "ULong64_t", "Int_t", "UInt_t",
"Short_t", "UShort_t", "Char_t", "Byte_t", "TObject",
"Double_t*", "Float_t*", "Long64_t*", "ULong64_t*", "Int_t*", "UInt_t*",
"Short_t*", "UShort_t*", "Char_t*", "Byte_t*", "TObject*",
"Double_t**", "Float_t**", "Long_t64**", "ULong_t64**", "Int_t**",
"UInt_t**", "Short_t**", "UShort_t**", "Char_t**", "Byte_t**",
"TObject**" };
return type[itype];
}
size_t THaVar::GetTypeSize( VarType itype )
{
static const size_t size[] = {
sizeof(Double_t), sizeof(Float_t), sizeof(Long64_t), sizeof(ULong64_t),
sizeof(Int_t), sizeof(UInt_t), sizeof(Short_t), sizeof(UShort_t),
sizeof(Char_t), sizeof(Byte_t), 0,
sizeof(Double_t), sizeof(Float_t), sizeof(Long64_t), sizeof(ULong64_t),
sizeof(Int_t), sizeof(UInt_t), sizeof(Short_t), sizeof(UShort_t),
sizeof(Char_t), sizeof(Byte_t), 0,
sizeof(Double_t), sizeof(Float_t), sizeof(Long64_t), sizeof(ULong64_t),
sizeof(Int_t), sizeof(UInt_t), sizeof(Short_t), sizeof(UShort_t),
sizeof(Char_t), sizeof(Byte_t), 0 };
return size[itype];
}
const Int_t* THaVar::GetObjArrayLenPtr() const
{
static Int_t len = 0;
if( fObject == NULL )
return NULL;
const TSeqCollection* c = static_cast<const TSeqCollection*>( fObject );
if( c->IsA()->InheritsFrom("TObjArray") )
len = static_cast<const TObjArray*>(c)->GetLast()+1;
else
len = c->GetSize();
return &len;
}
Double_t THaVar::GetValueFromObject( Int_t i ) const
{
if( fObject == NULL )
return kInvalid;
void* obj;
if( fOffset != -1 ) {
const Int_t* pi = GetObjArrayLenPtr();
if( i<0 || !pi || i >= *pi )
return kInvalid;
obj = static_cast<const TSeqCollection*>( fObject )->At(i);
if( !obj )
return kInvalid;
if( !fMethod ) {
ULong_t loc = (ULong_t)obj + fOffset;
if( !loc || (fType >= kDoubleP && (*(void**)loc == NULL )) )
return kInvalid;
switch( fType ) {
case kDouble:
return *((Double_t*)loc);
case kFloat:
return static_cast<Double_t>( *(((Float_t*)loc)) );
case kLong:
return static_cast<Double_t>( *(((Long64_t*)loc)) );
case kULong:
return static_cast<Double_t>( *(((ULong64_t*)loc)) );
case kInt:
return static_cast<Double_t>( *(((Int_t*)loc)) );
case kUInt:
return static_cast<Double_t>( *(((UInt_t*)loc)) );
case kShort:
return static_cast<Double_t>( *(((Short_t*)loc)) );
case kUShort:
return static_cast<Double_t>( *(((UShort_t*)loc)) );
case kChar:
return static_cast<Double_t>( *(((Char_t*)loc)) );
case kByte:
return static_cast<Double_t>( *(((Byte_t*)loc)) );
case kDoubleP:
return **((Double_t**)loc);
case kFloatP:
return static_cast<Double_t>( **(((Float_t**)loc)) );
case kLongP:
return static_cast<Double_t>( **(((Long64_t**)loc)) );
case kULongP:
return static_cast<Double_t>( **(((ULong64_t**)loc)) );
case kIntP:
return static_cast<Double_t>( **(((Int_t**)loc)) );
case kUIntP:
return static_cast<Double_t>( **(((UInt_t**)loc)) );
case kShortP:
return static_cast<Double_t>( **(((Short_t**)loc)) );
case kUShortP:
return static_cast<Double_t>( **(((UShort_t**)loc)) );
case kCharP:
return static_cast<Double_t>( **(((Char_t**)loc)) );
case kByteP:
return static_cast<Double_t>( **(((Byte_t**)loc)) );
default:
;
}
return kInvalid;
}
} else {
if( !fMethod )
return kInvalid;
obj = const_cast<void*>(fObject);
}
if( fType != kDouble && fType != kFloat ) {
Long_t result;
fMethod->Execute( obj, result );
return static_cast<Double_t>( result );
} else {
Double_t result;
fMethod->Execute( obj, result );
return result;
}
}
Int_t THaVar::Index( const THaArrayString& elem ) const
{
if( elem.IsError() ) return -1;
if( !elem.IsArray() ) return 0;
if( fCount ) {
if( elem.GetNdim() != 1 ) return -2;
return *elem.GetDim();
}
Byte_t ndim = GetNdim();
if( ndim != elem.GetNdim() ) return -2;
const Int_t *subs = elem.GetDim(), *adim = GetDim();
Int_t index = subs[0];
for( Byte_t i = 0; i<ndim; i++ ) {
if( subs[i]+1 > adim[i] ) return -1;
if( i>0 )
index = index*adim[i] + subs[i];
}
if( index >= GetLen() || index > kMaxInt ) return -1;
return index;
}
Int_t THaVar::Index( const char* s ) const
{
size_t len = strlen(s);
if( len == 0 ) return 0;
char* str = new char[ len+4 ];
str[0] = 't';
str[1] = '[';
str[len+2] = ']';
str[len+3] = '\0';
strncpy( str+2, s, len );
THaArrayString elem(str);
delete [] str;
return Index( elem );
}
void THaVar::Print(Option_t* option) const
{
TNamed::Print(option);
if( strcmp( option, "FULL" )) return;
cout << "(" << GetTypeName() << ")[";
if( fCount ) cout << "*";
cout << GetLen() << "]";
for( int i=0; i<GetLen(); i++ ) {
cout << " " << GetValue(i);
}
cout << endl;
}
void THaVar::SetName( const char* name )
{
TNamed::SetName( name );
fArrayData = name;
}
void THaVar::SetNameTitle( const char* name, const char* descript )
{
TNamed::SetNameTitle( name, descript );
fArrayData = name;
}
ClassImp(THaVar)
Last change: Sat Nov 7 21:26:56 2009
Last generated: 2009-11-07 21:26
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.