#include "variable.h" typedef Bu::ExceptionBase VariableException; Variable::Variable() : eType( tNull ), iValue( 0 ) { } Variable::Variable( const Variable &src ) : eType( tNull ), iValue( 0 ) { (*this) = src; } Variable::Variable( Type eType ) : eType( eType ), iValue( 0 ) { initType(); } Variable::Variable( int64_t iValue ) : eType( tInt ), iValue( iValue ) { } Variable::Variable( double fValue ) : eType( tFloat ), fValue( fValue ) { } Variable::Variable( bool bValue ) : eType( tBool ), bValue( bValue ) { } Variable::Variable( const Bu::String &sValue ) : eType( tString ), iValue( 0 ) { this->sValue = new Bu::String( sValue ); } Variable::~Variable() { deinitType(); } Variable &Variable::operator=( const Variable &rhs ) { deinitType(); eType = rhs.eType; initType(); switch( eType ) { case tNull: break; case tBool: bValue = rhs.bValue; break; case tInt: iValue = rhs.iValue; break; case tFloat: fValue = rhs.fValue; break; case tString: (*sValue) = *rhs.sValue; break; case tList: (*lValue) = *rhs.lValue; break; case tDictionary: (*hValue) = *rhs.hValue; break; } } /* Variable &Variable::operator+=( const Variable &rhs ); Variable &Variable::operator-=( const Variable &rhs ); Variable &Variable::operator*=( const Variable &rhs ); Variable &Variable::operator/=( const Variable &rhs ); Variable &Variable::operator+( const Variable &rhs ) const; Variable &Variable::operator-( const Variable &rhs ) const; Variable &Variable::operator*( const Variable &rhs ) const; Variable &Variable::operator/( const Variable &rhs ) const; */ bool Variable::operator==( const Variable &rhs ) const { if( eType != rhs.eType ) return false; switch( eType ) { case tNull: return true; case tBool: return bValue == rhs.bValue; case tInt: return iValue == rhs.iValue; case tFloat: return fValue == rhs.fValue; case tString: return (*sValue) == (*rhs.sValue); case tList: return (*lValue) == (*rhs.lValue); case tDictionary: return (*hValue) == (*rhs.hValue); } } bool Variable::operator!=( const Variable &rhs ) const { return !(*this == rhs); } /* Variable &Variable::operator>( const Variable &rhs ); Variable &Variable::operator<( const Variable &rhs ); Variable &Variable::operator>=( const Variable &rhs ); Variable &Variable::operator<=( const Variable &rhs ); */ void Variable::initType() { iValue = 0; switch( eType ) { case tString: sValue = new Bu::String(); break; case tList: lValue = new VList(); break; case tDictionary: hValue = new VHash(); break; } } void Variable::deinitType() { switch( eType ) { case tString: delete sValue; break; case tList: delete lValue; break; case tDictionary: delete hValue; break; } iValue = 0; } template<> uint32_t Bu::__calcHashCode( const Variable &k ) { switch( k.getType() ) { case Variable::tNull: return 0; case Variable::tInt: return k.iValue; case Variable::tFloat: return k.fValue; case Variable::tString: return Bu::__calcHashCode( *k.sValue ); case Variable::tList: throw VariableException("You cannot use a list as a key in a dictionary."); case Variable::tDictionary: throw VariableException("You cannot use a dictionary as a key in a dictionary."); } } template<> bool Bu::__cmpHashKeys( const Variable &a, const Variable &b ) { return a == b; }