Jump to page: 1 2 3
Thread overview
Events
Feb 15, 2005
Craig Black
Feb 16, 2005
Walter
Feb 16, 2005
Craig Black
Feb 16, 2005
Walter
Feb 16, 2005
John Reimer
Feb 17, 2005
Walter
Feb 17, 2005
John Reimer
Feb 17, 2005
Kris
Re: Events [OT]
Feb 17, 2005
pragma
Feb 17, 2005
John Reimer
Feb 17, 2005
Kris
Feb 17, 2005
John Reimer
Feb 16, 2005
Craig Black
Feb 17, 2005
Walter
Feb 17, 2005
Craig Black
Feb 17, 2005
Chris Sauls
Feb 18, 2005
Craig Black
Feb 16, 2005
Ben Hinkle
Feb 16, 2005
Craig Black
Feb 16, 2005
Craig Black
Feb 17, 2005
Manfred Nowak
Feb 18, 2005
Craig Black
Feb 22, 2005
Manfred Nowak
Feb 22, 2005
Chris Sauls
Feb 22, 2005
Manfred Nowak
Feb 23, 2005
Craig Black
Feb 24, 2005
Manfred Nowak
Feb 24, 2005
Craig Black
Feb 24, 2005
Georg Wrede
Feb 25, 2005
Manfred Nowak
February 15, 2005
An event class would contain a time stamp, a delegate/function and any
parameters
associated with the delegate/function.

For example:

void myFunc(int i)
{
    printf("%d\n", i);
}

class MyEvent
{
    float time;
    void function(int) func;
    int i;
    void invoke() { func(i); }
}

void main(char[][] args)
{
   MyEvent event;
   event.func = &myFunc;
   event.i = 5;
   event.invoke();
}

Is there a way to describe an event generically in D so that a new class does not have to be created for each new function/method signature?  If not, perhaps D could provide a mechanism for this.

-Craig


February 16, 2005
I think what you're looking for are called "delegates" in D.


February 16, 2005
"Craig Black" <cblack@ara.com> wrote in message news:cuu06o$1mse$1@digitaldaemon.com...
> An event class would contain a time stamp, a delegate/function and any
> parameters
> associated with the delegate/function.
>
> For example:
>
> void myFunc(int i)
> {
>    printf("%d\n", i);
> }
>
> class MyEvent
> {
>    float time;
>    void function(int) func;
>    int i;
>    void invoke() { func(i); }
> }
>
> void main(char[][] args)
> {
>   MyEvent event;
>   event.func = &myFunc;
>   event.i = 5;
>   event.invoke();
> }
>
> Is there a way to describe an event generically in D so that a new class does not have to be created for each new function/method signature?  If not, perhaps D could provide a mechanism for this.
>
> -Craig
>

The closest D comes to a generic mechanism is probably the vararg ... syntax. That would put the type information into the _arguments array that the callee can pick apart. You could also try using templates. Remember D is strongly typed so keeping the types known at compile time will probably be easier than trying to use an generic mechanism that hides the types.


February 16, 2005
> The closest D comes to a generic mechanism is probably the vararg ... syntax. That would put the type information into the _arguments array that the callee can pick apart. You could also try using templates. Remember D is strongly typed so keeping the types known at compile time will probably be easier than trying to use an generic mechanism that hides the types.

With some clever templated code, varargs just might do the trick.  Good
idea.
Thanks.


February 16, 2005
"Walter" <newshound@digitalmars.com> wrote in message news:cuu835$1tsi$1@digitaldaemon.com...
>
> I think what you're looking for are called "delegates" in D.
>

Thanks for the reply Walter.  To my knowledge delegates do not store the parameter information.  This is required for a generic event mechanism.  The event structure must be able to hold both the delegate and the parameters associated with the method to be invoked.

-Craig


February 16, 2005
Wait a minute.  In order to match varargs type information with the delegate, one must know the delegate's parameters' type information.  Is it possible to know this information at compile time or even run time?  A compile time check would be ideal, but is it even possible to do at run time?

-Craig


February 16, 2005
"Craig Black" <cblack@ara.com> wrote in message news:cv096s$v9e$1@digitaldaemon.com...
>
> "Walter" <newshound@digitalmars.com> wrote in message news:cuu835$1tsi$1@digitaldaemon.com...
> >
> > I think what you're looking for are called "delegates" in D.
> >
>
> Thanks for the reply Walter.  To my knowledge delegates do not store the parameter information.  This is required for a generic event mechanism.
The
> event structure must be able to hold both the delegate and the parameters associated with the method to be invoked.

Delegates are a pair consisting of a 'this' pointer and a function address. The 'this' pointer can point to anything, including an object that contains any needed state information.


February 16, 2005
Walter wrote:
> "Craig Black" <cblack@ara.com> wrote in message
> news:cv096s$v9e$1@digitaldaemon.com...
> 
>>"Walter" <newshound@digitalmars.com> wrote in message
>>news:cuu835$1tsi$1@digitaldaemon.com...
>>
>>>I think what you're looking for are called "delegates" in D.
>>>
>>
>>Thanks for the reply Walter.  To my knowledge delegates do not store the
>>parameter information.  This is required for a generic event mechanism.
> 
> The
> 
>>event structure must be able to hold both the delegate and the parameters
>>associated with the method to be invoked.
> 
> 
> Delegates are a pair consisting of a 'this' pointer and a function address.
> The 'this' pointer can point to anything, including an object that contains
> any needed state information.
> 
> 

How do you access object state information from within a delegate?  Last I checked, you can't access the 'this' pointer from the delegate.
February 16, 2005
> Delegates are a pair consisting of a 'this' pointer and a function
> address.
> The 'this' pointer can point to anything, including an object that
> contains
> any needed state information.

For member methods, the this pointer should not be overwritten.  It would be possible, theoretically to hold a function pointer in a delegate structure and use the this pointer as something else, but I think it would be easier to create a class that holds a function pointer and a void pointer.

However, even if we do have a pointer to hold an object that contains the parameters, this still does not solve the problem of having to write a new class for the object.  Using a traditional approach, each new method or function signature requires a new data structure to be defined, as well as code that performs the invokation with the parameters in the data structure.

I am seeking a way to define a GENERIC event data structure, perhaps with a template or a new language feature, that does turns all this coding into a one-liner.

For example, something like,

void myFunc(char[] str)
{
   // print str to the console, however D does it
}

// this event will be invoked at t = 1.0, it will print "hey" to the console Event event = new Event(1.0, myFunc, "hey");

-Craig


February 17, 2005
"John Reimer" <brk_6502@yahoo.com> wrote in message news:cv0aj4$10jm$1@digitaldaemon.com...
> How do you access object state information from within a delegate?  Last I checked, you can't access the 'this' pointer from the delegate.

You don't access it directly, you call the delegate, just as you would a pointer to a function.


« First   ‹ Prev
1 2 3