home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / NeoIntroAlone3.0 folder / Standalone / Laughs / Source / CPerson.cp < prev   
Encoding:
Text File  |  1994-09-02  |  6.6 KB  |  329 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.     fShoeSize = aShoeSize;
  227. }
  228.  
  229. CNeoPersist *CClown::New(void)
  230. {    
  231.     return new CClown();
  232. }
  233.  
  234. NeoID CClown::getClassID(void) const
  235. {
  236.     return kClownID;
  237. }
  238.  
  239. long CClown::getFileLength(void) const
  240. {
  241.     return kPersonFileLength + sizeof(fShoeSize);
  242. }
  243.  
  244. #pragma segment NeoRead
  245. void CClown::readObject(CNeoStream *aStream, const NeoTag aTag)
  246. {
  247.     inherited::readObject(aStream, aTag);
  248.  
  249.     fShoeSize = aStream->readLong();
  250. }
  251.  
  252. #pragma segment NeoWrite
  253. void CClown::writeObject(CNeoStream *aStream, const NeoTag aTag)
  254. {
  255.     inherited::writeObject(aStream, aTag);
  256.  
  257.     aStream->writeLong(fShoeSize);
  258. }
  259.  
  260. CPie *CClown::bakePie(const char *aFilling)
  261. {
  262.     CPie *            pie;
  263.  
  264.     // Create a new pie object having the proper type of filling.
  265.     // The default filling type is Custard, my favorite!
  266.     pie = new CPie(aFilling);
  267.  
  268.     // Add it to the database.
  269.     // Note: An object ID is assigned automaticly by addObject.
  270.     gNeoDatabase->addObject(pie);
  271.  
  272.     // Add the pie to this clown's part list
  273.     addToList(pie);
  274.  
  275.     return pie;
  276. }
  277.  
  278. CPie *CClown::getPie(const long aIndex)
  279. {
  280.     CPie *                    pie;
  281.     CNeoPartListIterator    iterator(gNeoDatabase, getListClassID(), getListRoot(FALSE), nil, TRUE, kPieID);
  282.  
  283.     // Randomly pick a pie
  284.     iterator.leap(aIndex);
  285.     pie = (CPie *)iterator.currentObject();
  286.  
  287.     // Iterators don't add references to objects. So we add one ourselves.
  288.     if (pie)
  289.         pie->referTo();
  290.  
  291.     return pie;
  292. }
  293.  
  294. void CClown::skill(void)
  295. {
  296.     long    count    = getPieCount();
  297.     CPie *    pie;
  298.  
  299.     if (count) {
  300.         // Randomly pick a pie
  301.         pie = getPie((rand()&0x7FFFFFFF) % count);
  302.         NeoAssert(pie);
  303.     
  304.         printf("Throws pies: ");
  305.         throwPie(pie);
  306.     
  307.         // Don't forget to remove our reference to the pie.
  308.         pie->unrefer();
  309.     }
  310.     else
  311.         printf("Has no pies to throw.\n");
  312. }
  313.  
  314. void CClown::throwPie(CPie *aPie)
  315. {
  316.     char    filling[kMaxFillingName];
  317.  
  318.     // Remove the pie from this clown's part list
  319.     deleteFromList(aPie);
  320.  
  321.     // Now remove it from the database completely.
  322.     gNeoDatabase->removeObject(aPie);
  323.  
  324.     // Throw it!
  325.     aPie->getFilling(filling);
  326.     printf("Here's a %s pie in your face!\n", filling);
  327. }
  328.  
  329.