View previous topic :: View next topic |
Author |
Message |
Robin
Joined: 21 Apr 2004 Posts: 2
|
Posted: Wed Apr 21, 2004 7:47 pm Post subject: Template question |
|
|
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 |
|
|
Carlos
Joined: 19 Mar 2004 Posts: 396 Location: Canyon, TX
|
Posted: Wed Apr 21, 2004 10:33 pm Post subject: Re: Template question |
|
|
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 |
|
|
Robin
Joined: 21 Apr 2004 Posts: 2
|
Posted: Thu Apr 22, 2004 11:05 am Post subject: Re: Template question |
|
|
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 |
|
|
Carlos
Joined: 19 Mar 2004 Posts: 396 Location: Canyon, TX
|
Posted: Thu Apr 22, 2004 4:50 pm Post subject: Re: Template question |
|
|
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 |
|
|
l8night
Joined: 03 May 2004 Posts: 32 Location: UK
|
Posted: Mon May 03, 2004 4:48 pm Post subject: |
|
|
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 |
|
|
Hohums
Joined: 08 Apr 2004 Posts: 71 Location: Western Australia
|
Posted: Mon May 03, 2004 7:21 pm Post subject: Re: Template question |
|
|
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 |
|
|
|
|
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
|