Prev: How to recognize, Socket(Asynchronous) is closed by the client
Next: Why is the bool type 4 bytes it's a wast of memory..
From: Hector Santos on 27 May 2010 18:01 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? -- HLS
From: Rick Lones on 27 May 2010 18:36 Hector Santos wrote: > 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"); } } }
From: Hector Santos on 27 May 2010 20:18
Rick Lones wrote: > Hector Santos wrote: >> 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. -- HLS |