PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
int64.h
Go to the documentation of this file.
1 #ifndef PTLIB_INT64_H
2 #define PTLIB_INT64_H
3 
5 // Really big integer class for architectures without
6 
7 #ifdef P_NEEDS_INT64
8 
9 class PInt64__ {
10  public:
11  operator long() const { return (long)low; }
12  operator int() const { return (int)low; }
13  operator short() const { return (short)low; }
14  operator char() const { return (char)low; }
15 
16  operator unsigned long() const { return (unsigned long)low; }
17  operator unsigned int() const { return (unsigned int)low; }
18  operator unsigned short() const { return (unsigned short)low; }
19  operator unsigned char() const { return (unsigned char)low; }
20 
21  protected:
22  PInt64__() { }
23  PInt64__(unsigned long l) : low(l), high(0) { }
24  PInt64__(unsigned long l, unsigned long h) : low(l), high(h) { }
25 
26  void operator=(const PInt64__ & v) { low = v.low; high = v.high; }
27 
28  void Inc() { if (++low == 0) ++high; }
29  void Dec() { if (--low == 0) --high; }
30 
31  void Or (long v) { low |= v; }
32  void And(long v) { low &= v; }
33  void Xor(long v) { low ^= v; }
34 
35  void Add(const PInt64__ & v);
36  void Sub(const PInt64__ & v);
37  void Mul(const PInt64__ & v);
38  void Div(const PInt64__ & v);
39  void Mod(const PInt64__ & v);
40  void Or (const PInt64__ & v) { low |= v.low; high |= v.high; }
41  void And(const PInt64__ & v) { low &= v.low; high &= v.high; }
42  void Xor(const PInt64__ & v) { low ^= v.low; high ^= v.high; }
43  void ShiftLeft(int bits);
44  void ShiftRight(int bits);
45 
46  PBoolean Eq(unsigned long v) const { return low == v && high == 0; }
47  PBoolean Ne(unsigned long v) const { return low != v || high != 0; }
48 
49  PBoolean Eq(const PInt64__ & v) const { return low == v.low && high == v.high; }
50  PBoolean Ne(const PInt64__ & v) const { return low != v.low || high != v.high; }
51 
52  unsigned long low, high;
53 };
54 
55 
56 #define DECL_OPS(cls, type) \
57  const cls & operator=(type v) { PInt64__::operator=(cls(v)); return *this; } \
58  cls operator+(type v) const { cls t = *this; t.Add(v); return t; } \
59  cls operator-(type v) const { cls t = *this; t.Sub(v); return t; } \
60  cls operator*(type v) const { cls t = *this; t.Mul(v); return t; } \
61  cls operator/(type v) const { cls t = *this; t.Div(v); return t; } \
62  cls operator%(type v) const { cls t = *this; t.Mod(v); return t; } \
63  cls operator|(type v) const { cls t = *this; t.Or (v); return t; } \
64  cls operator&(type v) const { cls t = *this; t.And(v); return t; } \
65  cls operator^(type v) const { cls t = *this; t.Xor(v); return t; } \
66  cls operator<<(type v) const { cls t = *this; t.ShiftLeft((int)v); return t; } \
67  cls operator>>(type v) const { cls t = *this; t.ShiftRight((int)v); return t; } \
68  const cls & operator+=(type v) { Add(v); return *this; } \
69  const cls & operator-=(type v) { Sub(v); return *this; } \
70  const cls & operator*=(type v) { Mul(v); return *this; } \
71  const cls & operator/=(type v) { Div(v); return *this; } \
72  const cls & operator|=(type v) { Or (v); return *this; } \
73  const cls & operator&=(type v) { And(v); return *this; } \
74  const cls & operator^=(type v) { Xor(v); return *this; } \
75  const cls & operator<<=(type v) { ShiftLeft((int)v); return *this; } \
76  const cls & operator>>=(type v) { ShiftRight((int)v); return *this; } \
77  PBoolean operator==(type v) const { return Eq(v); } \
78  PBoolean operator!=(type v) const { return Ne(v); } \
79  PBoolean operator< (type v) const { return Lt(v); } \
80  PBoolean operator> (type v) const { return Gt(v); } \
81  PBoolean operator>=(type v) const { return !Gt(v); } \
82  PBoolean operator<=(type v) const { return !Lt(v); } \
83 
84 
85 class PInt64 : public PInt64__ {
86  public:
87  PInt64() { }
88  PInt64(long l) : PInt64__(l, l < 0 ? -1 : 0) { }
89  PInt64(unsigned long l, long h) : PInt64__(l, h) { }
90  PInt64(const PInt64__ & v) : PInt64__(v) { }
91 
92  PInt64 operator~() const { return PInt64(~low, ~high); }
93  PInt64 operator-() const { return operator~()+1; }
94 
95  PInt64 operator++() { Inc(); return *this; }
96  PInt64 operator--() { Dec(); return *this; }
97 
98  PInt64 operator++(int) { PInt64 t = *this; Inc(); return t; }
99  PInt64 operator--(int) { PInt64 t = *this; Dec(); return t; }
100 
101  DECL_OPS(PInt64, char)
102  DECL_OPS(PInt64, unsigned char)
103  DECL_OPS(PInt64, short)
104  DECL_OPS(PInt64, unsigned short)
105  DECL_OPS(PInt64, int)
106  DECL_OPS(PInt64, unsigned int)
107  DECL_OPS(PInt64, long)
108  DECL_OPS(PInt64, unsigned long)
109  DECL_OPS(PInt64, const PInt64 &)
110 
111  friend ostream & operator<<(ostream &, const PInt64 &);
112  friend istream & operator>>(istream &, PInt64 &);
113 
114  protected:
115  void Add(long v) { Add(PInt64(v)); }
116  void Sub(long v) { Sub(PInt64(v)); }
117  void Mul(long v) { Mul(PInt64(v)); }
118  void Div(long v) { Div(PInt64(v)); }
119  void Mod(long v) { Mod(PInt64(v)); }
120  PBoolean Lt(long v) const { return Lt(PInt64(v)); }
121  PBoolean Gt(long v) const { return Gt(PInt64(v)); }
122  PBoolean Lt(const PInt64 &) const;
123  PBoolean Gt(const PInt64 &) const;
124 };
125 
126 
127 class PUInt64 : public PInt64__ {
128  public:
129  PUInt64() { }
130  PUInt64(unsigned long l) : PInt64__(l, 0) { }
131  PUInt64(unsigned long l, unsigned long h) : PInt64__(l, h) { }
132  PUInt64(const PInt64__ & v) : PInt64__(v) { }
133 
134  PUInt64 operator~() const { return PUInt64(~low, ~high); }
135 
136  const PUInt64 & operator++() { Inc(); return *this; }
137  const PUInt64 & operator--() { Dec(); return *this; }
138 
139  PUInt64 operator++(int) { PUInt64 t = *this; Inc(); return t; }
140  PUInt64 operator--(int) { PUInt64 t = *this; Dec(); return t; }
141 
142  DECL_OPS(PUInt64, char)
143  DECL_OPS(PUInt64, unsigned char)
144  DECL_OPS(PUInt64, short)
145  DECL_OPS(PUInt64, unsigned short)
146  DECL_OPS(PUInt64, int)
147  DECL_OPS(PUInt64, unsigned int)
148  DECL_OPS(PUInt64, long)
149  DECL_OPS(PUInt64, unsigned long)
150  DECL_OPS(PUInt64, const PUInt64 &)
151 
152  friend ostream & operator<<(ostream &, const PUInt64 &);
153  friend istream & operator>>(istream &, PUInt64 &);
154 
155  protected:
156  void Add(long v) { Add(PUInt64(v)); }
157  void Sub(long v) { Sub(PUInt64(v)); }
158  void Mul(long v) { Mul(PUInt64(v)); }
159  void Div(long v) { Div(PUInt64(v)); }
160  void Mod(long v) { Mod(PUInt64(v)); }
161  PBoolean Lt(long v) const { return Lt(PUInt64(v)); }
162  PBoolean Gt(long v) const { return Gt(PUInt64(v)); }
163  PBoolean Lt(const PUInt64 &) const;
164  PBoolean Gt(const PUInt64 &) const;
165 };
166 
167 #undef DECL_OPS
168 
169 #endif // P_NEEDS_INT64
170 
171 #endif // PTLIB_INT64_H
172 
173 
174 // End Of File ///////////////////////////////////////////////////////////////
bool PBoolean
Definition: object.h:174