home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / alt / sources / 2871 < prev    next >
Encoding:
Text File  |  1992-12-23  |  24.4 KB  |  798 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 1 of 4
  5. Message-ID: <1992Dec23.191457.10338@netcom.com>
  6. Organization: International Foundation for Internal Freedom
  7. Date: Wed, 23 Dec 1992 19:14:57 GMT
  8. Lines: 788
  9.  
  10. #!/bin/sh
  11. # This is a shell archive (produced by shar 3.49)
  12. # To extract the files from this archive, save it to a file, remove
  13. # everything above the "!/bin/sh" line above, and type "sh file_name".
  14. #
  15. # made 12/23/1992 19:09 UTC by thinman@netcom.com
  16. # Source directory /usr/lance/vr/cool/src
  17. #
  18. # existing files will NOT be overwritten unless -c is specified
  19. #
  20. # This shar contains:
  21. # length  mode       name
  22. # ------ ---------- ------------------------------------------
  23. #   4857 -rw-r--r-- README
  24. #   1868 -rw-r--r-- TODO
  25. #   2070 -rw-r--r-- Makefile
  26. #   1622 -rw-r--r-- descrip.mms
  27. #  10108 -rw-r--r-- cool.3
  28. #  17641 -rw-r--r-- class.c
  29. #   2528 -rw-r--r-- msg.c
  30. #   8509 -rw-r--r-- lookup.c
  31. #   1895 -rw-r--r-- exception.c
  32. #   2536 -rw-r--r-- util.c
  33. #   1945 -rw-r--r-- cool.h
  34. #  12130 -rw-r--r-- coolint.h
  35. #   1236 -rw-r--r-- cool.spec
  36. #   1086 -rw-r--r-- tst1.c
  37. #   1762 -rw-r--r-- tst2.c
  38. #   2298 -rw-r--r-- tst3.c
  39. #    375 -rw-r--r-- tst4.c
  40. #   2592 -rw-r--r-- tst5.c
  41. #   1753 -rw-r--r-- tst6.c
  42. #   2788 -rw-r--r-- tst7.c
  43. #   1600 -rw-r--r-- tst8.c
  44. #   2781 -rw-r--r-- bench.c
  45. #   3319 -rw-r--r-- timer.c
  46. #    348 -rw-r--r-- benchall
  47. #    745 -rw-r--r-- out.unix386
  48. #     84 -rw-r--r-- tstall.com
  49. #     22 -rw-r--r-- patchlevel.h
  50. #   3440 -rw-r--r-- cool_elk.c
  51. #   6375 -rw-r--r-- cool_scm.c
  52. #
  53. # ============= README ==============
  54. if test -f 'README' -a X"$1" != X"-c"; then
  55.     echo 'x - skipping README (File already exists)'
  56. else
  57. echo 'x - extracting README (Text)'
  58. sed 's/^X//' << 'SHAR_EOF' > 'README' &&
  59. X
  60. X
  61. X
  62. X        COOL - C Object-Oriented Library
  63. X
  64. XCOOL is: Copyright 1991 by Lance Norskog - ABANDONED
  65. X
  66. XI'm writing a large software project [1] and need something better
  67. Xthan C.  Something which is absolutely as portable as C.  After looking
  68. Xat C++, SmallTalk, and Objective C, I gave up and wrote COOL instead.
  69. X
  70. XIt adds objects to C with a minimum of code-junk and no preprocessing.
  71. XThe implementation is as simple as is reasonable.  Object messaging,
  72. Xcreation, and deletion are reasonably streamlined.  Little attempt has
  73. Xbeen made for high performance.  If you design for performance,
  74. Xyou usually get software that's fast but useless.
  75. X
  76. XSee the man page for more information and a rudimentary coding example.
  77. XSee the tst?.c programs for info on exercising features.
  78. X
  79. XThis is released to the net for general interest, information on
  80. Xhow a rudimentary object system works internally, and to serve
  81. Xas a vehicle for prototyping new OO styles.  Instead of conducting
  82. Xgedanken experiments about how objects should work, or spending
  83. Xyears designing new languages & compilers, now you can implement 
  84. Xyour variant and play around with it coding up your favorite examples.
  85. X
  86. XInheritance:
  87. XMultiple inheritance is implemented.  No ambiguous methods are
  88. Xallowed in a class definition.  Inheritance is implemented
  89. Xessentially as multiple objects with one name; these objects
  90. Xcan call SELF with methods they themselves don't implement.
  91. X
  92. XPorting:
  93. X
  94. XObjects are methods are given as strings or magic ID numbers.  
  95. XThe library contains routines which transform strings to IDs.
  96. XThe object and method ID spaces are both segmented into two
  97. Xpieces.  Object numbers are an array index and a generation
  98. Xnumber.  Method numbers are an array index and a parent index.
  99. X
  100. XThese number spaces will have to resegmented for your computer.
  101. XThe release includes macros for 386 UNIX, and assumes that
  102. Xstring addresses are in the data segment or in the stack
  103. Xlocal area.  The object and method spaces pack their two numbers
  104. Xavoiding these bit addresses, so that the library can distinguish
  105. Xbetween a string and pre-cached ID on the fly.
  106. X
  107. XThis implementation is mostly bogus.  Instead, it should
  108. Xhave a magic marker (I prefer 0xdeadbeef) at the beginning
  109. Xof an object.  But, encoding the generation number in
  110. Xthe object is still worthwhile.
  111. X
  112. XAlso, the exception system uses setjmp and longjmp.  If yours
  113. Xdon't work, you don't get catchable exceptions.  The object 
  114. Xlibrary uses exceptions, but doesn't catch them, so it will
  115. Xwork anyway.
  116. X
  117. XBeyond that, any 32-bit machine should be able to run COOL.
  118. XWith the above magic marker trick, it should run under DOS
  119. Xand Windows also.
  120. X
  121. XBenchmarks:
  122. X
  123. X'bench.c' makes counts an integer from 0 to a very large number
  124. Xin 6 different ways.  The first way uses canonical safe message
  125. Xcalls.  The second way uses unsafe cached messaging.  The remaining
  126. Xways use native C in various ways.  A case statement and a
  127. Xprocedure call is used as a standard against which to measure 
  128. Xmessaging performance.
  129. X
  130. X'benchall' compiles the benchmark and the library with the
  131. Xnative C compiler and with GCC, both with and without optimization,
  132. Xand runs the benchmark.
  133. X
  134. XThe files out.unix386 and out.xenix contain the benchmark results
  135. Xon those operating systems.
  136. X
  137. XPorts also included for 68020 and SPARC SUNs, and VMS.
  138. XThese ports, and some bug fixes, where contributed by Henk
  139. XDavids from Germany and Nicolas ?? from France.
  140. X
  141. XThis is the fourth release, patchlevel 4.  Patchlevel 2 was
  142. XNicolas' port to SUNs and bug fixes, which he posted to
  143. Xalt.sources.  These changes have been incorporated.
  144. XPatchlevel 4 didn't do grandparent classes right,
  145. Xand I've added components, overloading,
  146. Xand the Scheme interface files.  Cool_elk.c allows
  147. XELK Scheme to use COOL as an extension interface.
  148. XCool_scm.c does likewise with SCM Scheme.  The beauty
  149. Xpart is that you just write your extensions in C
  150. Xas classes, test them, then link them with Scheme.
  151. XYou don't have to write any custom code to bridge the two.
  152. X
  153. XCool_scm.c implements overloaded operators.  Cool_elk
  154. Xdoes not.  Overloaded operators are not well documented,
  155. Xand the implementation is of necessity iffy because
  156. Xof the coruscating plasticity of C's calling sequence.
  157. X
  158. XPlease send me all updates, ports, and benchmark results you can.
  159. XCOOL is a basic technology for my VR window system project,
  160. Xand I am the maintenence person.  I may rewrite it for 
  161. Xefficiency at some point.
  162. X
  163. XAcknowledgements:
  164. X    Nicolas ?? at Electricite De France <nicolas@cli52or.edf.fr>
  165. X        for SUN-3 & SUN-4 ports and bug fixes
  166. X    Henk Davids <hdavids@mswe.dnet.ms.philips.nl>
  167. X        for SUN-4 port and bug fixes
  168. X    David L. Williams <dlw@atherton.com>
  169. X        for NeXT port
  170. X
  171. XEnjoy!
  172. X
  173. XLance Norskog
  174. Xthinman@netcom.com
  175. X
  176. X[1] A Virtual Reality window system based on Scheme & C extensions.
  177. XSomething like NeWS...
  178. X
  179. Xp.s.    tst4 is supposed to cause a core dump.  That's how
  180. X    exceptions work.
  181. SHAR_EOF
  182. chmod 0644 README ||
  183. echo 'restore of README failed'
  184. Wc_c="`wc -c < 'README'`"
  185. test 4857 -eq "$Wc_c" ||
  186.     echo 'README: original size 4857, current size' "$Wc_c"
  187. fi
  188. # ============= TODO ==============
  189. if test -f 'TODO' -a X"$1" != X"-c"; then
  190.     echo 'x - skipping TODO (File already exists)'
  191. else
  192. echo 'x - extracting TODO (Text)'
  193. sed 's/^X//' << 'SHAR_EOF' > 'TODO' &&
  194. XTime efficiency
  195. X    Create, Delete, Init, & Exit go in pre-defined slots.
  196. X    Local area is preallocated, max 20 bytes or something like that.
  197. X    
  198. XSelf-style objects/protos/instances:
  199. X    Create methods to prototype objects cause instantiations.
  200. X    Delete methods to objects destroy themselves.
  201. X
  202. XSpace efficiency
  203. X    Use two arrays instead of one:
  204. X        Array of objects
  205. X        Array of maps
  206. X    Classes and prototype objects "own" a map;
  207. X    live objects refer to someone else's map.
  208. X    When live objects want to change their map,
  209. X    they do a copy-on-write and they get their own map.
  210. X    A clone of a live object points to the source object's map.
  211. X
  212. XParameter checking from method :syntax.
  213. X
  214. X    C types if lower case, classes if upper case
  215. X
  216. XFront-end interpreter
  217. X
  218. X    Simple I/O classes for front-end interpreter.
  219. X        Create front-end for C++ class library.
  220. X    Strings, files.
  221. X    {} handler in lexer does local vars somehow.
  222. X    List handlers
  223. X    Code class reads in data
  224. X    procedures:
  225. X        Code func name {
  226. X            stuff
  227. X        }
  228. X        name ( stuff )
  229. X
  230. XMultiple Inheritance
  231. X    Need nicer MI lookup.  
  232. X        Self-style priorities?
  233. X    Want resends for method wrapping.
  234. X        cool_resend(object, method)
  235. X            start after current method.
  236. X            Need a method stack for this?
  237. X
  238. XClasses:
  239. X    Garbage collection
  240. X    Use multiple inheritance.
  241. X        Stack style: inherit from StackGC parent
  242. X            Init routine pushes self on top of stack.
  243. X            Mark method returns top of stack.
  244. X            Free (Marker) method returns all StackGC obs
  245. X            alloc'd since that Marker.
  246. X        Mark/Sweep style: two parents: Holder & GCable
  247. X            Holders refer to objects in their private area
  248. X            A GCable object must be pointed to by one
  249. X            of the Holders, or it will be freed.
  250. X            An object may inherit from either or both.
  251. X            Sweep method to class Holder does a mark/sweep.
  252. X            Also, running out of objects does a mark/sweep
  253. X            while doing a realloc().
  254. X
  255. X    A class/object may inherit from StackGC and/or Holder/GCable.
  256. X
  257. SHAR_EOF
  258. chmod 0644 TODO ||
  259. echo 'restore of TODO failed'
  260. Wc_c="`wc -c < 'TODO'`"
  261. test 1868 -eq "$Wc_c" ||
  262.     echo 'TODO: original size 1868, current size' "$Wc_c"
  263. fi
  264. # ============= Makefile ==============
  265. if test -f 'Makefile' -a X"$1" != X"-c"; then
  266.     echo 'x - skipping Makefile (File already exists)'
  267. else
  268. echo 'x - extracting Makefile (Text)'
  269. sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
  270. X# COOL makefile for 386 Unix
  271. X# Copyright 1991 by Lance Norskog.  This software may be used for any purpose.
  272. X
  273. XOBJ= class.o msg.o lookup.o exception.o util.o
  274. X
  275. XFILES= README TODO Makefile descrip.mms cool.3 \
  276. X    class.c msg.c lookup.c exception.c util.c \
  277. X    cool.h coolint.h cool.spec \
  278. X    tst1.c tst2.c tst3.c tst4.c tst5.c tst6.c tst7.c tst8.c \
  279. X    bench.c timer.c benchall out.unix386 tstall.com \
  280. X    patchlevel.h \
  281. X    cool_elk.c cool_scm.c 
  282. X
  283. X# Works for UNIX/386 and Xenix/386
  284. X# Remove -DUSG for BSD machines, -Di386 for non-386 machines
  285. X
  286. XSYS=-DUSG -Di386
  287. X# SYS=-DSPARC
  288. X# SYS=-Dmc68020
  289. X
  290. X# -DSELFISH for Self-inspired object-only programming
  291. X# Not implemented yet.
  292. X
  293. X# -fpcc-struct-return I think is for SYSV bogus compilers only
  294. XGCC=gcc -fwritable-strings -fpcc-struct-return -pipe
  295. XCC=$(GCC)
  296. XCC=cc
  297. X
  298. XO=-g
  299. X
  300. XCFLAGS=$O $(SYS)
  301. X
  302. X# make a library
  303. XAR=ar r
  304. X
  305. X# library post-processing
  306. X# RANLIB=ranlib
  307. XRANLIB=true
  308. X
  309. X# The author's setup
  310. XSHAR= /usr2/tools/shar/shar -F -s thinman@netcom.com -o coolshar -l 40
  311. X
  312. X# the usual setup
  313. X# SHAR= /usr/local/bin/shar
  314. X
  315. Xall: libcool.a tests
  316. X
  317. Xlibcool.a: $(OBJ)
  318. X    rm -f $@
  319. X    $(AR) $@ $(OBJ)
  320. X    $(RANLIB) $@
  321. X
  322. Xinstall:
  323. X    cp libcool.a /usr/lib
  324. X
  325. Xtests: tst1 tst2 tst3 tst4 tst5 tst6 tst7 tst8 bench
  326. X
  327. Xtst1: tst1.c libcool.a
  328. X    $(CC) $(CFLAGS) $@.c -o $@ libcool.a
  329. X
  330. Xtst2: tst2.c libcool.a
  331. X    $(CC) $(CFLAGS) $@.c -o $@ libcool.a
  332. X
  333. Xtst3: tst3.c libcool.a
  334. X    $(CC) $(CFLAGS) $@.c -o $@ libcool.a
  335. X
  336. Xtst4: tst4.c libcool.a
  337. X    $(CC) $(CFLAGS) $@.c -o $@ libcool.a
  338. X
  339. Xtst5: tst5.c libcool.a
  340. X    $(CC) $(CFLAGS) $@.c -o $@ libcool.a
  341. X
  342. Xtst6: tst6.c libcool.a
  343. X    $(CC) $(CFLAGS) $@.c -o $@ libcool.a
  344. X
  345. Xtst7: tst7.c libcool.a
  346. X    $(CC) $(CFLAGS) $@.c -o $@ libcool.a
  347. X
  348. Xtst8: tst8.c libcool.a
  349. X    $(CC) $(CFLAGS) $@.c -o $@ libcool.a
  350. X
  351. Xbench: bench.c timer.c libcool.a
  352. X    $(CC) $(CFLAGS) -o $@ bench.c timer.c libcool.a
  353. X
  354. Xgraphics: graphics.c libcool.a
  355. X    $(GCC) $(CFLAGS) -o $@ graphics.c libcool.a
  356. X
  357. X$(OBJ): cool.h coolint.h
  358. X
  359. Xshar:
  360. X    @cp ../doc/cool.3 .
  361. X    @$(SHAR) $(FILES) 
  362. X
  363. Xbenchthem:
  364. X    CFLAGS="$(CFLAGS)" sh benchall 
  365. X
  366. Xclean:
  367. X    rm -f *.o 
  368. X    rm -f libcool.a 
  369. X    rm -f tst? 
  370. X    rm -f core
  371. X    rm -f bench
  372. SHAR_EOF
  373. chmod 0644 Makefile ||
  374. echo 'restore of Makefile failed'
  375. Wc_c="`wc -c < 'Makefile'`"
  376. test 2070 -eq "$Wc_c" ||
  377.     echo 'Makefile: original size 2070, current size' "$Wc_c"
  378. fi
  379. # ============= descrip.mms ==============
  380. if test -f 'descrip.mms' -a X"$1" != X"-c"; then
  381.     echo 'x - skipping descrip.mms (File already exists)'
  382. else
  383. echo 'x - extracting descrip.mms (Text)'
  384. sed 's/^X//' << 'SHAR_EOF' > 'descrip.mms' &&
  385. X# COOL makefile for VMS
  386. X# Copyright 1991 by Lance Norskog.  This software may be used for any purpose.
  387. X
  388. XOBJ= class.obj,msg.obj,lookup.obj,exception.obj,util.obj
  389. X
  390. XFILES= README Makefile cool.3 class.c msg.c lookup.c exception.c util.c \
  391. X    cool.h coolint.h cool.spec tst1.c tst2.c tst3.c tst4.c tst5.c tst6.c \
  392. X    bench.c benchall out.unix386 out.xenix patchlevel.h \
  393. X    bench_vms.c timer_vms.c changelog
  394. X
  395. X# /define=SELFISH for Self-inspired object-only programming
  396. X# Not implemented yet.
  397. X
  398. XCC=cc
  399. X# Use 'MMS/MACRO=DEBUG=1' to generate debug versions
  400. X.ifdef DEBUG
  401. XCFLAGS =/debug/noopt
  402. XLINKFLAGS =/debug
  403. X.else
  404. XCFLAGS =/optimize=noinline
  405. XLINKFLAGS =/notrace
  406. X.endif
  407. XLIBRARIES=dsk:libcool/libr
  408. X
  409. Xall : libcool.olb tests
  410. X    ! done
  411. X
  412. Xlibcool.olb : $(OBJ)
  413. X    libr/create $@ $(OBJ)
  414. X
  415. Xtests : tst1.exe tst2.exe tst3.exe tst4.exe tst5.exe tst6.exe bench_vms.exe
  416. X    ! done
  417. X
  418. Xtst1.exe : tst1.obj libcool.olb
  419. X    link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
  420. X
  421. Xtst2.exe : tst2.obj libcool.olb
  422. X    link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
  423. X
  424. Xtst3.exe : tst3.obj libcool.olb
  425. X    link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
  426. X
  427. Xtst4.exe : tst4.obj libcool.olb
  428. X    link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
  429. X
  430. Xtst5.exe : tst5.obj libcool.olb
  431. X    link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
  432. X
  433. Xtst6.exe : tst6.obj libcool.olb
  434. X    link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
  435. X
  436. Xbench_vms.exe : bench_vms.obj timer_vms.obj libcool.olb
  437. X    link $(LINKFLAGS) /exe=$@ $*.obj,timer_vms,$(LIBRARIES)
  438. X
  439. X$(OBJ) : cool.h coolint.h
  440. X
  441. X# shar :
  442. X#     @cp ./doc/cool.3 .
  443. X#     @/usr2/tools/shar/shar -F -s thinman@netcom.com $(FILES) 
  444. X# 
  445. X# benchthem :
  446. X#     CFLAGS="$(LINKFLAGS)" sh benchall 
  447. SHAR_EOF
  448. chmod 0644 descrip.mms ||
  449. echo 'restore of descrip.mms failed'
  450. Wc_c="`wc -c < 'descrip.mms'`"
  451. test 1622 -eq "$Wc_c" ||
  452.     echo 'descrip.mms: original size 1622, current size' "$Wc_c"
  453. fi
  454. # ============= cool.3 ==============
  455. if test -f 'cool.3' -a X"$1" != X"-c"; then
  456.     echo 'x - skipping cool.3 (File already exists)'
  457. else
  458. echo 'x - extracting cool.3 (Text)'
  459. sed 's/^X//' << 'SHAR_EOF' > 'cool.3' &&
  460. X." from libpbm.3
  461. X.de Ss
  462. X.    sp
  463. X.    ft CW
  464. X.    nf
  465. X..
  466. X.de Se
  467. X.    fi
  468. X.    ft R
  469. X.    sp
  470. X..
  471. X.TH COOL 3
  472. X.SH NAME
  473. XCOOL - C Object-Oriented Library
  474. X.SH SYNOPSIS
  475. X.Ss
  476. X#include <cool.h>
  477. Xcc ... libcool.a
  478. X
  479. Xtypedef char    *object_t, *method_t, *ret_t;
  480. Xvoid        cool_init();
  481. Xret_t        cool_msg(object_t object, method_t method [, args ]);
  482. X#define    cool(type, (msg ...))
  483. Xobject_t     cool_object(object_t object);
  484. Xint         cool_isobject(object_t object);
  485. Xmethod_t     cool_method(object_t object, method_t method);
  486. Xint         cool_hasmethod(object_t object, method_t method);
  487. X
  488. Xvoid        cool_raise(char *exception, fmt [, args ...] );
  489. X#define    cool_exception(char *exception, 
  490. X        { normal code },
  491. X        { exception code }
  492. X        )
  493. X.Se
  494. X.SH DESCRIPTION
  495. XThe
  496. X.I COOL 
  497. Xlibrary provides 
  498. X.B class 
  499. Xoperations in C.
  500. X.B Objects 
  501. Xmay be sent one or more
  502. X.B messages
  503. Xwith
  504. X.IR cool_msg ().
  505. XObjects and methods may be named by text strings, 
  506. Xor by magic numbers.
  507. X.IR cool_object ()
  508. Xreturns the magic number for a given object. 
  509. X.IR cool_method ()
  510. Xreturns the magic number for a given object and method.
  511. XIf an object or method doesn't exist, 
  512. Xan exception is raised.
  513. X.IR cool_isobject () 
  514. Xand
  515. X.IR cool_hasobject ()
  516. Xreturn boolean values for the respective questions,
  517. Xinstead of raising exceptions.
  518. XMessaging is optimized for use with these magic numbers.
  519. X.P
  520. X.IR cool_exception ()
  521. Xis a preprocessor macro, not a C subroutine.
  522. XIt registers an
  523. X.I exception 
  524. Xnamed
  525. X.I event,
  526. Xand executes the first code block.
  527. XIf 
  528. X.IR cool_raise ( event )
  529. Xoccurs before 
  530. Xwhile in the first code block,
  531. Xthe code execution is interrupted and the
  532. X.I handler ()
  533. Xroutine is called.
  534. X.IR cool_raise ()
  535. Xmay be called at any time without pre-registering
  536. Xthe event name.
  537. XIn this case, the default exception handler prints out the
  538. Xevent name and the program exits.
  539. X.SH PREDEFINED\ MESSAGES
  540. XThere are several predefined messages which have intrinsic meaning in 
  541. X.B COOL:
  542. X.TP 10
  543. XCreate
  544. Xmakes a new object.
  545. XCreate, given a class name, creates a new object of that class.
  546. XThe third argument gives the name of the new object.
  547. XIt may be NULL to indicate that the object has no name.
  548. XThe object_t code for the object is always returned.
  549. X.TP 10
  550. XDestroy
  551. Xdestroys an existing object.
  552. XDestroy is sent not to the object, but to the object's class.
  553. X.TP 10
  554. XMethod
  555. Xadds a method to a class.
  556. XThe second argument is the method name.
  557. XThe third argument gives the function handler for the method.
  558. XThe fourth argument gives the number of arguments to the method.
  559. XWhen a message is sent to an instance of the class
  560. Xwith the given method,
  561. Xthe given function will be called with the arguments after the method.
  562. X.TP 10
  563. XInherit
  564. Xcauses a class or object to inherit a method
  565. Xfrom one of its parent classes.
  566. XThe method is given as the third argument,
  567. Xand the parent class is given as the fourth argument.
  568. XThis breaks ambiguities which occur
  569. Xwhen two or more parents provide the same method.
  570. X.TP 10
  571. XClone
  572. Xcreates a ``deep copy'' of an object.
  573. X.B Clone
  574. Xis sent to a class with two arguments:
  575. Xthe object to be copied, and the name of the new object.
  576. XThe private data structures of the class are copied.
  577. XIf the class has a 
  578. X.B Copy
  579. Xmethod, that method is then called.
  580. XIf the class private data structure contains separately allocated
  581. Xmemory, the class should have a
  582. X.B Copy
  583. Xmethod.
  584. X." .TP 10
  585. X." Wrap
  586. X." adds a method also, 
  587. X."  and is given the same arguments.
  588. X."  If there is already a method with the given name,
  589. X." the new method will call the old method at some point.
  590. X." .TP 10
  591. X." Seal
  592. X." stops the addition of methods to a class.
  593. X." .TP 10
  594. X." Walk
  595. X." expects a class name and a function pointer as arguments.
  596. X." It calls the function repeatedly with the name of each of
  597. X." its sub-classes.
  598. X." .TP 10
  599. X." Parents
  600. X." also expects a class name and a function pointer as arguments.
  601. X." It calls the function repeatedly with the name of each of its parent classes.
  602. X." .TP 10
  603. X." Function
  604. X." returns the function pointer which would handle the particular
  605. X." class and type list in the argument list.
  606. X." .TP 10
  607. X." Sizeof
  608. X." returns the size of the local storage used by a class.
  609. X." .TP 10
  610. X." Comment
  611. X." expects a class or method name 
  612. X.SH METHODS
  613. XMethods are of the form "name:arglist",
  614. Xwhere arglist is a comma-seperated list of arguments to the method.
  615. XThese arguments should be base C types, "object_t", or "method_t".
  616. XThe message-passer can use these to verify on the fly that the
  617. Xcorrect arguments are being passed to the method.
  618. XIf you don't wish to specify the types of the arguments,
  619. Xuse empty strings.  
  620. XFor example, the method specifier "add" will cause the function to be called 
  621. Xwith no arguments, "add:" will pass one argument, "add:," will pass two.
  622. X.P
  623. XIf the method is called ``Init'',
  624. Xit is called upon creation of an object of this class.
  625. XIf the method is called ``Copy'',
  626. Xit is called upon the cloning of an object of this class.
  627. XIf the method is called ``Exit'',
  628. Xit is called upon destruction of an object of this class.
  629. X.SH CLASS LIFE CYCLE
  630. XTo create a new class, send the message ``Create'' to object ``Class''.
  631. XThe third argument gives the name of the new class.
  632. XThe fourth argument gives the size of the local storage used by
  633. Xthe class.
  634. XThe fifth and subsequent arguments give 
  635. Xthe NULL-terminated list of parent classes of this class.
  636. XIf none, the fifth argument must be NULL.
  637. X.P
  638. XTo create a new object from one of your classes, 
  639. Xsend the message "Create" to that class with the name
  640. Xof the object as the third argument.
  641. XIf you don't want to give a text name to the object,
  642. Xgive NULL as the name.
  643. XThe response to the message is the object ID.
  644. X.SH EXAMPLE
  645. XThe following example creates and uses an up-down counter class.
  646. X.Ss
  647. X\fI/* Class creation */\fP
  648. X.P
  649. Xvoid    increment(int *ip)        { *ip++; }
  650. Xvoid    decrement(int *ip)        { *ip--; }
  651. Xint    value(int *ip)        { return *ip; }
  652. Xvoid    reset(int *ip)        { *ip = 0; }
  653. X.P
  654. Xcool_init();        \fI/* Must be called first */\fP
  655. X.P
  656. Xcool_msg("Class", "Create", "Counter", sizeof(int), NULL);    \fI/* No parent classes */\fP
  657. Xcool_msg("Counter", "Method", "Increment", increment, 0);
  658. Xcool_msg("Counter", "Method", "Decrement", decrement, 0);
  659. Xcool_msg("Counter", "Method", "Value", value, 0);    
  660. Xcool_msg("Counter", "Method", "Reset", reset, 0);
  661. X." cool_msg("Counter", "Seal");
  662. X.P
  663. X\fI/* Class usage */\fP
  664. X.P
  665. Xc = cool_msg("Counter", "Create", NULL);
  666. Xcool_msg(c, "Reset");
  667. Xcool_msg(c, "Increment");
  668. Xcool_msg(c, "Increment");
  669. Xcool_msg(c, "Decrement");
  670. Xprintf("This better be one: %d\\n", cool_msg(c, "Value", NULL));
  671. X.Se
  672. XThis example shows the basics of using COOL to create an abstract data type.
  673. XA class Counter is created with a local storage of one integer,
  674. Xand no parent classes.
  675. XIt has four methods: Increment, Decrement, Reset, and Value.
  676. XNone of the methods takes an argument,
  677. Xthus their argument lists are empty strings.
  678. X.SH INHERITANCE
  679. XWhen a class is created with parent classes, 
  680. Xit inherits all methods from the parents.
  681. XIf there are conflicting method names from more than one parent,
  682. Xthe class itself must supply new methods to break the conflict.
  683. XIf an object is created before all conflicts are broken,
  684. Xthe "MethodConflict" exception is raised.
  685. X.P
  686. X.I "Inheritance is Aggregation".
  687. XAn object with inherited classes secretly consists of several 
  688. X.I "partial objects"
  689. Xwith one name.
  690. XEach partial object has its own private data storage.
  691. XThe 
  692. X.IR SELFOF () 
  693. Xand 
  694. X.IR CLASSOF () 
  695. Xmacros yield the object_t variables which name this object.
  696. XMethods in such in an object may call inherited methods 
  697. Xby sending messages to 
  698. X.IR SELFOF (mydata).
  699. X." .SH EXAMPLE
  700. X." Need a better example with more stuff here
  701. X.SH EXCEPTIONS
  702. X.IR COOL
  703. Xmaintains an exception system.
  704. XIt is implemented with the 
  705. X.IR setjmp () 
  706. Xand 
  707. X.IR longjmp () 
  708. Xlibrary routines.
  709. XThe 
  710. X.IR cool_exception ()
  711. Xmacro maintains an exception stack.
  712. X.IR cool_raise ( exception )
  713. Xcauses an
  714. X.I exception
  715. Xto occur.  If a parent subroutine handles this particular
  716. Xexception via the
  717. X.IR cool_exception ()
  718. Xmacro, 
  719. X.IR cool_raise ()
  720. Xwill short-circuit the normal subroutine return stack
  721. Xand jump into the
  722. X.I handler 
  723. Xsection of the 
  724. X.IR cool_exception ()
  725. Xmacro.  If there is no handler set up for the exception,
  726. Xthe default exception-handler will be called.
  727. XThis routine prints out the 
  728. X.IR printf ()
  729. Xarguments after the exception name,
  730. Xand causes a core dump.
  731. X.SH IMPLEMENTATION
  732. X.I object_t 
  733. Xis an integer index into an internal table of classes.
  734. X.I method_t
  735. Xis in integer index into a table of methods registered for a class.
  736. XThey may also be given string data.
  737. X.IR cool_msg ()
  738. Xdistinguishes 
  739. X.I object_t 
  740. Xand
  741. X.I method_t
  742. Xfrom strings because strings are always in high memory,
  743. Xand there is a clear separation in the number space.
  744. XThere may not be a class and a method with the same name.
  745. X.P
  746. XThe string table lookup is as simple and thus as slow as possible.
  747. XMethod dispatch with when given both object_t and method_t is
  748. Xas fast as possible.
  749. XIf you're serious about performance, cache your code numbers.
  750. X.P
  751. XThe macros 
  752. X.IR coolt (),
  753. X.IR coolv (),
  754. X.IR coolta (),
  755. X.IR coolva (),
  756. Xdo message lookups in-line.
  757. XThe 't' and 'v' suffices refer to typed or void, and 'a' adds an
  758. Xargument list.
  759. XThese macros expect object_t and method_t code numbers.
  760. XThey do not do string lookups; you have to cache your object
  761. Xand method numbers.
  762. XThese macros also do not do any argument list checking;
  763. Xthey should only be used in select code which has been thoroughly debugged.
  764. X.SH PORTABILITY
  765. XCOOL is ferociously dependent on integer<->pointer conversion.
  766. XIt attempts to keep everything in pointer form, converting
  767. Xto integers for throwaway index values.
  768. X.P
  769. XSetjmp and longjmp are partially non-functional on many machines.
  770. XMany compilers go nuts when you have register variables in
  771. Xa routine that does setjmp's.  
  772. XPortable code has nothing but a 
  773. X.IR cool_exception ()
  774. Xmacro in a subroutine; this avoids the register problem.
  775. XIf setjmp/longjmp don't work
  776. Xon your machine, you can still use 
  777. X.IR cool_raise ()
  778. Xto print a message and kill your program.
  779. X.SH ACKNOWLEDGEMENTS
  780. XCOOL was inspired by the need for classes in a large C programming
  781. Xproject; C++ and Objective C were examined and found wanting.
  782. XCOOL is, of course, strongly influenced by the Smalltalk class system.
  783. SHAR_EOF
  784. chmod 0644 cool.3 ||
  785. echo 'restore of cool.3 failed'
  786. Wc_c="`wc -c < 'cool.3'`"
  787. test 10108 -eq "$Wc_c" ||
  788.     echo 'cool.3: original size 10108, current size' "$Wc_c"
  789. fi
  790. true || echo 'restore of class.c failed'
  791. echo End of part 1, continue with part 2
  792. exit 0
  793. -- 
  794.  
  795. Lance Norskog
  796.  
  797. Data is not information is not knowledge is not wisdom.
  798.