From c10c9ef627c7e79fde6170fe334238bbcb5d66e5 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 27 Aug 2010 21:53:54 +0000 Subject: Added formatter handlers for debugging, works really well. Also added a bunch more helpers to make it as easy to use as possible. --- src/boolean.cpp | 6 ++++ src/boolean.h | 2 ++ src/dictionary.cpp | 23 ++++++++++++++++ src/dictionary.h | 2 ++ src/float.cpp | 7 +++++ src/float.h | 2 ++ src/integer.cpp | 7 +++++ src/integer.h | 2 ++ src/list.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/list.h | 19 +++++++++++++ src/object.cpp | 28 +++++++++++++++++++ src/object.h | 3 ++ src/string.cpp | 22 +++++++++++++++ src/string.h | 5 ++++ 14 files changed, 209 insertions(+) diff --git a/src/boolean.cpp b/src/boolean.cpp index 087845a..e0600f2 100644 --- a/src/boolean.cpp +++ b/src/boolean.cpp @@ -1,5 +1,6 @@ #include "gats/boolean.h" +#include #include Gats::Boolean::Boolean() : @@ -40,3 +41,8 @@ void Gats::Boolean::read( Bu::Stream &rIn, char cType ) } } +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Boolean &b ) +{ + return f << "(bool) " << b.getValue(); +} + diff --git a/src/boolean.h b/src/boolean.h index 3035b32..270e578 100644 --- a/src/boolean.h +++ b/src/boolean.h @@ -24,4 +24,6 @@ namespace Gats }; }; +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Boolean &b ); + #endif diff --git a/src/dictionary.cpp b/src/dictionary.cpp index 1a9549f..b789bbf 100644 --- a/src/dictionary.cpp +++ b/src/dictionary.cpp @@ -6,6 +6,8 @@ #include "gats/string.h" #include "gats/list.h" +#include + template<> uint32_t Bu::__calcHashCode( const Gats::String &s ) { @@ -156,3 +158,24 @@ Gats::Dictionary *Gats::Dictionary::getDict( const Bu::FString &sKey ) 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; +} + diff --git a/src/dictionary.h b/src/dictionary.h index 8f4b949..39248d2 100644 --- a/src/dictionary.h +++ b/src/dictionary.h @@ -43,4 +43,6 @@ template<> uint32_t __calcHashCode( const Gats::String &s ); }; +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Dictionary &d ); + #endif diff --git a/src/float.cpp b/src/float.cpp index e257b37..139df3e 100644 --- a/src/float.cpp +++ b/src/float.cpp @@ -1,6 +1,8 @@ #include "gats/float.h" #include "gats/integer.h" +#include + Gats::Float::Float() : fVal( 0.0 ) { @@ -35,3 +37,8 @@ void Gats::Float::read( Bu::Stream &rIn, char cType ) sscanf( buf, "%la", &fVal ); } +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Float &flt ) +{ + return f << "(float) " << flt.getValue(); +} + diff --git a/src/float.h b/src/float.h index 1b3a06a..27233ae 100644 --- a/src/float.h +++ b/src/float.h @@ -23,4 +23,6 @@ namespace Gats }; } +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Float &flt ); + #endif diff --git a/src/integer.cpp b/src/integer.cpp index ad48eaf..2a713e3 100644 --- a/src/integer.cpp +++ b/src/integer.cpp @@ -1,5 +1,7 @@ #include "gats/integer.h" +#include + Gats::Integer::Integer() : iVal( 0 ) { @@ -25,3 +27,8 @@ void Gats::Integer::read( Bu::Stream &rIn, char cType ) readPackedInt( rIn, iVal ); } +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Integer &i ) +{ + return f << "(int) " << i.getValue(); +} + diff --git a/src/integer.h b/src/integer.h index 6ed7c49..be7c808 100644 --- a/src/integer.h +++ b/src/integer.h @@ -75,4 +75,6 @@ namespace Gats }; }; +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Integer &i ); + #endif diff --git a/src/list.cpp b/src/list.cpp index 995a764..0e5f56f 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -1,5 +1,11 @@ #include "gats/list.h" +#include "gats/string.h" +#include "gats/integer.h" +#include "gats/float.h" +#include "gats/boolean.h" + +#include #include Gats::List::List() @@ -31,3 +37,78 @@ void Gats::List::read( Bu::Stream &rIn, char cType ) } } +void Gats::List::append( const char *s ) +{ + Bu::List::append( new Gats::String( s ) ); +} + +void Gats::List::append( const Bu::FString &s ) +{ + Bu::List::append( new Gats::String( s ) ); +} + +void Gats::List::append( int32_t i ) +{ + Bu::List::append( new Gats::Integer( i ) ); +} + +void Gats::List::append( int64_t i ) +{ + Bu::List::append( new Gats::Integer( i ) ); +} + +void Gats::List::append( bool b ) +{ + Bu::List::append( new Gats::Boolean( b ) ); +} + +void Gats::List::append( double d ) +{ + Bu::List::append( new Gats::Float( d ) ); +} + + +void Gats::List::prepend( const char *s ) +{ + Bu::List::prepend( new Gats::String( s ) ); +} + +void Gats::List::prepend( const Bu::FString &s ) +{ + Bu::List::prepend( new Gats::String( s ) ); +} + +void Gats::List::prepend( int32_t i ) +{ + Bu::List::prepend( new Gats::Integer( i ) ); +} + +void Gats::List::prepend( int64_t i ) +{ + Bu::List::prepend( new Gats::Integer( i ) ); +} + +void Gats::List::prepend( bool b ) +{ + Bu::List::prepend( new Gats::Boolean( b ) ); +} + +void Gats::List::prepend( double d ) +{ + Bu::List::prepend( new Gats::Float( d ) ); +} + + +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::List &l ) +{ + f << "(list) ["; + f.incIndent(); + for( Gats::List::const_iterator i = l.begin(); i; i++ ) + { + f << f.nl << **i; + } + f.decIndent(); + f << f.nl << "]"; + return f; +} + diff --git a/src/list.h b/src/list.h index 48faf23..69cb846 100644 --- a/src/list.h +++ b/src/list.h @@ -3,6 +3,7 @@ #include "gats/object.h" #include +#include namespace Gats { @@ -16,7 +17,25 @@ namespace Gats virtual void write( Bu::Stream &rOut ) const; virtual void read( Bu::Stream &rIn, char cType ); + + void append( const char *s ); + void append( const Bu::FString &s ); + void append( int32_t i ); + void append( int64_t i ); + void append( bool b ); + void append( double d ); + using Bu::List::append; + + void prepend( const char *s ); + void prepend( const Bu::FString &s ); + void prepend( int32_t i ); + void prepend( int64_t i ); + void prepend( bool b ); + void prepend( double d ); + using Bu::List::prepend; }; }; +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::List &l ); + #endif diff --git a/src/object.cpp b/src/object.cpp index 9176b49..f5148ad 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -7,6 +7,7 @@ #include "gats/list.h" #include "gats/dictionary.h" +#include #include Gats::Object::Object() @@ -61,3 +62,30 @@ Gats::Object *Gats::Object::read( Bu::Stream &rIn ) return pObj; } +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj ) +{ + switch( obj.getType() ) + { + case Gats::typeDictionary: + return f << dynamic_cast(obj); + + case Gats::typeList: + return f << dynamic_cast(obj); + + case Gats::typeString: + return f << dynamic_cast(obj); + + case Gats::typeInteger: + return f << dynamic_cast(obj); + + case Gats::typeFloat: + return f << dynamic_cast(obj); + + case Gats::typeBoolean: + return f << dynamic_cast(obj); + + default: + return f << "***ERROR: Bad Gats type***"; + } +} + diff --git a/src/object.h b/src/object.h index 10bdc47..44f9da7 100644 --- a/src/object.h +++ b/src/object.h @@ -4,6 +4,7 @@ namespace Bu { class Stream; + class Formatter; }; namespace Gats @@ -36,4 +37,6 @@ namespace Gats }; }; +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj ); + #endif diff --git a/src/string.cpp b/src/string.cpp index 416375d..2134a01 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -2,10 +2,27 @@ #include "gats/integer.h" +#include + Gats::String::String() { } +Gats::String::String( const char *s ) : + Bu::FString( s ) +{ +} + +Gats::String::String( const char *s, long iLength ) : + Bu::FString( s, iLength ) +{ +} + +Gats::String::String( long iLength ) : + Bu::FString( iLength ) +{ +} + Gats::String::String( const String &s ) : Bu::FString( s ) { @@ -36,3 +53,8 @@ void Gats::String::read( Bu::Stream &rIn, char cType ) rIn.read( getStr(), iSize ); } +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::String &s ) +{ + return f << "(str) \"" << dynamic_cast(s) << "\""; +} + diff --git a/src/string.h b/src/string.h index 5343ba8..ea13517 100644 --- a/src/string.h +++ b/src/string.h @@ -10,6 +10,9 @@ namespace Gats { public: String(); + String( const char *s ); + String( const char *s, long iLength ); + String( long iLength ); String( const String &s ); String( const Bu::FString &s ); virtual ~String(); @@ -23,4 +26,6 @@ namespace Gats }; }; +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::String &s ); + #endif -- cgit v1.2.3