home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / NeoIntroTCL3.0 folder / TCL / Laughs / Source / CPerson.cp < prev   
Encoding:
Text File  |  1994-08-29  |  6.6 KB  |  328 lines  |  [TEXT/KAHL]

  1. /*****
  2.  * CPerson.cp
  3.  *
  4.  *    Implementation of application-specific persistent classes.
  5.  *
  6.  *    This sample app is derived from a contribution originally made
  7.  *    by Paul Gee (Thanks Paul!).
  8.  *
  9.  *  Copyright © 1994 NeoLogic Systems.  All rights reserved.
  10.  *
  11.  *****/
  12.  
  13. #include "NeoTypes.h"
  14. #include CNeoDatabaseH
  15. #include CNeoStreamH
  16. #include CNeoIDListH
  17. #include CNeoPartListIteratorH
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include "CPerson.h"
  22.  
  23. /* ****************************************************************** */
  24.                         /** CPerson Class **/
  25. /* ****************************************************************** */
  26. CPerson::CPerson(const CNeoString &aName)
  27. {
  28.     fName = aName;
  29.     setListClassID(kNeoIDListID);
  30. }
  31.  
  32. #pragma segment NeoRead
  33. void CPerson::readObject(CNeoStream *aStream, const NeoTag aTag)
  34. {
  35.     inherited::readObject(aStream, aTag);
  36.  
  37.     aStream->readNativeString(fName, sizeof(fName));
  38. }
  39.  
  40. #pragma segment NeoWrite
  41. void CPerson::writeObject(CNeoStream *aStream, const NeoTag aTag)
  42. {
  43.     inherited::writeObject(aStream, aTag);
  44.  
  45.     aStream->writeNativeString(fName, sizeof(fName));
  46. }
  47.  
  48. void CPerson::printName(void) const
  49. {
  50.     char    name[64];
  51.  
  52.     strcpy(name, fName);
  53.     printf("Name is %s\n", name);
  54. }
  55.  
  56. /* ****************************************************************** */
  57.                         /** CJoke Class **/
  58. /* ****************************************************************** */
  59. CJoke::CJoke(const char *aText)
  60. {
  61.     setJoke(aText);
  62. }
  63.  
  64. CNeoPersist *CJoke::New(void)
  65. {
  66.     return new CJoke();
  67. }
  68.  
  69. NeoID CJoke::getClassID(void) const
  70. {
  71.     return kJokeID;
  72. }
  73.  
  74. long CJoke::getFileLength(void) const
  75. {
  76.     return kNeoPersistFileLength + kMaxJokeLength;
  77. }
  78.  
  79. #pragma segment NeoRead
  80. void CJoke::readObject(CNeoStream *aStream, const NeoTag aTag)
  81. {
  82.     inherited::readObject(aStream, aTag);
  83.  
  84.     aStream->readString(fJoke, sizeof(fJoke));
  85. }
  86.  
  87. #pragma segment NeoWrite
  88. void CJoke::writeObject(CNeoStream *aStream, const NeoTag aTag)
  89. {
  90.     inherited::writeObject(aStream, aTag);
  91.  
  92.     aStream->writeString(fJoke, sizeof(fJoke));
  93. }
  94.  
  95. void CJoke::printJoke(void) const
  96. {
  97.     char    joke[64];
  98.  
  99.     strcpy(joke, fJoke);
  100.     printf("%s\n", joke);
  101. }
  102.  
  103. /* ****************************************************************** */
  104.                         /** CJoker Class **/
  105. /* ****************************************************************** */
  106. CJoker::CJoker(const CNeoString &aName)
  107.     : CPerson(aName)
  108. {
  109.     setObjClassID(kJokeID);
  110. }
  111.  
  112. CNeoPersist *CJoker::New(void)
  113. {
  114.     return new CJoker();
  115. }
  116.  
  117. NeoID CJoker::getClassID(void) const
  118. {
  119.     return kJokerID;
  120. }
  121.  
  122. long CJoker::getFileLength(void) const
  123. {
  124.     return kPersonFileLength;
  125. }
  126.  
  127. void CJoker::skill(void)
  128. {
  129.     long    count    = getJokeCount();
  130.     CJoke *    joke;
  131.  
  132.     if (count) {
  133.         // Randomly pick a joke
  134.         joke = getJoke((rand()&0x7FFFFFFF) % count);
  135.         NeoAssert(joke);
  136.     
  137.         printf("Tells jokes : ");
  138.         joke->printJoke();
  139.     
  140.         // Don't forget to remove our reference to the joke.
  141.         joke->unrefer();
  142.     }
  143.     else
  144.         printf("Has no jokes to tell.\n");
  145. }
  146.  
  147. void CJoker::forgetJoke(CJoke *aJoke)
  148. {
  149.     // Remove the joke from this joker's part list
  150.     deleteFromList(aJoke);
  151.  
  152.     // Note: This joke is still in the database!
  153.     // To remove the joke completely, we would say...
  154.     // if (fMark)
  155.     //     gNeoDatabase->removeObject(joke);
  156. }
  157.  
  158. CJoke *CJoker::getJoke(const long aIndex)
  159. {
  160.     CJoke *                    joke;
  161.     CNeoPartListIterator    iterator(gNeoDatabase, getListClassID(), getListRoot(FALSE), nil, TRUE, kJokeID);
  162.  
  163.     // Randomly pick a joke
  164.     iterator.leap(aIndex);
  165.     joke = (CJoke *)iterator.currentObject();
  166.  
  167.     // Iterators don't add references to objects. So we add one ourselves.
  168.     if (joke)
  169.         joke->referTo();
  170.  
  171.     return joke;
  172. }
  173.  
  174. void CJoker::learnJoke(CJoke *aJoke)
  175. {
  176.     // Add the joke to this joker's part list
  177.     addToList(aJoke);
  178. }
  179.  
  180. /* ****************************************************************** */
  181.                         /** CPie Class **/
  182. /* ****************************************************************** */
  183. CPie::CPie(const char *aFilling)
  184. {
  185.     setFilling(aFilling);
  186. }
  187.  
  188. CNeoPersist *CPie::New(void)
  189. {
  190.     return new CPie();
  191. }
  192.  
  193. NeoID CPie::getClassID(void) const
  194. {
  195.     return kPieID;
  196. }
  197.  
  198. long CPie::getFileLength(void) const
  199. {
  200.     return kNeoPersistFileLength + kMaxFillingName;
  201. }
  202.  
  203. #pragma segment NeoRead
  204. void CPie::readObject(CNeoStream *aStream, const NeoTag aTag)
  205. {
  206.     inherited::readObject(aStream, aTag);
  207.  
  208.     aStream->readString(fFilling, sizeof(fFilling));
  209. }
  210.  
  211. #pragma segment NeoWrite
  212. void CPie::writeObject(CNeoStream *aStream, const NeoTag aTag)
  213. {
  214.     inherited::writeObject(aStream, aTag);
  215.  
  216.     aStream->writeString(fFilling, sizeof(fFilling));
  217. }
  218.  
  219. /* ****************************************************************** */
  220.                         /** CClown Class **/
  221. /* ****************************************************************** */
  222. CClown::CClown(const CNeoString &aName, const long aShoeSize)
  223.     : CPerson(aName)
  224. {
  225.     setObjClassID(kPieID);
  226. }
  227.  
  228. CNeoPersist *CClown::New(void)
  229. {    
  230.     return new CClown();
  231. }
  232.  
  233. NeoID CClown::getClassID(void) const
  234. {
  235.     return kClownID;
  236. }
  237.  
  238. long CClown::getFileLength(void) const
  239. {
  240.     return kPersonFileLength + sizeof(fShoeSize);
  241. }
  242.  
  243. #pragma segment NeoRead
  244. void CClown::readObject(CNeoStream *aStream, const NeoTag aTag)
  245. {
  246.     inherited::readObject(aStream, aTag);
  247.  
  248.     fShoeSize = aStream->readLong();
  249. }
  250.  
  251. #pragma segment NeoWrite
  252. void CClown::writeObject(CNeoStream *aStream, const NeoTag aTag)
  253. {
  254.     inherited::writeObject(aStream, aTag);
  255.  
  256.     aStream->writeLong(fShoeSize);
  257. }
  258.  
  259. CPie *CClown::bakePie(const char *aFilling)
  260. {
  261.     CPie *            pie;
  262.  
  263.     // Create a new pie object having the proper type of filling.
  264.     // The default filling type is Custard, my favorite!
  265.     pie = new CPie(aFilling);
  266.  
  267.     // Add it to the database.
  268.     // Note: An object ID is assigned automaticly by addObject.
  269.     gNeoDatabase->addObject(pie);
  270.  
  271.     // Add the pie to this clown's part list
  272.     addToList(pie);
  273.  
  274.     return pie;
  275. }
  276.  
  277. CPie *CClown::getPie(const long aIndex)
  278. {
  279.     CPie *                    pie;
  280.     CNeoPartListIterator    iterator(gNeoDatabase, getListClassID(), getListRoot(FALSE), nil, TRUE, kPieID);
  281.  
  282.     // Randomly pick a pie
  283.     iterator.leap(aIndex);
  284.     pie = (CPie *)iterator.currentObject();
  285.  
  286.     // Iterators don't add references to objects. So we add one ourselves.
  287.     if (pie)
  288.         pie->referTo();
  289.  
  290.     return pie;
  291. }
  292.  
  293. void CClown::skill(void)
  294. {
  295.     long    count    = getPieCount();
  296.     CPie *    pie;
  297.  
  298.     if (count) {
  299.         // Randomly pick a pie
  300.         pie = getPie((rand()&0x7FFFFFFF) % count);
  301.         NeoAssert(pie);
  302.     
  303.         printf("Throws pies: ");
  304.         throwPie(pie);
  305.     
  306.         // Don't forget to remove our reference to the pie.
  307.         pie->unrefer();
  308.     }
  309.     else
  310.         printf("Has no pies to throw.\n");
  311. }
  312.  
  313. void CClown::throwPie(CPie *aPie)
  314. {
  315.     char    filling[kMaxFillingName];
  316.  
  317.     // Remove the pie from this clown's part list
  318.     deleteFromList(aPie);
  319.  
  320.     // Now remove it from the database completely.
  321.     gNeoDatabase->removeObject(aPie);
  322.  
  323.     // Throw it!
  324.     aPie->getFilling(filling);
  325.     printf("Here's a %s pie in your face!\n", filling);
  326. }
  327.  
  328.