FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template question

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
Robin



Joined: 21 Apr 2004
Posts: 2

PostPosted: Wed Apr 21, 2004 7:47 pm    Post subject: Template question Reply with quote

Is it possible to use templates to set how many and what type of parameters a function should take?

I want to make an EventHandler class which overloads the ~= operator for adding delegates(with parameters specified by template) and the () operator for executing all delegates again taking said parameters.

Here's an example of an un-templateized handler:

class KeyHandler {
public void opCatAssign(void delegate(int key) handler) {
handlers ~= handler;
}

public void opCall(int key) {
for(int i = 0; i < handlers.length; ++i) {
handlers[i](key);
}
}

private void delegate(int key)[] handlers;
}

It would be nice to not have to make a new one for each Handler type.
Back to top
View user's profile Send private message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Wed Apr 21, 2004 10:33 pm    Post subject: Re: Template question Reply with quote

Robin wrote:
Is it possible to use templates to set how many and what type of parameters a function should take?

I want to make an EventHandler class which overloads the ~= operator for adding delegates(with parameters specified by template) and the () operator for executing all delegates again taking said parameters.

Here's an example of an un-templateized handler:

class KeyHandler {
public void opCatAssign(void delegate(int key) handler) {
handlers ~= handler;
}

public void opCall(int key) {
for(int i = 0; i < handlers.length; ++i) {
handlers[i](key);
}
}

private void delegate(int key)[] handlers;
}

It would be nice to not have to make a new one for each Handler type.


You can do this:
Code:

class KeyHandler (T) {
   public void opCatAssign(void delegate(T key) handler) {
      handlers ~= handler;
   }

   public void opCall(T key) {
      for(int i = 0; i < handlers.length; ++i) {
         handlers[i](key);
      }
   }

   private void delegate(T key)[] handlers;
}
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Robin



Joined: 21 Apr 2004
Posts: 2

PostPosted: Thu Apr 22, 2004 11:05 am    Post subject: Re: Template question Reply with quote

Carlos wrote:
Robin wrote:
Is it possible to use templates to set how many and what type of parameters a function should take?

I want to make an EventHandler class which overloads the ~= operator for adding delegates(with parameters specified by template) and the () operator for executing all delegates again taking said parameters.

Here's an example of an un-templateized handler:

class KeyHandler {
public void opCatAssign(void delegate(int key) handler) {
handlers ~= handler;
}

public void opCall(int key) {
for(int i = 0; i < handlers.length; ++i) {
handlers[i](key);
}
}

private void delegate(int key)[] handlers;
}

It would be nice to not have to make a new one for each Handler type.


You can do this:
Code:

class KeyHandler (T) {
   public void opCatAssign(void delegate(T key) handler) {
      handlers ~= handler;
   }

   public void opCall(T key) {
      for(int i = 0; i < handlers.length; ++i) {
         handlers[i](key);
      }
   }

   private void delegate(T key)[] handlers;
}


Yes but that would still be just a key handler. What if I want a MouseButtonHandler which takes two arguments, namely x and y?
Back to top
View user's profile Send private message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Thu Apr 22, 2004 4:50 pm    Post subject: Re: Template question Reply with quote

Robin wrote:
Yes but that would still be just a key handler. What if I want a MouseButtonHandler which takes two arguments, namely x and y?


First thing that comes to my mind is you can do this:

Code:
class MouseButtonHandler (T,U) {
   public void opCatAssign(void delegate(T,U) handler) {
      handlers ~= handler;
   }

   public void opCall(T x,U y) {
      for(int i = 0; i < handlers.length; ++i) {
         handlers[i](x,y);
      }
   }

   private void delegate(T,U)[] handlers;
}


However, you're probably gonna end up with many types for a template. So what about a suggestion: why don't you go .net like and have a base EventArgs class and have others subclass it. Each subclass would define more attributes for its respective handlers.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
l8night



Joined: 03 May 2004
Posts: 32
Location: UK

PostPosted: Mon May 03, 2004 4:48 pm    Post subject: Reply with quote

you might want to consider this ...
Code:

class HandleArray( H ) {
   protected:
       alias H HandlerType;
      H[] handlers;
      bit findResult( bit delegate(H) predicate ) {
         foreach( HandlerType handler; handlers ) {
            if ( predicate( handler ) ) { return true; }
         }
         return false;
      }
      void onall( void delegate(H) action ) {
         foreach( HandlerType handler; handlers ) {
            action( handler );
         }
      }
   public:
    void opCatAssign( H handler ) {
      handlers ~= handler;
   }
}

class ProcHandler( P ) : HandleArray!( void delegate( P ) ) {
   public void opCall( P p ) {
      onall( delegate void( HandlerType h ) { h(p); } );
   }
}

class ProcHandler( P0, P1 ) : HandleArray!( void delegate(P0, P1) ) {
   public void opCall( P0 p0, P1 p1 ) {
      onall( delegate void( HandlerType h ) { h(p0, p1); } );
   }
}

class FuncHandler( R, P ) : HandleArray!( bit delegate( out R, P ) ) {
   public R opCall( P p ) {
      R rv = R.init;
      findResult( delegate bit( HandlerType h ) { return h( rv, p ); } );
      return rv;
   }
}
///-- example test code;
class IntScanner : FuncHandler!( int, int ) {
}

void test1() {
   IntScanner scan = new IntScanner();
   bit scanFor1( out int rv, int val ) { if ( val == 1 ) { rv = 10; return true; } return false; }
   bit scanFor3( out int rv, int val ) { if ( val == 3 ) { rv = 30; return true; } return false; }
   scan ~= &scanFor1;
   scan ~= &scanFor3;
   for ( int i = 0; i < 4; i++ ) {
      printf( "scan(?d) = ?d\n", i, scan( i ) );
   }
}

void test2() {
   ProcHandler!( int, char[] ) scan;
   scan = new typeof(scan)();
   void show( int val, char[] name ) { printf( "show[?d]:?.*s\n", val, name ); }
   void showagain( int val, char[] name ) { printf( "showagain[?d]:?.*s\n", val, name ); }
   scan ~= &show;
   scan ~= &showagain;
   scan( 1, "one" );
   scan( 2, "two" );
}

int main( char[][] args ) {
   test1();
   test2();
   return 0;
}

whilst you still need a templated class for each number of parameters you need to handle is is a very simple class eg ...
Code:

class ProcHandler( P0, P1, P2 ) : HandleArray!( void delegate(P0, P1,P2) ) {
   public void opCall( P0 p0, P1 p1, P2 p2 ) {
      onall( delegate void( HandlerType h ) { h(p0, p1, p2); } );
   }
}
Back to top
View user's profile Send private message
Hohums



Joined: 08 Apr 2004
Posts: 71
Location: Western Australia

PostPosted: Mon May 03, 2004 7:21 pm    Post subject: Re: Template question Reply with quote

Robin wrote:


Yes but that would still be just a key handler. What if I want a MouseButtonHandler which takes two arguments, namely x and y?


Just pass the template a struct. ie

class dispatcher(T)
{
....
}

struct mouseEvent
{
int x;
int y;
}

alias dispatcher!(mouseevent) dispatcher;
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group