/* * Copyright (C) 2007-2012 Xagasoft, All rights reserved. * * This file is part of the libgats library and is released under the * terms of the license contained in the file LICENSE. */ #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; } } Gats::Object *Gats::Dictionary::clone() const { Gats::Dictionary *pClone = new Gats::Dictionary; for( const_iterator i = begin(); i; i++ ) { Bu::String s(i.getKey()); pClone->insert( s.clone(), (*i)->clone() ); } return pClone; } 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::String &sKey, char i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, unsigned char i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, signed char i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, unsigned short i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, signed short i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, unsigned int i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, signed int i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, unsigned long i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, signed long i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, unsigned long long i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, signed long long i ) { ((Bu::Hash *)this)->insert( sKey, new Gats::Integer( i ) ); } /* void Gats::Dictionary::insert( const Bu::String &sKey, bool b ) { Bu::Hash::insert( sKey, new Gats::Boolean( b ) ); }*/ void Gats::Dictionary::insert( const Bu::String &sKey, float d ) { Bu::Hash::insert( sKey, new Gats::Float( d ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, double d ) { Bu::Hash::insert( sKey, new Gats::Float( d ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, const char *s ) { Bu::Hash::insert( sKey, new Gats::String( s ) ); } void Gats::Dictionary::insert( const Bu::String &sKey, const Bu::String &s ) { Bu::Hash::insert( sKey, new Gats::String( s ) ); } void Gats::Dictionary::insertBool( const Bu::String &sKey, bool b ) { Bu::Hash::insert( sKey, new Gats::Boolean( b ) ); } void Gats::Dictionary::insertInt( const Bu::String &sKey, int64_t i ) { Bu::Hash::insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insertFloat( const Bu::String &sKey, double d ) { Bu::Hash::insert( sKey, new Gats::Float( d ) ); } void Gats::Dictionary::insertStr( const Bu::String &sKey, const Bu::String &s ) { Bu::Hash::insert( sKey, new Gats::String( s ) ); } void Gats::Dictionary::insertList( const Bu::String &sKey, Gats::List *pL ) { Bu::Hash::insert( sKey, pL ); } void Gats::Dictionary::insertDict( const Bu::String &sKey, Gats::Dictionary *pD ) { Bu::Hash::insert( sKey, pD ); } Gats::List *Gats::Dictionary::insertList( const Bu::String &sKey ) { Gats::List *pLst = new Gats::List(); insertList( sKey, pLst ); return pLst; } Gats::Dictionary *Gats::Dictionary::insertDict( const Bu::String &sKey ) { Gats::Dictionary *pDict = new Gats::Dictionary(); insertDict( sKey, pDict ); return pDict; } bool Gats::Dictionary::getBool( const Bu::String &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::String &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::String &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::String Gats::Dictionary::getStr( const Bu::String &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::String &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::String &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::String &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::String &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::String &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::String Gats::Dictionary::getStr( const Bu::String &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::String &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::String &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; }