home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06052a < prev    next >
Text File  |  1991-04-24  |  3KB  |  123 lines

  1. //
  2. //      $$   class Thing V.1.01  --  2/27/91   $$
  3. //
  4. //    Michael Kelly  --  Author
  5. //
  6. //    Turbo C++ V.1.0
  7. //
  8. //    Your basic "thing" is a base class for
  9. //    data elements of any type.  Derived classes
  10. //    must overload the comparison operators and
  11. //    virtual functions so that sorts and prints
  12. //    will work.  The Thing class is not a virtual
  13. //    base class so that functions that operate on
  14. //    derived classes may create temporary instances
  15. //    of class Thing.
  16. //
  17. //    The virtual function type() is worth a note:
  18. //    the enum TypeOfThing value is returned in the
  19. //    upper 16 bits of the long int.  The lower 16
  20. //    bits contains the size of the Thing.  This
  21. //    is intended to help the programmer to use
  22. //    more than one struct-based Thing, for example,
  23. //    in a program.  If the two structs are the same
  24. //    size, the creator of the struct type Thing will
  25. //    have to devise some other way to differentiate
  26. //    the structs.  See one of the derived types,
  27. //    such as LongThing ( th_long.cpp and th_long.hpp ).
  28. //
  29. //    $$   See HSort.Cpp and TryThing.Cpp   $$
  30. //
  31.  
  32.  
  33. #if !defined(THING_HPP)
  34. #define THING_HPP
  35. #include <mem.h>
  36.  
  37. enum TypeOfThing {      
  38.     CharType, 
  39.     IntType,
  40.     LongType,
  41.     FloatType,
  42.     DoubleType,
  43.     StructType,
  44.     CharPtrType,
  45.     IntPtrType,
  46.     LongPtrType,
  47.     FloatPtrType,
  48.     DoublePtrType,
  49.     StructPtrType,
  50.     UnknownType
  51. };
  52.  
  53. class Thing  {
  54.   protected:
  55.     void        *thing;
  56.  
  57.   public:
  58.  
  59.     Thing()
  60.     {
  61.     thing = NULL; 
  62.     }
  63.     virtual ~Thing()
  64.     { 
  65.     if( thing )  {
  66.         delete thing;
  67.         thing = NULL;
  68.     }
  69.     }
  70.  
  71.     void        *ptr()     { return thing; }
  72.     
  73.   virtual long type()    
  74.   { 
  75.     return ( (long)UnknownType << 16 );
  76.   }
  77.   virtual unsigned data_size()
  78.   {
  79.     return (unsigned)type();
  80.   }
  81.   virtual void print()        {}    
  82.   virtual int  printable()     { return 0; }
  83.   virtual int  sortable()      { return 0; }
  84.   virtual int  storable()      { return 0; }
  85.  
  86. // turn off "parameter not used" warning
  87. // for now.
  88. //
  89. #pragma warn -par
  90. /*
  91.     Thing& operator =( Thing &some_thing )
  92.     {
  93.     unsigned size = some_thing.data_size();
  94.  
  95.     thing = new char[ size ];
  96.     if( thing )
  97.         memmove( ptr(), some_thing.ptr(), size );
  98.     return *this;
  99.     }
  100. */      
  101.     virtual operator ==(Thing &some_thing) 
  102.     {
  103.     return ( type() == some_thing.type() );
  104.     }
  105.     virtual operator !=(Thing &some_thing) 
  106.     { 
  107.     return !( *this == some_thing); 
  108.     }
  109.     virtual operator < (Thing &some_thing) { return 0; }
  110.     virtual operator <=(Thing &some_thing) { return 0; }
  111.     virtual operator > (Thing &some_thing) { return 0; }
  112.     virtual operator >=(Thing &some_thing) { return 0; }
  113. };
  114.  
  115. // restore "parameter not used" warning
  116. // to default state.
  117. //
  118. #pragma warn .par
  119.  
  120. extern const Thing TheNullThing;
  121.  
  122. #endif
  123.