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 CMyServerClassispatch[] = {
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?
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 CMyServerClassispatch[] = {
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?