WvCallback - How to create your own WvCallback?

First, you have to declare a new type of WvCallback using DeclareWvCallback. DeclareWvCallback is a macro that helps to create a new type of WvCallback.

Note: The macro of DeclareWvCallback is written in ~src/niti/src/wvstreams/include/wvcallback.h

The syntax of DeclareWvCallback is as follows:

DeclareWvCallback (n, ret, type, parms...)

The input parameters of DeclareWvCallback are explained as follows:

Reminder: WvCallback = function pointer or member function pointer

n - the number of parameters that the function pointed to by the WvCallback takes.

ret - the return type of the function pointed to by the WvCallback (e.g.: int, void, ...)

type - the name of the callback type (e.g.: WvConfCallback). This is the type with which you will declare your WvCallback with.

parms... - the types of the parameters that the function pointed to by the WvCallback.

To create your own WvCallback, you need to include wvcallback.h. Then you need to declare your own WvCallback type at the top level. You cannot run DeclareWvCallback inside a class; otherwise, it causes an internal compiler error (g++ 2.95.4). See the following simple example:

WvCallback - As a function pointer - function not inside any class

	  
/*
 * A WvCallback example.
 *
 */

#include "wvcallback.h"
#include <stdio.h>

//Declare a new type of WvCallback called WvMath
//This WvCallbak can point to functions that take 2 input parameters, both of type
//integer, and returns an integer value.
DeclareWvCallback(2, int, WvMath, int, int);

int addition(int a, int b)
{
    return a+b;
}

    
int main()
{
    WvMath callback(NULL); //Declare a WvCallback of type WvMath
    //callback = wvcallback(WvMath, *this, Math::addition);
    callback = addition; // Now callback becomes a function pointer to the addition function

    int answer = callback(5, 6); //Bind input parameter values to callback, same 
    //way as we bind values to the addition function.

    printf("answer = %d\n", answer);
    
    
}

 	 

WvCallback - As a member function pointer - function inside a class

	  
#include "wvcallback.h"
#include <stdio.h>

//Declare a new type of WvCallback called WvMath
//This WvCallbak can point to functions that take 2 input parameters, both of 
//type integer, and returns an integer value.
DeclareWvCallback(2, int, WvMath, int, int);

class Math
{
    public:
	int addition(int a, int b);
};

int Math::addition(int a, int b)
{
    return a+b;
}

    
int main()
{
    WvMath callback(NULL); //Declare a WvCallback of type WvMath
    Math object;
    callback = wvcallback(WvMath, object, Math::addition); //Here callback becomes a pointer 
    //to the member function (addition) that belongs to the WvMath class

    int answer = callback(5, 6); //Bind input parameter values to callback, //same way as we bind values to the addition function.

    printf("answer = %d\n", answer);
    
}