// ---------------------------------------------------------------------- // // ColumnAttribs.cc - implementation of the methods of the ColumnAttribs // class // // Are defined in this file: // string format() return the chform representing the column // for a block it will need to be concatenated // with other column's format with a comma (,) // delimiters // void print(ostream& o) // textual human readable output describing the // current information contain in the object // History: // ??-???-1997 ??????? Initial creation // 22-Aug-1997 John Marraffino Fix syntax of colons in spanned // column ChForm // 22-Apr-1999 J. Marraffino Make copy constructor and replacement // operator symmetric. #include "HepTuple/Column.h" #include "ColumnType.h" #include "HepTuple/TupleNameTag.h" #include USING( std::string ) ZM_BEGIN_NAMESPACE( zmht ) /* namespace zmht { */ // *************************** // // // // Constructors // // // // *************************** // ColumnAttribs::ColumnAttribs(const string& tagName) : tag(tagName), type(ColumnData_t_End), variable(0), function(0), getMethod(0), destination(0), destFunction(0), setMethod(0), defaultVal(0), nbits(0), rangeLo(0), rangeHi(0), indexLo(0), indexHi(0), ndim(0), isIndex(false) { // Set to default fortran type: char cc = tagName[0]; if( ((cc>='i')&&(cc<='n')) || ((cc>='I')&&(cc<='N')) ) { type = Int4_ct; } else { type = Float4_ct; } for (int i=0;i static bool ColumnAttribs_setDefaultValue(T val, ColumnAttribs* owner) { if ( ! owner->checkType(getColumnData_t(val)) ) { ZMthrow(ZMxHepTypeMismatch(string(" for ")+ owner->tag+string("'s default value: ") +string(TypeInformation[owner->type].shortcut) +string(" vs ") +string(TypeInformation[getColumnData_t(val)].shortcut)) ); return false; } if ( owner->defaultVal != NULL ) { // since we are not sure that the type did not change since // the last set defautl. delete (T*) owner->defaultVal; } owner->defaultVal = (void*) (new T); *(T*)(owner->defaultVal) = val; return true; } bool ColumnAttribs::setDefaultValue(Int1 val) { return ColumnAttribs_setDefaultValue(val,this); } bool ColumnAttribs::setDefaultValue(Int2 val) { return ColumnAttribs_setDefaultValue(val,this); } bool ColumnAttribs::setDefaultValue(Int4 val) { return ColumnAttribs_setDefaultValue(val,this); } #ifdef Int8 bool ColumnAttribs::setDefaultValue(Int8 val) { return ColumnAttribs_setDefaultValue(val,this); } #endif bool ColumnAttribs::setDefaultValue(UInt1 val) { return ColumnAttribs_setDefaultValue(val,this); } bool ColumnAttribs::setDefaultValue(UInt2 val) { return ColumnAttribs_setDefaultValue(val,this); } bool ColumnAttribs::setDefaultValue(UInt4 val) { return ColumnAttribs_setDefaultValue(val,this); } #ifdef UInt8 bool ColumnAttribs::setDefaultValue(UInt8 val) { return ColumnAttribs_setDefaultValue(val,Uint8_ct,this); } #endif bool ColumnAttribs::setDefaultValue(Float4 val) { return ColumnAttribs_setDefaultValue(val,this); } bool ColumnAttribs::setDefaultValue(Float8 val) { return ColumnAttribs_setDefaultValue(val,this); } #ifdef Float16 bool ColumnAttribs::setDefaultValue(Float16 val) { return ColumnAttribs_setDefaultValue(val,this); } #endif bool ColumnAttribs::setDefaultValue(bool val) { return ColumnAttribs_setDefaultValue(val,this); } bool ColumnAttribs::setDefaultValue(void* val) { return ColumnAttribs_setDefaultValue(val,this); } bool ColumnAttribs::copyDefaultValue(void *valP,ColumnData_t t) { if ( ! checkType(t) ) { ZMthrow(ZMxHepTypeMismatch(string(" for ")+ tag+string("'s default value: ") +string(TypeInformation[type].shortcut) +string(" vs ") +string(TypeInformation[t].shortcut)) ); return false; } if ( valP == NULL) { defaultVal = NULL; return true; }; switch (type) { case Int1_ct: return setDefaultValue(*(Int1* )valP); case Int2_ct: return setDefaultValue(*(Int2* )valP); case Int4_ct: return setDefaultValue(*(Int4* )valP); #ifdef Int8 case Int8_ct: return setDefaultValue(*(Int8* )valP); #endif case Uint1_ct: return setDefaultValue(*(UInt1 *)valP); case Uint2_ct: return setDefaultValue(*(UInt2 *)valP); case Uint4_ct: return setDefaultValue(*(UInt4 *)valP); #ifdef UInt8 case Uint8_ct: return setDefaultValue(*(UInt8 *)valP); #endif case Float4_ct:return setDefaultValue(*(Float4*)valP); case Float8_ct:return setDefaultValue(*(Float8*)valP); #ifdef Float16 case Float16_ct:return setDefaultValue(*(Float16*)valP); #endif case Bool_ct: return setDefaultValue(*(bool*)valP); case Ptr_ct: return setDefaultValue(*(void**)valP); default: ZMthrow(ZMxHepUnsupported("Unexpected type in copyDefaultValue")); return false; } } // ********************************************************************** // // // // bool ColumnAttribs::checkType(ColumnData_t t) // // // // return true if the type t is acceptable for the current ColumnAttribs // // // // Right the check is rudimentary .. maybe we should allow for promotion // // later (like float is accetable for double) // // // // ********************************************************************** // bool ColumnAttribs::checkType(ColumnData_t t) { return ( t == type ); } // ******************************************** // // // // int ColumnAttribs::sumDim() const // // // // return the sum of all dimension in extents. // // // // ******************************************** // int ColumnAttribs::sumDim() const { int sum = 1; for (int i = 0; i < ndim; i++) { sum *= extents[i]; } return sum; } // ******************************************************************** // // // // string ColumnAttribs::format() // // // // This return the format for ONE ColumnAttribs // // if there is several of them, they will need to be concatenated with // // comma (,) delimiters // // // // ******************************************************************** // // include for sprintf #include string ColumnAttribs::format() const { int j,ty; string cp; string chf_string; // temporary char* to use sscanf ?!:( // since it only copies numbers in 2040 should be enough ? char temp[2040]; // chf_string = new char[CHF_STRING_MAX]; // for(j=0;j 0 ) { chf_string += '('; for ( j = 0; j < ndim; ++j ) { if ( j > 0 ) chf_string += ","; if ( j < ndim-1 || index.length() == 0 ) { sprintf( temp, "%d", extents[j] ); chf_string += string(temp); } else { // j==ndim-1 AND index is not empty // we used an 'undefined' TupleNameTag because we // are already sure that index is a valid tag for the // current manager. TupleNameTag tag; tag.init(index, false, tag); chf_string += tag.column(); } } chf_string += ')'; } //get the data type and size chf_string += ':'; chf_string += TypeInformation[type].shortcut; // We still need to figure out if we have an integer, a real or // a character switch(type) { case Int1_ct : case Int2_ct : case Int4_ct : case Int8_ct : case Uint1_ct : case Uint2_ct : case Uint4_ct : case Uint8_ct : { ty = 1; break; } case Float4_ct : case Float8_ct : case Float16_ct : { ty = 2; break; } case Bool_ct : case Bool2_ct : case Bool4_ct : case Char_ct : case Char4_ct : case Char8_ct : case Char12_ct : case Char16_ct : case Char20_ct : case Char24_ct : case Char28_ct : case Char32_ct : case Ptr_ct : { ty = 0; break; } default: ZMthrow(ZMxHepUnsupported("101: Unknown column data type")); return (""); } //if packing is invoved..... if(nbits>0) { chf_string += ":"; sprintf(temp,"%-d",nbits); chf_string += temp; if( (ty==1) && (indexLo != indexHi) ) { chf_string += ":"; sprintf(temp,"[%-d,%-d]",indexLo,indexHi); chf_string += temp; } else if(rangeLo != rangeHi) { chf_string += ":"; sprintf(temp,"[%-.2f,%-.2f]",rangeLo,rangeHi); chf_string += temp; } } //else if range exist else if( (ty==1) && ((indexLo!=0)||(indexHi!=0)) ) { chf_string += "::"; sprintf(temp,"[%-d,%-d]",indexLo,indexHi); chf_string += temp; } else if( (ty==2)&&((rangeLo!=0)||(rangeHi!=0)) ) { chf_string += "::"; sprintf(temp,"[%-.2f,%-.2f]",rangeLo,rangeHi); chf_string += temp; } //this column is done! return chf_string; } // format() USING( namespace std ) // print out a ColumnAttribs void ColumnAttribs::print(ostream& o) const { int i; if(tag.length()==0) cout<<"ColumnAttribs::caprint() found no tag"<0) { cout<<"("; if(index.length()==0) for(i=0;i1) { for(i=0;i<(ndim-1);i++) cout << extents[i] << "," ; cout << index; } else cout << index; //ndim==1 } cout<<")"; } cout<