src/engine/Variable.h

Go to the documentation of this file.
00001 
00011 #ifndef VARIABLE_H
00012 #define VARIABLE_H
00013 
00014 #include <iostream>
00015 #include <boost/shared_ptr.hpp>
00016 
00017 namespace green
00018 {
00019 
00025         class Variable
00026         {
00027         public:
00029                 enum Type {FLOAT_VAR, INT_VAR};
00030                 
00035                 union Value {
00036                         Value() : intVal(0) {}
00037                         Value(int ival) : intVal(ival) {}
00038                         Value(float fval) : floatVal(fval) {}
00039                         int intVal; 
00040                         float floatVal; 
00041                 };
00042                 
00044                 Variable() : mType(INT_VAR) {mValue.intVal = 0;}
00045                 
00047                 Variable(int copy) {
00048                         mType = INT_VAR;
00049                         mValue.intVal = copy;
00050                 }
00051                 
00053                 Variable(float copy) {
00054                         mType = FLOAT_VAR;
00055                         mValue.floatVal = copy;
00056                 }
00057 
00063                 bool operator==(const Variable& rhs) const
00064                 {
00065                         bool ret = false;
00066                         if(rhs.mType == mType)
00067                         {
00068                                 switch(mType)
00069                                 {
00070                                         case INT_VAR:
00071                                                 if(mValue.intVal == rhs.mValue.intVal)
00072                                                         ret = true;
00073                                                 break;
00074                                         case FLOAT_VAR:
00075                                                 if(mValue.floatVal == rhs.mValue.floatVal)
00076                                                         ret = true;
00077                                                 break;
00078                                 }
00079                         }
00080                         return ret;
00081                 }
00082                 
00088                 Variable& operator=(int value)
00089                 {
00090                         mType = INT_VAR;
00091                         mValue.intVal = value;
00092                         return *this;
00093                 }
00094                 
00100                 Variable& operator=(float value)
00101                 {
00102                         mType = FLOAT_VAR;
00103                         mValue.floatVal = value;
00104                         return *this;
00105                 }
00106 
00112                 void Print(std::ostream& os) const
00113                 {
00114 //                      os << "Type: ";
00115 //                      switch(mType)
00116 //                      {
00117 //                              case INT_VAR:
00118 //                                      os << "INT_VAR VALUE: " << mValue.intVal;
00119 //                                      break;
00120 //                              case FLOAT_VAR:
00121 //                                      os << "FLOAT_VAR VALUE: " << mValue.floatVal;
00122 //                                      break;
00123 //                      }                       
00124                 }
00125                 
00127                 virtual ~Variable() {}
00128 
00130                 inline Variable::Type GetType() const { return mType; }
00131 
00135                 inline void SetType(Variable::Type type) { mType = type; }
00136 
00138                 inline Variable::Value GetValue() const { return mValue; }
00139 
00143                 inline void SetValue(Variable::Value value) {mValue = value; }
00144                 
00148                  inline void SetValue(int value) { *this = value; }
00149                  
00153                  inline void SetValue(float value) { *this = value; }
00154                  
00155         private:
00156                 Variable::Value mValue; 
00157                 Variable::Type mType; 
00158         };
00159         typedef boost::shared_ptr<Variable> VariablePtr;
00160         
00166         inline
00167         std::ostream& operator<<(std::ostream& lhs, const Variable& rhs)
00168         {
00169                 rhs.Print(lhs);
00170                 return lhs;
00171         }
00172         
00177         inline
00178         std::ostream& operator<<(std::ostream& lhs, const Variable::Type& rhs)
00179         {
00180                 switch(rhs)
00181                 {
00182                         case Variable::INT_VAR:
00183                                 lhs << "INT_VAR";
00184                                 break;
00185                         case Variable::FLOAT_VAR:
00186                                 lhs << "FLOAT_VAR";
00187                                 break;
00188                         default:
00189                                 lhs << "Unknown Variable::Type";
00190                                 break;
00191                 }
00192                 lhs << std::flush;
00193                 return lhs;
00194         }
00195 
00200         inline
00201                         std::ostream& operator<<(std::ostream& lhs, const Variable::Value& rhs)
00202         {
00203                 lhs << "(" << rhs.intVal << ", " << rhs.floatVal << ")" << std::flush;
00204                 return lhs;
00205         }
00206         
00212         inline
00213         void PrintValue(const Variable::Value& value, Variable::Type type, std::ostream& os)
00214         {
00215                 switch(type)
00216                 {
00217                         case Variable::INT_VAR:
00218                                 os << value.intVal;
00219                                 break;
00220                         case Variable::FLOAT_VAR:
00221                                 os << value.floatVal;
00222                                 break;
00223                         default:
00224                                 os << "unknown Variable::Type" << std::endl;
00225                                 break;
00226                 }
00227         }
00228 }
00229 #endif

Generated on Fri Apr 27 10:27:38 2007 for Green Engine by  doxygen 1.5.1