Re: Interface implementation

tzvetanmi@yahoo.com (Tzvetan Mikov)
17 Oct 2004 16:07:28 -0400

          From comp.compilers

Related articles
Interface implementation ramiro@cs.cornell.edu (Ramiro Rodriguez) (2004-10-12)
Re: Interface implementation andi@a4.complang.tuwien.ac.at (2004-10-17)
Re: Interface implementation tzvetanmi@yahoo.com (2004-10-17)
Re: Interface implementation vbdis@aol.com (2004-10-17)
Re: Interface implementation eeide@cs.utah.edu (Eric Eide) (2004-10-17)
Re: Interface implementation cfc@shell01.TheWorld.com (Chris F Clark) (2004-10-17)
| List of all articles for this month |

From: tzvetanmi@yahoo.com (Tzvetan Mikov)
Newsgroups: comp.compilers
Date: 17 Oct 2004 16:07:28 -0400
Organization: http://groups.google.com
References: 04-10-102
Keywords: design, OOP
Posted-Date: 17 Oct 2004 16:07:28 EDT

Ramiro Rodriguez <ramiro@cs.cornell.edu> wrote
> However for things like interfaces this seems
> to be not as easy if one allows multiple interface implementation. I
> could do a table which is referenced by the name of the function but I
> am wondering if there is some more efficient way of doing this.


Yes there is. You need a separate call table for each interface and a
separate pointer to each table in the object. Conceptually each
function accepts a "this" referencing its own table pointer within the
object - the function adjusts it in order to get to the actual object
(this is perhaps best done with a stub). Additionally, when assigning
an object pointer to an interface pointer you also need to adjust it
so it points to the appropriate table within the object.


Perhaps this is best explained with an example:


interface A { int funcA (); }
interface B { int funcB (); }
class AB implements A,B {
    int x, y, z;
    int funcA () { return z; }
    int funcB () { return x; }
    final int callObj () { return funcA() + funcB(); }
    final A getAInterface () { return this; }
}


To avoid dealing with assembler, we can translate this to (somewhat
non-portable) C in the following way:


struct A_function_table {
    int (*funcA)( struct A_function_table ** );
}
struct B_function_table {
    int (*funcB)( struct B_function_table ** );
}


/* This is the layout of an AB object in memory */
struct AB {
    struct A_function_table * _a_vft;
    struct B_function_table * _b_vft;
    int x, y, z;
}


struct A_function_table AB_A_vft = { &AB_funcA };
struct B_function_table AB_B_vft = { &AB_funcB };


int AB_funcA( struct A_function_table ** p ) {
    struct AB * this = (char *)p - offsetof( struct AB, _a_vft );
    return this->z;
}
int AB_funcB( struct B_function_table ** p ) {
    struct AB * this = (char *)p - offsetof( struct AB, _b_vft );
    return this->x;
}


/* callObj() illustrates how to access interface functions from the
object */
/* pointer: */
int AB_callObj ( struct AB * this ) {
    return
        this->_a_vft->funcA( &this->_a_vft ) +
        this->_b_vft->funcA( &this->_b_vft );
}


/* getAInterface() illustrates how to convert an object to an
interface */
struct A_function_table ** AB_getAInterface ( struct AB * this ) {
    return &this->_a_vft;
}




HTH,
Tzvetan


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.