home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmobject.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  5.1 KB  |  113 lines

  1. // CmObject.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Abstract object definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMOBJECT_H
  10. #define _CMOBJECT_H
  11.  
  12. #include <string.h>
  13. #include <iostream.h>
  14. #include <cm/include/cmdefs.h>
  15. #include <cm/include/cmresfil.h>
  16.  
  17. class CmObject {                                       // Object class definition.
  18. public:
  19.   CmObject() {}                                        // Default constructor.
  20.  
  21.   virtual ~CmObject() {}                               // Virtual destructor.
  22.  
  23.   virtual const char* isA     ()            const = 0; // Return class name.
  24.   virtual Bool        isA     (const char*) const = 0; // Compare class names.
  25.   virtual Bool        isTypeOf(const char*) const;     // Check inheritance.
  26.   virtual CmObject*   newCopy ()            const = 0; // Virtual copy ctor.
  27.  
  28.   virtual Bool     isEqual (CmObject*) const;          // Compare objects.
  29.   virtual int      compare (CmObject*) const;          // Compare objects.
  30.   virtual unsigned hash    (unsigned)  const;          // Hash function.
  31.   virtual void     printOn (ostream&)  const {}        // Print to stream.
  32.   virtual void     readFrom(istream&) {}               // Read from stream.
  33.   virtual Bool     write   (CmReserveFile&) const;     // Write to reserve.
  34.   virtual Bool     read    (CmReserveFile&);           // Read from reserve.
  35.  
  36.   static CmObject* readObject (CmReserveFile&);        // Read an object.
  37.   Bool             writeObject(CmReserveFile&);        // Write an object.
  38. };
  39.  
  40. // "isTypeOf" checks if this class is the same type as the specified name.
  41. inline Bool CmObject::isTypeOf(const char* s) const
  42. { return (!strcmp(s, "CmObject")); }
  43.  
  44. // "isEqual" function used by containers for object lookup.
  45. inline Bool CmObject::isEqual(CmObject* O) const
  46. { return (O == this); }
  47.  
  48. // "compare" used by containers for object compare (binary tree, etc.).
  49. inline int CmObject::compare(CmObject* O) const
  50. { return ((O == this) ? 0 : ((this > O) ? 1 : -1)); }
  51.  
  52. // "hash" hashing function used by hash table classes.
  53. inline unsigned CmObject::hash(unsigned m) const
  54. { return (unsigned) this % m; }
  55.  
  56. // "write" writes objects to the specified reserve file.
  57. inline Bool CmObject::write(CmReserveFile&) const
  58. { return TRUE; }
  59.  
  60. // "read" reads objects from the specified reserve file.
  61. inline Bool CmObject::read(CmReserveFile&)
  62. { return TRUE; }
  63.  
  64. // "<<" global output stream operator calls the object's print function.
  65. inline ostream& operator<<(ostream& strm, const CmObject& O)
  66. { O.printOn(strm); return strm; }
  67.  
  68. // ">>" global input stream operator calls the object's read function.
  69. inline istream& operator>>(istream& strm, CmObject& O)
  70. { O.readFrom(strm); return strm; }
  71.  
  72. // "<<" global output operator calls the object's write function.
  73. inline CmReserveFile& operator<<(CmReserveFile& F, CmObject& O)
  74. { O.write(F); return F; }
  75.  
  76. // "<<" global input operator calls the object's read function.
  77. inline CmReserveFile& operator>>(CmReserveFile& F, CmObject& O)
  78. { O.read(F); return F; }
  79.  
  80. // Shorthand macro for defining the pure virtual functions contained within
  81. // the "CmObject" definition.  The classname and parent classname are passed
  82. // as parameters to the macro.
  83. //
  84. // NOTE: This macro will not work with derived class that still contain
  85. //       thier own pure virtual functions.  See the macro definition
  86. //       below.  Also, for classes deriving from more than one "CmObject"
  87. //       classes, these macros cannot be used meaning that the programmer
  88. //       needs to put in the definitions by hand.
  89. //
  90. #define CMOBJECT_DEFINE(name, parent)                                 \
  91.   const char* isA() const                                             \
  92.         { return #name; }                                             \
  93.   Bool  isA(const char* s) const                                      \
  94.         { return (!strcmp(s, #name)); }                               \
  95.   Bool  isTypeOf(const char* s) const                                 \
  96.         { return (!strcmp(s, #name)) ? TRUE : parent::isTypeOf(s); }  \
  97.   CmObject* newCopy() const { return (CmObject*) new name(*this); }
  98.  
  99. // Shorthand macro for defining the pure virtual functions contained within
  100. // the "CmObject" class.  This macro is to be used when deriving classes
  101. // which still contain their own pure virtual functions.
  102. //
  103. #define CMOBJECT_ABSTRACT(name, parent)                               \
  104.   const char* isA() const                                             \
  105.         { return #name; }                                             \
  106.   Bool  isA(const char* s) const                                      \
  107.         { return (!strcmp(s, #name)); }                               \
  108.   Bool  isTypeOf(const char* s) const                                 \
  109.         { return (!strcmp(s, #name)) ? TRUE : parent::isTypeOf(s); }  \
  110.   virtual CmObject* newCopy() const = 0;
  111.  
  112. #endif
  113.