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