home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / BCPPOWL1.ZIP / CLSRC.ZIP / OBJECT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  10.5 KB  |  407 lines

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