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

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3.  
  4. // Contents ----------------------------------------------------------------
  5. //
  6. //      Object::~Object
  7. //      Object::isSortable
  8. //      Object::isAssociation 
  9. //      Object::operator new
  10. //      Object::forEach
  11. //      Object::firstThat 
  12. //      Object::lastThat  
  13. //      Object::operator delete 
  14. //
  15. //      Error::~Error
  16. //      Error::isA
  17. //      Error::nameOf 
  18. //      Error::printOn
  19. //      Error::hashValue  
  20. //      Error::isEqual
  21. //
  22. //      theErrorObject
  23. //         Object::ZERO                            initializer
  24. //
  25. // Description
  26. //
  27. //
  28. // End ---------------------------------------------------------------------
  29.  
  30. // Interface Dependencies ---------------------------------------------------
  31.  
  32. #ifndef __OBJECT_H
  33. #include <object.h>
  34. #endif
  35.  
  36. // End Interface Dependencies ------------------------------------------------
  37.  
  38.  
  39. Object::~Object()
  40.  
  41. // Summary -----------------------------------------------------------------
  42. //
  43. //      Default destructor for an object.  Doesn't do much, but it
  44. //      forces all classes derived from Object to have virtual
  45. //      destructors, which is essential for proper cleanup.  It also
  46. //      provides a good place for setting breakpoints, because every
  47. //      time an object gets destroyed, this function will be called.
  48. //
  49. // End ---------------------------------------------------------------------
  50. {
  51. }
  52. // End Destructor Object::~Object //
  53.  
  54.  
  55. // Member Function //
  56.  
  57. int Object::isSortable() const
  58.  
  59. // Summary -----------------------------------------------------------------
  60. //
  61. //      indicates whether the object defines comparison operators
  62. //
  63. // Parameters
  64. //
  65. //      none
  66. //
  67. // Remarks
  68. //
  69. //      A basic Object is not sortable
  70. //
  71. // End ---------------------------------------------------------------------
  72. {
  73.     return 0;
  74. }
  75. // End Member Function Object::isSortable //
  76.  
  77.  
  78. // Member Function //
  79.  
  80. int Object::isAssociation() const
  81.  
  82. // Summary -----------------------------------------------------------------
  83. //
  84. //      indicates whether the object is derived from class Association
  85. //
  86. // Parameters
  87. //
  88. //      none
  89. //
  90. // Remarks
  91. //
  92. //      A basic Object is not derived from class Association
  93. //
  94. // End ---------------------------------------------------------------------
  95. {
  96.     return 0;
  97. }
  98. // End Member Function Object::isAssociation //
  99.  
  100.  
  101. // Member Function //
  102.  
  103. void *Object::operator new( size_t s )
  104.  
  105. // Summary -----------------------------------------------------------------
  106. //
  107. //      replacement for the standard operator new().  Returns ZERO
  108. //      if attempted allocation fails.
  109. //
  110. // Parameters
  111. //
  112. //      s
  113. //
  114. //      number of bytes to allocate
  115. //
  116. // Functional Description
  117. //
  118. //      we call the global operator new() and check whether it succeeded.
  119. //      If it succeeded, we return the block that it allocated.  If it
  120. //      failed, we return ZERO.
  121. //
  122. // End ---------------------------------------------------------------------
  123. {
  124.     void *allocated = ::operator new( s );
  125.     if( allocated == 0 )
  126.         return ZERO;
  127.     else
  128.         return allocated;
  129. }
  130. // End Member Function Object::operator new //
  131.  
  132.  
  133. // Member Function //
  134.  
  135. void Object::forEach( iterFuncType actionPtr, void *paramListPtr )
  136.  
  137. // Summary -----------------------------------------------------------------
  138. //
  139. //      Calls the given iterator function on this object.
  140. //
  141. // Parameters
  142. //
  143. //      actionPtr
  144. //
  145. //      Pointer to the action routine which is to be called for this object.
  146. //
  147. //      paramListPtr
  148. //
  149. //      Pointer to the list of parameters which will be passed along to
  150. //      the action routine.
  151. //
  152. // Functional Description
  153. //
  154. //      We call the given function, passing our object and the list of
  155. //      parameters that was given to us.
  156. //
  157. // Remarks
  158. //
  159. //  warnings:
  160. //      The action routine must have a prototype of the form:
  161. //          void action( Object&, void * );
  162. //
  163. // End ---------------------------------------------------------------------
  164. {
  165.     ( *actionPtr )( *this, paramListPtr );
  166. }
  167. // End Member Function Object::forEach //
  168.  
  169.  
  170. // Member Function //
  171.  
  172. Object& Object::firstThat( condFuncType testFuncPtr, void *paramListPtr ) const
  173.  
  174. // Summary -----------------------------------------------------------------
  175. //
  176. //      Calls the given conditional test function on this object.
  177. //
  178. // Parameters
  179. //
  180. //      testFuncPtr
  181. //
  182. //      Pointer to the conditional test routine which is to be called 
  183. //      for this object.
  184. //
  185. //      paramListPtr
  186. //
  187. //      Pointer to the list of parameters which will be passed along to
  188. //      the conditional test routine.
  189. //
  190. // Return Value
  191. //
  192. //      Returns this if the this satisfies the condition.  Returns
  193. //      NOOBJECT otherwise.
  194. //
  195. // Functional Description
  196. //
  197. //      We call the given function, passing our object and the list of
  198. //      parameters that was given to us.  If the function returns
  199. //      a 1, we return this object, otherwise we return NOOBJECT.
  200. //
  201. // Remarks
  202. //
  203. //  warnings:
  204. //      The conditional test routine must have a prototype of the form:
  205. //          int test( Object&, void * );
  206. //      The conditional test routine must return 1 if the given object
  207. //      satisfies the condition.
  208. //
  209. // End ---------------------------------------------------------------------
  210. {
  211.     if( ( *testFuncPtr )( *this, paramListPtr ) )
  212.     {
  213.         return( *this );
  214.     }
  215.     else // our object doesn't satisfy the condition //
  216.     {
  217.         return( NOOBJECT );
  218.     }
  219. }
  220. // End Member Function Object::firstThat //
  221.  
  222.  
  223. // Member Function //
  224.  
  225. Object& Object::lastThat( condFuncType testFuncPtr, void *paramListPtr ) const
  226.  
  227. // Summary -----------------------------------------------------------------
  228. //
  229. //      Calls the given conditional test function on this object.  For
  230. //      non-container objects, lastThat is the same as firstThat.
  231. //
  232. // Parameters
  233. //
  234. //      testFuncPtr
  235. //
  236. //      Pointer to the conditional test routine which is to be called 
  237. //      for this object.
  238. //
  239. //      paramListPtr
  240. //
  241. //      Pointer to the list of parameters which will be passed along to
  242. //      the conditional test routine.
  243. //
  244. // Functional Description
  245. //
  246. //      We call the firstThat function.
  247. //
  248. // Remarks
  249. //
  250. //  warnings:
  251. //      The conditional test routine must have a prototype of the form:
  252. //          int test( Object&, void * );
  253. //      The conditional test routine must return 1 if the given object
  254. //      satisfies the condition.
  255. //
  256. // End ---------------------------------------------------------------------
  257. {
  258.     return Object::firstThat( testFuncPtr, paramListPtr );
  259. }
  260. // End Member Function Object::lastThat //
  261.  
  262.  
  263. // Destructor //
  264.  
  265. Error::~Error()
  266.  
  267. // Description -------------------------------------------------------------
  268. //
  269. //      We can't really destroy theErrorObject.
  270. //
  271. // End ---------------------------------------------------------------------
  272. {
  273. }
  274. // End Destructor Error::~Error //
  275.  
  276.  
  277. // Member Function //
  278.  
  279. void Error::operator delete( void * )
  280.  
  281. // Summary -----------------------------------------------------------------
  282. //
  283. //      Can't delete an Error object... so we pretend that we did.
  284. //
  285. // End ---------------------------------------------------------------------
  286. {
  287. }
  288. // End Member Function Error::operator delete //
  289.  
  290. // Member Function //
  291.  
  292. classType Error::isA() const
  293.  
  294. // Summary -----------------------------------------------------------------
  295. //
  296. //         Returns the class type of the error object.
  297. //
  298. // End ---------------------------------------------------------------------
  299. {
  300.     return errorClass; 
  301. }
  302. // End Member Function Error::isA //
  303.  
  304.  
  305. // Member Function //
  306.  
  307. char *Error::nameOf() const
  308.  
  309. // Summary -----------------------------------------------------------------
  310. //
  311. //         Returns a pointer to the character string "Error."
  312. //
  313. // End ---------------------------------------------------------------------
  314. {
  315.     return "Error";
  316. }
  317. // End Member Function Error::nameOf //
  318.  
  319.  
  320. // Member Function //
  321.  
  322. void Error::printOn( ostream& outputStream ) const
  323.  
  324. // Summary -----------------------------------------------------------------
  325. //
  326. //      Error class override of the usual printOn.  Since there isn't
  327. //      really any object to print, we emit an appropriate message.
  328. //
  329. // Parameters
  330. //
  331. //     outputStream
  332. //     The stream on which to display the formatted contents of the object.
  333. //
  334. // End ---------------------------------------------------------------------
  335. {
  336.     outputStream << "Error\n";
  337. }
  338. // End Member Function Error::printOn //
  339.  
  340.  
  341. // Member Function //
  342.  
  343. hashValueType   Error::hashValue() const
  344.  
  345. // Summary -----------------------------------------------------------------
  346. //
  347. //      Returns the value for use when hashing an error object.
  348. //      There should be only one object of class Error, so it's ok
  349. //      to return the same value every time.
  350. //
  351. // End ---------------------------------------------------------------------
  352. {
  353.     return ERROR_CLASS_HASH_VALUE;
  354. }
  355. // End Member Function Error::hashValue //
  356.  
  357.  
  358. // Member Function //
  359.  
  360. int Error::isEqual ( const Object& testObject ) const
  361.  
  362. // Summary -----------------------------------------------------------------
  363. //
  364. //      Determines whether the given object is theErrorObject.
  365. //
  366. // Parameters
  367. //
  368. //      testObject
  369. //
  370. //      The object we are testing against theErrorObject.
  371. //
  372. // Return Value
  373. //
  374. //      Returns 1 if the given object is theErrorObject, 0 otherwise.
  375. //
  376. // Functional Description
  377. //
  378. //      The only way we get called here is if this is a pointer to 
  379. //      theErrorObject.  We test the address of our given object to see
  380. //      if it is the address of theErrorObject.
  381. //
  382. // End ---------------------------------------------------------------------
  383. {
  384.     return &testObject == this;
  385. }
  386. // End Member Function Error::isEqual //
  387.  
  388.  
  389. // Variable //
  390.  
  391. Error    theErrorObject;
  392.  
  393. // Description -------------------------------------------------------------
  394. //
  395. //      Defines a dummy object to which Object::ZERO will point.  We only
  396. //      need this so we don't ever try to dereference a null pointer.
  397. //
  398. // End ---------------------------------------------------------------------
  399.  
  400.  
  401. // Initializer //
  402.  
  403. Object *Object::ZERO = (Object *)&theErrorObject;
  404.  
  405. // Description -------------------------------------------------------------
  406. //
  407. //      Initializes Object::ZERO.   We wait to do this here because we
  408. //      have to get theErrorObject defined before we initialize Object::ZERO.
  409. //
  410. // End ---------------------------------------------------------------------
  411.