From 1b797548dff7e2475826ba29a71c3f496008988f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 14 Aug 2010 07:12:29 +0000 Subject: libgats gets it's own repo. The rest of the history is in my misc repo. --- src/dictionary.cpp | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 src/dictionary.cpp (limited to 'src/dictionary.cpp') diff --git a/src/dictionary.cpp b/src/dictionary.cpp new file mode 100644 index 0000000..385960f --- /dev/null +++ b/src/dictionary.cpp @@ -0,0 +1,145 @@ +#include "gats/dictionary.h" + +#include "gats/boolean.h" +#include "gats/integer.h" +#include "gats/float.h" +#include "gats/string.h" +#include "gats/list.h" + +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, int64_t 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, double d ) +{ +// Bu::Hash::insert( +// sKey, new Gats::Float( d ) +// ); +} + +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 cast item '%s' to bool.", + 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 cast item '%s' to int.", + sKey.getStr() ); + + return pOb->getValue(); +} + +double Gats::Dictionary::getFloat( const Bu::FString &sKey ) +{/* + Gats::Boolean *pOb = dynamic_cast( get( sKey ) ); + if( pOb ) + throw Bu::ExceptionBase("Cannot cast item '%s' to bool.", + sKey.getStr() ); + + return pOb->getValue();*/ + return 0.0; +} + +Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey ) +{ + Gats::String *pOb = dynamic_cast( get( sKey ) ); + if( pOb ) + throw Bu::ExceptionBase("Cannot cast item '%s' to string.", + 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 cast item '%s' to list.", + 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 cast item '%s' to dictionary.", + sKey.getStr() ); + + return pOb; +} + -- cgit v1.2.3