From df412b348f10ee46830e6e117b4bb0dd3b6b057b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 20 Dec 2011 09:43:36 -0700 Subject: Most of the variable type conversion routine is done. --- src/variable.cpp | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/variable.h | 10 +-- 2 files changed, 216 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/variable.cpp b/src/variable.cpp index a698b1c..6487474 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -1,5 +1,7 @@ #include "variable.h" +#include + typedef Bu::ExceptionBase VariableException; Variable::Variable() : @@ -52,6 +54,114 @@ Variable::~Variable() deinitType(); } +Variable Variable::to( Type e ) const +{ + if( e == eType ) + return *this; + + switch( eType ) + { + case tNull: + switch( e ) + { + case tNull: break; + case tBool: break; + case tInt: break; + case tFloat: break; + case tString: return Variable("(null)"); + case tList: break; + case tDictionary: break; + } + break; + + case tBool: + switch( e ) + { + case tNull: break; + case tBool: break; + case tInt: break; + case tFloat: break; + case tString: + return bValue?Variable("true"):Variable("false"); + case tList: break; + case tDictionary: break; + } + break; + + case tInt: + switch( e ) + { + case tNull: break; + case tBool: break; + case tInt: break; + case tFloat: return Variable( (double)iValue ); + case tString: + return Variable( Bu::String("%1").arg( iValue ) ); + case tList: break; + case tDictionary: break; + } + break; + + case tFloat: + switch( e ) + { + case tNull: break; + case tBool: break; + case tInt: return Variable( (int64_t)fValue ); + case tFloat: break; + case tString: + return Variable( Bu::String("%1").arg( fValue ) ); + case tList: break; + case tDictionary: break; + } + break; + + case tString: + switch( e ) + { + case tNull: break; + case tBool: break; + case tInt: + return Variable( strtoll(sValue->getStr(), NULL, 10 ) ); + case tFloat: + return Variable( strtod(sValue->getStr(), NULL ) ); + case tString: break; + case tList: break; + case tDictionary: break; + } + break; + + case tList: + switch( e ) + { + case tNull: break; + case tBool: break; + case tInt: break; + case tFloat: break; + case tString: break; + case tList: break; + case tDictionary: break; + } + break; + + case tDictionary: + switch( e ) + { + case tNull: break; + case tBool: break; + case tInt: break; + case tFloat: break; + case tString: break; + case tList: break; + case tDictionary: break; + } + break; + } + + throw VariableException("Could not convert from %d to %d.\n", + eType, e ); +} + Variable &Variable::operator=( const Variable &rhs ) { deinitType(); @@ -88,15 +198,110 @@ Variable &Variable::operator=( const Variable &rhs ) break; } } + +Variable &Variable::operator+=( const Variable &rhs ) +{ + switch( eType ) + { + case tNull: + throw VariableException("You cannot add nulls."); + + case tBool: + throw VariableException("You cannot add bools."); + + case tInt: + switch( rhs.eType ) + { + case tInt: + iValue += rhs.iValue; + break; + + case tFloat: + { + double dTmp = iValue; + eType = tFloat; + fValue = dTmp + rhs.fValue; + } + break; + + default: + throw VariableException("Int += invalid..."); + } + break; + + case tFloat: + switch( rhs.eType ) + { + case tInt: + fValue += rhs.iValue; + break; + + case tFloat: + fValue += rhs.fValue; + break; + + default: + throw VariableException("Int += invalid..."); + } + break; + + case tString: + (*sValue).append( *(rhs.to( tString ).sValue) ); + break; + } + + return *this; +} + /* -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; +*/ +Variable Variable::operator+( const Variable &rhs ) const +{ + if( eType != rhs.eType ) + { + throw VariableException("Adding between dissimilar types is not yet supported."); + } + else + { + switch( eType ) + { + case tNull: + throw VariableException("You cannot add nulls."); + + case tBool: + throw VariableException("You cannot add booleans."); + + case tInt: + return Variable( iValue + rhs.iValue ); + + case tFloat: + return Variable( fValue + rhs.fValue ); + + case tString: + return Variable( *sValue + *rhs.sValue ); + + case tList: + { + Variable vRet( tList ); + vRet.lValue->append( *lValue ); + vRet.lValue->append( *rhs.lValue ); + return vRet; + } + + case tDictionary: + throw VariableException("You cannot add dictionaries."); + break; + } + } +} + +/* +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 diff --git a/src/variable.h b/src/variable.h index 0236951..67f35c8 100644 --- a/src/variable.h +++ b/src/variable.h @@ -33,15 +33,17 @@ public: Type getType() const { return eType; } + Variable to( Type e ) const; + 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; + 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; -- cgit v1.2.3