- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/include/frost_utils_uint.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 : unsigned 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_uint_t
    00023     {
    00024     public :
    00025
    00026         enum Type
    00027         {
    00028             INTEGER,
    00029             INTEGER_INF,
    00030             INTEGER_NAN
    00031         };
    00032
    00033         s_uint_t();
    00034
    00035         s_uint_t(const T& uiValue);
    00036
    00037         explicit s_uint_t(const Type& mType);
    00038
    00039         explicit s_uint_t(const float& fValue);
    00040
    00041         explicit s_uint_t(const double& dValue);
    00042
    00043         template<class N>
    00044         explicit s_uint_t(const s_uint_t<N>& uiValue)
    00045         {
    00046             mType_ = static_cast<Type>(uiValue.GetType());
    00047             uiValue_ = static_cast<T>(uiValue.Get());
    00048         }
    00049
    00050         template<class N>
    00051         explicit s_uint_t(const s_int_t<N>& iValue)
    00052         {
    00053             if (iValue.GetType() == s_int_t<N>::INTEGER_NAN)
    00054             {
    00055                 mType_ = INTEGER_NAN;
    00056                 uiValue_ = 0u;
    00057             }
    00058             else if ( (iValue.GetType() == s_int_t<N>::INTEGER) && (iValue >= 0u) )
    00059             {
    00060                 mType_ = INTEGER;
    00061                 uiValue_ = static_cast<uint>(iValue.Get());
    00062             }
    00063             else
    00064             {
    00065                 mType_ = INTEGER_INF;
    00066                 uiValue_ = 0u;
    00067             }
    00068         }
    00069
    00070         template<class N>
    00071         explicit s_uint_t(const s_float_t<N>& fValue)
    00072         {
    00073             if (fValue.GetType() == s_float_t<N>::FLOAT_NAN)
    00074             {
    00075                 mType_ = INTEGER_NAN;
    00076                 uiValue_ = 0u;
    00077             }
    00078             else if ( (fValue.GetType() == s_float_t<N>::FLOAT) && (fValue >= 0.0f) )
    00079             {
    00080                 mType_ = INTEGER;
    00081                 uiValue_ = static_cast<T>(round(fValue.Get()));
    00082             }
    00083             else
    00084             {
    00085                 mType_ = INTEGER_INF;
    00086                 uiValue_ = 0u;
    00087             }
    00088         }
    00089
    00090         explicit s_uint_t(const s_bool& bValue);
    00091
    00092         template<class N>
    00093         explicit s_uint_t(const s_str_t<N>& sValue)
    00094         {
    00095             typename TypeTraits<T>::SignedType i = StringToInt(sValue);
    00096
    00097             if (i < 0)
    00098             {
    00099                 mType_ = INTEGER_INF;
    00100                 uiValue_ = 0u;
    00101             }
    00102             else
    00103             {
    00104                 mType_ = INTEGER;
    00105                 uiValue_ = static_cast<T>(i);
    00106             }
    00107         }
    00108
    00109         /// Returns a const reference to the uint.
    00110         /** \return A const reference to the uint
    00111         */
    00112         inline const T& Get() const { return uiValue_; }
    00113
    00114         /// Returns a reference to the uint.
    00115         /** \return A reference to the uint
    00116         */
    00117         inline T& GetR() { return uiValue_; }
    00118
    00119         /// Adjusts this uint's value to be contained into the provided interval.
    00120         /** \param uiMin The minimum value
    00121         *   \param uiMax The maximum value
    00122         */
    00123         void Clamp(const s_uint_t& uiMin, const s_uint_t& uiMax);
    00124
    00125         /// Returns the power of two just above the value (or equal).
    00126         /** \return The associated power of two (2^n) (superior of equal)
    00127         */
    00128         s_uint_t GetNearestPowerOfTwo() const;
    00129
    00130         /// Returns the type of this uint.
    00131         /** \return The type of this uint (infinite, NaN, ...)
    00132         */
    00133         Type GetType() const;
    00134
    00135         /// Checks if this uint is infinite
    00136         /** \return 'true' if this uint is infinite
    00137         */
    00138         s_bool IsInfinite() const;
    00139
    00140         /// Checks if this uint is contained into the provided range.
    00141         s_bool IsInRange(const s_uint_t<T>& uiMin, const s_uint_t<T>& uiMax) const;
    00142
    00143         /// Checks if this uint is a Not a Number (NaN)
    00144         /** \return 'true' if this uint is NaN
    00145         */
    00146         s_bool IsNaN() const;
    00147
    00148         /// Checks if this uint equals zero.
    00149         /** \return 'true' if this uint equals zero
    00150         */
    00151         s_bool IsNull() const;
    00152
    00153         /// Checks if this uint is a valid number.
    00154         /** \return 'true' if this uint is not infinite and a number
    00155         */
    00156         s_bool IsValid() const;
    00157
    00158         /// Elevates this uint to a certain power (this^n).
    00159         /** \param uiPower The power...
    00160         */
    00161         void Pow(const s_uint_t& uiPower);
    00162
    00163         /// Sets the value of the uint to a random number.
    00164         /** \param uiMin The lower bound (minimum)
    00165         *   \param uiMax The upper bound (maximum)
    00166         */
    00167         void Randomize(const s_uint_t& uiMin = 0u, const s_uint_t& uiMax = 1u);
    00168
    00169         /// Sets this uint to infinite.
    00170         void SetInfinite();
    00171
    00172         /// Set this uint to Not a Number state.
    00173         void SetNaN();
    00174
    00175         s_int_t<typename TypeTraits<T>::SignedType> operator - () const;
    00176
    00177         s_uint_t& operator ++ ();
    00178         s_uint_t operator ++ (int);
    00179         s_uint_t& operator -- ();
    00180         s_uint_t operator -- (int);
    00181
    00182         s_uint_t operator + (const s_uint_t& uiValue) const;
    00183         s_uint_t operator - (const s_uint_t& uiValue) const;
    00184         s_uint_t operator * (const s_uint_t& uiValue) const;
    00185         s_uint_t operator / (const s_uint_t& uiValue) const;
    00186         s_uint_t operator % (const s_uint_t& uiValue) const;
    00187
    00188         void operator += (const s_uint_t& uiValue);
    00189         void operator -= (const s_uint_t& uiValue);
    00190         void operator *= (const s_uint_t& uiValue);
    00191         void operator /= (const s_uint_t& uiValue);
    00192         void operator %= (const s_uint_t& uiValue);
    00193
    00194         s_uint_t& operator << (const s_uint_t& uiValue);
    00195
    00196         s_bool operator == (const s_uint_t& uiValue) const;
    00197         s_bool operator != (const s_uint_t& uiValue) const;
    00198         s_bool operator < (const s_uint_t& uiValue) const;
    00199         s_bool operator > (const s_uint_t& uiValue) const;
    00200         s_bool operator <= (const s_uint_t& uiValue) const;
    00201         s_bool operator >= (const s_uint_t& uiValue) const;
    00202
    00203         static const s_uint_t NaN;
    00204         static const s_uint_t INF;
    00205
    00206         /// Returns the lowest value of the two provided ones.
    00207         /** \param uiLeft  The first value
    00208         *   \param uiRight The second value
    00209         *   \return The lowest value of the two provided ones
    00210         */
    00211         static s_uint_t Min(const s_uint_t& uiLeft, const s_uint_t& uiRight);
    00212
    00213         /// Returns the highest value of the two provided ones.
    00214         /** \param uiLeft  The first value
    00215         *   \param uiRight The second value
    00216         *   \return The highest value of the two provided ones
    00217         */
    00218         static s_uint_t Max(const s_uint_t& uiLeft, const s_uint_t& uiRight);
    00219
    00220         /// Clamps the provided value into the provided interval.
    00221         /** \param uiValue The value to clamp
    00222         *   \param uiMin   The minimum value
    00223         *   \param uiMax   The maximum value
    00224         *   \return The clamped value
    00225         */
    00226         static s_uint_t Clamp(const s_uint_t& uiValue, const s_uint_t& uiMin, const s_uint_t& uiMax);
    00227
    00228         /// Returns a random uint in the provided range.
    00229         /** \param uiMin The lower bound (minimum)
    00230         *   \param uiMax The upper bound (maximum)
    00231         *   \return A random uint in the provided range
    00232         */
    00233         static s_uint_t Random(const s_uint_t& uiMin = 0u, const s_uint_t& uiMax = 1u);
    00234
    00235     private :
    00236
    00237         T uiValue_;
    00238
    00239         Type mType_;
    00240     };
    00241
    00242     template<class T>
    00243     s_uint_t<T> operator + (const T& uiLeft, const s_uint_t<T>& uiRight);
    00244     template<class T>
    00245     s_uint_t<T> operator - (const T& uiLeft, const s_uint_t<T>& uiRight);
    00246     template<class T>
    00247     s_uint_t<T> operator * (const T& uiLeft, const s_uint_t<T>& uiRight);
    00248     template<class T>
    00249     s_uint_t<T> operator / (const T& uiLeft, const s_uint_t<T>& uiRight);
    00250     template<class T>
    00251     s_uint_t<T> operator % (const T& uiLeft, const s_uint_t<T>& uiRight);
    00252
    00253     template<class T>
    00254     s_uint_t<T> operator + (const typename TypeTraits<T>::SignedType& iLeft, const s_uint_t<T>& uiRight);
    00255     template<class T>
    00256     s_uint_t<T> operator - (const typename TypeTraits<T>::SignedType& iLeft, const s_uint_t<T>& uiRight);
    00257     template<class T>
    00258     s_uint_t<T> operator * (const typename TypeTraits<T>::SignedType& iLeft, const s_uint_t<T>& uiRight);
    00259     template<class T>
    00260     s_uint_t<T> operator / (const typename TypeTraits<T>::SignedType& iLeft, const s_uint_t<T>& uiRight);
    00261     template<class T>
    00262     s_uint_t<T> operator % (const typename TypeTraits<T>::SignedType& iLeft, const s_uint_t<T>& uiRight);
    00263
    00264     /** \cond NOT_REMOVE_FROM_DOC
    00265     */
    00266     template<> class TypeTraits<uchar>
    00267     {
    00268     public :
    00269         typedef uchar           Type;
    00270         typedef uchar           BaseType;
    00271         typedef s_uint_t<uchar> FrostType;
    00272         typedef uchar           CharType;
    00273         typedef uchar&          RefType;
    00274         typedef uchar           CRefType;
    00275         typedef uchar*          PointerType;
    00276
    00277         typedef uchar IsInteger;
    00278         typedef char  SignedType;
    00279
    00280         static inline RefType  GetValue(RefType m)  { return m; }
    00281         static inline CRefType GetValue(CRefType m) { return m; }
    00282     };
    00283     template<> class TypeTraits<ushort>
    00284     {
    00285     public :
    00286         typedef ushort           Type;
    00287         typedef ushort           BaseType;
    00288         typedef s_uint_t<ushort> FrostType;
    00289         typedef ushort&          RefType;
    00290         typedef ushort           CRefType;
    00291         typedef ushort*          PointerType;
    00292
    00293         typedef ushort IsInteger;
    00294         typedef short  SignedType;
    00295
    00296         static inline RefType  GetValue(RefType m)  { return m; }
    00297         static inline CRefType GetValue(CRefType m) { return m; }
    00298     };
    00299     template<> class TypeTraits<uint>
    00300     {
    00301     public :
    00302         typedef uint           Type;
    00303         typedef uint           BaseType;
    00304         typedef s_uint_t<uint> FrostType;
    00305         typedef uint           CharType;
    00306         typedef uint&          RefType;
    00307         typedef const uint&    CRefType;
    00308         typedef uint*          PointerType;
    00309
    00310         typedef uint IsInteger;
    00311         typedef int  SignedType;
    00312
    00313         static inline RefType  GetValue(RefType m)  { return m; }
    00314         static inline CRefType GetValue(CRefType m) { return m; }
    00315     };
    00316     template<> class TypeTraits<ulong>
    00317     {
    00318     public :
    00319         typedef ulong           Type;
    00320         typedef ulong           BaseType;
    00321         typedef s_uint_t<ulong> FrostType;
    00322         typedef ulong&          RefType;
    00323         typedef const ulong&    CRefType;
    00324         typedef ulong*          PointerType;
    00325
    00326         typedef ulong IsInteger;
    00327         typedef long  SignedType;
    00328
    00329         static inline RefType  GetValue(RefType m)  { return m; }
    00330         static inline CRefType GetValue(CRefType m) { return m; }
    00331     };
    00332     template<> class TypeTraits<unsigned long long>
    00333     {
    00334     public :
    00335         typedef unsigned long long           Type;
    00336         typedef unsigned long long           BaseType;
    00337         typedef s_uint_t<unsigned long long> FrostType;
    00338         typedef unsigned long long&          RefType;
    00339         typedef const unsigned long long&    CRefType;
    00340         typedef unsigned long long*          PointerType;
    00341
    00342         typedef unsigned long long IsInteger;
    00343         typedef long long          SignedType;
    00344
    00345         static inline RefType  GetValue(RefType m)  { return m; }
    00346         static inline CRefType GetValue(CRefType m) { return m; }
    00347     };
    00348
    00349     template<class T> class TypeTraits< s_uint_t<T> >
    00350     {
    00351     public :
    00352         typedef s_uint_t<T>        Type;
    00353         typedef T                  BaseType;
    00354         typedef s_uint_t<T>        FrostType;
    00355         typedef s_uint_t<T>&       RefType;
    00356         typedef const s_uint_t<T>& CRefType;
    00357         typedef s_uint_t<T>*       PointerType;
    00358
    00359         typedef s_uint_t<T> IsInteger;
    00360         typedef s_int_t<typename TypeTraits<T>::SignedType> SignedType;
    00361
    00362         static inline typename TypeTraits<BaseType>::RefType  GetValue(RefType m)  { return m.Get(); }
    00363         static inline typename TypeTraits<BaseType>::CRefType GetValue(CRefType m) { return m.Get(); }
    00364     };
    00365     /** \endcond
    00366     */
    00367 }