Using Delegates?

H

Hector Santos

I'm trying to port this logic over to C#. In C/C++ I have a typedef
struct in an abstract class:

class CAbstractServer: public TThread {
public:
struct TSPDispatch {
char *cmd;
BOOL (CAbstractServer::*f)(char *args);
};
...
};

In a derived sub-class, I add functions, for example:

class CMyServerClass: public CAbstractServer {
typedef CAbstractServer inherited;
public:
...
private:
static TSPDispatch Dispatch[];

BOOL FUNC_CMD1(char *args);
BOOL FUNC_CMD2(char *args);
BOOL FUNC_CMD3(char *args);
BOOL FUNC_CMD4(char *args);

..
};

and then using a macro, I can do this:

#define SPCMD(cls, cmd, func) \
{cmd, (BOOL (CAbstractServer::*)(char *))(&cls::func)}

CAbstractServer::TSPDispatch CMyServerClass:Dispatch[] = {
SPCMD(CMyServerClass, "CMD1", FUNC_CMD1),
SPCMD(CMyServerClass, "CMD2", FUNC_CMD2),
SPCMD(CMyServerClass, "CMD3", FUNC_CMD3),
SPCMD(CMyServerClass, "CMD4", FUNC_CMD4),
{0}
};

Overall, what I want is a map of command string vs function calls to
make.

I can of course, resolve it using a long switch statement, but I've
trying to repeat the above by studying delegates and I don't enough
understanding of it yet to figure it out.

Any tips? Examples of the above?
 
R

Rick Lones

Hector said:
I'm trying to port this logic over to C#. In C/C++ I have a typedef
struct in an abstract class:

class CAbstractServer: public TThread {
public:
struct TSPDispatch {
char *cmd;
BOOL (CAbstractServer::*f)(char *args);
};
...
};

In a derived sub-class, I add functions, for example:

class CMyServerClass: public CAbstractServer {
typedef CAbstractServer inherited;
public:
...
private:
static TSPDispatch Dispatch[];

BOOL FUNC_CMD1(char *args);
BOOL FUNC_CMD2(char *args);
BOOL FUNC_CMD3(char *args);
BOOL FUNC_CMD4(char *args);

..
};

and then using a macro, I can do this:

#define SPCMD(cls, cmd, func) \
{cmd, (BOOL (CAbstractServer::*)(char *))(&cls::func)}

CAbstractServer::TSPDispatch CMyServerClass:Dispatch[] = {
SPCMD(CMyServerClass, "CMD1", FUNC_CMD1),
SPCMD(CMyServerClass, "CMD2", FUNC_CMD2),
SPCMD(CMyServerClass, "CMD3", FUNC_CMD3),
SPCMD(CMyServerClass, "CMD4", FUNC_CMD4),
{0}
};

Overall, what I want is a map of command string vs function calls to make.

I can of course, resolve it using a long switch statement, but I've
trying to repeat the above by studying delegates and I don't enough
understanding of it yet to figure it out.

Any tips? Examples of the above?

HTH,
-rick-

using System;
using System.Collections.Generic;

namespace ConsoleApplication9
{
class Program
{
delegate void simpleDelegate();

static void Main(string[] args)
{
// create jump table
Dictionary<string, simpleDelegate> jumpTable;
jumpTable = new Dictionary<string, simpleDelegate>();

// Initialize it
jumpTable.Add("CMD1", Proc1);
jumpTable.Add("CMD2", Proc2);

// Test it
(jumpTable["CMD2"])();
(jumpTable["CMD1"])();
Console.ReadLine();
}

private static void Proc1()
{
Console.WriteLine("Proc1 invoked");
}

private static void Proc2()
{
Console.WriteLine("Proc2 invoked");
}

}
}
 
H

Hector Santos

Rick said:
Hector said:
I'm trying to port this logic over to C#. In C/C++ I have a typedef
struct in an abstract class:

class CAbstractServer: public TThread {
public:
struct TSPDispatch {
char *cmd;
BOOL (CAbstractServer::*f)(char *args);
};
...
};

In a derived sub-class, I add functions, for example:

class CMyServerClass: public CAbstractServer {
typedef CAbstractServer inherited;
public:
...
private:
static TSPDispatch Dispatch[];

BOOL FUNC_CMD1(char *args);
BOOL FUNC_CMD2(char *args);
BOOL FUNC_CMD3(char *args);
BOOL FUNC_CMD4(char *args);

..
};

and then using a macro, I can do this:

#define SPCMD(cls, cmd, func) \
{cmd, (BOOL (CAbstractServer::*)(char *))(&cls::func)}

CAbstractServer::TSPDispatch CMyServerClass:Dispatch[] = {
SPCMD(CMyServerClass, "CMD1", FUNC_CMD1),
SPCMD(CMyServerClass, "CMD2", FUNC_CMD2),
SPCMD(CMyServerClass, "CMD3", FUNC_CMD3),
SPCMD(CMyServerClass, "CMD4", FUNC_CMD4),
{0}
};

Overall, what I want is a map of command string vs function calls to
make.

I can of course, resolve it using a long switch statement, but I've
trying to repeat the above by studying delegates and I don't enough
understanding of it yet to figure it out.

Any tips? Examples of the above?

HTH,
-rick-

using System;
using System.Collections.Generic;

namespace ConsoleApplication9
{
class Program
{
delegate void simpleDelegate();

static void Main(string[] args)
{
// create jump table
Dictionary<string, simpleDelegate> jumpTable;
jumpTable = new Dictionary<string, simpleDelegate>();

// Initialize it
jumpTable.Add("CMD1", Proc1);
jumpTable.Add("CMD2", Proc2);

// Test it
(jumpTable["CMD2"])();
(jumpTable["CMD1"])();
Console.ReadLine();
}

private static void Proc1()
{
Console.WriteLine("Proc1 invoked");
}

private static void Proc2()
{
Console.WriteLine("Proc2 invoked");
}

}
}


Rick, not even two minutes ago I used a collection map as a 2nd
cleaner way. :)

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top