src/tools/values/vCore.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 
00024 ***************************************************************************
00025 
00026 */
00027 
00030 
00031 #ifndef ARMAGETRON_vCore_h
00032 #define ARMAGETRON_vCore_h
00033 
00034 #include "tString.h"
00035 #include <deque>
00036 #include <iomanip>
00037 #include <iostream>
00038 #include <boost/shared_ptr.hpp>
00039 #include <boost/variant.hpp>
00040 
00041 namespace vValue {
00042 namespace Expr {
00043 namespace Core {
00044 class Base;
00045 }
00046 }
00047 namespace Type {
00048 typedef boost::shared_ptr<Expr::Core::Base> BasePtr; 
00049 
00050 typedef boost::variant<int, float, std::string> Variant;
00051 typedef std::deque<BasePtr> arglist;
00052 }
00053 using namespace Type;
00054 
00055 namespace Expr {
00056 namespace Core {
00058 class Base {
00059     int m_precision; 
00060     int m_minsize;   
00061     char m_fill;     
00062 protected:
00064     template<typename T> inline tString Output(T value, Base const *other=0) const;
00065     //template<typename T> inline T VariantConvert(Variant const) const;
00066 public:
00067     Base(int precision=1, int minsize=0, char fill='0'); 
00068     Base(Base const &other); 
00069     virtual ~Base() { };
00070 
00071     virtual Base *copy(void) const; 
00072 
00073     virtual int GetInt(void) const; 
00074     virtual float GetFloat(void) const; 
00075     virtual tString GetString(Base const *other=0) const; 
00076     virtual Variant GetValue(void) const; 
00077 
00078     template<class T> T Get() const;
00079 
00080     void SetPrecision(int precision); 
00081     void SetMinsize(int size); 
00082     void SetFill(char fill); 
00083 
00084     operator int() const { return GetInt(); }
00085     operator float() const { return GetFloat(); }
00086     operator tString() const { return GetString(); }
00087 
00088     virtual bool operator==(Base const &other) const; 
00089     virtual bool operator!=(Base const &other) const; 
00090     virtual bool operator>=(Base const &other) const; 
00091     virtual bool operator<=(Base const &other) const; 
00092     virtual bool operator> (Base const &other) const; 
00093     virtual bool operator< (Base const &other) const; 
00094 };
00095 
00103 template<typename T> inline tString Base::Output(T value, Base const *other) const {
00104     if (other==0) other=this;
00105     std::ostringstream buf("");
00106     buf << std::setfill(other->m_fill) << std::setw(other->m_minsize) << std::fixed << std::setprecision(other->m_precision) << value; return buf.str();
00107 }
00108 
00109 /*
00110 template<typename T> inline T Base::VariantConvert(Variant v) const {
00111         if (T *ptr = boost::get<T>(v))
00112                 return *ptr;
00113         std::stringstream stream("");
00114         stream << v;
00115         T i;
00116         stream >> i;
00117     return i;
00118 }
00119 */
00120 
00121 }
00122 }
00123 namespace Type {
00124 
00126 class Set {
00127     BasePtr m_min; 
00128     BasePtr m_max; 
00129     BasePtr m_val; 
00130 public:
00131     /*
00132     Set(Base *val = new Base,
00133         Base *min = new Base,
00134         Base *max = new Base); //!< Default constructor
00135     */
00136     Set(); 
00137     Set(BasePtr &val); 
00138     Set(BasePtr &val, BasePtr &min, BasePtr &max); 
00139     //    Set(BasePtr &val = BasePtr(), BasePtr &min = BasePtr(), BasePtr &max = BasePtr()); //!< Constructor using std::auto_ptr
00140     Set(const Set &other); 
00141     ~Set(); 
00142     void operator=(Set const &other); 
00143     Expr::Core::
00144     Base const &GetMin(void) const {return *m_min;} 
00145     Expr::Core::
00146     Base const &GetMax(void) const {return *m_max;} 
00147     Expr::Core::
00148     Base const &GetVal(void) const {return *m_val;} 
00149 };
00150 }
00151 namespace Expr {
00152 namespace Core {
00153 
00157 template<typename T> class Number : public Base {
00158     T m_value; 
00159 public:
00160     Number(T value); 
00161     Number(Base const &other); 
00162     /*
00163     Number(BaseExt const &other); //!< Copy constructor
00164     */
00165     virtual ~Number() { };
00166 
00167     Base *copy(void) const;
00168 
00169     virtual Variant GetValue(void) const; 
00170     virtual int GetInt(void) const; 
00171     virtual float GetFloat(void) const; 
00172     virtual tString GetString(Base const *other=0) const; 
00173 
00174     bool operator==(Base const &other) const;
00175     bool operator!=(Base const &other) const;
00176     bool operator>=(Base const &other) const;
00177     bool operator<=(Base const &other) const;
00178     bool operator> (Base const &other) const;
00179     bool operator< (Base const &other) const;
00180 };
00181 
00184 template<typename T> Number<T>::Number(T value):
00185         Base(),
00186         m_value(value)
00187 { }
00188 
00191 template<typename T> Number<T>::Number(Base const &other):
00192         Base(other), // TODO: check if Base or BaseExt goes here
00193         m_value(static_cast<T>(other))
00194 { }
00195 
00196 /*
00199 template<typename T> Number<T>::Number(BaseExt const &other):
00200         BaseExt(other),
00201         m_value(static_cast<T>(other))
00202 { }
00203 */
00204 
00207 // TODO: Changed from Number<T> * to Base*
00208 template<typename T> Base *Number<T>::copy(void) const {
00209     return new Number(*this);
00210 }
00211 
00214 template<typename T> Variant Number<T>::GetValue(void) const {
00215     return static_cast<T>(m_value);
00216 }
00217 
00220 template<typename T> int Number<T>::GetInt(void) const {
00221     return static_cast<int>(m_value);
00222 }
00223 
00226 template<typename T> float Number<T>::GetFloat(void) const {
00227     return static_cast<float>(m_value);
00228 }
00229 
00232 template<typename T> tString Number<T>::GetString(Base const *other) const {
00233     return Output(m_value, other);
00234 }
00235 
00236 template<typename T> bool Number<T>::operator==(Base const &other) const {
00237     return m_value == static_cast<T>(other);
00238 }
00239 template<typename T> bool Number<T>::operator!=(Base const &other) const {
00240     return m_value != static_cast<T>(other);
00241 }
00242 template<typename T> bool Number<T>::operator>=(Base const &other) const {
00243     return m_value >= static_cast<T>(other);
00244 }
00245 template<typename T> bool Number<T>::operator<=(Base const &other) const {
00246     return m_value <= static_cast<T>(other);
00247 }
00248 template<typename T> bool Number<T>::operator> (Base const &other) const {
00249     return m_value > static_cast<T>(other);
00250 }
00251 template<typename T> bool Number<T>::operator< (Base const &other) const {
00252     return m_value < static_cast<T>(other);
00253 }
00254 
00255 typedef Number<float> Float;
00256 typedef Number<int> Int;
00257 
00259 class String : public Base {
00260     tString m_value; 
00261 public:
00262     String(tString value); 
00263     String(char const *value); 
00264     String(Base const &other); 
00265 
00266     Base *copy(void) const;
00267     virtual ~String() { };
00268 
00269     virtual Variant GetValue(void) const; 
00270     virtual tString GetString(Base const *other=0) const;
00271 
00272     bool operator==(Base const &other) const;
00273     bool operator!=(Base const &other) const;
00274     bool operator>=(Base const &other) const;
00275     bool operator<=(Base const &other) const;
00276     bool operator> (Base const &other) const;
00277     bool operator< (Base const &other) const;
00278 };
00279 
00281 class UnaryOp : public Base {
00282 protected:
00283     BasePtr m_value; 
00284 public:
00285     UnaryOp(BasePtr value); 
00286     UnaryOp(UnaryOp const &other); 
00287     virtual Base *copy(void) const; 
00288 };
00289 
00290 #define DeclStdUnaryOp(classname)       \
00291 class classname : public UnaryOp {      \
00292 public: \
00293     classname(BasePtr value) : UnaryOp(value) {}; \
00294     classname(UnaryOp const &other) : UnaryOp(other) {}; \
00295     virtual Variant GetValue(void) const; \
00296     virtual Base *copy(void) const; \
00297 };
00298 
00300 class BinaryOp : public Base {
00301 protected:
00302     BasePtr m_lvalue; 
00303     BasePtr m_rvalue; 
00304 public:
00305     BinaryOp(BasePtr lvalue, BasePtr rvalue); 
00306     BinaryOp(BinaryOp const &other); 
00307     virtual Base *copy(void) const; 
00308 };
00309 
00310 #define DeclStdBinaryOp(classname)      \
00311 class classname : public BinaryOp {     \
00312 public: \
00313     classname(BasePtr lvalue, BasePtr rvalue) : BinaryOp(lvalue, rvalue) {}; \
00314     classname(BinaryOp const &other) : BinaryOp(other) {}; \
00315     virtual Variant GetValue(void) const; \
00316     virtual Base *copy(void) const; \
00317 };
00318 
00319 }
00320 using namespace Core;
00321 }
00322 
00323 template<typename C> class Creator {
00324 public:
00325     static Expr::Base *create() {
00326         return new C();
00327     }
00328     template<typename T> static Expr::Base *create(T t) {
00329         return new C(t);
00330     }
00331     template<typename T, typename U> static Expr::Base *create(T t, U u) {
00332         return new C(t, u);
00333     }
00334     template<typename T, typename U, typename V> static Expr::Base *create(T t, U u, V v) {
00335         return new C(t, u, v);
00336     }
00337     template<typename T, typename U, typename V, typename W> static Expr::Base *create(T t, U u, V v, W w) {
00338         return new C(t, u, v, w);
00339     }
00340     template<typename T, typename U, typename V, typename W, typename X> static Expr::Base *create(T t, U u, V v, W w, X x) {
00341         return new C(t, u, v, w, x);
00342     }
00343 };
00344 
00345 
00346 using namespace Type;
00347 }
00348 
00349 #endif

Generated on Sat Mar 15 22:56:03 2008 for Armagetron Advanced by  doxygen 1.5.4