Developing Yed objects - Writing methods

In "Interface definition" paragraph we have seen the use of pointers to function in order to incapsulate methods inside our structure/object. Now it's the time to see in detail how to write a method for our Yed object.

In Yed scenario, the implementation of a method must follow these directives:

The last item is also absolutely fundamental in using Yed objects in multithreading scenario: 'this' pointer allows the right manipulation of attributes in case of multiple threads using many objects of the same type at the same time.

An example will better clear this concept.

This is the interface of our Yed object example, Myclass, coded into the header file myclass.h:

typedef struct{                     
                                   
PUBLIC char my_attr;            
PUBLIC int (*My_Func)(void *);    
                                    
char PRIVATE(protected);               
                                   
} Myclass;                         
             

This is an example of implementation of the method My_Func in object MyClass:

#include "yedstd.h"
#include "myclass.h"

int My_Func(void *pVWork) {
MyClass *this=pvWork;        // 'THIS' POINTER OF THE INSTANCE!!!
printf("The protected member value is [%d]\n", this->PRIVATE(protected));
return 0;
}

PRIVATE macro allows manipulation of protected attributes of the MyClass object instance, as we have seen in ' Data Hiding ' paragraph.

The red instruction it's a normal assignment of the generic pointer received in invocation to a pointer of a current instance of the object. From this point, method knows and can use the pointer 'this'.

This is an example of invocation of this method:

#include "yedstd.h"
#include "myclass.h"

main(){
MyClass *myClassInstance;

.....

myClassInstance->My_Func(myClassInstance);

.....

}

My_Func method, through his first void * parameter, receives a pointer to the instance myClassInstance that invoked it; therefore, it can show the value of protected attribute of this instance.

Clearly, besides 'this' pointer, a method can have any number of parameters.

Now, we know how to write methods of our Yed object: but our methods are still normal functions; how can we link the implementation of the methods to their interface ? This is what we'll see in next paragraph: ' The constructor '.


http://yed.sourceforge.net