• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/Int32.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _int32_h
00018 #define _int32_h
00019 
00020 #include <spl/types.h>
00021 #include <spl/Numeric.h>
00022 #include <spl/String.h>
00023 
00024 #ifdef HAVE_LIMITS_H
00025 #include <limits.h>
00026 #endif
00027 
00034 #define INT32_MAJIC 0x0010              //< Majic number for ASSERT's in Compare and Convert
00035 
00039 class Int32 : public Numeric
00040 {
00041 private:
00042         int32 m_val;
00043 
00044 public:
00045         inline Int32()
00046         : m_val(0)
00047         {
00048         }
00049 
00050         inline Int32(const Int32& i)
00051         : m_val(i.m_val)
00052         {
00053 
00054         }       
00055         inline Int32(const int32 i)
00056         : m_val(i)
00057         {
00058         }
00059         
00060         virtual ~Int32();
00061 
00062         virtual int32 HashCode() const;
00063 
00064         virtual bool Equals(const IComparable *a) const;
00065         virtual int Compare(const IComparable *a) const;
00066         virtual bool Equals( const IComparable& a ) const;
00067         virtual int Compare( const IComparable& a ) const;
00068         virtual int32 MajicNumber() const;
00069 
00070         virtual int ToInt() const;
00071         virtual double ToDouble() const;
00072         virtual StringPtr ToString() const;
00073 
00074         inline operator int32() const
00075         {
00076                 return m_val;
00077         }
00078         
00079         inline bool operator ==(const int32 i)
00080         {
00081                 return m_val == i;
00082         }
00083 
00084         inline bool operator !=(const int32 i)
00085         {
00086                 return m_val != i;
00087         }
00088         
00089         inline bool operator >(const int32 i)
00090         {
00091                 return m_val > i;
00092         }
00093         
00094         inline bool operator <(const int32 i)
00095         {
00096                 return m_val < i;
00097         }
00098         
00099         inline bool operator >=(const int32 i)
00100         {
00101                 return m_val >= i;
00102         }
00103         
00104         inline bool operator <=(const int32 i)
00105         {
00106                 return m_val <= i;
00107         }
00108         
00109 #ifdef DEBUG
00110         virtual void ValidateMem() const;
00111         virtual void CheckMem() const;
00112 #endif
00113 
00114         static int32 Parse(const char *cp, const int cplen, int radix);
00115         inline static int32 Parse(const char *cp, const int cplen) { return Parse(cp, cplen, 10); }
00116         inline static int32 Parse(const String& str) { return Parse(str.GetChars(), str.Length(), 10); }
00117         inline static int32 Parse(const String *str) { return Parse(str->GetChars(), str->Length(), 10); }
00118         inline static int32 Parse(const String& str, int radix) { return Parse(str.GetChars(), str.Length(), radix); }
00119         inline static int32 Parse(const String *str, int radix) { return Parse(str->GetChars(), str->Length(), radix); }
00120 
00121 
00122         static bool IsInt(const char *cp, const int cplen, int radix);  
00123         inline static bool IsInt(const char *cp, const int cplen) { return IsInt(cp, cplen, 10); }
00124         inline static bool IsInt(const String& str) { return IsInt(str.GetChars(), str.Length(), 10); }
00125         inline static bool IsInt(const String *str) { return IsInt(str->GetChars(), str->Length(), 10); }
00126         inline static bool IsInt(const String& str, int radix) { return IsInt(str.GetChars(), str.Length(), radix); }
00127         inline static bool IsInt(const String *str, int radix) { return IsInt(str->GetChars(), str->Length(), radix); }
00128 
00129         static StringPtr ToString(int32 i, int radix);
00130         inline static StringPtr ToString(int32 i) { return ToString(i, 10); }
00131 
00132         inline static int MaxValue() { return INT_MAX; }
00133         inline static int MinValue() { return INT_MIN; }
00134 };
00135 
00136 REGISTER_TYPEOF( 566, Int32 );
00137 
00140 #endif