home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: alt.sources
- Path: sparky!uunet!spool.mu.edu!news.cs.indiana.edu!umn.edu!csus.edu!netcom.com!thinman
- From: thinman@netcom.com (Technically Sweet)
- Subject: COOL: C Object-Oriented Library: part 1 of 4
- Message-ID: <1992Dec23.191457.10338@netcom.com>
- Organization: International Foundation for Internal Freedom
- Date: Wed, 23 Dec 1992 19:14:57 GMT
- Lines: 788
-
- #!/bin/sh
- # This is a shell archive (produced by shar 3.49)
- # To extract the files from this archive, save it to a file, remove
- # everything above the "!/bin/sh" line above, and type "sh file_name".
- #
- # made 12/23/1992 19:09 UTC by thinman@netcom.com
- # Source directory /usr/lance/vr/cool/src
- #
- # existing files will NOT be overwritten unless -c is specified
- #
- # This shar contains:
- # length mode name
- # ------ ---------- ------------------------------------------
- # 4857 -rw-r--r-- README
- # 1868 -rw-r--r-- TODO
- # 2070 -rw-r--r-- Makefile
- # 1622 -rw-r--r-- descrip.mms
- # 10108 -rw-r--r-- cool.3
- # 17641 -rw-r--r-- class.c
- # 2528 -rw-r--r-- msg.c
- # 8509 -rw-r--r-- lookup.c
- # 1895 -rw-r--r-- exception.c
- # 2536 -rw-r--r-- util.c
- # 1945 -rw-r--r-- cool.h
- # 12130 -rw-r--r-- coolint.h
- # 1236 -rw-r--r-- cool.spec
- # 1086 -rw-r--r-- tst1.c
- # 1762 -rw-r--r-- tst2.c
- # 2298 -rw-r--r-- tst3.c
- # 375 -rw-r--r-- tst4.c
- # 2592 -rw-r--r-- tst5.c
- # 1753 -rw-r--r-- tst6.c
- # 2788 -rw-r--r-- tst7.c
- # 1600 -rw-r--r-- tst8.c
- # 2781 -rw-r--r-- bench.c
- # 3319 -rw-r--r-- timer.c
- # 348 -rw-r--r-- benchall
- # 745 -rw-r--r-- out.unix386
- # 84 -rw-r--r-- tstall.com
- # 22 -rw-r--r-- patchlevel.h
- # 3440 -rw-r--r-- cool_elk.c
- # 6375 -rw-r--r-- cool_scm.c
- #
- # ============= README ==============
- if test -f 'README' -a X"$1" != X"-c"; then
- echo 'x - skipping README (File already exists)'
- else
- echo 'x - extracting README (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'README' &&
- X
- X
- X
- X COOL - C Object-Oriented Library
- X
- XCOOL is: Copyright 1991 by Lance Norskog - ABANDONED
- X
- XI'm writing a large software project [1] and need something better
- Xthan C. Something which is absolutely as portable as C. After looking
- Xat C++, SmallTalk, and Objective C, I gave up and wrote COOL instead.
- X
- XIt adds objects to C with a minimum of code-junk and no preprocessing.
- XThe implementation is as simple as is reasonable. Object messaging,
- Xcreation, and deletion are reasonably streamlined. Little attempt has
- Xbeen made for high performance. If you design for performance,
- Xyou usually get software that's fast but useless.
- X
- XSee the man page for more information and a rudimentary coding example.
- XSee the tst?.c programs for info on exercising features.
- X
- XThis is released to the net for general interest, information on
- Xhow a rudimentary object system works internally, and to serve
- Xas a vehicle for prototyping new OO styles. Instead of conducting
- Xgedanken experiments about how objects should work, or spending
- Xyears designing new languages & compilers, now you can implement
- Xyour variant and play around with it coding up your favorite examples.
- X
- XInheritance:
- XMultiple inheritance is implemented. No ambiguous methods are
- Xallowed in a class definition. Inheritance is implemented
- Xessentially as multiple objects with one name; these objects
- Xcan call SELF with methods they themselves don't implement.
- X
- XPorting:
- X
- XObjects are methods are given as strings or magic ID numbers.
- XThe library contains routines which transform strings to IDs.
- XThe object and method ID spaces are both segmented into two
- Xpieces. Object numbers are an array index and a generation
- Xnumber. Method numbers are an array index and a parent index.
- X
- XThese number spaces will have to resegmented for your computer.
- XThe release includes macros for 386 UNIX, and assumes that
- Xstring addresses are in the data segment or in the stack
- Xlocal area. The object and method spaces pack their two numbers
- Xavoiding these bit addresses, so that the library can distinguish
- Xbetween a string and pre-cached ID on the fly.
- X
- XThis implementation is mostly bogus. Instead, it should
- Xhave a magic marker (I prefer 0xdeadbeef) at the beginning
- Xof an object. But, encoding the generation number in
- Xthe object is still worthwhile.
- X
- XAlso, the exception system uses setjmp and longjmp. If yours
- Xdon't work, you don't get catchable exceptions. The object
- Xlibrary uses exceptions, but doesn't catch them, so it will
- Xwork anyway.
- X
- XBeyond that, any 32-bit machine should be able to run COOL.
- XWith the above magic marker trick, it should run under DOS
- Xand Windows also.
- X
- XBenchmarks:
- X
- X'bench.c' makes counts an integer from 0 to a very large number
- Xin 6 different ways. The first way uses canonical safe message
- Xcalls. The second way uses unsafe cached messaging. The remaining
- Xways use native C in various ways. A case statement and a
- Xprocedure call is used as a standard against which to measure
- Xmessaging performance.
- X
- X'benchall' compiles the benchmark and the library with the
- Xnative C compiler and with GCC, both with and without optimization,
- Xand runs the benchmark.
- X
- XThe files out.unix386 and out.xenix contain the benchmark results
- Xon those operating systems.
- X
- XPorts also included for 68020 and SPARC SUNs, and VMS.
- XThese ports, and some bug fixes, where contributed by Henk
- XDavids from Germany and Nicolas ?? from France.
- X
- XThis is the fourth release, patchlevel 4. Patchlevel 2 was
- XNicolas' port to SUNs and bug fixes, which he posted to
- Xalt.sources. These changes have been incorporated.
- XPatchlevel 4 didn't do grandparent classes right,
- Xand I've added components, overloading,
- Xand the Scheme interface files. Cool_elk.c allows
- XELK Scheme to use COOL as an extension interface.
- XCool_scm.c does likewise with SCM Scheme. The beauty
- Xpart is that you just write your extensions in C
- Xas classes, test them, then link them with Scheme.
- XYou don't have to write any custom code to bridge the two.
- X
- XCool_scm.c implements overloaded operators. Cool_elk
- Xdoes not. Overloaded operators are not well documented,
- Xand the implementation is of necessity iffy because
- Xof the coruscating plasticity of C's calling sequence.
- X
- XPlease send me all updates, ports, and benchmark results you can.
- XCOOL is a basic technology for my VR window system project,
- Xand I am the maintenence person. I may rewrite it for
- Xefficiency at some point.
- X
- XAcknowledgements:
- X Nicolas ?? at Electricite De France <nicolas@cli52or.edf.fr>
- X for SUN-3 & SUN-4 ports and bug fixes
- X Henk Davids <hdavids@mswe.dnet.ms.philips.nl>
- X for SUN-4 port and bug fixes
- X David L. Williams <dlw@atherton.com>
- X for NeXT port
- X
- XEnjoy!
- X
- XLance Norskog
- Xthinman@netcom.com
- X
- X[1] A Virtual Reality window system based on Scheme & C extensions.
- XSomething like NeWS...
- X
- Xp.s. tst4 is supposed to cause a core dump. That's how
- X exceptions work.
- SHAR_EOF
- chmod 0644 README ||
- echo 'restore of README failed'
- Wc_c="`wc -c < 'README'`"
- test 4857 -eq "$Wc_c" ||
- echo 'README: original size 4857, current size' "$Wc_c"
- fi
- # ============= TODO ==============
- if test -f 'TODO' -a X"$1" != X"-c"; then
- echo 'x - skipping TODO (File already exists)'
- else
- echo 'x - extracting TODO (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'TODO' &&
- XTime efficiency
- X Create, Delete, Init, & Exit go in pre-defined slots.
- X Local area is preallocated, max 20 bytes or something like that.
- X
- XSelf-style objects/protos/instances:
- X Create methods to prototype objects cause instantiations.
- X Delete methods to objects destroy themselves.
- X
- XSpace efficiency
- X Use two arrays instead of one:
- X Array of objects
- X Array of maps
- X Classes and prototype objects "own" a map;
- X live objects refer to someone else's map.
- X When live objects want to change their map,
- X they do a copy-on-write and they get their own map.
- X A clone of a live object points to the source object's map.
- X
- XParameter checking from method :syntax.
- X
- X C types if lower case, classes if upper case
- X
- XFront-end interpreter
- X
- X Simple I/O classes for front-end interpreter.
- X Create front-end for C++ class library.
- X Strings, files.
- X {} handler in lexer does local vars somehow.
- X List handlers
- X Code class reads in data
- X procedures:
- X Code func name {
- X stuff
- X }
- X name ( stuff )
- X
- XMultiple Inheritance
- X Need nicer MI lookup.
- X Self-style priorities?
- X Want resends for method wrapping.
- X cool_resend(object, method)
- X start after current method.
- X Need a method stack for this?
- X
- XClasses:
- X Garbage collection
- X Use multiple inheritance.
- X Stack style: inherit from StackGC parent
- X Init routine pushes self on top of stack.
- X Mark method returns top of stack.
- X Free (Marker) method returns all StackGC obs
- X alloc'd since that Marker.
- X Mark/Sweep style: two parents: Holder & GCable
- X Holders refer to objects in their private area
- X A GCable object must be pointed to by one
- X of the Holders, or it will be freed.
- X An object may inherit from either or both.
- X Sweep method to class Holder does a mark/sweep.
- X Also, running out of objects does a mark/sweep
- X while doing a realloc().
- X
- X A class/object may inherit from StackGC and/or Holder/GCable.
- X
- SHAR_EOF
- chmod 0644 TODO ||
- echo 'restore of TODO failed'
- Wc_c="`wc -c < 'TODO'`"
- test 1868 -eq "$Wc_c" ||
- echo 'TODO: original size 1868, current size' "$Wc_c"
- fi
- # ============= Makefile ==============
- if test -f 'Makefile' -a X"$1" != X"-c"; then
- echo 'x - skipping Makefile (File already exists)'
- else
- echo 'x - extracting Makefile (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
- X# COOL makefile for 386 Unix
- X# Copyright 1991 by Lance Norskog. This software may be used for any purpose.
- X
- XOBJ= class.o msg.o lookup.o exception.o util.o
- X
- XFILES= README TODO Makefile descrip.mms cool.3 \
- X class.c msg.c lookup.c exception.c util.c \
- X cool.h coolint.h cool.spec \
- X tst1.c tst2.c tst3.c tst4.c tst5.c tst6.c tst7.c tst8.c \
- X bench.c timer.c benchall out.unix386 tstall.com \
- X patchlevel.h \
- X cool_elk.c cool_scm.c
- X
- X# Works for UNIX/386 and Xenix/386
- X# Remove -DUSG for BSD machines, -Di386 for non-386 machines
- X
- XSYS=-DUSG -Di386
- X# SYS=-DSPARC
- X# SYS=-Dmc68020
- X
- X# -DSELFISH for Self-inspired object-only programming
- X# Not implemented yet.
- X
- X# -fpcc-struct-return I think is for SYSV bogus compilers only
- XGCC=gcc -fwritable-strings -fpcc-struct-return -pipe
- XCC=$(GCC)
- XCC=cc
- X
- XO=-g
- X
- XCFLAGS=$O $(SYS)
- X
- X# make a library
- XAR=ar r
- X
- X# library post-processing
- X# RANLIB=ranlib
- XRANLIB=true
- X
- X# The author's setup
- XSHAR= /usr2/tools/shar/shar -F -s thinman@netcom.com -o coolshar -l 40
- X
- X# the usual setup
- X# SHAR= /usr/local/bin/shar
- X
- Xall: libcool.a tests
- X
- Xlibcool.a: $(OBJ)
- X rm -f $@
- X $(AR) $@ $(OBJ)
- X $(RANLIB) $@
- X
- Xinstall:
- X cp libcool.a /usr/lib
- X
- Xtests: tst1 tst2 tst3 tst4 tst5 tst6 tst7 tst8 bench
- X
- Xtst1: tst1.c libcool.a
- X $(CC) $(CFLAGS) $@.c -o $@ libcool.a
- X
- Xtst2: tst2.c libcool.a
- X $(CC) $(CFLAGS) $@.c -o $@ libcool.a
- X
- Xtst3: tst3.c libcool.a
- X $(CC) $(CFLAGS) $@.c -o $@ libcool.a
- X
- Xtst4: tst4.c libcool.a
- X $(CC) $(CFLAGS) $@.c -o $@ libcool.a
- X
- Xtst5: tst5.c libcool.a
- X $(CC) $(CFLAGS) $@.c -o $@ libcool.a
- X
- Xtst6: tst6.c libcool.a
- X $(CC) $(CFLAGS) $@.c -o $@ libcool.a
- X
- Xtst7: tst7.c libcool.a
- X $(CC) $(CFLAGS) $@.c -o $@ libcool.a
- X
- Xtst8: tst8.c libcool.a
- X $(CC) $(CFLAGS) $@.c -o $@ libcool.a
- X
- Xbench: bench.c timer.c libcool.a
- X $(CC) $(CFLAGS) -o $@ bench.c timer.c libcool.a
- X
- Xgraphics: graphics.c libcool.a
- X $(GCC) $(CFLAGS) -o $@ graphics.c libcool.a
- X
- X$(OBJ): cool.h coolint.h
- X
- Xshar:
- X @cp ../doc/cool.3 .
- X @$(SHAR) $(FILES)
- X
- Xbenchthem:
- X CFLAGS="$(CFLAGS)" sh benchall
- X
- Xclean:
- X rm -f *.o
- X rm -f libcool.a
- X rm -f tst?
- X rm -f core
- X rm -f bench
- SHAR_EOF
- chmod 0644 Makefile ||
- echo 'restore of Makefile failed'
- Wc_c="`wc -c < 'Makefile'`"
- test 2070 -eq "$Wc_c" ||
- echo 'Makefile: original size 2070, current size' "$Wc_c"
- fi
- # ============= descrip.mms ==============
- if test -f 'descrip.mms' -a X"$1" != X"-c"; then
- echo 'x - skipping descrip.mms (File already exists)'
- else
- echo 'x - extracting descrip.mms (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'descrip.mms' &&
- X# COOL makefile for VMS
- X# Copyright 1991 by Lance Norskog. This software may be used for any purpose.
- X
- XOBJ= class.obj,msg.obj,lookup.obj,exception.obj,util.obj
- X
- XFILES= README Makefile cool.3 class.c msg.c lookup.c exception.c util.c \
- X cool.h coolint.h cool.spec tst1.c tst2.c tst3.c tst4.c tst5.c tst6.c \
- X bench.c benchall out.unix386 out.xenix patchlevel.h \
- X bench_vms.c timer_vms.c changelog
- X
- X# /define=SELFISH for Self-inspired object-only programming
- X# Not implemented yet.
- X
- XCC=cc
- X# Use 'MMS/MACRO=DEBUG=1' to generate debug versions
- X.ifdef DEBUG
- XCFLAGS =/debug/noopt
- XLINKFLAGS =/debug
- X.else
- XCFLAGS =/optimize=noinline
- XLINKFLAGS =/notrace
- X.endif
- XLIBRARIES=dsk:libcool/libr
- X
- Xall : libcool.olb tests
- X ! done
- X
- Xlibcool.olb : $(OBJ)
- X libr/create $@ $(OBJ)
- X
- Xtests : tst1.exe tst2.exe tst3.exe tst4.exe tst5.exe tst6.exe bench_vms.exe
- X ! done
- X
- Xtst1.exe : tst1.obj libcool.olb
- X link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
- X
- Xtst2.exe : tst2.obj libcool.olb
- X link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
- X
- Xtst3.exe : tst3.obj libcool.olb
- X link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
- X
- Xtst4.exe : tst4.obj libcool.olb
- X link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
- X
- Xtst5.exe : tst5.obj libcool.olb
- X link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
- X
- Xtst6.exe : tst6.obj libcool.olb
- X link $(LINKFLAGS) /exe=$@ $*.obj,$(LIBRARIES)
- X
- Xbench_vms.exe : bench_vms.obj timer_vms.obj libcool.olb
- X link $(LINKFLAGS) /exe=$@ $*.obj,timer_vms,$(LIBRARIES)
- X
- X$(OBJ) : cool.h coolint.h
- X
- X# shar :
- X# @cp ./doc/cool.3 .
- X# @/usr2/tools/shar/shar -F -s thinman@netcom.com $(FILES)
- X#
- X# benchthem :
- X# CFLAGS="$(LINKFLAGS)" sh benchall
- SHAR_EOF
- chmod 0644 descrip.mms ||
- echo 'restore of descrip.mms failed'
- Wc_c="`wc -c < 'descrip.mms'`"
- test 1622 -eq "$Wc_c" ||
- echo 'descrip.mms: original size 1622, current size' "$Wc_c"
- fi
- # ============= cool.3 ==============
- if test -f 'cool.3' -a X"$1" != X"-c"; then
- echo 'x - skipping cool.3 (File already exists)'
- else
- echo 'x - extracting cool.3 (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'cool.3' &&
- X." from libpbm.3
- X.de Ss
- X. sp
- X. ft CW
- X. nf
- X..
- X.de Se
- X. fi
- X. ft R
- X. sp
- X..
- X.TH COOL 3
- X.SH NAME
- XCOOL - C Object-Oriented Library
- X.SH SYNOPSIS
- X.Ss
- X#include <cool.h>
- Xcc ... libcool.a
- X
- Xtypedef char *object_t, *method_t, *ret_t;
- Xvoid cool_init();
- Xret_t cool_msg(object_t object, method_t method [, args ]);
- X#define cool(type, (msg ...))
- Xobject_t cool_object(object_t object);
- Xint cool_isobject(object_t object);
- Xmethod_t cool_method(object_t object, method_t method);
- Xint cool_hasmethod(object_t object, method_t method);
- X
- Xvoid cool_raise(char *exception, fmt [, args ...] );
- X#define cool_exception(char *exception,
- X { normal code },
- X { exception code }
- X )
- X.Se
- X.SH DESCRIPTION
- XThe
- X.I COOL
- Xlibrary provides
- X.B class
- Xoperations in C.
- X.B Objects
- Xmay be sent one or more
- X.B messages
- Xwith
- X.IR cool_msg ().
- XObjects and methods may be named by text strings,
- Xor by magic numbers.
- X.IR cool_object ()
- Xreturns the magic number for a given object.
- X.IR cool_method ()
- Xreturns the magic number for a given object and method.
- XIf an object or method doesn't exist,
- Xan exception is raised.
- X.IR cool_isobject ()
- Xand
- X.IR cool_hasobject ()
- Xreturn boolean values for the respective questions,
- Xinstead of raising exceptions.
- XMessaging is optimized for use with these magic numbers.
- X.P
- X.IR cool_exception ()
- Xis a preprocessor macro, not a C subroutine.
- XIt registers an
- X.I exception
- Xnamed
- X.I event,
- Xand executes the first code block.
- XIf
- X.IR cool_raise ( event )
- Xoccurs before
- Xwhile in the first code block,
- Xthe code execution is interrupted and the
- X.I handler ()
- Xroutine is called.
- X.IR cool_raise ()
- Xmay be called at any time without pre-registering
- Xthe event name.
- XIn this case, the default exception handler prints out the
- Xevent name and the program exits.
- X.SH PREDEFINED\ MESSAGES
- XThere are several predefined messages which have intrinsic meaning in
- X.B COOL:
- X.TP 10
- XCreate
- Xmakes a new object.
- XCreate, given a class name, creates a new object of that class.
- XThe third argument gives the name of the new object.
- XIt may be NULL to indicate that the object has no name.
- XThe object_t code for the object is always returned.
- X.TP 10
- XDestroy
- Xdestroys an existing object.
- XDestroy is sent not to the object, but to the object's class.
- X.TP 10
- XMethod
- Xadds a method to a class.
- XThe second argument is the method name.
- XThe third argument gives the function handler for the method.
- XThe fourth argument gives the number of arguments to the method.
- XWhen a message is sent to an instance of the class
- Xwith the given method,
- Xthe given function will be called with the arguments after the method.
- X.TP 10
- XInherit
- Xcauses a class or object to inherit a method
- Xfrom one of its parent classes.
- XThe method is given as the third argument,
- Xand the parent class is given as the fourth argument.
- XThis breaks ambiguities which occur
- Xwhen two or more parents provide the same method.
- X.TP 10
- XClone
- Xcreates a ``deep copy'' of an object.
- X.B Clone
- Xis sent to a class with two arguments:
- Xthe object to be copied, and the name of the new object.
- XThe private data structures of the class are copied.
- XIf the class has a
- X.B Copy
- Xmethod, that method is then called.
- XIf the class private data structure contains separately allocated
- Xmemory, the class should have a
- X.B Copy
- Xmethod.
- X." .TP 10
- X." Wrap
- X." adds a method also,
- X." and is given the same arguments.
- X." If there is already a method with the given name,
- X." the new method will call the old method at some point.
- X." .TP 10
- X." Seal
- X." stops the addition of methods to a class.
- X." .TP 10
- X." Walk
- X." expects a class name and a function pointer as arguments.
- X." It calls the function repeatedly with the name of each of
- X." its sub-classes.
- X." .TP 10
- X." Parents
- X." also expects a class name and a function pointer as arguments.
- X." It calls the function repeatedly with the name of each of its parent classes.
- X." .TP 10
- X." Function
- X." returns the function pointer which would handle the particular
- X." class and type list in the argument list.
- X." .TP 10
- X." Sizeof
- X." returns the size of the local storage used by a class.
- X." .TP 10
- X." Comment
- X." expects a class or method name
- X.SH METHODS
- XMethods are of the form "name:arglist",
- Xwhere arglist is a comma-seperated list of arguments to the method.
- XThese arguments should be base C types, "object_t", or "method_t".
- XThe message-passer can use these to verify on the fly that the
- Xcorrect arguments are being passed to the method.
- XIf you don't wish to specify the types of the arguments,
- Xuse empty strings.
- XFor example, the method specifier "add" will cause the function to be called
- Xwith no arguments, "add:" will pass one argument, "add:," will pass two.
- X.P
- XIf the method is called ``Init'',
- Xit is called upon creation of an object of this class.
- XIf the method is called ``Copy'',
- Xit is called upon the cloning of an object of this class.
- XIf the method is called ``Exit'',
- Xit is called upon destruction of an object of this class.
- X.SH CLASS LIFE CYCLE
- XTo create a new class, send the message ``Create'' to object ``Class''.
- XThe third argument gives the name of the new class.
- XThe fourth argument gives the size of the local storage used by
- Xthe class.
- XThe fifth and subsequent arguments give
- Xthe NULL-terminated list of parent classes of this class.
- XIf none, the fifth argument must be NULL.
- X.P
- XTo create a new object from one of your classes,
- Xsend the message "Create" to that class with the name
- Xof the object as the third argument.
- XIf you don't want to give a text name to the object,
- Xgive NULL as the name.
- XThe response to the message is the object ID.
- X.SH EXAMPLE
- XThe following example creates and uses an up-down counter class.
- X.Ss
- X\fI/* Class creation */\fP
- X.P
- Xvoid increment(int *ip) { *ip++; }
- Xvoid decrement(int *ip) { *ip--; }
- Xint value(int *ip) { return *ip; }
- Xvoid reset(int *ip) { *ip = 0; }
- X.P
- Xcool_init(); \fI/* Must be called first */\fP
- X.P
- Xcool_msg("Class", "Create", "Counter", sizeof(int), NULL); \fI/* No parent classes */\fP
- Xcool_msg("Counter", "Method", "Increment", increment, 0);
- Xcool_msg("Counter", "Method", "Decrement", decrement, 0);
- Xcool_msg("Counter", "Method", "Value", value, 0);
- Xcool_msg("Counter", "Method", "Reset", reset, 0);
- X." cool_msg("Counter", "Seal");
- X.P
- X\fI/* Class usage */\fP
- X.P
- Xc = cool_msg("Counter", "Create", NULL);
- Xcool_msg(c, "Reset");
- Xcool_msg(c, "Increment");
- Xcool_msg(c, "Increment");
- Xcool_msg(c, "Decrement");
- Xprintf("This better be one: %d\\n", cool_msg(c, "Value", NULL));
- X.Se
- XThis example shows the basics of using COOL to create an abstract data type.
- XA class Counter is created with a local storage of one integer,
- Xand no parent classes.
- XIt has four methods: Increment, Decrement, Reset, and Value.
- XNone of the methods takes an argument,
- Xthus their argument lists are empty strings.
- X.SH INHERITANCE
- XWhen a class is created with parent classes,
- Xit inherits all methods from the parents.
- XIf there are conflicting method names from more than one parent,
- Xthe class itself must supply new methods to break the conflict.
- XIf an object is created before all conflicts are broken,
- Xthe "MethodConflict" exception is raised.
- X.P
- X.I "Inheritance is Aggregation".
- XAn object with inherited classes secretly consists of several
- X.I "partial objects"
- Xwith one name.
- XEach partial object has its own private data storage.
- XThe
- X.IR SELFOF ()
- Xand
- X.IR CLASSOF ()
- Xmacros yield the object_t variables which name this object.
- XMethods in such in an object may call inherited methods
- Xby sending messages to
- X.IR SELFOF (mydata).
- X." .SH EXAMPLE
- X." Need a better example with more stuff here
- X.SH EXCEPTIONS
- X.IR COOL
- Xmaintains an exception system.
- XIt is implemented with the
- X.IR setjmp ()
- Xand
- X.IR longjmp ()
- Xlibrary routines.
- XThe
- X.IR cool_exception ()
- Xmacro maintains an exception stack.
- X.IR cool_raise ( exception )
- Xcauses an
- X.I exception
- Xto occur. If a parent subroutine handles this particular
- Xexception via the
- X.IR cool_exception ()
- Xmacro,
- X.IR cool_raise ()
- Xwill short-circuit the normal subroutine return stack
- Xand jump into the
- X.I handler
- Xsection of the
- X.IR cool_exception ()
- Xmacro. If there is no handler set up for the exception,
- Xthe default exception-handler will be called.
- XThis routine prints out the
- X.IR printf ()
- Xarguments after the exception name,
- Xand causes a core dump.
- X.SH IMPLEMENTATION
- X.I object_t
- Xis an integer index into an internal table of classes.
- X.I method_t
- Xis in integer index into a table of methods registered for a class.
- XThey may also be given string data.
- X.IR cool_msg ()
- Xdistinguishes
- X.I object_t
- Xand
- X.I method_t
- Xfrom strings because strings are always in high memory,
- Xand there is a clear separation in the number space.
- XThere may not be a class and a method with the same name.
- X.P
- XThe string table lookup is as simple and thus as slow as possible.
- XMethod dispatch with when given both object_t and method_t is
- Xas fast as possible.
- XIf you're serious about performance, cache your code numbers.
- X.P
- XThe macros
- X.IR coolt (),
- X.IR coolv (),
- X.IR coolta (),
- X.IR coolva (),
- Xdo message lookups in-line.
- XThe 't' and 'v' suffices refer to typed or void, and 'a' adds an
- Xargument list.
- XThese macros expect object_t and method_t code numbers.
- XThey do not do string lookups; you have to cache your object
- Xand method numbers.
- XThese macros also do not do any argument list checking;
- Xthey should only be used in select code which has been thoroughly debugged.
- X.SH PORTABILITY
- XCOOL is ferociously dependent on integer<->pointer conversion.
- XIt attempts to keep everything in pointer form, converting
- Xto integers for throwaway index values.
- X.P
- XSetjmp and longjmp are partially non-functional on many machines.
- XMany compilers go nuts when you have register variables in
- Xa routine that does setjmp's.
- XPortable code has nothing but a
- X.IR cool_exception ()
- Xmacro in a subroutine; this avoids the register problem.
- XIf setjmp/longjmp don't work
- Xon your machine, you can still use
- X.IR cool_raise ()
- Xto print a message and kill your program.
- X.SH ACKNOWLEDGEMENTS
- XCOOL was inspired by the need for classes in a large C programming
- Xproject; C++ and Objective C were examined and found wanting.
- XCOOL is, of course, strongly influenced by the Smalltalk class system.
- SHAR_EOF
- chmod 0644 cool.3 ||
- echo 'restore of cool.3 failed'
- Wc_c="`wc -c < 'cool.3'`"
- test 10108 -eq "$Wc_c" ||
- echo 'cool.3: original size 10108, current size' "$Wc_c"
- fi
- true || echo 'restore of class.c failed'
- echo End of part 1, continue with part 2
- exit 0
- --
-
- Lance Norskog
-
- Data is not information is not knowledge is not wisdom.
-