Callback function and C++

hi all ?
I read the title “Integration photon project and C++ classes” below.
so I try doing this way. but have a few question for this way.

If static method cover functions is written, photon callback function must
create objects
to call static method cover function ,whenever phton callback function is
called.

I.E

photonCallback (Ptwidet_t *widget, ApInfo_t *apinfo, PtCallbackInfo *cbinfo)
{
// create object
Cmyclass myclass;

// call static method cover functions
myclass.fn_X_cover(&myclass);
}

class Cmyclass
{
public:
void fn_x(){
// cleate other objects
// or other functions
}
static void fn_X_cover(Cmyclass *mcPtr);
};

void fn_X_cover(Cmyclass *mcPtr)
{
mcPtr->fn_x();
}

  1. I wish to create myclass object outside callback funtion. before calling
    photonCallback function. how to do ?
    I can’t create myclass object in admain.cc becouse that admain.cc must
    be generated by PhAB tool.


    thank you

    \

Gye jin Ahn
Tel: 82-31-639-6091
E-mail: gyejin@haco.co.kr

gyejin wrote:

hi all ?
I read the title “Integration photon project and C++ classes” below.
so I try doing this way. but have a few question for this way.

If static method cover functions is written, photon callback function must
create objects
to call static method cover function ,whenever phton callback function is
called.

Well, the whole idea behind this is somewhat revolting, but the theory
is that you instantiate your C++ class in the Realized callback (closest
thing to a “constructor” callback).

i.e.

// somewhere in a .cc file


static Foo *Bar; // where Foo is a C++ class

int callback_attached_to_realized(…)
{
Bar=new Foo;
}


// and then create the covers for method invocations as described

Of course, if you want multiple instances of (in this case Foo class),
you need to do something else (like attaching the pointer to the object
in the Pt_ARG_USER_DATA of the widget, for instance).

// somewhere in a .cc file


// note there is no static

int callback_attached_to_realized(…)
{
Foo *Bar=new Foo;

Bar=new Foo;

// Bar is a pointer, so we are only storing the ptr
PtSetResource(widget, Pt_ARG_USER_DATA, &Bar, sizeof(Bar));
}

// inside one of the cover functions
Foo **Bar;

PtGetResource(widget, Pt_ARG_USER_DATA, &Bar, 0);

// now with access to the object you can call methods…

If that doesn’t make you want to vomit, then your ready for C++ under
PhAB :slight_smile:

Rennie

I am not sure I understand what you want, but here it goes.

1 ) In phab, in the Application menu goto Startup Info/Modules.There is
an “Initialization Function” field, witch is for application
initialization. In the lower right of the dialog, there is a “Setup
Function” witch is for Window initialization.

2 ) Usally when you create a static method in a class to be attach as a
callback, that method will need to refer to elements of an object of the
class. To do this you add the callback in the class constructor and pass
the this pointer as the data pointer in PtAddCallback function. Make
sure to remove the callback in the class destructor.

Michel Belanger

gyejin wrote:

hi all ?
I read the title “Integration photon project and C++ classes” below.
so I try doing this way. but have a few question for this way.

If static method cover functions is written, photon callback function must
create objects
to call static method cover function ,whenever phton callback function is
called.

I.E

photonCallback (Ptwidet_t *widget, ApInfo_t *apinfo, PtCallbackInfo *cbinfo)
{
// create object
Cmyclass myclass;

// call static method cover functions
myclass.fn_X_cover(&myclass);
}

class Cmyclass
{
public:
void fn_x(){
// cleate other objects
// or other functions
}
static void fn_X_cover(Cmyclass *mcPtr);
};

void fn_X_cover(Cmyclass *mcPtr)
{
mcPtr->fn_x();
}

  1. I wish to create myclass object outside callback funtion. before calling
    photonCallback function. how to do ?
    I can’t create myclass object in admain.cc becouse that admain.cc must
    be generated by PhAB tool.

the focus of my questions is

  1. where to create objects of my classes.
    I try to do “Setup Function” to create object, yet althought my
    class are created in “Setup Function” code, I can’t refrence my object which
    is created at “Setup Function” in photon callback function because my object
    only have a meaning at “Setup Function”.
    but, if my objects are declared by global variable, this way may be
    possible such as above “Re:Callback function and C++” reply

is there the way my classes are created to be referenced in photon
callback function.?

  1. if all my classe must be created in photon callback function to
    refrence or call methods of my classes.
    it is not effetive. whenever photon callback function is called, all
    my classe must be created and destructed.



    “Michel Belanger” <micbel@videotron.ca> wrote in message
    news:3CE9A0B1.9090804@videotron.ca

I am not sure I understand what you want, but here it goes.

1 ) In phab, in the Application menu goto Startup Info/Modules.There is
an “Initialization Function” field, witch is for application
initialization. In the lower right of the dialog, there is a “Setup
Function” witch is for Window initialization.

2 ) Usally when you create a static method in a class to be attach as a
callback, that method will need to refer to elements of an object of the
class. To do this you add the callback in the class constructor and pass
the this pointer as the data pointer in PtAddCallback function. Make
sure to remove the callback in the class destructor.

Michel Belanger

gyejin wrote:

hi all ?
I read the title “Integration photon project and C++ classes” below.
so I try doing this way. but have a few question for this way.

If static method cover functions is written, photon callback function
must
create objects
to call static method cover function ,whenever phton callback function
is
called.

I.E

photonCallback (Ptwidet_t *widget, ApInfo_t *apinfo, PtCallbackInfo
*cbinfo)
{
// create object
Cmyclass myclass;

// call static method cover functions
myclass.fn_X_cover(&myclass);
}

class Cmyclass
{
public:
void fn_x(){
// cleate other objects
// or other functions
}
static void fn_X_cover(Cmyclass *mcPtr);
};

void fn_X_cover(Cmyclass *mcPtr)
{
mcPtr->fn_x();
}

  1. I wish to create myclass object outside callback funtion. before
    calling
    photonCallback function. how to do ?
    I can’t create myclass object in admain.cc becouse that admain.cc
    must
    be generated by PhAB tool.


    \

It does not matter how you do it ( Rennie’s way or this way ) in both
case your object as to be allocated using new and you have to save your
object pointer somewhere (Within the widget like Rennie or in the
Callback list like this )

// Globals
MyClass_s *MyObject = NULL;

// this is the Window setup function
int SetupFunctionCB( PtWidget_t *widget, ApInfo_t *apinfo,
PtCallbackInfo_t *cbinfo )
{

// you need to allocate the object with new if you want
// it’s life cycle to go futher then this function
MyObject = new MyClass_s(widget) ;
}

int AnyCallbackCB(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t
*cbinfo )
{
// MyObject is global you can access it anywhere
if( MyObject != NULL )
MyObject->AnyMethod() ;
}


class MyClass_s
{
public :

PtWidget_t *MyButton ;

MyClass_s( PtWidget_t *parent )
{
PtArg_t args[0] ;

PtSetArg( &args[0],Pt_ARG_TEXT_STRING,“Press me”, 0 );
PtSetArg( &args[1], Pt_ARG_FLAGS, Pt_TRUE, Pt_SELECTABLE |
Pt_GETS_FOCUS | Pt_FOCUS_RENDER ) ;

MyButton = PtCreateWidget( PtButton, parent, 2, args ) ;

// when MyButton is press, MyStaticMethod will be invoke
if( MyButton != NULL )
PtAddCallback( MyButton, Pt_CB_ACTIVATE,MyStaticMethod, this ) ;
}

~MyClass_s()
{
if( MyButton != NULL )
{
PtRemoveCallback( MyButton, Pt_CB_ACTIVATE,MyStaticMethod, this ) ;
PtDestroyWidget( MyButton ) ;
}
}

static int MyStaticMethod(PtWidget_t *widget, void *data,
PtCallbackInfo_t *cbinfo )
{
MyClass_s *my_object ;

my_object = ( MyClass_s *)data ;

// now I can access method from my object
my_object->AnyMethod() ;
}

void AnyMethod()
{
}
}

Michel Belanger