- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/include/frost_utils_int.h

Go to the documentation of this file.

    00001 // Warning : If you need to use this file, include frost_utils_types.h
    00002 namespace Frost
    00003 {
    00004     /// Base type : integer
    00005     /** Frost's base types are made to allow simpler
    00006     *   manipulation of numbers, booleans and strings.
    00007     *   They simplify conversions (mostly to string),
    00008     *   allow easy access to math/string functions and
    00009     *   implements new functions that are not present
    00010     *   in the standard libraries.<br>
    00011     *   <b>Note</b> : Most of Frost's base types are
    00012     *   slower than C++ base types. The difference is
    00013     *   negligible for small operations, but if you need
    00014     *   to do a lot of calculations (matrices, ...) it
    00015     *   can bloat your performances. Use prebuilt types
    00016     *   in this case.
    00017     *   <b>Note</b> : This class is about 60 times slower
    00018     *   than the wrapped POD (20 times in debug, without
    00019     *   any compiler optimizations).
    00020     */
    00021     template<class T>
    00022     class s_int_t
    00023     {
    00024     public :
    00025
    00026         enum Type
    00027         {
    00028             INTEGER,
    00029             INTEGER_INF_PLUS,
    00030             INTEGER_INF_MINUS,
    00031             INTEGER_NAN
    00032         };
    00033
    00034         s_int_t();
    00035
    00036         s_int_t(const T& iValue);
    00037
    00038         explicit s_int_t(const Type& mType);
    00039
    00040         explicit s_int_t(const float& fValue);
    00041
    00042         explicit s_int_t(const double& dValue);
    00043
    00044         template<class N>
    00045         explicit s_int_t(const s_int_t<N>& iValue)
    00046         {
    00047             mType_ = static_cast<Type>(iValue.GetType());
    00048             iValue_ = static_cast<T>(iValue.Get());
    00049         }
    00050
    00051         template<class N>
    00052         explicit s_int_t(const s_uint_t<N>& uiValue)
    00053         {
    00054             if (uiValue.GetType() == s_uint_t<N>::INTEGER_NAN)
    00055             {
    00056                 mType_ = INTEGER_NAN;
    00057                 iValue_ = 0;
    00058             }
    00059             else if (uiValue.GetType() == s_uint_t<N>::INTEGER_INF)
    00060             {
    00061                 mType_ = INTEGER_INF_PLUS;
    00062                 iValue_ = 0;
    00063             }
    00064             else
    00065             {
    00066                 mType_ = INTEGER;
    00067                 iValue_ = static_cast<T>(uiValue.Get());
    00068             }
    00069         }
    00070
    00071         template<class N>
    00072         explicit s_int_t(const s_float_t<N>& fValue)
    00073         {
    00074             if (fValue.GetType() == s_float_t<N>::FLOAT_NAN)
    00075             {
    00076                 mType_ = INTEGER_NAN;
    00077                 iValue_ = 0;
    00078             }
    00079             else if (fValue.GetType() == s_float_t<N>::FLOAT_INF_PLUS)
    00080             {
    00081                 mType_ = INTEGER_INF_PLUS;
    00082                 iValue_ = 0;
    00083             }
    00084             else if (fValue.GetType() == s_float_t<N>::FLOAT_INF_MINUS)
    00085             {
    00086                 mType_ = INTEGER_INF_MINUS;
    00087                 iValue_ = 0;
    00088             }
    00089             else
    00090             {
    00091                 mType_ = INTEGER;
    00092                 iValue_ = static_cast<T>(round(fValue.Get()));
    00093             }
    00094         }
    00095
    00096         explicit s_int_t(const s_bool& bValue);
    00097
    00098         template<class N>
    00099         explicit s_int_t(const s_str_t<N>& sValue)
    00100         {
    00101             mType_ = INTEGER;
    00102             iValue_ = StringToInt(sValue);
    00103         }
    00104
    00105         /// Returns a const reference to the int.
    00106         /** \return A const reference to the int
    00107         */
    00108         inline const T& Get() const { return iValue_; }
    00109
    00110         /// Returns a reference to the int.
    00111         /** \return A reference to the int
    00112         */
    00113         inline T& GetR() { return iValue_; }
    00114
    00115         /// Adjusts this int's value to be contained into the provided interval.
    00116         /** \param iMin The minimum value
    00117         *   \param iMax The maximum value
    00118         */
    00119         void Clamp(const s_int_t& iMin, const s_int_t& iMax);
    00120
    00121         /// Returns the power of two just above the value (or equal).
    00122         /** \return The associated power of two (2^n) (superior of equal)
    00123         */
    00124         s_uint_t<typename TypeTraits<T>::UnsignedType> GetNearestPowerOfTwo() const;
    00125
    00126         /// Returns the type of this int.
    00127         /** \return The type of this int (infinite, NaN, ...)
    00128         */
    00129         Type GetType() const;
    00130
    00131         /// Checks if this int is infinite and negative.
    00132         /** \return 'true' if this int is infinite and negative
    00133         */
    00134         s_bool IsInfiniteMinus() const;
    00135
    00136         /// Checks if this int is infinite and positive.
    00137         /** \return 'true' if this int is infinite and positive
    00138         */
    00139         s_bool IsInfinitePlus() const;
    00140
    00141         /// Checks if this int is contained into the provided range.
    00142         s_bool IsInRange(const s_int_t<T>& iMin, const s_int_t<T>& iMax) const;
    00143
    00144         /// Checks if this int is a Not a Number (NaN).
    00145         /** \return 'true' if this int is NaN
    00146         */
    00147         s_bool IsNaN() const;
    00148
    00149         /// Checks if this int equals zero.
    00150         /** \return 'true' if this int equals zero
    00151         */
    00152         s_bool IsNull() const;
    00153
    00154         /// Checks if this int is a valid number.
    00155         /** \return 'true' if this int is not infinite and a number
    00156         */
    00157         s_bool IsValid() const;
    00158
    00159         /// Checks if this int is an infinite number.
    00160         /** \return 'true' if this int is an infinite number
    00161         */
    00162         s_bool IsInfinite() const;
    00163
    00164         /// Elevates this int to a certain power (this^n).
    00165         /** \param uiPower The power
    00166         */
    00167         void Pow(const s_uint_t<typename TypeTraits<T>::UnsignedType>& uiPower);
    00168
    00169         /// Sets the value of the int to a random number.
    00170         /** \param iMin The lower bound (minimum)
    00171         *   \param iMax The upper bound (maximum)
    00172         */
    00173         void Randomize(const s_int_t& iMin = 0, const s_int_t& iMax = 1);
    00174
    00175         /// Sets this int to infinite (negative).
    00176         void SetInfiniteMinus();
    00177
    00178         /// Sets this int to infinite (positive).
    00179         void SetInfinitePlus();
    00180
    00181         /// Set this int to Not a Number state.
    00182         void SetNaN();
    00183
    00184         /// Returns the sign of this int (either 1 or -1).
    00185         /** \return The sign of this int (either 1 or -1)
    00186         */
    00187         s_int_t GetSign() const;
    00188
    00189         s_int_t& operator ++ ();
    00190
    00191         s_int_t operator ++ (int);
    00192
    00193         s_int_t operator - () const;
    00194
    00195         s_int_t& operator -- ();
    00196
    00197         s_int_t operator -- (int);
    00198
    00199         s_int_t operator + (const s_int_t& iValue) const;
    00200
    00201         s_int_t operator - (const s_int_t& iValue) const;
    00202
    00203         s_int_t operator * (const s_int_t& iValue) const;
    00204
    00205         s_int_t operator / (const s_int_t& iValue) const;
    00206
    00207         s_int_t operator % (const s_int_t& iValue) const;
    00208
    00209         void operator += (const s_int_t& iValue);
    00210
    00211         void operator -= (const s_int_t& iValue);
    00212
    00213         void operator *= (const s_int_t& iValue);
    00214
    00215         void operator /= (const s_int_t& iValue);
    00216
    00217         void operator %= (const s_int_t& iValue);
    00218
    00219         template<class N>
    00220         s_int_t& operator << (const s_uint_t<N>& uiValue)
    00221         {
    00222             if (IsValid())
    00223             {
    00224                 iValue_ = iValue_ << uiValue.Get();
    00225             }
    00226             return *this;
    00227         }
    00228
    00229         s_int_t& operator << (const uint& uiValue);
    00230
    00231         s_bool operator == (const s_int_t& iValue) const;
    00232
    00233         s_bool operator != (const s_int_t& iValue) const;
    00234
    00235         s_bool operator < (const s_int_t& iValue) const;
    00236
    00237         s_bool operator > (const s_int_t& iValue) const;
    00238
    00239         s_bool operator <= (const s_int_t& iValue) const;
    00240
    00241         s_bool operator >= (const s_int_t& iValue) const;
    00242
    00243         static const s_int_t NaN;
    00244         static const s_int_t INFPLUS;
    00245         static const s_int_t INFMINUS;
    00246
    00247         /// Returns the lowest value of the two provided ones.
    00248         /** \param iLeft  The first value
    00249         *   \param iRight The second value
    00250         *   \return The lowest value of the two provided ones
    00251         */
    00252         static s_int_t Min(const s_int_t& iLeft, const s_int_t& iRight);
    00253
    00254         /// Returns the highest value of the two provided ones.
    00255         /** \param iLeft  The first value
    00256         *   \param iRight The second value
    00257         *   \return The highest value of the two provided ones
    00258         */
    00259         static s_int_t Max(const s_int_t& iLeft, const s_int_t& iRight);
    00260
    00261         /// Clamps the provided value into the provided interval.
    00262         /** \param iValue The value to clamp
    00263         *   \param iMin   The minimum value
    00264         *   \param iMax   The maximum value
    00265         *   \return The clamped value
    00266         */
    00267         static s_int_t Clamp(const s_int_t& iValue, const s_int_t& iMin, const s_int_t& iMax);
    00268
    00269         /// Returns a random int in the provided range.
    00270         /** \param iMin The lower bound (minimum)
    00271         *   \param iMax The upper bound (maximum)
    00272         *   \return A random int in the provided range
    00273         */
    00274         static s_int_t Random(const s_int_t& iMin = 0, const s_int_t& iMax = 1);
    00275
    00276         /// Returns the sign of the provided int (either 1 or -1).
    00277         /** \return The sign of the provided int (either 1 or -1)
    00278         */
    00279         s_int_t Sign(const s_int_t& iValue) const;
    00280
    00281     private :
    00282
    00283         T iValue_;
    00284
    00285         Type mType_;
    00286     };
    00287
    00288     template<class T>
    00289     s_int_t<T> operator + (const T& iLeft, const s_int_t<T>& iRight);
    00290     template<class T>
    00291     s_int_t<T> operator - (const T& iLeft, const s_int_t<T>& iRight);
    00292     template<class T>
    00293     s_int_t<T> operator * (const T& iLeft, const s_int_t<T>& iRight);
    00294     template<class T>
    00295     s_int_t<T> operator / (const T& iLeft, const s_int_t<T>& iRight);
    00296     template<class T>
    00297     s_int_t<T> operator % (const T& iLeft, const s_int_t<T>& iRight);
    00298
    00299     template<class T>
    00300     s_int_t<T> operator + (const typename TypeTraits<T>::UnsignedType& uiLeft, const s_int_t<T>& iRight);
    00301     template<class T>
    00302     s_int_t<T> operator - (const typename TypeTraits<T>::UnsignedType& uiLeft, const s_int_t<T>& iRight);
    00303     template<class T>
    00304     s_int_t<T> operator * (const typename TypeTraits<T>::UnsignedType& uiLeft, const s_int_t<T>& iRight);
    00305     template<class T>
    00306     s_int_t<T> operator / (const typename TypeTraits<T>::UnsignedType& uiLeft, const s_int_t<T>& iRight);
    00307     template<class T>
    00308     s_int_t<T> operator % (const typename TypeTraits<T>::UnsignedType& uiLeft, const s_int_t<T>& iRight);
    00309
    00310     /** \cond NOT_REMOVE_FROM_DOC
    00311     */
    00312     template<> class TypeTraits<char>
    00313     {
    00314     public :
    00315         typedef char          Type;
    00316         typedef char          BaseType;
    00317         typedef s_int_t<char> FrostType;
    00318         typedef char          CharType;
    00319         typedef char&         RefType;
    00320         typedef const char&   CRefType;
    00321         typedef char*         PointerType;
    00322
    00323         typedef char  IsInteger;
    00324         typedef uchar UnsignedType;
    00325
    00326         static inline RefType  GetValue(RefType m)  { return m; }
    00327         static inline CRefType GetValue(CRefType m) { return m; }
    00328     };
    00329     template<> class TypeTraits<short>
    00330     {
    00331     public :
    00332         typedef short          Type;
    00333         typedef short          BaseType;
    00334         typedef s_int_t<short> FrostType;
    00335         typedef short&         RefType;
    00336         typedef const short&   CRefType;
    00337         typedef short*         PointerType;
    00338
    00339         typedef short  IsInteger;
    00340         typedef ushort UnsignedType;
    00341
    00342         static inline RefType  GetValue(RefType m)  { return m; }
    00343         static inline CRefType GetValue(CRefType m) { return m; }
    00344     };
    00345     template<> class TypeTraits<int>
    00346     {
    00347     public :
    00348         typedef int          Type;
    00349         typedef int          BaseType;
    00350         typedef s_int_t<int> FrostType;
    00351         typedef int&         RefType;
    00352         typedef const int&   CRefType;
    00353         typedef int*         PointerType;
    00354
    00355         typedef int  IsInteger;
    00356         typedef uint UnsignedType;
    00357
    00358         static inline RefType  GetValue(RefType m)  { return m; }
    00359         static inline CRefType GetValue(CRefType m) { return m; }
    00360     };
    00361     template<> class TypeTraits<long>
    00362     {
    00363     public :
    00364         typedef long          Type;
    00365         typedef long          BaseType;
    00366         typedef s_int_t<long> FrostType;
    00367         typedef long&         RefType;
    00368         typedef const long&   CRefType;
    00369         typedef long*         PointerType;
    00370
    00371         typedef long  IsInteger;
    00372         typedef ulong UnsignedType;
    00373
    00374         static inline RefType  GetValue(RefType m)  { return m; }
    00375         static inline CRefType GetValue(CRefType m) { return m; }
    00376     };
    00377     template<> class TypeTraits<long long>
    00378     {
    00379     public :
    00380         typedef long long          Type;
    00381         typedef long long          BaseType;
    00382         typedef s_int_t<long long> FrostType;
    00383         typedef long long&         RefType;
    00384         typedef const long long&   CRefType;
    00385         typedef long long*         PointerType;
    00386
    00387         typedef long long          IsInteger;
    00388         typedef unsigned long long UnsignedType;
    00389
    00390         static inline RefType  GetValue(RefType m)  { return m; }
    00391         static inline CRefType GetValue(CRefType m) { return m; }
    00392     };
    00393
    00394     template<class T> class TypeTraits< s_int_t<T> >
    00395     {
    00396     public :
    00397         typedef s_int_t<T>        Type;
    00398         typedef T                 BaseType;
    00399         typedef s_int_t<T>        FrostType;
    00400         typedef s_int_t<T>&       RefType;
    00401         typedef const s_int_t<T>& CRefType;
    00402         typedef s_int_t<T>*       PointerType;
    00403
    00404         typedef s_int_t<T> IsInteger;
    00405         typedef s_uint_t<typename TypeTraits<T>::UnsignedType> UnsignedType;
    00406
    00407         static inline typename TypeTraits<BaseType>::RefType  GetValue(RefType m)  { return m.GetR(); }
    00408         static inline typename TypeTraits<BaseType>::CRefType GetValue(CRefType m) { return m.Get(); }
    00409     };
    00410     /** \endcond
    00411     */
    00412 }