Skip to content

MDL Pattern | Interface

Mason Bially edited this page Feb 4, 2014 · 2 revisions

The following MDL pattern is a holdover until an MDL object system extension exists. Languages which don't support that extension will have to use something like the following anyway.

Pattern

The names object, internal, and this below are conventional.

    type IThingStruct {
        # Internal object
        internal: *void,
        # Concrete madz object (e.x. *ThingImplementationStruct)
        object: *void,

        # Implementation specific methods/variables
        a_func: (this *IThingStruct) -> *void
    };
    type IThing *IThingStruct;

    type ThingImplementationStruct {
        # Internal object
        internal : *void,
        # Interface implementations
        IThing : IThing,
        
        # Implementation specific methods/variables
        set_thingy : (this *ThingImplementationStruct, thingy *void)-> void,
        foo : *int64
    };
# The declaration of the struct holding the interface information.
TypeDeclaration("IExampleStruct", TypeStruct([
    # The pointer to the object that this interface came from. (i.e. the object of the class providing this interface)
    TypeStructElement("object", TypeNone.Pointer()),
    # A pointer for the implementation of this interface to use.
    TypeStructElement("internal", TypeNone.Pointer()),

    # Function pointers for the features the interface provides
    DocumentationAttribute("""
    Does some stuff.
    """)(
    TypeStructElement("do_thing", TypeFunction(
        TypeNone, [
            # Every function must have a this pointer which will get the instance of this struct the function was retrived from.
            TypeFunctionArgument("this", TypePointer("IExampleStruct")),
            TypeFunctionArgument("an_argument", TypePointer("FooStruct"))
        ]))),

    ...
])),

# The actual type passed in and out of functions.
TypeDeclaration("IExample", NamedType("IExampleStruct").Pointer()),

Clone this wiki locally