From d534a56d95bca7bdd812be024d9eacba4734e2b7 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 9 Nov 2012 17:20:11 +0000 Subject: Many changes: tabconv'd the C++ code, added a license, BSD, and docs. --- c++-libbu++/src/dictionary.cpp | 373 +++++++++++++++++++++-------------------- 1 file changed, 190 insertions(+), 183 deletions(-) (limited to 'c++-libbu++/src/dictionary.cpp') diff --git a/c++-libbu++/src/dictionary.cpp b/c++-libbu++/src/dictionary.cpp index b59d652..c6b08a1 100644 --- a/c++-libbu++/src/dictionary.cpp +++ b/c++-libbu++/src/dictionary.cpp @@ -1,3 +1,10 @@ +/* + * 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" @@ -11,7 +18,7 @@ template<> uint32_t Bu::__calcHashCode( const Gats::String &s ) { - return __calcHashCode( dynamic_cast(s) ); + return __calcHashCode( dynamic_cast(s) ); } Gats::Dictionary::Dictionary() @@ -20,361 +27,361 @@ Gats::Dictionary::Dictionary() Gats::Dictionary::~Dictionary() { - for( iterator i = begin(); i; i++ ) - { - delete *i; - } + 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() ); - } + 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; + 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 ); + 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 ); + 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 ) - ); - } + ((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 ) - ); + ((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 ) - ); + ((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 ) - ); + ((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 ) - ); + ((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 ) - ); + ((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 ) - ); + ((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 ) - ); + ((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 ) - ); + ((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 ) - ); + ((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 ) - ); + ((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 ) - ); + ((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 ) - ); + 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 ) - ); + 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 ) - ); + 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 ) - ); + 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 ) - ); + 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 ) - ); + 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 ) - ); + 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 ) - ); + 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 ) - ); + Bu::Hash::insert( + sKey, new Gats::String( s ) + ); } void Gats::Dictionary::insertList( const Bu::String &sKey, Gats::List *pL ) { - Bu::Hash::insert( - sKey, pL - ); + Bu::Hash::insert( + sKey, pL + ); } void Gats::Dictionary::insertDict( const Bu::String &sKey, - Gats::Dictionary *pD ) + Gats::Dictionary *pD ) { - Bu::Hash::insert( - sKey, 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::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; + 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() ); + 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(); + 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() ); + 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(); + 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() ); + 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(); + 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() ); + 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; + 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() ); + 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; + 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() ); + 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; + 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() ); + 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(); + 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() ); + 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(); + 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() ); + 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(); + 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() ); + 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; + 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() ); + 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; + 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() ); + 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; + 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; + 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; } -- cgit v1.2.3