#include "gats/dictionary.h" #include "gats/boolean.h" #include "gats/integer.h" #include "gats/float.h" #include "gats/string.h" #include "gats/list.h" #include template<> uint32_t Bu::__calcHashCode( const Gats::String &s ) { return __calcHashCode( dynamic_cast(s) ); } Gats::Dictionary::Dictionary() { } Gats::Dictionary::~Dictionary() { for( iterator i = begin(); i; i++ ) { delete *i; } } void Gats::Dictionary::write( Bu::Stream &rOut ) const { rOut.write("d", 1 ); for( const_iterator i= begin(); i; i++ ) { i.getKey().write( rOut ); (*i)->write( rOut ); } rOut.write("e", 1 ); } void Gats::Dictionary::read( Bu::Stream &rIn, char cType ) { for(;;) { char cNext; rIn.read( &cNext, 1 ); if( cNext == 'e' ) break; if( cNext != 's' ) throw Bu::ExceptionBase("You can only use strings as keys."); Gats::String sKey; sKey.read( rIn, cNext ); ((Bu::Hash *)this)->insert( sKey, Gats::Object::read( rIn ) ); } } void Gats::Dictionary::insert( const Bu::FString &sKey, char i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, unsigned char i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, signed char i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, unsigned short i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, signed short i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, unsigned int i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, signed int i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, unsigned long i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, signed long i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, unsigned long long i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, signed long long i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } /* void Gats::Dictionary::insert( const Bu::FString &sKey, bool b ) { Bu::Hash::insert( sKey, new Gats::Boolean( b ) ); }*/ void Gats::Dictionary::insert( const Bu::FString &sKey, float d ) { Bu::Hash::insert( sKey, new Gats::Float( d ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, double d ) { Bu::Hash::insert( sKey, new Gats::Float( d ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, const char *s ) { Bu::Hash::insert( sKey, new Gats::String( s ) ); } void Gats::Dictionary::insert( const Bu::FString &sKey, const Bu::FString &s ) { Bu::Hash::insert( sKey, new Gats::String( s ) ); } bool Gats::Dictionary::getBool( const Bu::FString &sKey ) { Gats::Boolean *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as bool.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return pOb->getValue(); } int64_t Gats::Dictionary::getInt( const Bu::FString &sKey ) { Gats::Integer *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as int.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return pOb->getValue(); } double Gats::Dictionary::getFloat( const Bu::FString &sKey ) { Gats::Float *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as float.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return pOb->getValue(); } Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey ) { Gats::String *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as string.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return *pOb; } Gats::List *Gats::Dictionary::getList( const Bu::FString &sKey ) { Gats::List *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as list.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return pOb; } Gats::Dictionary *Gats::Dictionary::getDict( const Bu::FString &sKey ) { Gats::Dictionary *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as dictionary.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return pOb; } bool Gats::Dictionary::getBool( const Bu::FString &sKey ) const { Gats::Boolean *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as bool.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return pOb->getValue(); } int64_t Gats::Dictionary::getInt( const Bu::FString &sKey ) const { Gats::Integer *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as int.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return pOb->getValue(); } double Gats::Dictionary::getFloat( const Bu::FString &sKey ) const { Gats::Float *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as float.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return pOb->getValue(); } Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey ) const { Gats::String *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as string.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return *pOb; } Gats::List *Gats::Dictionary::getList( const Bu::FString &sKey ) const { Gats::List *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as list.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return pOb; } Gats::Dictionary *Gats::Dictionary::getDict( const Bu::FString &sKey ) const { Gats::Dictionary *pOb = dynamic_cast( get( sKey ) ); if( !pOb ) throw Bu::ExceptionBase("Cannot use %s item '%s' as dictionary.", typeToStr( get( sKey )->getType() ), sKey.getStr() ); return pOb; } Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Dictionary &d ) { f << "(dict) {"; f.incIndent(); int iMax = 0; for( Gats::Dictionary::const_iterator i = d.begin(); i; i++ ) { if( i.getKey().getSize() > iMax ) iMax = i.getKey().getSize(); } iMax += 2; for( Gats::Dictionary::const_iterator i = d.begin(); i; i++ ) { f << f.nl << Bu::Fmt( iMax ) << i.getKey() + ": " << *i.getValue(); } f.decIndent(); f << f.nl << "}"; return f; }