/* An implementation of a Counter class written in C. * Author: Mark A. Sheldon * Copyright: March 2008 */ /* * Types for classes. * A class is a kind of object. Need to clean it up more: * o add field for superclass * o make operations take a first parameter, like this in regular object, but * then the operations will ignore it. * o Add private and public class variables (besides the special name field). */ typedef struct counter_class { const char *name; struct counter_class_ops *ops; } *counter_class_t; struct counter_class_ops { struct counter_instance *(*new) (int initial_value); }; /* * Types for instances. * Need to do or resolve: * o public instance variables should go directly in struct or in a * contained struct? * o Do another version that does dynamic dispatch so we can get * inheritance */ typedef struct counter_instance { void *private; counter_class_t class; struct counter_instance_ops *ops; } *counter_t; typedef counter_t Counter; struct counter_instance_ops { int (*value) (counter_t this); void (*increment) (counter_t this); void (*increment_by) (counter_t this, int n); void (*decrement) (counter_t this); void (*decrement_by) (counter_t this, int n); void (*reset) (counter_t this); BOOL (*equals) (counter_t this, counter_t c); void (*destroy) (counter_t this); }; counter_class_t CounterClass;