home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
yacl-012.zip
/
base
/
integer.h
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-08
|
11KB
|
431 lines
#ifndef _integer_h_
#define _integer_h_
/*
*
* Copyright (C) 1994, M. A. Sridhar
*
*
* This software is Copyright M. A. Sridhar, 1994. You are free
* to copy, modify or distribute this software as you see fit,
* and to use it for any purpose, provided this copyright
* notice and the following disclaimer are included with all
* copies.
*
* DISCLAIMER
*
* The author makes no warranties, either expressed or implied,
* with respect to this software, its quality, performance,
* merchantability, or fitness for any particular purpose. This
* software is distributed AS IS. The user of this software
* assumes all risks as to its quality and performance. In no
* event shall the author be liable for any direct, indirect or
* consequential damages, even if the author has been advised
* as to the possibility of such damages.
*
*/
// This class is intended to make an integer look like a first-class YACL
// object, complete with notification capabilities. It supports all of the
// usual integer operators as well as the protocol inherited from
// CL_Object.
#ifdef __GNUC__
#pragma interface
#endif
#include "base/object.h"
#include "base/string.h"
class CL_EXPORT CL_Integer: public CL_Object {
public:
//
// ------------------------ Construction and destruction -----------------
//
CL_Integer (long l)
{_value = l;}
CL_Integer ()
{_value = 0;}
CL_Integer (const CL_Integer& i)
{_value = i.Value();};
~CL_Integer();
//
// ------------------------- Conversions ------------------------------
//
long Value() const {return _value;};
operator long() const {return _value;};
CL_String InRadix (short r) const;
// Return a String containing the radix-$r$ representation of this
// integer. The radix must be in the range 2..16; otherwise, the null
// string is returned. Also, for a non-decimal radix, this integer is
// treated as unsigned.
//
// Comparison
//
bool operator< (const CL_Object& o) const;
bool operator<= (const CL_Object& o) const;
bool operator> (const CL_Object& o) const;
bool operator>= (const CL_Object& o) const;
bool operator== (const CL_Object& o) const;
bool operator!= (const CL_Object& o) const;
bool operator< (const CL_Integer& o) const
{return _value < o.Value();};
bool operator<= (const CL_Integer& o) const
{return _value <= o.Value();};
bool operator> (const CL_Integer& o) const
{return _value > o.Value();};
bool operator>= (const CL_Integer& o) const
{return _value >= o.Value();};
bool operator== (const CL_Integer& o) const
{return _value == o.Value();};
bool operator!= (const CL_Integer& o) const
{return _value != o.Value();};
bool operator< (long o) const
{return _value < o;};
bool operator<= (long o) const
{return _value <= o;};
bool operator> (long o) const
{return _value > o;};
bool operator>= (long o) const
{return _value >= o;};
bool operator== (long o) const
{return _value == o;};
bool operator!= (long o) const
{return _value != o;};
bool operator< (int o) const
{return _value < o;};
bool operator<= (int o) const
{return _value <= o;};
bool operator> (int o) const
{return _value > o;};
bool operator>= (int o) const
{return _value >= o;};
bool operator== (int o) const
{return _value == o;};
bool operator!= (int o) const
{return _value != o;};
short Compare (const CL_Object& o) const;
short Compare (const CL_Integer& o) const;
//
// Arithmetic operations:
//
CL_Integer operator+ (const CL_Integer& i) const
{return CL_Integer(_value+i.Value());};
CL_Integer operator- (const CL_Integer& i) const
{return CL_Integer(_value-i.Value());};
CL_Integer operator* (const CL_Integer& i) const
{return CL_Integer(_value*i.Value());};
CL_Integer operator/ (const CL_Integer& i) const
{return CL_Integer(_value/i.Value());};
CL_Integer operator% (const CL_Integer& i) const
{return CL_Integer(_value%i.Value());};
CL_Integer operator+ (long i) const
{return CL_Integer(_value + i);};
CL_Integer operator- (long i) const
{return CL_Integer(_value - i);};
CL_Integer operator* (long i) const
{return CL_Integer(_value * i);};
CL_Integer operator/ (long i) const
{return CL_Integer(_value / i);};
CL_Integer operator% (long i) const
{return CL_Integer(_value % i);};
CL_Integer operator++ ();
// Prefix increment.
CL_Integer operator++ (int);
// Postfix increment.
CL_Integer operator-- ();
// Prefix decrement.
CL_Integer operator-- (int);
// Postfix decrement.
//
// Bitwise operations:
//
CL_Integer operator| (const CL_Integer& i) const
{return CL_Integer(_value | i.Value());};
CL_Integer operator& (const CL_Integer& i) const
{return CL_Integer(_value & i.Value());};
CL_Integer operator<< (const CL_Integer& i) const
{return CL_Integer(_value << i.Value());};
CL_Integer operator>> (const CL_Integer& i) const
{return CL_Integer(_value >> i.Value());};
CL_Integer operator~ () const
{return CL_Integer(~_value);};
CL_Integer operator^ (const CL_Integer& i) const
{return CL_Integer(_value ^ i.Value());};
CL_Integer operator| (long i) const
{return CL_Integer(_value | i);};
CL_Integer operator& (long i) const
{return CL_Integer(_value & i);};
CL_Integer operator<< (long i) const
{return CL_Integer(_value << i);};
CL_Integer operator>> (long i) const
{return CL_Integer(_value >> i);};
CL_Integer operator^ (long i) const
{return CL_Integer(_value ^ i);};
//
// ------------------ Assignments of various kinds --------------------
//
virtual void operator= (const CL_Object&);
virtual void operator= (const CL_String&);
virtual CL_Integer& operator= (const CL_Integer&);
virtual CL_Integer& operator= (long i);
CL_Integer& operator+= (const CL_Integer& i)
{*this = _value + i.Value(); return *this;};
CL_Integer& operator-= (const CL_Integer& i)
{*this = _value - i.Value(); return *this;};
CL_Integer& operator*= (const CL_Integer& i)
{*this = _value * i.Value(); return *this;};
CL_Integer& operator/= (const CL_Integer& i)
{*this = _value / i.Value(); return *this;};
CL_Integer& operator%= (const CL_Integer& i)
{*this = _value % i.Value(); return *this;};
CL_Integer& operator|= (const CL_Integer& i)
{*this = _value | i.Value(); return *this;};
CL_Integer& operator&= (const CL_Integer& i)
{*this = _value & i.Value(); return *this;};
CL_Integer& operator^= (const CL_Integer& i)
{*this = _value ^ i.Value(); return *this;};
CL_Integer& operator<<= (const CL_Integer& i)
{*this = _value << i.Value(); return *this;};
CL_Integer& operator>>= (const CL_Integer& i)
{*this = _value >> i.Value(); return *this;};
CL_Integer& operator+= (long i)
{*this = _value+i; return *this;};
CL_Integer& operator-= (long i)
{*this = _value-i; return *this;};
CL_Integer& operator*= (long i)
{*this = _value*i; return *this;};
CL_Integer& operator/= (long i)
{*this = _value/i; return *this;};
CL_Integer& operator%= (long i)
{*this = _value%i; return *this;};
CL_Integer& operator|= (long i)
{*this = _value | i; return *this;};
CL_Integer& operator&= (long i)
{*this = _value & i; return *this;};
CL_Integer& operator^= (long i)
{*this = _value ^ i; return *this;};
CL_Integer& operator<<= (long i)
{*this = _value << i; return *this;};
CL_Integer& operator>>= (long i)
{*this = _value >> i; return *this;};
// --------------- Storage and retrieval ----------------------------
long StorableFormWidth () const
{ return sizeof (CL_ClassId) + sizeof _value; };
bool ReadFrom (const CL_Stream&);
bool WriteTo (CL_Stream&) const;
CL_String AsString () const;
void FromStream (istream& stream);
// Override the method inherited from {\small\tt CL_Object}.
// --------------------------- Basic methods -------------------
CL_ClassId ClassId() const { return _CL_Integer_CLASSID;};
const char* ClassName() const {return "CL_Integer";};
CL_Object* Clone() const {return new CL_Integer (*this);};
// ------------------ End public protocol ----------------------
protected:
long _value;
};
inline short CL_Integer::Compare (const CL_Integer& o) const
{
return _value < o._value ? -1 : ((_value == o._value) ? 0 : 1);
}
inline short CL_Integer::Compare (const CL_Object& o) const
{
return IsA (o) ? Compare ((const CL_Integer&) o)
: (this < (CL_Integer*) &o ? -1 : 1);
}
inline bool CL_Integer::operator< (const CL_Object& o) const
{
return IsA (o) && _value < ((CL_Integer&) o).Value();
}
inline bool CL_Integer::operator<= (const CL_Object& o) const
{
return IsA(o) && _value <= ((CL_Integer&) o).Value();
}
inline bool CL_Integer::operator== (const CL_Object& o) const
{
return IsA (o) && _value == ((CL_Integer&) o).Value();
}
inline bool CL_Integer::operator>= (const CL_Object& o) const
{
return IsA(o) && _value >= ((CL_Integer&) o).Value();
}
inline bool CL_Integer::operator> (const CL_Object& o) const
{
return IsA(o) && _value > ((CL_Integer&) o).Value();
}
inline bool CL_Integer::operator!= (const CL_Object& o) const
{
return !IsA(o) || _value != ((CL_Integer&) o).Value();
}
inline void CL_Integer::operator= (const CL_Object& s)
{
if (CheckClassType (s, "CL_Integer::operator="))
*this = (const CL_Integer&) s;
}
inline void CL_Integer::operator= (const CL_String& s)
{
*this = s.AsLong ();
}
#endif