The following simple function correctly allocates and returns space for an
instance of a class given a pointer to its class object @<cls>.
\begin{prog}
- void *allocate_instance(const SodClass *cls) \\ \ind
+ void *allocate_instance(const SodClass *cls) \\ \ind
\{ return malloc(cls@->cls.initsz); \}
\end{prog}
The following simple function imprints storage at address @<p> as an instance
of a class, given a pointer to its class object @<cls>.
\begin{prog}
- void imprint_instance(const SodClass *cls, void *p) \\ \ind
+ void imprint_instance(const SodClass *cls, void *p) \\ \ind
\{ cls@->cls.imprint(p); \}
\end{prog}
This simple protocol can be used, for example, to implement a reference
counting system, as follows.
\begin{prog}
- [nick = ref] \\
- class ReferenceCountedObject \{ \\ \ind
- unsigned nref = 1; \\-
- void inc() \{ me@->ref.nref++; \} \\-
- [role = around] \\
- int obj.teardown() \\
- \{ \\ \ind
- if (--\,--me@->ref.nref) return (1); \\
- else return (CALL_NEXT_METHOD); \- \\
- \} \- \\
+ [nick = ref] \\
+ class ReferenceCountedObject \{ \\ \ind
+ unsigned nref = 1; \\-
+ void inc() \{ me@->ref.nref++; \} \\-
+ [role = around] \\
+ int obj.teardown() \\
+ \{ \\ \ind
+ if (--\,--me@->ref.nref) return (1); \\
+ else return (CALL_NEXT_METHOD); \-\\
+ \} \-\\
\}
\end{prog}