Menu

Interface

Phillip Kilgore

An interface is an explicit specification of an object's behavior that is separated from its state. It can alternatively be thought of as the specification for an object for which the state has been omitted.

C Specification

This section describe how to specify an interface with WFCO's C library. You may find the description of the macros used here.

Creating an Interface

An interface may be created using the WFCO_INTERFACE and WFCO_END_INTERFACE macros. The following example is derived from edible.h in the animals example. It contains one abstract method, void edible::beConsumed():

WFCO_INTERFACE(edible)
    WFCO_DECLARE_ABSTRACT_METHOD(edible, beConsumed, void, struct consumer* consumer);
WFCO_END_INTERFACE(edible);

This creates a new interface whose type is struct edible and which is typedef'd to edible. Unlike a class, the WFCO_METHODS is implied because an interface has no state. Although abstract methods are most appropriate for most interfaces, interfaces are permitted to have concrete methods because it is occasionally the case that a sensible, generic default implementation can be provided:

WFCO_INTERFACE(consumer)
    WFCO_DECLARE_METHOD(consumer, eat, void, struct* edible edible);
WFCO_END_INTERFACE(consumer)

extern WFCO_METHOD(consumer, eat, void, struct edible* edible);

/* ... */

WFCO_METHOD(consumer, eat, void, edible* target) {
    WFCO_OBJ_CALL(target, eat, WFCO_THIS);
}

In either case, the interface should have a vtable associated with it. This contains the method

Creating a Subinterface

A subinterface is a refinement of an interface and happens when an interface implements another interface.. For instance, consider that

WFCO_INTERFACE_IMPLEMENTS(living, edible)
    WFCO_DECLARE_ABSTRACT_METHOD(living, eat, void, edible* edible);
     WFCO_DECLARE_ABSTRACT_METHOD(living, die, void);
     WFCO_DECLARE_ABSTRACT_METHOD(living, isDead, int);
WFCO_END_INTERFACE(living)

Related

Wiki: Home
Wiki: object.h