Declaring an Interface

Here is a small sample showing how to declare an interface to a class (called ‘A’):

// Declaration of class A

class A

: public IlvDvInterface

{

public:

void setX(int x) { _x = x; } < /FONT >

int getX() const { return _x; }

 

protected:

int _x;

};

 

// Implementation file of A. Use the following macros to

// introspect methods setX, getX, and field _x:

 

IlvDvBeginInterface(A)

Method1(SetX, setX)

TypedMethod (GetX, getX)

Field(X, _x)

IlvDvEndInterface()

 

 

....

// Using an instance of A as an interface, it is possible

// to invoke its methods and to modify its field

// without being aware of class A !!!

A* a = new A;

IlvDvInterface* interf = a;

 

// First we invoke its methods:

IlvDvValue returnedValue;

interf->callMethod(IlvGetSymbol("SetX"), &returnedValue, 100);

interf->callMethod(IlvGetSymbol("GetX"), &returnedValue);

assert((IlvInt) returnedValue == 100);

 

// Then, we modify its field directly:

interf->setFieldValue(IlvGetSymbol("X"), 200);

assert((IlvInt)interf->getFieldValue(IlvGetSymbol("X"),

&returnedValue) == 200);