00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00111
00112
00113
00114
00115
00116
00117
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
00133
00134
00135
00136 Set();
00137 Set(BasePtr &val);
00138 Set(BasePtr &val, BasePtr &min, BasePtr &max);
00139
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
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),
00193 m_value(static_cast<T>(other))
00194 { }
00195
00196
00199
00200
00201
00202
00203
00204
00207
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