/* * 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-qt/dictionary.h" #include "gats-qt/boolean.h" #include "gats-qt/integer.h" #include "gats-qt/float.h" #include "gats-qt/string.h" #include "gats-qt/list.h" Gats::Dictionary::Dictionary() { } Gats::Dictionary::~Dictionary() { for( iterator i = begin(); i != end(); i++ ) { delete *i; } } Gats::Object *Gats::Dictionary::clone() const { Gats::Dictionary *pClone = new Gats::Dictionary; for( const_iterator i = begin(); i != end(); i++ ) { QByteArray bKey = i.key(); bKey.detach(); pClone->insert( bKey, (*i)->clone() ); } return pClone; } void Gats::Dictionary::write( QIODevice &rOut ) const { rOut.write("d", 1 ); for( const_iterator i= begin(); i != end(); i++ ) { Gats::String s( i.key() ); s.write( rOut ); (*i)->write( rOut ); } rOut.write("e", 1 ); } void Gats::Dictionary::read( QIODevice &rIn, char cType ) { for(;;) { char cNext; rIn.read( &cNext, 1 ); if( cNext == 'e' ) break; if( cNext != 's' ) throw "PUT GOOD EXCEPTION HERE"; Gats::String sKey; sKey.read( rIn, cNext ); ((QHash *)this)->insert( sKey, Gats::Object::read( rIn ) ); } } void Gats::Dictionary::insert( const QByteArray &sKey, char i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, unsigned char i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, signed char i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, unsigned short i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, signed short i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, unsigned int i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, signed int i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, unsigned long i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, signed long i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, unsigned long long i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, signed long long i ) { ((QHash *)this)->insert( sKey, new Gats::Integer( i ) ); } /* void Gats::Dictionary::insert( const QByteArray &sKey, bool b ) { QHash::insert( sKey, new Gats::Boolean( b ) ); }*/ void Gats::Dictionary::insert( const QByteArray &sKey, float d ) { QHash::insert( sKey, new Gats::Float( d ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, double d ) { QHash::insert( sKey, new Gats::Float( d ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, const char *s ) { QHash::insert( sKey, new Gats::String( s ) ); } void Gats::Dictionary::insert( const QByteArray &sKey, const QByteArray &s ) { QHash::insert( sKey, new Gats::String( s ) ); } void Gats::Dictionary::insertBool( const QByteArray &sKey, bool b ) { QHash::insert( sKey, new Gats::Boolean( b ) ); } void Gats::Dictionary::insertInt( const QByteArray &sKey, int64_t i ) { QHash::insert( sKey, new Gats::Integer( i ) ); } void Gats::Dictionary::insertFloat( const QByteArray &sKey, double d ) { QHash::insert( sKey, new Gats::Float( d ) ); } void Gats::Dictionary::insertStr( const QByteArray &sKey, const QByteArray &s ) { QHash::insert( sKey, new Gats::String( s ) ); } void Gats::Dictionary::insertList( const QByteArray &sKey, Gats::List *pL ) { QHash::insert( sKey, pL ); } void Gats::Dictionary::insertDict( const QByteArray &sKey, Gats::Dictionary *pD ) { QHash::insert( sKey, pD ); } Gats::List *Gats::Dictionary::insertList( const QByteArray &sKey ) { Gats::List *pLst = new Gats::List(); insertList( sKey, pLst ); return pLst; } Gats::Dictionary *Gats::Dictionary::insertDict( const QByteArray &sKey ) { Gats::Dictionary *pDict = new Gats::Dictionary(); insertDict( sKey, pDict ); return pDict; } bool Gats::Dictionary::valueBool( const QByteArray &sKey ) { Gats::Boolean *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return pOb->getValue(); } int64_t Gats::Dictionary::valueInt( const QByteArray &sKey ) { Gats::Integer *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return pOb->getValue(); } double Gats::Dictionary::valueFloat( const QByteArray &sKey ) { Gats::Float *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return pOb->getValue(); } QByteArray Gats::Dictionary::valueStr( const QByteArray &sKey ) { Gats::String *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return *pOb; } Gats::List *Gats::Dictionary::valueList( const QByteArray &sKey ) { Gats::List *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return pOb; } Gats::Dictionary *Gats::Dictionary::valueDict( const QByteArray &sKey ) { Gats::Dictionary *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return pOb; } bool Gats::Dictionary::valueBool( const QByteArray &sKey ) const { Gats::Boolean *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return pOb->getValue(); } int64_t Gats::Dictionary::valueInt( const QByteArray &sKey ) const { Gats::Integer *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return pOb->getValue(); } double Gats::Dictionary::valueFloat( const QByteArray &sKey ) const { Gats::Float *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return pOb->getValue(); } QByteArray Gats::Dictionary::valueStr( const QByteArray &sKey ) const { Gats::String *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return *pOb; } Gats::List *Gats::Dictionary::valueList( const QByteArray &sKey ) const { Gats::List *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; return pOb; } Gats::Dictionary *Gats::Dictionary::valueDict( const QByteArray &sKey ) const { Gats::Dictionary *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) throw "PUT GOOD EXCEPTION HERE"; 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.valueKey().valueSize() > iMax ) iMax = i.valueKey().valueSize(); } iMax += 2; for( Gats::Dictionary::const_iterator i = d.begin(); i; i++ ) { f << f.nl << Bu::Fmt( iMax ) << i.valueKey() + ": " << *i.getValue(); } f.decIndent(); f << f.nl << "}"; return f; } */