Thread

Use SDK event handler inside a class instance (Sdk)

Use SDK event handler inside a class instance // Sdk

1  |  

neophile

Mar 5, 2005, 3:25pm
Hello, I don't understand how to do for make aw_event_set inside an
instance fonction like (it's not the really code but ligh for the example):

// Constructor
awctrl::awctrl ()
{
aw_init(AW_BUILD); // Work
aw_event_set (AW_EVENT_CHAT, on_chat); // Make my compiler error //message:
// C:\folder\test\CAwCtrl.cpp argument of type `void awctrl::)()'
// does not match `void (*)()'
}

// instance function for a chat event
void awctrl::on_chat (void)
{
...... // code for a chat event
}

can you help me for my problem ?
I am trying to make a class for using SDK

tony m

Mar 5, 2005, 6:28pm
Hi neophile,

Class methods cannot be used for event handlers, as they have an implicit 'this' parameter. You must use plain C functions.

[View Quote]

scifair

Mar 5, 2005, 9:47pm
neophile-

You can use a static member function as an event handler. If you want an
instance method to handle the event, you can make the static one call to its
instances.

I think it is possible to use an instance function as an event handler, but
you'd have to use an awkward and dangerous hack of some sort. (I know that
if you wrap the SDK for .NET, you can use delegates that wrap instance
functions as handlers.)

However, this is a bad idea, and I don't think it will do what you want,
especially if you are installing the handler in a class constructor. Think
what happens if you create a second instance of the class: it takes over the
event handling. Then if you destroy the class, you no longer have a
functional handler.

You're much better off making a static handler, which need not be a plain C
function. Maybe you want an event-handling class with static event
handlers, to which your other objects may register as event listeners?
Consider deriving classes that will be listeners from an event-listener
abstract class.

--
Rich

[View Quote]

strike rapier

Mar 5, 2005, 10:18pm
Use a static.

If you need to use them in your class directly (with relevant this), just do
a straight call to a global class name.

- MR


[View Quote]

neophile

Mar 7, 2005, 4:25pm
tks scifair for your response. I see and prefer to use standard SDK of AW

I ever use static function for making me sure to have only one instance
of the class : defining constructor in private and have a static pointer
create function how return adress of existing instance or if not, create
it and return adress like:
in Ctrlaw.h :

class ctrlaw
{
ctrlaw::ctrlaw();
ctrlaw::~ctrlaw();
static ctrlaw* ptctrlaw;
public:
ctrlaw* Create();
void kill();
}

in ctrlaw.cpp:

ctrlaw* ctrlaw::ptctrlaw=0;

ctrlaw* ctrlaw::Create()
{
if (!ptctrlaw) ptctrlaw=new ctrlaw;
return ptctrlaw;
}

so if I understand your method, just add:

private:
static void InitEvent();
public:
static void On_Chat(void);

in the class proto and for cpp definition:

void ctrlaw::InitEvent()
{
aw_event_set(AW_EVENT_CHAT, On_Chat);
}

void ctrlaw::OnChat()
{
.... code here
}

of course, with the method for having only one instance from the class,
no possibility to have few objects and conflict with using Static handler...

I am not yet try it but, what do you thing about this?

for your info, I try to make meself a class how I'could use in future
for all my bot under AW environment and in the same time, with the
Object Oriented Language....

scifair a écrit :
> neophile-
>
> You can use a static member function as an event handler. If you want an
> instance method to handle the event, you can make the static one call to its
> instances.
>
> I think it is possible to use an instance function as an event handler, but
> you'd have to use an awkward and dangerous hack of some sort. (I know that
> if you wrap the SDK for .NET, you can use delegates that wrap instance
> functions as handlers.)
>
> However, this is a bad idea, and I don't think it will do what you want,
> especially if you are installing the handler in a class constructor. Think
> what happens if you create a second instance of the class: it takes over the
> event handling. Then if you destroy the class, you no longer have a
> functional handler.
>
> You're much better off making a static handler, which need not be a plain C
> function. Maybe you want an event-handling class with static event
> handlers, to which your other objects may register as event listeners?
> Consider deriving classes that will be listeners from an event-listener
> abstract class.
>
> --
> Rich
>
[View Quote]

neophile

Mar 7, 2005, 4:39pm
Error of my person : in the .h class define : add static like this :


tks scifair for your response. I see and prefer to use standard SDK of AW

I ever use static function for making me sure to have only one instance
of the class : defining constructor in private and have a static pointer
create function how return adress of existing instance or if not, create
it and return adress like:
in Ctrlaw.h :

class ctrlaw
{
ctrlaw::ctrlaw();
ctrlaw::~ctrlaw();
static ctrlaw* ptctrlaw;
public:
static ctrlaw* Create(); // <--- Here is my mistake now rectified
void kill();
}

in ctrlaw.cpp:

ctrlaw* ctrlaw::ptctrlaw=0;

ctrlaw* ctrlaw::Create()
{
if (!ptctrlaw) ptctrlaw=new ctrlaw;
return ptctrlaw;
}

so if I understand your method, just add:

private:
static void InitEvent();
public:
static void On_Chat(void);

in the class proto and for cpp definition:

void ctrlaw::InitEvent()
{
aw_event_set(AW_EVENT_CHAT, On_Chat);
}

void ctrlaw::OnChat()
{
.... code here
}

of course, with the method for having only one instance from the class,
no possibility to have few objects and conflict with using Static handler...

I am not yet try it but, what do you thing about this?

for your info, I try to make meself a class how I'could use in future
for all my bot under AW environment and in the same time, with the
Object Oriented Language....

scifair a écrit :

> neophile-
>
> You can use a static member function as an event handler. If you
want an instance method to handle the event, you can make the static one
call to its instances.
>
> I think it is possible to use an instance function as an event
handler, but you'd have to use an awkward and dangerous hack of some
sort. (I know that if you wrap the SDK for .NET, you can use delegates
that wrap instance functions as handlers.)
>
> However, this is a bad idea, and I don't think it will do what you
want, especially if you are installing the handler in a class
constructor. Think what happens if you create a second instance of the
class: it takes over the event handling. Then if you destroy the class,
you no longer have a functional handler.
>
> You're much better off making a static handler, which need not be a
plain C function. Maybe you want an event-handling class with static
event handlers, to which your other objects may register as event
listeners? Consider deriving classes that will be listeners from an
event-listener abstract class.
>
> --
> Rich
>
[View Quote]

1  |  
Awportals.com is a privately held community resource website dedicated to Active Worlds.
Copyright (c) Mark Randall 2006 - 2024. All Rights Reserved.
Awportals.com   ·   ProLibraries Live   ·   Twitter   ·   LinkedIn