home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / alt / sources / 2872 < prev    next >
Encoding:
Text File  |  1992-12-23  |  39.3 KB  |  1,411 lines

  1. Newsgroups: alt.sources
  2. Path: sparky!uunet!spool.mu.edu!news.cs.indiana.edu!umn.edu!csus.edu!netcom.com!thinman
  3. From: thinman@netcom.com (Technically Sweet)
  4. Subject: COOL: C Object-Oriented Library: part 3 of 4
  5. Message-ID: <1992Dec23.191553.10523@netcom.com>
  6. Organization: International Foundation for Internal Freedom
  7. Date: Wed, 23 Dec 1992 19:15:53 GMT
  8. Lines: 1401
  9.  
  10. #!/bin/sh
  11. # This is part 03 of a multipart archive
  12. # ============= coolint.h ==============
  13. if test -f 'coolint.h' -a X"$1" != X"-c"; then
  14.     echo 'x - skipping coolint.h (File already exists)'
  15. else
  16. echo 'x - extracting coolint.h (Text)'
  17. sed 's/^X//' << 'SHAR_EOF' > 'coolint.h' &&
  18. X/*
  19. X *    COOL: C Object-Oriented Library
  20. X *
  21. X *    Internal data structures
  22. X *
  23. X *     Copyright 1991 by Lance Norskog
  24. X *
  25. X * Permission to use, copy, modify, and distribute this software and its
  26. X * documentation for any purpose and without fee is hereby granted, provided
  27. X * that the above copyright notice appear in all copies and that both that
  28. X * copyright notice and this permission notice appear in supporting
  29. X * documentation.  This software is provided "as is" without express or
  30. X * implied warranty.
  31. X */
  32. X
  33. X/* Table sizes */
  34. X#define    MAXOBJECTS    300        /* Possible total number of objects */
  35. X#ifdef    i386
  36. X#define    MAXMETHODS    64        /* Possible number of methods classes */
  37. X#else
  38. X#define    MAXMETHODS    32        /* Possible number of methods classes */
  39. X#endif
  40. X#define    MAXPARENTS    7        /* Possible number of parent classes */
  41. X#define MYMETHODS    MAXPARENTS    /* List of class's methods */
  42. X#define ALLCLASSES    (MAXPARENTS+1)    /* Total set of class method lists */
  43. X#define MAXCOMP        10        /* Possible number of components */
  44. X
  45. X#define MAXARGS        9        /* Total arguments to a method */
  46. X
  47. X#define    MAXEXCEPTIONS    50        /* Possible size of exception stack */
  48. X
  49. Xtypedef unsigned int cint;
  50. Xtypedef unsigned short sint;
  51. X
  52. X/* Table of methods for an object */
  53. X/* DO i need separate handling for arguments? */
  54. Xtypedef struct cool_methtab {
  55. X    char         *m_name;        /* Method Name */
  56. X    ret_t        (*m_func)();        /* function handler */
  57. X    char         *m_rettype;        /* Method return type */
  58. X    char         *m_argtypes[MAXARGS];    /* Method argument types */
  59. X    short        m_nargs;        /* Number of arguments */
  60. X    short        m_gap;
  61. X} methtab_t;
  62. X
  63. X/* If type is one of known types, the string is replaced with this code: */
  64. X#define CVOID    ((char *)  1)
  65. X#define CINT    ((char *)  2)
  66. X#define CBOOL    ((char *)  3)
  67. X#define CCHAR    ((char *)  4)
  68. X#define CSTR    ((char *)  5)
  69. X#define COBJ    ((char *)  6)
  70. X#define CMSG    ((char *)  7)
  71. X#define CDBL    ((char *)  8)
  72. X#define CIVECT    ((char *)  9)
  73. X#define CDVECT    ((char *) 10)
  74. X#define CANY    ((char *) 11)
  75. X/* VR add-ons: */
  76. X#define CDPOINT    ((char *) 12)
  77. X#define CDHOMOG    ((char *) 13)
  78. X/* table of strings is in lookup.c */
  79. X
  80. X/* Table of registered objects */
  81. Xstruct cool_objtab {
  82. X    char         *ob_name;        /* Object Name */
  83. X/*    int        ob_methods;        /* Number of methods */
  84. X    sint        ob_next;        /* Next in alloc or free list */
  85. X    sint        ob_ambiguous;        /* Parent conflict counter */
  86. X    sint        ob_generation;        /* Parent conflict counter */
  87. X    methtab_t    *ob_methtab[ALLCLASSES];/* Tables of methods */
  88. X    cool_priv_t     *ob_private[ALLCLASSES];/* Private data structures */
  89. X                        /* start with a cool_priv_t */
  90. X    object_t    ob_components[MAXCOMP];    /* Component list */
  91. X};
  92. X
  93. Xtypedef struct cool_objtab objtab_t;
  94. Xobjtab_t cool_objtab[MAXOBJECTS];
  95. Xint cool_numobs; /* bogus */
  96. Xsint cool_freelist;
  97. X
  98. X#ifdef    __STDC__
  99. X#define    P(a) a
  100. X#define    P2(a, b) a, b
  101. X#else
  102. X#define P(a)
  103. X#define P2(a, b)
  104. X#endif
  105. X
  106. X/* Malloc front-ends */
  107. Xchar    *cool_malloc(P(int));
  108. Xvoid    cool_free(P(char *));
  109. Xchar    *cool_realloc(P2(char *, int));
  110. X
  111. X/* String front-ends */
  112. Xchar    *cool_strdup(P(char *));
  113. Xvoid    cool_strfree(P(char *));
  114. Xchar    *cool_makename();
  115. Xint    cool_streq(P2(char *, char *));
  116. X
  117. X#ifdef    i386
  118. X#define    RAM_INC        4096            /* table expansion increment */
  119. X/* Objects are indices into a table */
  120. X#define MKOBJECT(o,gen)    ((object_t) (o | (gen << 9)))
  121. X#define MAXIMUMGEN    0x3fff
  122. X#define BADGEN        0xffff
  123. X#define GENOF(o)    (((cint) o >> 9) & MAXIMUMGEN)
  124. X#define OBJECTOF(o)    (((cint) o & 0x1ff))
  125. X#define    ISOBJECT(o)    ((cint) o < 0x100000)
  126. X#define GOODGEN(o)    (cool_objtab[OBJECTOF(o)].ob_generation == GENOF(o))
  127. X/* Object "Class" is object #0 */
  128. X#define    ISCLASS(o)    (((cint) o < MAXOBJECTS) && \
  129. X                (cool_objtab[(cint)o].ob_parent == 0))
  130. X/* Methods are indices into the method table of an object */
  131. X#define ISMETHOD(m)    ((cint) m < 512)    /* table & parent bits */
  132. X
  133. X#define    OBJECTERR    ((object_t) -1)
  134. X#define    METHODERR    ((method_t) -2)
  135. X
  136. X/* Method address space segmentation: note MAXMETHODS & MAXPARENTS above */
  137. X#define PARENTOF(m)    (((cint) m) >> 6)
  138. X#define METHODOF(m)    (((cint) m) & 63)
  139. X#define MKMETHOD(p,m)    ((method_t) ((((cint) p & 7) << 6) | ((cint) m & 63)))
  140. X#endif
  141. X
  142. X/* Nicolas addition */
  143. X#ifdef    sparc
  144. X#define    RAM_INC        4096            /* table expansion increment */
  145. X/* Objects are indices into a table */
  146. X#define MKOBJECT(o,gen)    ((object_t) (o | (gen << 9)))
  147. X#define MAXIMUMGEN    0x3fff
  148. X#define BADGEN        0xffff
  149. X#define GENOF(o)    (((cint) o >> 9) & MAXIMUMGEN)
  150. X#define OBJECTOF(o)    (((cint) o & 0x1ff))
  151. X#define    ISOBJECT(o)    ((cint) o < 0x6000)
  152. X#define GOODGEN(o)    (cool_objtab[OBJECTOF(o)].ob_generation == GENOF(o))
  153. X/* Object "Class" is object #0 */
  154. X#define    ISCLASS(o)    (((cint) o < MAXOBJECTS) && \
  155. X                (cool_objtab[(cint)o].ob_parent == 0))
  156. X/* Methods are indices into the method table of an object */
  157. X#define ISMETHOD(m)    ((cint) m < 256)
  158. X
  159. X#define    OBJECTERR    ((object_t) -1)
  160. X#define    METHODERR    ((method_t) -2)
  161. X
  162. X/* Method address space segmentation: note MAXMETHODS & MAXPARENTS above */
  163. X#define PARENTOF(m)    (((cint) m) >> 5)
  164. X#define METHODOF(m)    (((cint) m) & 31)
  165. X#define MKMETHOD(p,m)    ((method_t) ((((cint) p & 7) << 5) | ((cint) m & 31)))
  166. X#endif
  167. X
  168. X/* nicolas addition */
  169. X#ifdef    mc68020
  170. X#define    RAM_INC        4096            /* table expansion increment */
  171. X/* Objects are indices into a table */
  172. X#define MKOBJECT(o,gen)    ((object_t) (o | (gen << 9)))
  173. X#define MAXIMUMGEN    0x3fff
  174. X#define BADGEN        0xffff
  175. X#define GENOF(o)    (((cint) o >> 9) & MAXIMUMGEN)
  176. X#define OBJECTOF(o)    (((cint) o & 0x1ff))
  177. X#define    ISOBJECT(o)    ((cint) o < 0x6000)
  178. X#define GOODGEN(o)    (cool_objtab[OBJECTOF(o)].ob_generation == GENOF(o))
  179. X/* Object "Class" is object #0 */
  180. X#define    ISCLASS(o)    (((cint) o < MAXOBJECTS) && \
  181. X                (cool_objtab[(cint)o].ob_parent == 0))
  182. X/* Methods are indices into the method table of an object */
  183. X#define ISMETHOD(m)    ((cint) m < 256)
  184. X
  185. X#define    OBJECTERR    ((object_t) -1)
  186. X#define    METHODERR    ((method_t) -2)
  187. X
  188. X/* Method address space segmentation: note MAXMETHODS & MAXPARENTS above */
  189. X#define PARENTOF(m)    (((cint) m) >> 5)
  190. X#define METHODOF(m)    (((cint) m) & 31)
  191. X#define MKMETHOD(p,m)    ((method_t) ((((cint) p & 7) << 5) | ((cint) m & 31)))
  192. X#endif
  193. X
  194. X/* Henk Davids' version of SPARC support. */
  195. X#ifdef    SUN4
  196. X#define    RAM_INC        4096            /* table expansion increment */
  197. X/* Objects are indices into a table */
  198. X/* Use the same method as for VMS (see below) to identify object ID's
  199. X * Observation: constant strings have addresses from 0x4000 and above;
  200. X * memory allocated by malloc is above that, but on my machine the last
  201. X * was at 0x5a43c50 - this obviously will depend on your system, but
  202. X * I'm fairly confident that the 0x80000000 will not be reached for normal
  203. X * programs...
  204. X */
  205. X#define OBJ_FLAG    (0x80000000)
  206. X#define MKOBJECT(o,gen)    ((object_t) (o | (gen << 9) | OBJ_FLAG))
  207. X#define MAXIMUMGEN    0x3fff
  208. X#define BADGEN        0xffff
  209. X#define GENOF(o)    (((cint) o >> 9) & MAXIMUMGEN)
  210. X#define OBJECTOF(o)    (((cint) o & 0x1ff))
  211. X#define    ISOBJECT(o)    ((cint) (((cint)o & OBJ_FLAG) == OBJ_FLAG))
  212. X#define GOODGEN(o)    (cool_objtab[OBJECTOF(o)].ob_generation == GENOF(o))
  213. X/* Object "Class" is object #0 */
  214. X/* Not used either. Need to strip OBJ_FLAG before use? */
  215. X#define    ISCLASS(o)    (((cint) o < MAXOBJECTS) && \
  216. X                (cool_objtab[(cint)o].ob_parent == 0))
  217. X/* Methods are indices into the method table of an object */
  218. X/* Sun should have no problem here. String pointers assumed to be always
  219. X   higher than the 256 below
  220. X */
  221. X#define ISMETHOD(m)    ((cint) m < 256)
  222. X
  223. X#define    OBJECTERR    ((object_t) -1)
  224. X#define    METHODERR    ((method_t) -2)
  225. X
  226. X/* Method address space segmentation: note MAXMETHODS & MAXPARENTS above */
  227. X#define PARENTOF(m)    (((cint) m) >> 5)
  228. X#define METHODOF(m)    (((cint) m) & 31)
  229. X#define MKMETHOD(p,m)    ((method_t) ((((cint) p & 7) << 5) | ((cint) m & 31)))
  230. X#endif /* SUN4 */
  231. X
  232. X#ifdef    VMS
  233. X#define    RAM_INC        4096            /* table expansion increment */
  234. X/* Objects are indices into a table */
  235. X/* On VMS, we could mark objects by adding the OBJ (high) bit. String pointers
  236. X   can then be identified by the macro's below.
  237. X   The original method relies on strings in high memory - that does
  238. X   not work on VMS. Adding the high bit will produce addresses in S0/S1
  239. X   space, which cannot occur in user-mode programs.
  240. X   Alternatives:
  241. X    - we could move the $CHAR_STRINGS_CONSTANTS psect into high memory?
  242. X */
  243. X#define OBJ_FLAG    (0x80000000)
  244. X#define MKOBJECT(o,gen)    ((object_t) (o | (gen << 9) | OBJ_FLAG))
  245. X#define MAXIMUMGEN    0x3fff
  246. X#define BADGEN        0xffff
  247. X#define GENOF(o)    (((cint) o >> 9) & MAXIMUMGEN)
  248. X#define OBJECTOF(o)    (((cint) o & 0x1ff))
  249. X#define    ISOBJECT(o)    ((cint) (((cint)o & OBJ_FLAG) == OBJ_FLAG))
  250. X/* Not used (and incorrect): */
  251. X/* #define GOODGEN(o)    (cool_objtab[OBJOF(o)].ob_generation == GENOF(o)) */
  252. X/* should be: */
  253. X#define GOODGEN(o)    (cool_objtab[OBJECTOF(o)].ob_generation == GENOF(o))
  254. X/* Object "Class" is object #0 */
  255. X/* Not used either. Need to strip OBJ_FLAG before use? */
  256. X#define    ISCLASS(o)    (((cint) o < MAXOBJECTS) && \
  257. X                (cool_objtab[(cint)o].ob_parent == 0))
  258. X/* Methods are indices into the method table of an object */
  259. X/* VMS should have no problem here. String pointers assumed to be always
  260. X   higher than the 256 below
  261. X */
  262. X#define ISMETHOD(m)    ((cint) m < 256)
  263. X
  264. X#define    OBJECTERR    ((object_t) -1)
  265. X#define    METHODERR    ((method_t) -2)
  266. X
  267. X/* Method address space segmentation: note MAXMETHODS & MAXPARENTS above */
  268. X#define PARENTOF(m)    (((cint) m) >> 5)
  269. X#define METHODOF(m)    (((cint) m) & 31)
  270. X#define MKMETHOD(p,m)    ((method_t) ((((cint) p & 7) << 5) | ((cint) m & 31)))
  271. X#endif /* VMS */
  272. X
  273. X/* Standard methods for every class.  Fixed places for fast ob build/nuke. */
  274. X/* Create, Destroy, Method, Inherit, Component */
  275. X#define    METHOD_CREATE        MKMETHOD(MYMETHODS, 0)
  276. X#define    METHOD_CREATE2        MKMETHOD(MYMETHODS, 1)
  277. X#define    METHOD_DESTROY        MKMETHOD(MYMETHODS, 2)
  278. X#define    METHOD_METHOD        MKMETHOD(MYMETHODS, 3)
  279. X#define    METHOD_INHERIT        MKMETHOD(MYMETHODS, 4)
  280. X#define    METHOD_COMPONENT    MKMETHOD(MYMETHODS, 5)
  281. X#define    METHOD_CLONE        MKMETHOD(MYMETHODS, 6)
  282. X#define CLASSMETHODS        7
  283. X#define    METHOD_INIT        MKMETHOD(MYMETHODS, 7)
  284. X#define    METHOD_EXIT        MKMETHOD(MYMETHODS, 8)
  285. X#define    METHOD_COPY        MKMETHOD(MYMETHODS, 9)
  286. X#define STDMETHODS        10
  287. X/* More? */
  288. X
  289. Xobject_t     cool_object(P(object_t)), cool_getobject(P(object_t));
  290. Xmethod_t     cool_method(P2(object_t, method_t));
  291. Xmethod_t    cool_getmethod(P2(method_t, methtab_t *));
  292. X
  293. X#if    defined(USG) || defined(VMS)
  294. X#define        bcopy(from, to, count)    memcpy(to, from, count)
  295. X#define        bzero(to, count)    memset(to, 0, count)
  296. X#endif
  297. X
  298. X/* Strangely enough, this is reentrant */
  299. Xint        __cool_parent;
  300. Xobjtab_t    *__cool_obj;
  301. Xmethtab_t    *__cool_mt;
  302. X
  303. X/* Dangerous but fast.  Don't use until you're fully debugged. */
  304. X/* type/void, with arguments. Don't call these with strings! */
  305. X#define        coolt(type, o, m) \
  306. X((type) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
  307. X    __cool_parent = PARENTOF(m), \
  308. X    __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
  309. X    (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1)))
  310. X
  311. X#define        coolv(o, m) \
  312. X((void) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
  313. X    __cool_parent = PARENTOF(m), \
  314. X    __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
  315. X    (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1)))
  316. X
  317. X#define        coolta(type, o, m, arg) \
  318. X((type) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
  319. X    __cool_parent = PARENTOF(m), \
  320. X    __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
  321. X    (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1, arg)))
  322. X
  323. X#define        coolta2(type, o, m, arg, arg2) \
  324. X((type) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
  325. X    __cool_parent = PARENTOF(m), \
  326. X    __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
  327. X    (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1, arg, arg2)))
  328. X
  329. X#define        coolva(o, m, arg) \
  330. X((void) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
  331. X    __cool_parent = PARENTOF(m), \
  332. X    __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
  333. X    (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1, arg)))
  334. X
  335. X#define        coolva2(o, m, arg, arg2) \
  336. X((void) (__cool_obj = &cool_objtab[OBJECTOF(o)], \
  337. X    __cool_parent = PARENTOF(m), \
  338. X    __cool_mt = &__cool_obj->ob_methtab[__cool_parent][METHODOF(m)], \
  339. X    (* __cool_mt->m_func)(__cool_obj->ob_private[__cool_parent] + 1, arg, arg2)))
  340. X
  341. X/* Pathetic attempt at speed */
  342. X#define    cool_vars    register int __cool_parent; register objtab_t *__cool_obj; register methtab_t *__cool_mt;
  343. SHAR_EOF
  344. chmod 0644 coolint.h ||
  345. echo 'restore of coolint.h failed'
  346. Wc_c="`wc -c < 'coolint.h'`"
  347. test 12130 -eq "$Wc_c" ||
  348.     echo 'coolint.h: original size 12130, current size' "$Wc_c"
  349. fi
  350. # ============= cool.spec ==============
  351. if test -f 'cool.spec' -a X"$1" != X"-c"; then
  352.     echo 'x - skipping cool.spec (File already exists)'
  353. else
  354. echo 'x - extracting cool.spec (Text)'
  355. sed 's/^X//' << 'SHAR_EOF' > 'cool.spec' &&
  356. XCopyright 1991 by Lance Norskog
  357. X
  358. XThe COOL (C Object-Oriented Library) is a research project to add
  359. Xdynamic classing to C with no language-mangling.
  360. X
  361. XImplementation:
  362. XThere is an object table.
  363. XClasses are objects.
  364. XEvery object has a table of methods.
  365. XEvery object and every method has a string name.
  366. XObject names are in the form [a-zA-Z]([a-zA-Z0-9_])*.
  367. XMethod names are in the form 
  368. X[a-zA-Z][a-zA-Z0-9_]*(:[a-zA-Z][a-zA-Z0-9_](,[a-zA-Z][a-zA-Z0-9_])*)
  369. XThis syntax is not enforced.
  370. X
  371. XThe index in the object table may always be used in place of the object's
  372. Xname, and a method's index in the method table of an object may always
  373. Xbe used in place of a method string name.  For example, these might be
  374. Xsynonymous:
  375. X    cool_msg("Point3", (method_t) 4, 5.0, 7.0, 9.0);
  376. X    cool_msg((object_t) 59, "Draw", 5.0, 7.0, 9.0);
  377. X    coolv((object_t) 59, (method_t) 4, 5.0, 7.0, 9.0);
  378. X
  379. XThe object and method number spaces are divided.
  380. XThe object number space is in two parts: object table index and
  381. Xobject generation number.  If you use an object number for 
  382. Xan object which has been destroyed, the object generation number will
  383. Xbe wrong.  The method number space is divided into the parent
  384. Xclass of the method, and the index into that parent's method table.
  385. X
  386. X
  387. SHAR_EOF
  388. chmod 0644 cool.spec ||
  389. echo 'restore of cool.spec failed'
  390. Wc_c="`wc -c < 'cool.spec'`"
  391. test 1236 -eq "$Wc_c" ||
  392.     echo 'cool.spec: original size 1236, current size' "$Wc_c"
  393. fi
  394. # ============= tst1.c ==============
  395. if test -f 'tst1.c' -a X"$1" != X"-c"; then
  396.     echo 'x - skipping tst1.c (File already exists)'
  397. else
  398. echo 'x - extracting tst1.c (Text)'
  399. sed 's/^X//' << 'SHAR_EOF' > 'tst1.c' &&
  400. X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
  401. X
  402. X/* Class creation test */
  403. X
  404. X#include <setjmp.h>
  405. X#include <stdio.h>
  406. X#include "cool.h"
  407. X
  408. X/* Counter class: local storage for class is an integer. */
  409. X
  410. Xvoid    increment(ip)    int *ip;    { (*ip)++; }
  411. Xvoid    decrement(ip)    int *ip;    { (*ip)--; }
  412. Xint    value(ip)     int *ip;    { return *ip; }
  413. Xvoid    reset(ip)     int *ip;    { *ip = 0; }
  414. X
  415. Xcounter_init() {
  416. X    /* No parent classes */
  417. X    cool_msg("Class", "Create", "Counter", sizeof(int), NULL);    
  418. X    cool_msg("Counter", "Method", "Increment", increment, 0);
  419. X    cool_msg("Counter", "Method", "Decrement", decrement, 0);
  420. X    cool_msg("Counter", "Method", "Value", value, 0);    
  421. X    cool_msg("Counter", "Method", "Reset", reset, 0);
  422. X}
  423. X
  424. Xmain() {
  425. X    object_t c;
  426. X
  427. X    cool_init();        /* Must be called first */
  428. X    counter_init();        /* Set up Counter class */
  429. X
  430. X    /* Class usage */
  431. X
  432. X    c = cool_msg("Counter", "Create", NULL);
  433. X    cool_msg(c, "Reset");
  434. X    cool_msg(c, "Increment");
  435. X    cool_msg(c, "Increment");
  436. X    cool_msg(c, "Decrement");
  437. X    printf("This better be one: %d\n", cool(int, (c, "Value", NULL)));
  438. X    cool_msg("Counter", "Destroy", c);
  439. X}
  440. X
  441. X
  442. SHAR_EOF
  443. chmod 0644 tst1.c ||
  444. echo 'restore of tst1.c failed'
  445. Wc_c="`wc -c < 'tst1.c'`"
  446. test 1086 -eq "$Wc_c" ||
  447.     echo 'tst1.c: original size 1086, current size' "$Wc_c"
  448. fi
  449. # ============= tst2.c ==============
  450. if test -f 'tst2.c' -a X"$1" != X"-c"; then
  451.     echo 'x - skipping tst2.c (File already exists)'
  452. else
  453. echo 'x - extracting tst2.c (Text)'
  454. sed 's/^X//' << 'SHAR_EOF' > 'tst2.c' &&
  455. X/* Copyright 1991 by Lance Norskog */
  456. X
  457. X/* Class inheritance test */
  458. X
  459. X#include <setjmp.h>
  460. X#include <stdio.h>
  461. X#include "cool.h"
  462. X
  463. X/* Number abstract superclass: local storage for class is not used. */
  464. X/* 
  465. X * Superclass of integer-like things: ints, shorts, 256-degree angles.
  466. X */
  467. X
  468. Xvoid    print(junk)    
  469. Xint *junk;    
  470. X{ 
  471. X    printf("%d", cool(int, (SELFOF(junk), "Get", NULL))); 
  472. X}
  473. X
  474. Xvoid    scan(junk, numstr)    
  475. Xchar *junk;    
  476. Xchar *numstr;
  477. X{ 
  478. X    int local;
  479. X
  480. X    if (sscanf(numstr, "%d", &local) == 1)
  481. X        cool_msg(SELFOF(junk), "Set", local); 
  482. X    else
  483. X        cool_raise("NotANumber", "%s", numstr);
  484. X}
  485. X
  486. Xvoid number_init() {
  487. X    cool_msg("Class", "Create", "Number", sizeof(char), NULL);    
  488. X    cool_msg("Number", "Method", "Print", print, 0);
  489. X    cool_msg("Number", "Method", "Scan", scan, 1);
  490. X}
  491. X
  492. X/* Integer class: local storage for class is an integer. */
  493. X
  494. Xint    get(ip)         int *ip;        { 
  495. Xreturn *ip; 
  496. X}
  497. Xvoid    set(ip, i2)         int *ip, i2;        { 
  498. X*ip = i2; 
  499. X}
  500. Xvoid    add(ip, i2)        int *ip, i2;        { 
  501. X*ip += i2; 
  502. X}
  503. Xvoid    add2(ip, i2, i3)    int *ip, i2, i3;    { 
  504. X*ip = i2 + i3; 
  505. X}
  506. X
  507. Xvoid integer_init() {
  508. X
  509. X    /* Inherit Abstract Superclass Number */
  510. X    cool_msg("Class", "Create", "Integer", sizeof(int), "Number", NULL);    
  511. X    cool_msg("Integer", "Method", "Get", get, 0);
  512. X    cool_msg("Integer", "Method", "Set", set, 1);
  513. X    cool_msg("Integer", "Method", "Add:integer", add, 1);
  514. X    cool_msg("Integer", "Method", "Add:integer,integer", add2, 2);
  515. X}
  516. X
  517. Xmain() {
  518. X    object_t i;
  519. X
  520. X    cool_init();        /* Must be called first */
  521. X    number_init();
  522. X    integer_init();
  523. X
  524. X    /* Class usage */
  525. X
  526. X    i = cool_msg("Integer", "Create", NULL);
  527. X    cool_msg(i, "Scan", "15");
  528. X    cool_msg(i, "Add:integer", 20);
  529. X    cool_msg(i, "Add:integer,integer", cool(int, (i, "Get")), 8);
  530. X    printf("This better be 43: ");
  531. X    cool_msg(i, "Print");
  532. X    printf("\n");
  533. X    cool_msg("Integer", "Destroy", i);
  534. X}
  535. X
  536. X
  537. SHAR_EOF
  538. chmod 0644 tst2.c ||
  539. echo 'restore of tst2.c failed'
  540. Wc_c="`wc -c < 'tst2.c'`"
  541. test 1762 -eq "$Wc_c" ||
  542.     echo 'tst2.c: original size 1762, current size' "$Wc_c"
  543. fi
  544. # ============= tst3.c ==============
  545. if test -f 'tst3.c' -a X"$1" != X"-c"; then
  546.     echo 'x - skipping tst3.c (File already exists)'
  547. else
  548. echo 'x - extracting tst3.c (Text)'
  549. sed 's/^X//' << 'SHAR_EOF' > 'tst3.c' &&
  550. X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
  551. X
  552. X/* Class multiple inheritance test */
  553. X
  554. X#include <setjmp.h>
  555. X#include <stdio.h>
  556. X#include "cool.h"
  557. X
  558. X/* Number abstract superclass: local storage for class is not used. */
  559. X/* 
  560. X * Superclass of integer-like things: ints, shorts, 256-degree angles.
  561. X */
  562. X
  563. Xvoid    print(junk)    
  564. Xint *junk;    
  565. X{ 
  566. X    printf("%d", cool(int, (SELFOF(junk), "Get", NULL))); 
  567. X}
  568. X
  569. Xvoid    scan(junk, numstr)    
  570. Xchar *junk;    
  571. Xchar *numstr;
  572. X{ 
  573. X    int local;
  574. X
  575. X    if (sscanf(numstr, "%d", &local) == 1)
  576. X        cool_msg(SELFOF(junk), "Set", local); 
  577. X    else
  578. X        cool_raise("NotANumber", "%s", numstr);
  579. X}
  580. X
  581. Xvoid number_init() {
  582. X    cool_msg("Class", "Create", "Number", sizeof(char), NULL);    
  583. X    cool_msg("Number", "Method", "Print", print, 0);
  584. X    cool_msg("Number", "Method", "Scan", scan, 1);
  585. X}
  586. X
  587. X/* Subtraction abstract superclass: local storage for class is not used. */
  588. X/* 
  589. X * Superclass that does subtract.
  590. X */
  591. X
  592. Xvoid    sub(junk, i)    
  593. Xint *junk;    
  594. Xint i;
  595. X{ 
  596. X    int local;
  597. X
  598. X    local = (int) cool_msg(SELFOF(junk), "Get");
  599. X    local -= i;
  600. X    cool_msg(SELFOF(junk), "Set", local); 
  601. X}
  602. X
  603. X
  604. Xvoid subtractor_init() {
  605. X    cool_msg("Class", "Create", "Subtractor", sizeof(char), NULL);    
  606. X    cool_msg("Subtractor", "Method", "Subtract:integer", sub, 1);
  607. X}
  608. X
  609. X/* Integer class: local storage for class is an integer. */
  610. X
  611. Xint    get(ip)         int *ip;        { return *ip; }
  612. Xvoid    set(ip, i2)         int *ip, i2;        { *ip = i2; }
  613. Xvoid    add(ip, i2)        int *ip, i2;        { *ip += i2; }
  614. Xvoid    add2(ip, i2, i3)    int *ip, i2, i3;    { *ip = i2 + i3; }
  615. X
  616. Xvoid integer_init() {
  617. X
  618. X    /* Inherit Abstract Superclass Number */
  619. X    cool_msg("Class", "Create", "Integer", sizeof(int), 
  620. X        "Number", "Subtractor", NULL);    
  621. X    cool_msg("Integer", "Method", "Get", get, 0);
  622. X    cool_msg("Integer", "Method", "Set", set, 1);
  623. X    cool_msg("Integer", "Method", "Add:integer", add, 1);
  624. X    cool_msg("Integer", "Method", "Add:integer,integer", add2, 2);
  625. X}
  626. X
  627. Xmain() {
  628. X    object_t i;
  629. X
  630. X    cool_init();        /* Must be called first */
  631. X    number_init();
  632. X    subtractor_init();
  633. X    integer_init();
  634. X
  635. X    /* Class usage */
  636. X
  637. X    i = cool_msg("Integer", "Create", NULL);
  638. X    cool_msg(i, "Scan", "15");
  639. X    cool_msg(i, "Add:integer", 20);
  640. X    cool_msg(i, "Add:integer,integer", cool(int, (i, "Get")), 10);
  641. X    cool_msg(i, "Subtract:integer", 2);
  642. X    printf("This better be 43: ");
  643. X    cool_msg(i, "Print");
  644. X    printf("\n");
  645. X    cool_msg("Integer", "Destroy", i);
  646. X}
  647. X
  648. X
  649. SHAR_EOF
  650. chmod 0644 tst3.c ||
  651. echo 'restore of tst3.c failed'
  652. Wc_c="`wc -c < 'tst3.c'`"
  653. test 2298 -eq "$Wc_c" ||
  654.     echo 'tst3.c: original size 2298, current size' "$Wc_c"
  655. fi
  656. # ============= tst4.c ==============
  657. if test -f 'tst4.c' -a X"$1" != X"-c"; then
  658.     echo 'x - skipping tst4.c (File already exists)'
  659. else
  660. echo 'x - extracting tst4.c (Text)'
  661. sed 's/^X//' << 'SHAR_EOF' > 'tst4.c' &&
  662. X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
  663. X
  664. X/* Exception handling test. */
  665. X
  666. X#include <setjmp.h>
  667. X#include "cool.h"
  668. X
  669. Xmain() {
  670. X
  671. X    cool_exception("NastyBad",
  672. X    {     /* code */
  673. X        cool_raise("NastyBad", "Exceptions don\'t %s!", "work");
  674. X    },{    /* handler */
  675. X        printf("Exceptions do indeed work.\n");
  676. X    })
  677. X    cool_raise("ThisIsAnException", "Remove the core file");
  678. X}
  679. SHAR_EOF
  680. chmod 0644 tst4.c ||
  681. echo 'restore of tst4.c failed'
  682. Wc_c="`wc -c < 'tst4.c'`"
  683. test 375 -eq "$Wc_c" ||
  684.     echo 'tst4.c: original size 375, current size' "$Wc_c"
  685. fi
  686. # ============= tst5.c ==============
  687. if test -f 'tst5.c' -a X"$1" != X"-c"; then
  688.     echo 'x - skipping tst5.c (File already exists)'
  689. else
  690. echo 'x - extracting tst5.c (Text)'
  691. sed 's/^X//' << 'SHAR_EOF' > 'tst5.c' &&
  692. X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
  693. X
  694. X/* Class multiple inheritance clashing methods "Inherit" test */
  695. X
  696. X#include <setjmp.h>
  697. X#include <stdio.h>
  698. X#include "cool.h"
  699. X
  700. X/* Number abstract superclass: local storage for class is not used. */
  701. X/* 
  702. X * Superclass of integer-like things: ints, shorts, 256-degree angles.
  703. X */
  704. X
  705. Xvoid    print(junk)    
  706. Xint *junk;    
  707. X{ 
  708. X    printf("%d", cool(int, (SELFOF(junk), "Get", NULL))); 
  709. X}
  710. X
  711. Xvoid    scan(junk, numstr)    
  712. Xchar *junk;    
  713. Xchar *numstr;
  714. X{ 
  715. X    int local;
  716. X
  717. X    if (sscanf(numstr, "%d", &local) == 1)
  718. X        cool_msg(SELFOF(junk), "Set", local); 
  719. X    else
  720. X        cool_raise("NotANumber", "%s", numstr);
  721. X}
  722. X
  723. Xvoid number_init() {
  724. X    cool_msg("Class", "Create", "Number", sizeof(char), NULL);    
  725. X    cool_msg("Number", "Method", "Print", print, 0);
  726. X    cool_msg("Number", "Method", "Scan", scan, 1);
  727. X}
  728. X
  729. X/* Subtraction abstract superclass: local storage for class is not used. */
  730. X/* 
  731. X * Superclass that does subtract and printing.
  732. X */
  733. X
  734. Xvoid    sub(junk, i)    
  735. Xint *junk;    
  736. Xint i;
  737. X{ 
  738. X    int local;
  739. X
  740. X    local = (int) cool_msg(SELFOF(junk), "Get");
  741. X    local -= i;
  742. X    cool_msg(SELFOF(junk), "Set", local); 
  743. X}
  744. X
  745. X/* Another Print method. */
  746. Xvoid    sub_print(junk, i)    
  747. Xint *junk;    
  748. X{ 
  749. X    int local;
  750. X
  751. X    printf("%d", cool(int, (SELFOF(junk), "Get", NULL))); 
  752. X}
  753. X
  754. X
  755. Xvoid subtractor_init() {
  756. X    cool_msg("Class", "Create", "Subtractor", sizeof(char), NULL);    
  757. X    cool_msg("Subtractor", "Method", "Subtract:integer", sub, 1);
  758. X    cool_msg("Subtractor", "Method", "Print", sub_print, 1);
  759. X}
  760. X
  761. X/* Integer class: local storage for class is an integer. */
  762. X
  763. Xint    get(ip)         int *ip;        { return *ip; }
  764. Xvoid    set(ip, i2)         int *ip, i2;        { *ip = i2; }
  765. Xvoid    add(ip, i2)        int *ip, i2;        { *ip += i2; }
  766. Xvoid    add2(ip, i2, i3)    int *ip, i2, i3;    { *ip = i2 + i3; }
  767. X
  768. Xvoid integer_init() {
  769. X
  770. X    /* Inherit Abstract Superclass Number */
  771. X    cool_msg("Class", "Create", "Integer", sizeof(int), 
  772. X        "Number", "Subtractor", NULL);    
  773. X    cool_msg("Integer", "Method", "Get", get, 0);
  774. X    cool_msg("Integer", "Method", "Set", set, 1);
  775. X    cool_msg("Integer", "Method", "Add:integer", add, 1);
  776. X    cool_msg("Integer", "Method", "Add:integer,integer", add2, 2);
  777. X    cool_msg("Integer", "Inherit", "Print", "Subtractor");
  778. X}
  779. X
  780. Xmain() {
  781. X    object_t i;
  782. X
  783. X    cool_init();        /* Must be called first */
  784. X    number_init();
  785. X    subtractor_init();
  786. X    integer_init();
  787. X
  788. X    /* Class usage */
  789. X
  790. X    i = cool_msg("Integer", "Create", NULL);
  791. X    cool_msg(i, "Scan", "15");
  792. X    cool_msg(i, "Add:integer", 20);
  793. X    cool_msg(i, "Add:integer,integer", cool(int, (i, "Get")), 10);
  794. X    cool_msg(i, "Subtract:integer", 2);
  795. X    printf("This better be 43: ");
  796. X    cool_msg(i, "Print");
  797. X    printf("\n");
  798. X    cool_msg("Integer", "Destroy", i);
  799. X}
  800. X
  801. X
  802. SHAR_EOF
  803. chmod 0644 tst5.c ||
  804. echo 'restore of tst5.c failed'
  805. Wc_c="`wc -c < 'tst5.c'`"
  806. test 2592 -eq "$Wc_c" ||
  807.     echo 'tst5.c: original size 2592, current size' "$Wc_c"
  808. fi
  809. # ============= tst6.c ==============
  810. if test -f 'tst6.c' -a X"$1" != X"-c"; then
  811.     echo 'x - skipping tst6.c (File already exists)'
  812. else
  813. echo 'x - extracting tst6.c (Text)'
  814. sed 's/^X//' << 'SHAR_EOF' > 'tst6.c' &&
  815. X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
  816. X
  817. X/* Class creation test with init/exit/clone. */
  818. X
  819. X#include <setjmp.h>
  820. X#include <stdio.h>
  821. X#include "cool.h"
  822. X
  823. X/* Counter class: local storage for class is an integer. */
  824. X
  825. Xvoid    cinit(ip)    int *ip;    { printf("Counter created\n"); }
  826. Xvoid    ccopy(ip,ip2)    int *ip, *ip2;    { printf("Counter cloned\n"); }
  827. Xvoid    cexit(ip)    int *ip;    { printf("Counter destroyed\n"); }
  828. Xvoid    increment(ip)    int *ip;    { (*ip)++; }
  829. Xvoid    decrement(ip)    int *ip;    { (*ip)--; }
  830. Xint    value(ip)     int *ip;    { return *ip; }
  831. Xvoid    reset(ip)     int *ip;    { *ip = 0; }
  832. X
  833. Xmain() {
  834. X    object_t c, clone;
  835. X
  836. X    cool_init();        /* Must be called first */
  837. X
  838. X    /* No parent classes */
  839. X    cool_msg("Class", "Create", "Counter", sizeof(int), NULL);    
  840. X    cool_msg("Counter", "Method", "Init", cinit, 0);
  841. X    cool_msg("Counter", "Method", "Copy", ccopy, 0);
  842. X    cool_msg("Counter", "Method", "Exit", cexit, 0);
  843. X    cool_msg("Counter", "Method", "Increment", increment, 0);
  844. X    cool_msg("Counter", "Method", "Decrement", decrement, 0);
  845. X    cool_msg("Counter", "Method", "Value", value, 0);    
  846. X    cool_msg("Counter", "Method", "Reset", reset, 0);
  847. X
  848. X    /* Class usage */
  849. X
  850. X    c = cool_msg("Counter", "Create", NULL);
  851. X    cool_msg(c, "Reset");
  852. X    cool_msg(c, "Increment");
  853. X    cool_msg(c, "Increment");
  854. X    cool_msg(c, "Decrement");
  855. X    printf("This better be one: %d\n", cool(int, (c, "Value", NULL)));
  856. X    clone = cool_msg("Counter", "Clone", c, NULL);
  857. X    printf("This also better be one: %d\n",cool(int,(clone,"Value",NULL)));
  858. X    cool_msg(clone, "Increment");
  859. X    printf("This better be one: %d\n", cool(int, (c, "Value", NULL)));
  860. X    printf("This better be two: %d\n", cool(int, (clone, "Value", NULL)));
  861. X    cool_msg("Counter", "Destroy", c);
  862. X    printf("This still better be two: %d\n", cool(int, (clone, "Value", NULL)));
  863. X}
  864. X
  865. X
  866. SHAR_EOF
  867. chmod 0644 tst6.c ||
  868. echo 'restore of tst6.c failed'
  869. Wc_c="`wc -c < 'tst6.c'`"
  870. test 1753 -eq "$Wc_c" ||
  871.     echo 'tst6.c: original size 1753, current size' "$Wc_c"
  872. fi
  873. # ============= tst7.c ==============
  874. if test -f 'tst7.c' -a X"$1" != X"-c"; then
  875.     echo 'x - skipping tst7.c (File already exists)'
  876. else
  877. echo 'x - extracting tst7.c (Text)'
  878. sed 's/^X//' << 'SHAR_EOF' > 'tst7.c' &&
  879. X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
  880. X
  881. X/* Class 3rd-level inheritance test */
  882. X
  883. X#include <setjmp.h>
  884. X#include <stdio.h>
  885. X#include "cool.h"
  886. X
  887. X/* Number abstract superclass: local storage for class is not used. */
  888. X/* 
  889. X * Superclass of integer-like things: ints, shorts, 256-degree angles.
  890. X */
  891. X
  892. Xvoid    print(junk)    
  893. Xint *junk;    
  894. X{ 
  895. X    printf("%d", cool(int, (SELFOF(junk), "Get", NULL))); 
  896. X}
  897. X
  898. Xvoid    scan(junk, numstr)    
  899. Xchar *junk;    
  900. Xchar *numstr;
  901. X{ 
  902. X    int local;
  903. X
  904. X    if (sscanf(numstr, "%d", &local) == 1)
  905. X        cool_msg(SELFOF(junk), "Set", local); 
  906. X    else
  907. X        cool_raise("NotANumber", "%s", numstr);
  908. X}
  909. X
  910. Xvoid number_init() {
  911. X    cool_msg("Class", "Create", "Number", sizeof(char), NULL);    
  912. X    cool_msg("Number", "Method", "Print", print, 0);
  913. X    cool_msg("Number", "Method", "Scan", scan, 1);
  914. X}
  915. X
  916. X/* Subtraction abstract superclass: local storage for class is not used. */
  917. X/* 
  918. X * Superclass that does subtract.
  919. X */
  920. X
  921. Xvoid    sub(junk, i)    
  922. Xint *junk;    
  923. Xint i;
  924. X{ 
  925. X    int local;
  926. X
  927. X    local = (int) cool_msg(SELFOF(junk), "Get");
  928. X    local -= i;
  929. X    cool_msg(SELFOF(junk), "Set", local); 
  930. X}
  931. X
  932. X
  933. Xvoid subtractor_init() {
  934. X    cool_msg("Class", "Create", "Subtractor", sizeof(char), NULL);    
  935. X    cool_msg("Subtractor", "Method", "Subtract:integer", sub, 1);
  936. X}
  937. X
  938. X/* Integer class: local storage for class is an integer. */
  939. X
  940. Xint    get(ip)         int *ip;        { return *ip; }
  941. Xvoid    set(ip, i2)         int *ip, i2;        { *ip = i2; }
  942. Xvoid    add(ip, i2)        int *ip, i2;        { *ip += i2; }
  943. Xvoid    add2(ip, i2, i3)    int *ip, i2, i3;    { *ip = i2 + i3; }
  944. X
  945. Xvoid integer_init() {
  946. X
  947. X    /* Inherit Abstract Superclass Number */
  948. X    cool_msg("Class", "Create", "Integer", sizeof(int), 
  949. X        "Number", "Subtractor", NULL);    
  950. X    cool_msg("Integer", "Method", "Get", get, 0);
  951. X    cool_msg("Integer", "Method", "Set", set, 1);
  952. X    cool_msg("Integer", "Method", "Add:integer", add, 1);
  953. X    cool_msg("Integer", "Method", "Add:integer,integer", add2, 2);
  954. X}
  955. X
  956. Xint    ratget(ip)         int *ip;        { return *ip; }
  957. Xvoid    ratset(ip, i2)         int *ip, i2;        { *ip = i2; }
  958. X
  959. Xvoid rational_init() {
  960. X
  961. X    /* Inherit Abstract Superclass Number */
  962. X    cool_msg("Class", "Create", "Rational", sizeof(int), 
  963. X        "Integer", /* "Number", "Subtractor", */ NULL);    
  964. X    cool_msg("Rational", "Method", "RatGet", ratget, 0);
  965. X    cool_msg("Rational", "Method", "RatSet", ratset, 1);
  966. X}
  967. X
  968. Xmain() {
  969. X    object_t i;
  970. X
  971. X    cool_init();        /* Must be called first */
  972. X    number_init();
  973. X    subtractor_init();
  974. X    integer_init();
  975. X    rational_init();
  976. X
  977. X    /* Class usage */
  978. X
  979. X    i = cool_msg("Rational", "Create", "KingRat");
  980. X    cool_msg(i, "Scan", "15");
  981. X    cool_msg(i, "Add:integer", 20);
  982. X    cool_msg(i, "Add:integer,integer", cool(int, (i, "Get")), 10);
  983. X    cool_msg(i, "Subtract:integer", 2);
  984. X    printf("This better be 43: ");
  985. X    cool_msg(i, "Print");
  986. X    printf("\n");
  987. X    cool_msg(i, "RatSet", 86);
  988. X    printf("This better be 86: %d\n", cool_msg(i, "RatGet"));
  989. X    cool_msg("Rational", "Destroy", i);
  990. X}
  991. X
  992. X
  993. SHAR_EOF
  994. chmod 0644 tst7.c ||
  995. echo 'restore of tst7.c failed'
  996. Wc_c="`wc -c < 'tst7.c'`"
  997. test 2788 -eq "$Wc_c" ||
  998.     echo 'tst7.c: original size 2788, current size' "$Wc_c"
  999. fi
  1000. # ============= tst8.c ==============
  1001. if test -f 'tst8.c' -a X"$1" != X"-c"; then
  1002.     echo 'x - skipping tst8.c (File already exists)'
  1003. else
  1004. echo 'x - extracting tst8.c (Text)'
  1005. sed 's/^X//' << 'SHAR_EOF' > 'tst8.c' &&
  1006. X/* Copyright abandoned 1991 by Lance Norskog. Use as you will. */
  1007. X
  1008. X/* Class component test */
  1009. X
  1010. X#include <setjmp.h>
  1011. X#include <stdio.h>
  1012. X#include "cool.h"
  1013. X
  1014. X/* Printer class. */
  1015. X
  1016. Xprinter_print(junk, n)     int n;        { printf("%d\n", n); }
  1017. X
  1018. Xprinter_init() {
  1019. X    /* No parent classes */
  1020. X    cool_msg("Class", "Create", "Printer", 4, NULL);    
  1021. X    cool_msg("Printer", "Method", "Print", printer_print, 1);
  1022. X}
  1023. X
  1024. X/* Counter class: local storage for class is an integer. */
  1025. X
  1026. Xvoid    increment(ip)    int *ip;    { (*ip)++; }
  1027. Xvoid    decrement(ip)    int *ip;    { (*ip)--; }
  1028. Xint    value(ip)     int *ip;    { return *ip; }
  1029. Xvoid    reset(ip)     int *ip;    { *ip = 0; }
  1030. X
  1031. Xcounter_init() {
  1032. X    /* No parent classes */
  1033. X    cool_msg("Class", "Create", "Counter", sizeof(int), NULL);    
  1034. X    cool_msg("Counter", "Method", "Increment", increment, 0);
  1035. X    cool_msg("Counter", "Method", "Decrement", decrement, 0);
  1036. X    cool_msg("Counter", "Method", "Value", value, 0);    
  1037. X    cool_msg("Counter", "Method", "Reset", reset, 0);
  1038. X    cool_msg("Counter", "Component", "Printer", "P");
  1039. X}
  1040. X
  1041. Xmain() {
  1042. X    object_t c, p, p2;
  1043. X
  1044. X    cool_init();        /* Must be called first */
  1045. X    printer_init();        /* Set up Printer class */
  1046. X    counter_init();        /* Set up Counter class */
  1047. X
  1048. X    /* Class usage */
  1049. X
  1050. X    c = cool_msg("Counter", "Create", "C");
  1051. X    cool_msg(c, "Reset");
  1052. X    cool_msg(c, "Increment");
  1053. X    cool_msg(c, "Increment");
  1054. X    cool_msg(c, "Decrement");
  1055. X    p = cool_componentof(c, "P");
  1056. X    p2 = cool_object("C.P");
  1057. X    if (p != p2) {
  1058. X        printf("Object component lookups don't match!\n");
  1059. X        exit(1);
  1060. X    }
  1061. X    printf("This better be one: ");
  1062. X    cool(void, (p, "Print", cool(int, (c, "Value", NULL))));
  1063. X    printf("\n");
  1064. X    cool_msg("Counter", "Destroy", c);
  1065. X}
  1066. X
  1067. X
  1068. SHAR_EOF
  1069. chmod 0644 tst8.c ||
  1070. echo 'restore of tst8.c failed'
  1071. Wc_c="`wc -c < 'tst8.c'`"
  1072. test 1600 -eq "$Wc_c" ||
  1073.     echo 'tst8.c: original size 1600, current size' "$Wc_c"
  1074. fi
  1075. # ============= bench.c ==============
  1076. if test -f 'bench.c' -a X"$1" != X"-c"; then
  1077.     echo 'x - skipping bench.c (File already exists)'
  1078. else
  1079. echo 'x - extracting bench.c (Text)'
  1080. sed 's/^X//' << 'SHAR_EOF' > 'bench.c' &&
  1081. X/*
  1082. X * Copyright 1991 by Lance Norskog
  1083. X *
  1084. X * Updated from Henk Davids' VMS port.  Cleaner and portable now.
  1085. X */
  1086. X
  1087. X/* Simple benchmark of Object messaging v.s. case/subroutine */
  1088. X
  1089. X#include <setjmp.h>
  1090. X#include <stdio.h>
  1091. X#include <sys/types.h>
  1092. X#ifndef VMS 
  1093. X#include <sys/param.h>
  1094. X#include <sys/times.h>
  1095. X#endif
  1096. X#include "cool.h"
  1097. X#include "coolint.h"
  1098. X
  1099. X/* From Henk Davids' VMS port. */
  1100. Xvoid   startclock();
  1101. Xdouble cputime();
  1102. X
  1103. X/* If the fastest test takes less than 1 second, increase. */
  1104. X#define    ITERATIONS    5000000
  1105. X
  1106. X/* Counter class: local storage for class is an integer. */
  1107. X
  1108. Xvoid    increment(ip)    int *ip;    { (*ip)++; }
  1109. Xvoid    decrement(ip)    int *ip;    { (*ip)--; }
  1110. Xint    value(ip)     int *ip;    { return *ip; }
  1111. Xvoid    reset(ip)     int *ip;    { *ip = 0; }
  1112. X
  1113. Xcounter_init() {
  1114. X    /* No parent classes */
  1115. X    cool_msg("Class", "Create", "Counter", sizeof(int), NULL);    
  1116. X    cool_msg("Counter", "Method", "Increment", increment, 0);
  1117. X    cool_msg("Counter", "Method", "Decrement", decrement, 0);
  1118. X    cool_msg("Counter", "Method", "Value", value, 0);    
  1119. X    cool_msg("Counter", "Method", "Reset", reset, 0);
  1120. X}
  1121. X
  1122. Xmain() {
  1123. X    object_t c;
  1124. X    method_t inc;
  1125. X    double obtime, cooltime, tabtime, casetime, calltime, codetime, scale;
  1126. X    int i;
  1127. X    void (*functab[8])();
  1128. X    
  1129. X
  1130. X    cool_init();        /* Must be called first */
  1131. X    counter_init();        /* Set up Counter class */
  1132. X
  1133. X    /* Class usage */
  1134. X
  1135. X    c = cool_msg("Counter", "Create", NULL);
  1136. X    cool_msg(c, "Reset");
  1137. X    inc = cool_method(c, "Increment");
  1138. X    {
  1139. X        startclock();
  1140. X        for(i = 0; i < ITERATIONS; i++)
  1141. X            cool_msg(c, inc);
  1142. X        obtime = cputime();
  1143. X        printf("Object:\t%g\n", obtime);
  1144. X    }
  1145. X    {
  1146. X        cool_vars        /* Local variables for fast messaging */
  1147. X
  1148. X        startclock();
  1149. X        for(i = 0; i < ITERATIONS; i++)
  1150. X            coolv(c, inc);
  1151. X        cooltime = cputime();
  1152. X        printf("Coolt:\t%g\n", cooltime);
  1153. X    }
  1154. X    {
  1155. X        for(i = 0; i < 8; i++)
  1156. X            functab[i] = increment;
  1157. X        startclock();
  1158. X        i = 0;
  1159. X        while(i < ITERATIONS) 
  1160. X            (* functab[i & 7])(&i);
  1161. X        tabtime = cputime();
  1162. X        printf("Table:\t%g\n", tabtime);
  1163. X    }
  1164. X    {
  1165. X        startclock(0);
  1166. X        i = 0;
  1167. X        while(i < ITERATIONS) {
  1168. X            switch(i & 7) {
  1169. X                case 0:
  1170. X                    increment(&i);
  1171. X                    break;
  1172. X                case 2:
  1173. X                    increment(&i);
  1174. X                    break;
  1175. X                case 3:
  1176. X                    increment(&i);
  1177. X                    break;
  1178. X                case 6:
  1179. X                    increment(&i);
  1180. X                    break;
  1181. X                default:
  1182. X                    increment(&i);
  1183. X                    break;
  1184. X            }
  1185. X        }
  1186. X        casetime = cputime();
  1187. X        printf("Case:\t%g\n", casetime);
  1188. X    }
  1189. X    {
  1190. X        startclock();
  1191. X        i = 0;
  1192. X        while(i < ITERATIONS) 
  1193. X            increment(&i);
  1194. X        calltime = cputime();
  1195. X        printf("Call:\t%g\n", calltime);
  1196. X    }
  1197. X    {
  1198. X        startclock();
  1199. X        for(i = 0; i < ITERATIONS; i++)
  1200. X            continue;
  1201. X        codetime = cputime();
  1202. X        printf("Code:\t%g\n", codetime);
  1203. X    }
  1204. X    scale = 1.0/casetime;
  1205. X    printf("Object: %2.2g  Cool %2.2g  Table %2.2g  Case %2.2g  Call %2.2g  Code %2.2g\n", 
  1206. X        obtime * scale, cooltime * scale, tabtime * scale, 
  1207. X        casetime * scale, calltime * scale, codetime * scale);
  1208. X}
  1209. SHAR_EOF
  1210. chmod 0644 bench.c ||
  1211. echo 'restore of bench.c failed'
  1212. Wc_c="`wc -c < 'bench.c'`"
  1213. test 2781 -eq "$Wc_c" ||
  1214.     echo 'bench.c: original size 2781, current size' "$Wc_c"
  1215. fi
  1216. # ============= timer.c ==============
  1217. if test -f 'timer.c' -a X"$1" != X"-c"; then
  1218.     echo 'x - skipping timer.c (File already exists)'
  1219. else
  1220. echo 'x - extracting timer.c (Text)'
  1221. sed 's/^X//' << 'SHAR_EOF' > 'timer.c' &&
  1222. X/****************************\
  1223. X*                  *
  1224. X*  Timer functions for VMS   *
  1225. X*                  *
  1226. X\****************************/
  1227. X
  1228. X/* From Henk Davids. */
  1229. X
  1230. X/*
  1231. X * The SUN4 gives bogus readings using the BSD-style resource values.
  1232. X * I don't know if the code is bogus, or the SUN4 does resources wrong.
  1233. X */
  1234. X
  1235. X#include <stdio.h>
  1236. X#if    defined(USG) || defined(sparc)
  1237. X#include <sys/types.h>
  1238. X#include <sys/param.h>
  1239. X#include <sys/times.h>
  1240. X#else
  1241. X#include <sys/time.h>
  1242. X#include <sys/file.h>
  1243. X#ifdef VMS 
  1244. X#ifdef TESTING
  1245. X#include <unixio.h>
  1246. X#include <redexp.VMS>
  1247. X#endif /* TESTING */
  1248. X#else
  1249. X#include <limits.h>
  1250. X#include <sys/types.h>
  1251. X#include <sys/resource.h>
  1252. X#endif
  1253. X#endif
  1254. X
  1255. Xvoid   startclock();
  1256. Xdouble elapsedtime();
  1257. Xdouble cputime();
  1258. X
  1259. X#ifdef TESTING
  1260. Xstatic void doit();
  1261. X#define LOOPS 100000
  1262. X
  1263. Xmain(argc, argv)
  1264. X  int    argc;
  1265. X  char * argv[];
  1266. X{
  1267. X    double elapsed, cpu;
  1268. X    
  1269. X    startclock();
  1270. X
  1271. X    doit();
  1272. X
  1273. X    elapsed = elapsedtime();
  1274. X    cpu = cputime();
  1275. X    printf("%.2f sec, cpu: %.2f (%.2f%%)\n",
  1276. X        elapsed, cpu, (cpu/elapsed*100));
  1277. X    printf("Per loop:\n");
  1278. X    printf("%f sec, cpu: %f (%.2f%%)\n",
  1279. X        elapsed/LOOPS, cpu/LOOPS, (cpu/elapsed*100));
  1280. X    exit(0);
  1281. X}
  1282. X
  1283. Xstatic float dummy;
  1284. Xdouble sin();
  1285. Xvoid doit()
  1286. X{
  1287. X    int i;
  1288. X    for (i=0; i<LOOPS; i++) {
  1289. X    dummy = sin((1.0*i)/LOOPS);
  1290. X    }
  1291. X}
  1292. X#endif /* TESTING */
  1293. X
  1294. X/* ----- timer functions -----
  1295. X *    void   startclock();
  1296. X *    double elapsedtime();    in secs, since last startclock()
  1297. X *    double cputime();    in secs, since last startclock()
  1298. X */
  1299. X
  1300. X#define FACTORV5 100000.0
  1301. X#define FACTORV8 100000000.0
  1302. X
  1303. Xstatic double cpu_so_far();
  1304. Xstatic double time_so_far();
  1305. Xstatic void   error_exit();
  1306. X
  1307. Xstatic double last_cpustamp = 0.0;
  1308. Xstatic double last_timestamp = 0.0;
  1309. X
  1310. X#if    defined(USG) || defined(sparc)
  1311. Xint    hz = 0;
  1312. X#endif
  1313. X
  1314. Xvoid
  1315. Xstartclock()
  1316. X{
  1317. X#if    defined(USG) || defined(sparc)
  1318. X  if (! hz)
  1319. X    if (getenv("HZ") && atoi(getenv("HZ")))
  1320. X      hz = atoi(getenv("HZ"));
  1321. X    else  hz = HZ;
  1322. X#endif
  1323. X  last_cpustamp = cpu_so_far();
  1324. X  last_timestamp = time_so_far();
  1325. X}
  1326. X
  1327. Xdouble elapsedtime() {
  1328. X    return(time_so_far() - last_timestamp);
  1329. X}
  1330. Xdouble cputime() {
  1331. X    return(cpu_so_far() - last_cpustamp);
  1332. X}
  1333. X
  1334. Xstatic double 
  1335. Xcpu_so_far()
  1336. X{
  1337. X#ifdef VMS
  1338. X  tbuffer_t tms;
  1339. X
  1340. X  times(&tms);
  1341. X  return ((double) tms.proc_user_time) / ((double) CLK_TCK) +
  1342. X    ((double) tms.proc_system_time) / ((double) CLK_TCK);
  1343. X#elif defined(USG) || defined(sparc)
  1344. X  struct tms tms;
  1345. X  times(&tms);
  1346. X  return ((double) tms.tms_utime / (double) hz);
  1347. X#else
  1348. X  struct rusage rusage;
  1349. X
  1350. X  getrusage(RUSAGE_SELF, &rusage);
  1351. X  return
  1352. X    ((double) rusage.ru_utime.tv_sec) +
  1353. X      (((double) rusage.ru_utime.tv_usec) / FACTORV8) +
  1354. X        ((double) rusage.ru_stime.tv_sec) +
  1355. X          (((double) rusage.ru_stime.tv_usec) / FACTORV8);
  1356. X#endif
  1357. X}
  1358. X
  1359. Xstatic double
  1360. Xtime_so_far()
  1361. X{
  1362. X#ifdef VMS
  1363. X  timeb_t tms;
  1364. X
  1365. X  ftime(&tms);
  1366. X  return ((double) tms.time + (double) tms.millitm / FACTORV5);
  1367. X#elif defined(USG) || defined(sparc)
  1368. X  /* system time in clicks */
  1369. X  struct tms tms;
  1370. X  return (double) times(&tms) / (double) hz;
  1371. X#else
  1372. X  struct timeval tp;
  1373. X
  1374. X  if (gettimeofday(&tp, (struct timezone *) NULL) == -1)
  1375. X    error_exit("gettimeofday");
  1376. X  return ((double) (tp.tv_sec)) +
  1377. X    (((double) tp.tv_usec) / FACTORV8);
  1378. X#endif
  1379. X}
  1380. X
  1381. Xstatic void
  1382. Xerror_exit(message)
  1383. X  char * message;
  1384. X{
  1385. X  char buf[BUFSIZ];
  1386. X
  1387. X  sprintf(buf, "error in %s", message);
  1388. X  perror(buf);
  1389. X  exit(1);
  1390. X}
  1391. X
  1392. X
  1393. X
  1394. X
  1395. X
  1396. SHAR_EOF
  1397. chmod 0644 timer.c ||
  1398. echo 'restore of timer.c failed'
  1399. Wc_c="`wc -c < 'timer.c'`"
  1400. test 3319 -eq "$Wc_c" ||
  1401.     echo 'timer.c: original size 3319, current size' "$Wc_c"
  1402. fi
  1403. true || echo 'restore of benchall failed'
  1404. echo End of part 3, continue with part 4
  1405. exit 0
  1406. -- 
  1407.  
  1408. Lance Norskog
  1409.  
  1410. Data is not information is not knowledge is not wisdom.
  1411.