unumber.hpp

00001 /* ==================================================== ======== ======= *
00002  *
00003  *  unumber.hpp
00004  *  Ubit Project
00005  *  Part of the Ubit Toolkit: A Brick Construction Game Model for Creating GUIs
00006  *  (C) 1999-2006 / Eric Lecolinet / ENST Paris / http://www.enst.fr/~elc/ubit
00007  *
00008  * ***********************************************************************
00009  * COPYRIGHT NOTICE : 
00010  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE 
00011  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 
00012  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU 
00013  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; 
00014  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
00015  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
00016  * ***********************************************************************
00017  * ==================================================== [(c)Elc] ======= *
00018  * ==================================================== ======== ======= */
00019 
00020 #ifndef _unumber_hpp_
00021 #define _unumber_hpp_
00022 #include <iostream>
00023 #include <ubit/ubrick.hpp>
00024 //pragma ident  "@(#)unumber.hpp        ubit:05.05.00"
00025 namespace ubit {
00026 
00027 /* ==================================================== ===== ======= */
00031 class UNumber: public UBrick {
00032 public:
00033   //UBIT_CLASS(UNumber, UBrick)
00034 
00035   UNumber(UMask b_modes = 0) {}
00036     
00037   virtual int    intValue()    const = 0;
00038   virtual float  floatValue()  const = 0;
00039   virtual double doubleValue() const = 0;
00040   virtual UStr   toString()    const = 0;
00041 
00042   virtual UNumber& onChange(UCall&);
00044 
00045   virtual void changed(bool = true);
00047 };
00048   
00049 /* ==================================================== ===== ======= */
00052 class UInteger : public UNumber {
00053 public:
00054   UBIT_CLASS(UInteger, UNumber)
00055 
00056   UInteger(int v = 0) : value(v) {}
00057   UInteger(const UInteger& v) : value(v.value) {}
00058   //UInteger(const char*);  // ambigu
00059   UInteger(const UStr&);
00060   UInteger(const std::string&);
00061 
00062   operator int() const {return value;}
00064 
00065   friend std::istream& operator>>(std::istream&, UInteger&);
00067 
00068   int    intValue()    const {return value;}
00069   float  floatValue()  const {return float(value);}
00070   double doubleValue() const {return double(value);}
00071   UStr   toString()    const;
00072 
00073   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00074 
00075   UInteger& operator=(int v)              {return setImpl(v);}
00076   UInteger& operator=(const UInteger& v)  {return setImpl(v.value);}
00077 
00078   UInteger& operator=(const char* s)      {return setImpl(s);}
00079   UInteger& operator=(const UStr& s);
00080   UInteger& operator=(const std::string& s);
00081 
00082   friend std::ostream& operator<<(std::ostream&, const UInteger&);
00084 
00085   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00086 
00087   bool operator==(int v)             const {return value == v;}
00088   bool operator==(double v)          const {return value == v;}
00089   bool operator==(const UInteger& v) const {return value == v.value;}
00090 
00091   bool operator!=(int v)             const {return value != v;}
00092   bool operator!=(double v)          const {return value != v;}
00093   bool operator!=(const UInteger& v) const {return value != v.value;}
00094 
00095   bool operator<(int v)              const {return value < v;}
00096   bool operator<(double v)           const {return value < v;}
00097   bool operator<(const UInteger& v)  const {return value < v.value;}
00098 
00099   bool operator<=(int v)             const {return value <= v;}
00100   bool operator<=(double v)          const {return value <= v;}
00101   bool operator<=(const UInteger& v) const {return value <= v.value;}
00102 
00103   bool operator>(int v)              const {return value > v;}
00104   bool operator>(double v)           const {return value > v;}
00105   bool operator>(const UInteger& v)  const {return value > v.value;}
00106 
00107   bool operator>=(int v)             const {return value >= v;}
00108   bool operator>=(double v)          const {return value >= v;}
00109   bool operator>=(const UInteger& v) const {return value >= v.value;}
00110   
00111   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00112 
00113   UInteger& operator++() {return setImpl(value+1);}
00114   UInteger  operator++(int);
00115   UInteger& operator--() {return setImpl(value-1);}
00116   UInteger  operator--(int);
00117 
00118   UInteger& operator+=(int v) {return setImpl(value+v);}
00119   UInteger& operator+=(const UInteger& v) {return setImpl(value+v.value);}
00120   UInteger& operator-=(int v) {return setImpl(value-v);}
00121   UInteger& operator-=(const UInteger& v) {return setImpl(value-v.value);}
00122   UInteger& operator*=(int v) {return setImpl(value*v);}
00123   UInteger& operator*=(const UInteger& v) {return setImpl(value*v.value);}
00124   UInteger& operator/=(int v) {return setImpl(value/v);}
00125   UInteger& operator/=(const UInteger& v) {return setImpl(value/v.value);}
00126   
00127   // - - - Impl. - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00128 
00129   virtual UInteger& setImpl(int, bool call_callbacks = true);
00130   virtual UInteger& setImpl(const char*, bool call_callbacks = true);
00132   
00133 private:
00134   friend class UFloat;
00135   friend class UDouble;
00136   int value;
00137 };
00138 
00139 /* ==================================================== ===== ======= */
00142 class UFloat : public UNumber {
00143 public:
00144   UBIT_CLASS(UFloat, UNumber)
00145 
00146   UFloat(float v = 0.0)     : value(v) {}
00147   UFloat(const UFloat& v)   : value(v.value) {}
00148   UFloat(const UInteger& v) : value(v.value) {}
00149   //UFloat(const char*);  ambigu
00150   UFloat(const UStr&);
00151   UFloat(const std::string&);
00152 
00153   operator float() const {return value;}
00155 
00156   friend std::ostream& operator<<(std::ostream&, const UFloat&);
00158 
00159   int    intValue()    const {return int(value);}
00160   float  floatValue()  const {return value;}
00161   double doubleValue() const {return double(value);}
00162   UStr   toString()    const;
00163   
00164    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00165 
00166   UFloat& operator=(float v)           {return setImpl(v);}
00167   UFloat& operator=(const UFloat& v)   {return setImpl(v.value);}
00168   UFloat& operator=(const UInteger& v) {return setImpl(v.value);}
00169   UFloat& operator=(const char* s)     {return setImpl(s);}
00170   UFloat& operator=(const UStr& s);
00171   UFloat& operator=(const std::string& s);
00172 
00173   friend std::istream& operator>>(std::istream&, UFloat&);
00175 
00176   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00177 
00178   bool operator==(float v)          const {return value == v;}
00179   bool operator==(double v)         const {return value == v;}
00180   bool operator==(const UFloat& v)  const {return value == v.value;}
00181 
00182   bool operator!=(float v)          const {return value != v;}
00183   bool operator!=(double v)         const {return value != v;}
00184   bool operator!=(const UFloat& v)  const {return value != v.value;}
00185 
00186   bool operator<(float v)           const {return value < v;}
00187   bool operator<(double v)          const {return value < v;}
00188   bool operator<(const UFloat& v)   const {return value < v.value;}
00189 
00190   bool operator<=(float v)          const {return value <= v;}
00191   bool operator<=(double v)         const {return value <= v;}
00192   bool operator<=(const UFloat& v)  const {return value <= v.value;}
00193 
00194   bool operator>(float v)           const {return value > v;}
00195   bool operator>(double v)          const {return value > v;}
00196   bool operator>(const UFloat& v)   const {return value > v.value;}
00197 
00198   bool operator>=(float v)          const {return value >= v;}
00199   bool operator>=(double v)         const {return value >= v;}
00200   bool operator>=(const UFloat& v)  const {return value >= v.value;}
00201 
00202   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00203 
00204   UFloat& operator++() {return setImpl(value+1);}
00205   UFloat  operator++(int);
00206   UFloat& operator--() {return setImpl(value-1);}
00207   UFloat  operator--(int);
00208 
00209   UFloat& operator+=(int v) {return setImpl(value+v);}
00210   UFloat& operator+=(const UFloat& v) {return setImpl(value+v.value);}
00211   UFloat& operator-=(int v) {return setImpl(value-v);}
00212   UFloat& operator-=(const UFloat& v) {return setImpl(value-v.value);}
00213   UFloat& operator*=(int v) {return setImpl(value*v);}
00214   UFloat& operator*=(const UFloat& v) {return setImpl(value*v.value);}
00215   UFloat& operator/=(int v) {return setImpl(value/v);}
00216   UFloat& operator/=(const UFloat& v) {return setImpl(value/v.value);}
00217 
00218   // - - - Impl. - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00219 
00220   virtual UFloat& setImpl(float, bool call_callbacks = true);
00221   virtual UFloat& setImpl(const char*, bool call_callbacks = true);
00223   
00224 private:
00225   friend class UInteger;
00226   friend class UDouble;
00227   float value;
00228 };
00229 
00230 /* ==================================================== ===== ======= */
00233 class UDouble : public UNumber {
00234 public:
00235   UBIT_CLASS(UDouble, UNumber)
00236 
00237   UDouble(double v = 0.0)    : value(v) {}
00238   UDouble(const UDouble& v)  : value(v.value) {}
00239   UDouble(const UFloat& v)   : value(v.value) {}
00240   UDouble(const UInteger& v) : value(v.value) {}
00241   //UDouble(const char*);  //ambigu
00242   UDouble(const UStr&);
00243   UDouble(const std::string&);
00244 
00245   operator double() const {return value;}
00247 
00248   friend std::ostream& operator<<(std::ostream&, const UDouble&);
00250 
00251   int    intValue()    const {return int(value);}
00252   float  floatValue()  const {return float(value);}
00253   double doubleValue() const {return value;}
00254   UStr   toString()    const;
00255 
00256   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00257 
00258   UDouble& operator=(double v)           {return setImpl(v);}
00259   UDouble& operator=(const UDouble& v)   {return setImpl(v.value);}
00260   UDouble& operator=(const UFloat& v)    {return setImpl(v.value);}
00261   UDouble& operator=(const UInteger& v)  {return setImpl(v.value);}
00262 
00263   UDouble& operator=(const char* s)      {return setImpl(s);}
00264   UDouble& operator=(const UStr& s);
00265   UDouble& operator=(const std::string& s);
00266 
00267   friend std::istream& operator>>(std::istream&, UDouble&);
00269 
00270   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00271 
00272   bool operator==(float v)          const {return value == v;}
00273   bool operator==(double v)         const {return value == v;}
00274   bool operator==(const UDouble& v) const {return value == v.value;}
00275 
00276   bool operator!=(float v)          const {return value != v;}
00277   bool operator!=(double v)         const {return value != v;}
00278   bool operator!=(const UDouble& v) const {return value != v.value;}
00279 
00280   bool operator<(float v)           const {return value < v;}
00281   bool operator<(double v)          const {return value < v;}
00282   bool operator<(const UDouble& v)  const {return value < v.value;}
00283 
00284   bool operator<=(float v)          const {return value <= v;}
00285   bool operator<=(double v)         const {return value <= v;}
00286   bool operator<=(const UDouble& v) const {return value <= v.value;}
00287 
00288   bool operator>(float v)           const {return value > v;}
00289   bool operator>(double v)          const {return value > v;}
00290   bool operator>(const UDouble& v)  const {return value > v.value;}
00291 
00292   bool operator>=(float v)          const {return value >= v;}
00293   bool operator>=(double v)         const {return value >= v;}
00294   bool operator>=(const UDouble& v) const {return value >= v.value;}
00295   
00296   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00297 
00298   UDouble& operator++() {return setImpl(value+1);}
00299   UDouble  operator++(int);
00300   UDouble& operator--() {return setImpl(value-1);}
00301   UDouble  operator--(int);
00302 
00303   UDouble& operator+=(int v) {return setImpl(value+v);}
00304   UDouble& operator+=(const UDouble& v) {return setImpl(value+v.value);}
00305   UDouble& operator-=(int v) {return setImpl(value-v);}
00306   UDouble& operator-=(const UDouble& v) {return setImpl(value-v.value);}
00307   UDouble& operator*=(int v) {return setImpl(value*v);}
00308   UDouble& operator*=(const UDouble& v) {return setImpl(value*v.value);}
00309   UDouble& operator/=(int v) {return setImpl(value/v);}
00310   UDouble& operator/=(const UDouble& v) {return setImpl(value/v.value);}
00311 
00312   // - - - Impl. - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00313 
00314   virtual UDouble& setImpl(double, bool call_callbacks = true);
00315   virtual UDouble& setImpl(const char*, bool call_callbacks = true);
00317 
00318 private:
00319   friend class UInteger;
00320   friend class UFloat;
00321   double value;
00322 };
00323 
00324 }
00325 #endif
00326 /* ==================================================== [TheEnd] ======= */
00327 /* ==================================================== [(c)Elc] ======= */

Generated on Mon Jan 29 00:20:39 2007 for Ubit by  doxygen 1.4.7