home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / CALCPLUS.ZIP / CALCTYPE.H < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-02  |  7.7 KB  |  271 lines

  1. /*******************************************************
  2.  
  3.     The CalcPlus Class Library Version 1.0,
  4.     Author: Vladimir Schipunov, Copyright (C) 1996
  5.  
  6.     This library is free software. Permission to use,
  7.     copy, modify and redistribute the CalcPlus library
  8.     for any purpose is hereby granted without fee,
  9.     provided that the above copyright notice appear
  10.     in all copies.
  11.  
  12. *******************************************************/
  13.  
  14. #ifndef __CALCTYPE_H
  15. #define __CALCTYPE_H
  16. class istream;
  17. class ostream;
  18.  
  19. //
  20. //  Type identificators, C++ RTTI and templates
  21. //  are not used for compatibility with older compilers
  22. //
  23.  
  24. enum {
  25.     idNil,
  26.     idBool,
  27.     idLong,
  28.     idDouble,
  29.     idString,
  30.     idArray,
  31.     idDate,
  32.     idUserType
  33. };
  34.  
  35. //
  36. //  CType is the root class for all types of data built in interpreter.
  37. //  All names of the classes based on CType begin with letter 'C'.
  38. //  Class derived from CType should redefine several methods of CType.
  39. //
  40. //  Pure virtual methods:
  41. //
  42. //      int         type() - returns type identifier
  43. //      int         size() - size of data pointed by methods ptr && data
  44. //      const void* data() - constant pointer to the data buffer
  45. //      void*       ptr()  - nonconstant pointer to the same data buffer
  46. //      const char* name() - string with the name of type
  47. //      CType*      copy() - makes copy of the object
  48. //
  49.  
  50. class CType
  51. {
  52. public:
  53.     int id;
  54.     CType( int a ) : id( a ){}
  55.     CType( const CType& t ) : id( t.id ){}
  56.     virtual ~CType(){}
  57.     virtual const void *data() const = 0;
  58.     virtual size() const = 0;
  59.     virtual void *ptr() = 0;
  60.     virtual CType& operator =( const CType& t );
  61.     virtual CType* copy() const = 0;
  62.     virtual void print( ostream& ) const = 0;   // prints on a stream
  63.     virtual void get( istream& ) = 0;           // reads from stream
  64.     virtual void read( char* );                 // reads from char buffer
  65.     virtual compare( const CType& ) const;      // 0 - the same
  66.     virtual const char *name() const = 0;
  67.     virtual type() const = 0;
  68.     virtual const char *s( char *p = 0 ) const; // conversion to char*
  69.     virtual empty() const;                      // nonzero if empty
  70. };
  71.  
  72. extern ostream& operator << ( ostream& o, const CType& t );
  73. extern istream& operator >> ( istream& i, CType& t );
  74.  
  75. //
  76. //  Nil values are assigned to all uninitialized variables,
  77. //  this is also value of expressions which cannot be calculated.
  78. //
  79.  
  80. class CNil : public CType
  81. {
  82. public:
  83.     char dummy;
  84.     CNil() : CType( idNil) {}
  85.     const void* data() const { return &dummy; }
  86.     void *ptr() { return &dummy; }
  87.     size() const { return sizeof( dummy ); }
  88.     const char* name() const { return "Nil"; }
  89.     type() const { return idNil; }
  90.     empty() const { return 1; }
  91.     void get( istream& ){}
  92.     void print( ostream& ) const;
  93.     CType* copy() const { return new CNil; }
  94. };
  95.  
  96. //
  97. //  Boolean type.
  98. //
  99.  
  100. class CBool : public CType
  101. {
  102. public:
  103.     int n;
  104.     CBool( int a = 0 ) : CType( idBool), n( a ){}
  105.     const void* data() const { return &n; }
  106.     void* ptr() { return &n; }
  107.     size() const { return sizeof(n); }
  108.     const char* name() const { return "Bool"; }
  109.     type() const { return idBool; }
  110.     empty() const { return !n; }
  111.     void get( istream& );
  112.     void print( ostream& ) const;
  113.     CType* copy() const { return new CBool( n ); }
  114.     CBool& operator=( int k ) { n = k; return *this; }
  115.     CType& operator =( const CType& t ){ return CType::operator =(t); }
  116.     operator int() const { return n; }
  117. };
  118.  
  119. //
  120. //  Long type. There is no 2-bytes integer type,
  121. //  all integer expressions have values of type Long.
  122. //
  123.  
  124. class CLong : public CType
  125. {
  126. public:
  127.     long n;
  128.     CLong( long a = 0 ) : CType( idLong ), n( a ){}
  129.     const void* data() const { return &n; }
  130.     void* ptr() { return &n; }
  131.     size() const { return sizeof(n); }
  132.     const char* name() const { return "Number"; }
  133.     type() const { return idLong; }
  134.     empty() const { return n==0L; }
  135.     void get( istream& );
  136.     void print( ostream& ) const;
  137.     CType *copy() const { return new CLong( n ); }
  138.     operator long() const { return n; }
  139.     CType& operator =( const CType& t );
  140.     CType& operator =( long k ){ n = k; return *this; }
  141. };
  142.  
  143. class CDouble : public CType
  144. {
  145. public:
  146.     double n;
  147.     CDouble( double a = 0 ) : CType( idDouble ), n( a ){}
  148.     const void* data() const { return &n; }
  149.     void* ptr() { return &n; }
  150.     size() const { return sizeof(n); }
  151.     const char* name() const { return "Float"; }
  152.     type() const { return idDouble; }
  153.     empty() const { return n==0; }
  154.     void get( istream& );
  155.     void print( ostream& ) const;
  156.     CType *copy() const { return new CDouble( n ); }
  157.     operator double() const { return n; }
  158. };
  159.  
  160. //
  161. //  String may be zero-terminated (in this case field
  162. //  zero is set (to non-zero;)) or not zero-terminated.
  163. //
  164.  
  165. class CString : public CType
  166. {
  167. public:
  168.     char *p;
  169.     int n, zero;
  170.  
  171.     CString( const char* = "", int = 0 );
  172.     CString( const CString& );
  173.     CString( int );
  174.     ~CString(){ delete[] p; }
  175.     const void* data() const { return p; }
  176.     void* ptr() { return p; }
  177.     size() const { return n-zero; }
  178.     const char* name() const { return "String"; }
  179.     type() const { return idString; }
  180.     operator const char*() const { return p; }
  181.     virtual const char *s( char *p = 0 ) const;
  182.     CType *copy() const { return new CString( *this ); }
  183.     void get( istream& );
  184.     void print( ostream& ) const;
  185.     CType& operator =( const CType& t );
  186.     CType& operator =( const char* t );
  187.     CString& right( char = ' ' );
  188.     CString& left ( char = ' ' );
  189.     operator==(const char*s) const;
  190.     char& operator[]( int ) const;
  191. };
  192.  
  193. //
  194. //  Array type represents is the collection of values which
  195. //  may be indexed by array of CString's.
  196. //
  197.  
  198. class CArray : public CType
  199. {
  200. public:
  201.     CType **array;
  202.     CArray *structure;
  203.     int n;
  204.     const char* name() const { return "Array"; }
  205.     type() const { return idArray; }
  206.     const void* data() const { return 0; }
  207.     void* ptr() { return 0; }
  208.     size() const { return n; }
  209.     empty() const { return !n; }
  210.     CType* copy() const { return new CArray( *this ); }
  211.     void get( istream& ){}
  212.     void print( ostream& ) const;
  213.  
  214.     CArray() : CType( idArray ), array( 0 ),
  215.         n( 0 ), structure( 0 ){}
  216.     CArray( const CArray& );
  217.     ~CArray();
  218.     void add( CType* );
  219.     void clear();
  220.     compare( const CType& ) const;
  221.     CType& operator=(const CType& );
  222.     CArray& operator=(const CArray&);
  223.     CType& operator []( int );
  224.     CType& operator []( const char* );
  225.     index( const char* ) const;
  226.     remove( int );
  227. };
  228.  
  229. //
  230. //  Date may be converted to number.
  231. //  Static field MonthIsFirst shows the style of date:
  232. //  dd/mm/yy or mm/dd/yy. Day is first by default.
  233. //
  234.  
  235. class CDate : public CType
  236. {
  237. public:
  238.     int d, m, y;
  239.  
  240.     const char* name() const    { return "Date"; }
  241.     type() const                { return idDate; }
  242.     const void* data() const    { return &d; }
  243.     void* ptr()                 { return &d; }
  244.     size() const        { return sizeof(d)+sizeof(m)+sizeof(y); }
  245.     empty() const       { return d==0 && m==0 && y==0; }
  246.     CType *copy() const { return new CDate( *this ); }
  247.  
  248.     void get( istream& );
  249.     void print( ostream& ) const;
  250.     void set( long );
  251.     long i() const;
  252.     static MonthIsFirst;
  253.  
  254.     CDate( long n ) : CType( idDate ) { set( n ); }
  255.     CDate( int a1, int a2, int a3 ) :
  256.         CType( idDate ), y( a3 ),
  257.         d( MonthIsFirst ? a2 : a1 ),
  258.         m( MonthIsFirst ? a1 : a2 )
  259.     {}
  260.     CDate( const CDate& dt ) :
  261.         CType( idDate ),
  262.         d( dt.d ),
  263.         m( dt.m ),
  264.         y( dt.y )
  265.     {}
  266.     CDate();
  267.     ~CDate(){}
  268. };
  269.  
  270. #endif
  271.