From c33bfc4ede827d3335e353fd35815c1613257942 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 19 Dec 2011 09:42:16 -0700 Subject: Variables are assignable and comperable now. Not >, <. >=, <=, but those should be really easy. --- src/variable.cpp | 182 +++++++++++++++++++++++++++++++++++++++++++++++++++---- src/variable.h | 48 ++++++++++----- 2 files changed, 203 insertions(+), 27 deletions(-) diff --git a/src/variable.cpp b/src/variable.cpp index fab0c46..a698b1c 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -1,41 +1,47 @@ #include "variable.h" +typedef Bu::ExceptionBase VariableException; + Variable::Variable() : - eType( Null ), + eType( tNull ), + iValue( 0 ) +{ +} + +Variable::Variable( const Variable &src ) : + eType( tNull ), iValue( 0 ) { + (*this) = src; } Variable::Variable( Type eType ) : eType( eType ), iValue( 0 ) { - if( eType == String ) - { - sValue = new Bu::String(); - } + initType(); } Variable::Variable( int64_t iValue ) : - eType( Int ), + eType( tInt ), iValue( iValue ) { } Variable::Variable( double fValue ) : - eType( Float ), + eType( tFloat ), fValue( fValue ) { } Variable::Variable( bool bValue ) : - eType( Bool ), + eType( tBool ), bValue( bValue ) { } Variable::Variable( const Bu::String &sValue ) : - eType( String ), + eType( tString ), iValue( 0 ) { this->sValue = new Bu::String( sValue ); @@ -43,7 +49,161 @@ Variable::Variable( const Bu::String &sValue ) : Variable::~Variable() { - if( eType == String ) - delete sValue; + 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; } diff --git a/src/variable.h b/src/variable.h index c9665f5..0236951 100644 --- a/src/variable.h +++ b/src/variable.h @@ -8,20 +8,22 @@ class Variable { +friend uint32_t Bu::__calcHashCode( const Variable &k ); public: enum Type { - Null, - Int, - Float, - Bool, - String, - List, - Dictionary + tNull, + tBool, + tInt, + tFloat, + tString, + tList, + tDictionary }; public: Variable(); + Variable( const Variable &src ); Variable( Type eType ); Variable( int64_t iValue ); Variable( double fValue ); @@ -29,20 +31,27 @@ public: Variable( const Bu::String &sValue ); virtual ~Variable(); + Type getType() const { return eType; } + Variable &operator=( const Variable &rhs ); Variable &operator+=( const Variable &rhs ); Variable &operator-=( const Variable &rhs ); Variable &operator*=( const Variable &rhs ); Variable &operator/=( const Variable &rhs ); - Variable &operator+( const Variable &rhs ); - Variable &operator-( const Variable &rhs ); - Variable &operator*( const Variable &rhs ); - Variable &operator/( const Variable &rhs ); - Variable &operator!=( const Variable &rhs ); - Variable &operator>( const Variable &rhs ); - Variable &operator<( const Variable &rhs ); - Variable &operator>=( const Variable &rhs ); - Variable &operator<=( const Variable &rhs ); + Variable &operator+( const Variable &rhs ) const; + Variable &operator-( const Variable &rhs ) const; + Variable &operator*( const Variable &rhs ) const; + Variable &operator/( const Variable &rhs ) const; + bool operator==( const Variable &rhs ) const; + bool operator!=( const Variable &rhs ) const; + bool operator>( const Variable &rhs ) const; + bool operator<( const Variable &rhs ) const; + bool operator>=( const Variable &rhs ) const; + bool operator<=( const Variable &rhs ) const; + +private: + void initType(); + void deinitType(); private: Type eType; @@ -61,4 +70,11 @@ private: }; }; +namespace Bu +{ + template<> uint32_t __calcHashCode( const Variable &k ); + template<> bool __cmpHashKeys( const Variable &a, const Variable &b ); +} + + #endif -- cgit v1.2.3