home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP10-8.ZIP / CLASSINC.ZIP / OBJECT.H < prev    next >
C/C++ Source or Header  |  1990-09-26  |  10KB  |  356 lines

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