home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / base / integer.cxx < prev    next >
C/C++ Source or Header  |  1995-04-04  |  4KB  |  187 lines

  1.  
  2.  
  3.  
  4.  
  5. /*
  6.  *
  7.  *          Copyright (C) 1994, M. A. Sridhar
  8.  *  
  9.  *
  10.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  11.  *     to copy, modify or distribute this software  as you see fit,
  12.  *     and to use  it  for  any  purpose, provided   this copyright
  13.  *     notice and the following   disclaimer are included  with all
  14.  *     copies.
  15.  *
  16.  *                        DISCLAIMER
  17.  *
  18.  *     The author makes no warranties, either expressed or implied,
  19.  *     with respect  to  this  software, its  quality, performance,
  20.  *     merchantability, or fitness for any particular purpose. This
  21.  *     software is distributed  AS IS.  The  user of this  software
  22.  *     assumes all risks  as to its quality  and performance. In no
  23.  *     event shall the author be liable for any direct, indirect or
  24.  *     consequential damages, even if the  author has been  advised
  25.  *     as to the possibility of such damages.
  26.  *
  27.  */
  28.  
  29.  
  30.  
  31.  
  32.  
  33. #ifdef __GNUC__
  34. #pragma implementation
  35. #endif
  36.  
  37.  
  38.  
  39. #include <iostream.h>
  40.  
  41. #include "base/integer.h"
  42. #include "base/bytarray.h"
  43. #include "base/bytstrng.h"
  44. #include "base/string.h"
  45. #include "base/stream.h"
  46.  
  47.  
  48.  
  49.  
  50. CL_DEFINE_CLASS(CL_Integer, _CL_Integer_CLASSID);
  51.  
  52.  
  53. CL_String CL_Integer::AsString () const
  54. {
  55.     return CL_String (_value, 1, ' ');
  56. }
  57.  
  58.  
  59. const char digits[] = "0123456789ABCDEF";
  60.  
  61. CL_String CL_Integer::InRadix (short radix) const
  62. {
  63.     if (radix == 10 || _value == 0)
  64.         return AsString();
  65.     if (radix < 2 || radix > 16)
  66.         return "";
  67.     unsigned long v = _value;
  68.     CL_String retVal;
  69.     while (v != 0) {
  70.         char digit = digits[v % radix];
  71.         v /= radix;
  72.         retVal = CL_String (digit, 1) + retVal;
  73.     }
  74.     return retVal;
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. bool CL_Integer::WriteTo (CL_Stream& s) const
  84. {
  85.     return s.Write (ClassId())  && s.Write (_value);
  86. }
  87.  
  88.  
  89. bool CL_Integer::ReadFrom  (const CL_Stream& s)
  90. {
  91.     if (!PrepareToChange() || !ReadClassId (s) || !s.Read (_value))
  92.         return FALSE;
  93.     Notify ();
  94.     return TRUE;
  95. }
  96.  
  97. CL_Integer::~CL_Integer ()
  98. {
  99. }
  100.  
  101.  
  102.  
  103.  
  104. CL_Integer CL_Integer::operator++ () // Prefix increment
  105. {
  106.     if (PrepareToChange ()) {
  107.         ++_value;
  108.         Notify();
  109.     }
  110.     return _value;
  111. }
  112.  
  113.  
  114. CL_Integer CL_Integer::operator++ (int) // Postfix increment
  115. {
  116.     long l = _value;
  117.     if (PrepareToChange ()) {
  118.         l = _value++;
  119.         Notify();
  120.     }
  121.     return l;
  122. }
  123.  
  124.  
  125. CL_Integer CL_Integer::operator-- () // Prefix decrement
  126. {
  127.     if (PrepareToChange ()) {
  128.         --_value;
  129.         Notify();
  130.     }
  131.     return _value;
  132. }
  133.  
  134.  
  135. CL_Integer CL_Integer::operator-- (int) // Postfix decrement
  136. {
  137.     long l = _value;
  138.     if (PrepareToChange ()) {
  139.         l = _value--;
  140.         Notify();
  141.     }
  142.     return l;
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149. CL_Integer& CL_Integer::operator= (const CL_Integer& i)
  150. {
  151.     if (this == &i || !PrepareToChange())
  152.         return *this;
  153.     _value = i._value;
  154.     Notify ();
  155.     return *this;
  156. }
  157.  
  158.  
  159. CL_Integer& CL_Integer::operator= (long i)
  160. {
  161.     if (!PrepareToChange())
  162.         return *this;
  163.     _value = i;
  164.     Notify ();
  165.     return *this;
  166. }
  167.  
  168.  
  169.  
  170.  
  171. void CL_Integer::FromStream (istream& stream)
  172. {
  173.     stream >> _value;
  174. }
  175.  
  176.  
  177.  
  178. #if defined(__GNUC__) && __GNUC_MINOR__ >= 6
  179. #include "base/cmparatr.h"
  180. #include "base/basicops.h"
  181. #include "base/binding.h"
  182.  
  183. template class CL_Binding<CL_Integer>;
  184. template class CL_Comparator<CL_Integer>;
  185. template class CL_Basics<CL_Integer>;
  186. #endif
  187.