home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 1.ddi / CLASSINC.ZIP / OBJECT.H < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  9.8 KB  |  347 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // Contents ----------------------------------------------------------------
  4. //
  5. //      Object
  6. //      NOOBJECT
  7. //      Object::Object                          constructor
  8. //      Object::Object                          copy constructor
  9. //
  10. //      Error
  11. //
  12. //      operator <<
  13. //         operator ==
  14. //         operator !=
  15. //
  16. // Description
  17. //
  18. //      Defines the abstract base class Object.  Object is the class
  19. //      at the root of the class library hierarchy.  Also defines the
  20. //      instance class Error, which is used to indicate the presence of
  21. //      no object reference.
  22. //
  23. // End ---------------------------------------------------------------------
  24.  
  25. // Interface Dependencies ---------------------------------------------------
  26.  
  27. #ifndef __OBJECT_H
  28. #define __OBJECT_H
  29.  
  30. #ifndef __IOSTREAM_H
  31. #include <iostream.h>
  32. #define __IOSTREAM_H
  33. #endif
  34.  
  35. #ifndef __STDDEF_H
  36. #include <stddef.h>
  37. #define __STDDEF_H
  38. #endif
  39.  
  40. #ifndef __CLSTYPES_H
  41. #include <clstypes.h>
  42. #endif
  43.  
  44. #ifndef __CLSDEFS_H
  45. #include <clsdefs.h>
  46. #endif
  47.  
  48. // End Interface Dependencies ------------------------------------------------
  49.  
  50.  
  51. // Class //
  52.  
  53. class Object
  54. {
  55. public:
  56.             Object();
  57.             Object( Object& );    
  58.     virtual ~Object();
  59.  
  60.     virtual classType       isA() const = 0;
  61.     virtual char            *nameOf() const = 0;
  62.     virtual hashValueType   hashValue() const = 0;
  63.     virtual int             isEqual( const Object& ) const = 0;
  64.     virtual int             isSortable() const;
  65.     virtual int             isAssociation() const;
  66.  
  67.             void           *operator new( size_t s );
  68.     virtual void            forEach( iterFuncType, void * );
  69.     virtual Object&         firstThat( condFuncType, void * ) const;
  70.     virtual Object&         lastThat( condFuncType, void * ) const;
  71.     virtual void            printOn( ostream& ) const = 0;
  72.  
  73.     static  Object         *ZERO;
  74.  
  75. protected:
  76.     friend    ostream& operator <<( ostream&, const Object& );
  77. };
  78.  
  79. // Description -------------------------------------------------------------
  80. //
  81. //     Defines the abstract base class Object.  Object is the root of the
  82. //     hierarchy, as most classes within the hierarchy are derived from it.
  83. //     To create an class as part of this hierarchy, derive that class
  84. //     from Object and provide the required functions.  You may then
  85. //     use the derived class anywhere Object is called for.
  86. //
  87. // Constructors
  88. //
  89. //      Object()
  90. //
  91. //      Vanilla constructor.  Forces each derived class to provide one,
  92. //      even if it's one that the compiler has to generate.
  93. //
  94. //      Object( Object& )
  95. //
  96. //      Copy constructor.  Constructs an object, then copies the contents
  97. //      of the given object onto the new object.
  98. //
  99. // Destructors
  100. //
  101. //      ~Object
  102. //
  103. //      Run-of-the-mill destructor.  Turns out to be a useful place to set
  104. //      breakpoints sometimes.
  105. //
  106. // Public Members
  107. //
  108. //         isA
  109. //
  110. //         Returns an unique identifying quantity of type classType.  You may
  111. //         test this quantity to make sure that the object is of the class
  112. //         desired.
  113. //
  114. //         nameOf
  115. //
  116. //         Returns a pointer to the character string which is the class name.
  117. //     
  118. //         hashValue
  119. //
  120. //         Returns a unique key based on the value of an object.  The method
  121. //         used in obtaining the key depends upon the implementation of the
  122. //         function for each class.
  123. //
  124. //      isEqual
  125. //
  126. //      Returns 1 if the objects are the same type and the elements of the
  127. //      object are equal, 0 otherwise.
  128. //
  129. //      operator new
  130. //
  131. //      Returns ZERO if the allocation of a new object fails.
  132. //
  133. //      forEach
  134. //
  135. //      Performs a function on each of the subobjects in an object.  If
  136. //      an object has no subobject, forEach operates on that object.
  137. //
  138. //      firstThat
  139. //
  140. //      Returns a reference to the first object for which the given
  141. //      conditional function returns a 1.  For object of non-container
  142. //      classes, this will always be a reference to the object.
  143. //
  144. //      lastThat
  145. //
  146. //      Returns a reference to the last object for which the given
  147. //      conditional function returns a 1.  For object of non-container
  148. //      classes, this will always be a reference to the object.
  149. //
  150. //      ZERO
  151. //
  152. //      A reference to an error object.  Note that this differs from a
  153. //      reference to a null object.  This is used by the Error class
  154. //      to handle problems when the operator new cannot allocate space
  155. //      for an object.
  156. //
  157. //         printOn
  158. //
  159. //         Displays the contents of an object.  The format of the output
  160. //         is dictated by the implementation of the printOn function for
  161. //         each class.
  162. //
  163. // Remarks
  164. //
  165. // Friends:
  166. //         The operator << is overloaded and made of friend of the class Object 
  167. //         so that invocations of << may call the protected member function,
  168. //         printOn.
  169. //
  170. // End ---------------------------------------------------------------------
  171.  
  172.  
  173. // Macro //
  174.  
  175. #define NOOBJECT        *(Object::ZERO)
  176.  
  177. // Summary -----------------------------------------------------------------
  178. //
  179. //      Provides an easy reference to theErrorObject
  180. //
  181. // End ---------------------------------------------------------------------
  182.  
  183.  
  184. // Constructor //
  185.  
  186. inline  Object::Object()
  187.  
  188. // Summary -----------------------------------------------------------------
  189. //
  190. //      Default constructor for an object.  Not useful for much, but it
  191. //      does provide a good place for setting breakpoints, because every
  192. //      time an object gets created, this function must be called.
  193. //
  194. // End ---------------------------------------------------------------------
  195. {
  196. }
  197. // End Constructor Object::Object //
  198.  
  199.  
  200. // Constructor //
  201.  
  202. inline  Object::Object( Object& )
  203.  
  204. // Summary -----------------------------------------------------------------
  205. //
  206. //      Copy constructor for an object.  Again, not useful for much except
  207. //      breakpoints.  This function will be called every time one object
  208. //      is copied to another.
  209. //
  210. // End ---------------------------------------------------------------------
  211. {
  212. }
  213. // End Constructor Object::Object //
  214.  
  215.  
  216. // Class //
  217.  
  218. class Error:  private Object
  219. {
  220. public:
  221.     virtual ~Error();
  222.     virtual classType       isA() const;
  223.     virtual char            *nameOf() const;
  224.     virtual hashValueType   hashValue() const;
  225.     virtual int             isEqual( const Object& ) const;
  226.     virtual void            printOn( ostream& ) const;
  227.             void            operator delete( void * );
  228. };
  229.  
  230. // Description -------------------------------------------------------------
  231. //
  232. //      Defines the class Error.  The is exactly one instantiation of
  233. //      class Error, namely theErrorObject.  The static object pointer
  234. //      Object::ZERO points to this object.  The define NOOBJECT
  235. //      redefines Object::ZERO (see CLSDEFS.H).  The operator Object::new
  236. //      returns a pointer to theErrorObject if an attempt to allocate
  237. //      an object fails.  You may test the return value of the new
  238. //      operator against NOOBJECT to see whether the allocation failed.
  239. //
  240. // Public Members
  241. //
  242. //         isA
  243. //
  244. //         Returns the correct value for the Error class.
  245. //
  246. //         nameOf
  247. //
  248. //         Returns a pointer to the character string "Error".
  249. //     
  250. //      hashValue
  251. //
  252. //      Returns a pre-defined value for the Error class.  All objects
  253. //      of class Error (there is usually only one, theErrorObject) have
  254. //      the same hash value.  This makes them hard to distinguish from
  255. //      each other, but since there's only one, it doesn't matter.
  256. //
  257. //      isEqual
  258. //
  259. //      Determines whether the given object is theErrorObject.
  260. //
  261. //      printOn
  262. //
  263. //      Overrides the default printOn function since the Error class is
  264. //      an instance class.
  265. //
  266. // End ---------------------------------------------------------------------
  267.  
  268.  
  269. // Friend //
  270.  
  271. inline ostream& operator <<( ostream& outputStream, const Object& anObject )
  272.  
  273. // Summary -----------------------------------------------------------------
  274. //
  275. //         Write an object value to an output stream.
  276. //
  277. // Parameters
  278. //
  279. //         outputStream
  280. //         The stream on which to display the formatted contents of the object.
  281. //
  282. //         anObject
  283. //         The object to display.
  284. //
  285. // End ---------------------------------------------------------------------
  286. {
  287.     anObject.printOn( outputStream );
  288.     return outputStream;
  289. }
  290. // End Friend operator << //
  291.  
  292.  
  293. // Function //
  294.  
  295. inline  int operator ==( const Object& test1, const Object& test2 )
  296.  
  297. // Summary -----------------------------------------------------------------
  298. //
  299. //      Determines whether the first object is equal to the second.  We
  300. //      do type checking on the two objects (objects of different
  301. //      classes can't be equal, even if they're derived from each other).
  302. //
  303. // Parameters
  304. //
  305. //      test1
  306. //
  307. //      The first object we are testing.
  308. //
  309. //      test2
  310. //
  311. //      The second object we are testing.
  312. //
  313. // End ---------------------------------------------------------------------
  314. {
  315.     return ( (test1.isA() == test2.isA()) && test1.isEqual( test2 ) );
  316. }
  317. // End Function operator == //
  318.  
  319.  
  320. // Function //
  321.  
  322. inline  int operator !=( const Object& test1, const Object& test2 )
  323.  
  324. // Summary -----------------------------------------------------------------
  325. //
  326. //      Determines whether the given object is not equal to this.  We
  327. //      just reverse the condition returned from operator ==.
  328. //
  329. // Parameters
  330. //
  331. //      test1
  332. //
  333. //      The first object we are testing.
  334. //
  335. //      test2
  336. //
  337. //      The second object we are testing.
  338. //
  339. // End ---------------------------------------------------------------------
  340. {
  341.     return ( !( test1 == test2 ) );
  342. }
  343. // End Function operator != //
  344.  
  345.  
  346. #endif // ifndef __OBJECT_H //
  347.