From 9dc8cc535ef5fc4ea78f967fe285fe4424ff4458 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 18 Aug 2010 16:17:15 +0000 Subject: getting it all tuned up. --- src/dictionary.cpp | 24 +++++++++++++++++++----- src/dictionary.h | 6 ++++-- src/float.h | 17 +++++++++++++++++ src/object.cpp | 4 ++++ src/unit/basic.unit | 29 +++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/dictionary.cpp b/src/dictionary.cpp index 385960f..2223c8b 100644 --- a/src/dictionary.cpp +++ b/src/dictionary.cpp @@ -54,6 +54,13 @@ void Gats::Dictionary::read( Bu::Stream &rIn, char cType ) } } +void Gats::Dictionary::insert( const Bu::FString &sKey, int32_t i ) +{ + ((Bu::Hash *)this)->insert( + sKey, new Gats::Integer( i ) + ); +} + void Gats::Dictionary::insert( const Bu::FString &sKey, int64_t i ) { ((Bu::Hash *)this)->insert( @@ -75,6 +82,13 @@ void Gats::Dictionary::insert( const Bu::FString &sKey, double 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( @@ -85,7 +99,7 @@ void Gats::Dictionary::insert( const Bu::FString &sKey, const Bu::FString &s ) bool Gats::Dictionary::getBool( const Bu::FString &sKey ) { Gats::Boolean *pOb = dynamic_cast( get( sKey ) ); - if( pOb ) + if( !pOb ) throw Bu::ExceptionBase("Cannot cast item '%s' to bool.", sKey.getStr() ); @@ -95,7 +109,7 @@ bool Gats::Dictionary::getBool( const Bu::FString &sKey ) int64_t Gats::Dictionary::getInt( const Bu::FString &sKey ) { Gats::Integer *pOb = dynamic_cast( get( sKey ) ); - if( pOb ) + if( !pOb ) throw Bu::ExceptionBase("Cannot cast item '%s' to int.", sKey.getStr() ); @@ -116,7 +130,7 @@ double Gats::Dictionary::getFloat( const Bu::FString &sKey ) Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey ) { Gats::String *pOb = dynamic_cast( get( sKey ) ); - if( pOb ) + if( !pOb ) throw Bu::ExceptionBase("Cannot cast item '%s' to string.", sKey.getStr() ); @@ -126,7 +140,7 @@ Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey ) Gats::List *Gats::Dictionary::getList( const Bu::FString &sKey ) { Gats::List *pOb = dynamic_cast( get( sKey ) ); - if( pOb ) + if( !pOb ) throw Bu::ExceptionBase("Cannot cast item '%s' to list.", sKey.getStr() ); @@ -136,7 +150,7 @@ Gats::List *Gats::Dictionary::getList( const Bu::FString &sKey ) Gats::Dictionary *Gats::Dictionary::getDict( const Bu::FString &sKey ) { Gats::Dictionary *pOb = dynamic_cast( get( sKey ) ); - if( pOb ) + if( !pOb ) throw Bu::ExceptionBase("Cannot cast item '%s' to dictionary.", sKey.getStr() ); diff --git a/src/dictionary.h b/src/dictionary.h index 3bcaec6..8f4b949 100644 --- a/src/dictionary.h +++ b/src/dictionary.h @@ -19,11 +19,13 @@ namespace Gats virtual Type getType() const { return typeDictionary; } virtual void write( Bu::Stream &rOut ) const; virtual void read( Bu::Stream &rIn, char cType ); - + + void insert( const Bu::FString &sKey, const char *s ); + void insert( const Bu::FString &sKey, const Bu::FString &s ); + void insert( const Bu::FString &sKey, int32_t i ); void insert( const Bu::FString &sKey, int64_t i ); void insert( const Bu::FString &sKey, bool b ); void insert( const Bu::FString &sKey, double d ); - void insert( const Bu::FString &sKey, const Bu::FString &s ); using Bu::Hash::insert; bool getBool( const Bu::FString &sKey ); diff --git a/src/float.h b/src/float.h index e69de29..cf20010 100644 --- a/src/float.h +++ b/src/float.h @@ -0,0 +1,17 @@ +#ifndef GATS_FLOAT_H +#define GATS_FLOAT_H + +namespace Gats +{ + class Float + { + public: + Float(); + Float( double f ); + virtual ~Float(); + + private: + }; +} + +#endif diff --git a/src/object.cpp b/src/object.cpp index 3d7765e..4c1f3ca 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -41,6 +41,10 @@ Gats::Object *Gats::Object::read( Bu::Stream &rIn ) pObj = new Gats::List(); break; + case 'd': + pObj = new Gats::Dictionary(); + break; + case 'e': return NULL; diff --git a/src/unit/basic.unit b/src/unit/basic.unit index 9389c70..744d679 100644 --- a/src/unit/basic.unit +++ b/src/unit/basic.unit @@ -119,4 +119,33 @@ suite Basic unitFailed("Bad string."); } } + + test dictionary + { + MemBuf mb; + + { + Gats::Dictionary dict; + dict.insert("bool", true ); + dict.insert("int", 33403055 ); + dict.insert("str", "Hey there" ); + dict.write( mb ); + } + + mb.setPos( 0 ); + + { + Gats::Object *pRead = Gats::Object::read( mb ); + unitTest( pRead != NULL ); + Gats::Dictionary *pDict = dynamic_cast(pRead); + unitTest( pDict != NULL ); + + unitTest( pDict->getBool("bool") == true ); + unitTest( pDict->getInt("int") == 33403055 ); + unitTest( pDict->getStr("str") == "Hey there" ); + unitTest( pDict->getSize() == 3 ); + + delete pDict; + } + } } -- cgit v1.2.3