Valhalla Legends Forums Archive | C/C++ Programming | map events

AuthorMessageTime
R.a.B.B.i.T
I've been interested in this for a little while, but I've never gotten around to learning about it, and now I have.  I've been tinkering with Winsock2, and I've got that connecting fine and all, but my desire is to map events so that they fire whenever they occur, as opposed to having my program sit in a loop waiting for them.  I know I can do event mapping, and I've done it in MSVC++, but it's ugly and not explained very well.  I've primarily been working in Notepad2 and using MSYS/MinGW to write some small C programs, which pretty much eradicates all of the IDE automation MSVC++ has, and  I was wondering how I would go about mapping events (specifically for Winsock2).  I've had some ideas, but none worked (like using BEGIN_MESSAGE_MAP(this, this) and the such).  Google and MSDN also failed in answering my questions, and so here I am.  Anyone?
May 19, 2005, 9:58 PM
OnlyMeat
BEGIN_MESSAGE_MAP is actually just a macro MFC uses to map window events to their handlers instead of having massive winproc switch statements. wxWidgets uses similar macros.

They are designed for GUI based apps really. I use them on my front ends. If you intend on using the sockets API directly which i would recommend rather then using MFC socket classes (which are not flexible), then you can simulate events through class inheritance.

Here is a simple example:-

[code]
// Usually this class would setup a socket, and use a secondry thread to wait for socket events via WSASelectEvent and WSAWaitForMultipleEvents
// this class would then invoke the virtual handlers and via polymorphism the handlers in derived classes will be called
Class A
{

private:

      // Abstract class
      virtual void OnNetworkRead() = 0;
};

// Derive this class from A, This could process the connection events and post WM_* events to a GUI front end or simply output to a console stream depending on which subsystem you use
Class B : public A
{

Private:
     
      // Concrete class handling simulated event through
      // class inheritance
      void OnNetworkRead() {//Process incoming data};
   
};
[/code]

Alternatively you can just post messages to a front end from the first class. However using the inheritance route allows you to separate the connection responsibility from the UI layer, which enforces dependency inversion.
May 20, 2005, 1:58 PM
R.a.B.B.i.T
Molto grazie!  Ho capisco!

Well, sort of.  I'm not bothering with GUI at all at this point, mainly because MFC sucks and really turned me away from GUI's.

I'll give that a try.

[edit]
Any way I can do this in C?  It compiles fine in C++, but C doesn't like classes.
Nextly, I'm still unsure of how exactly this works, could you explain please?
May 20, 2005, 3:49 PM
OnlyMeat
[quote author=rabbit link=topic=11642.msg113129#msg113129 date=1116604145]

Any way I can do this in C?  It compiles fine in C++, but C doesn't like classes.
[/quote]

C++ is an object orientated language unlike C. Consequently you can't use classes, inheritance, polymorphism or encapsulation in C.

The only real way to emulate this in C would be to use function pointers (which is basically what polymorphism elegantly does with virtual function tables).

[quote author=rabbit link=topic=11642.msg113129#msg113129 date=1116604145]
Nextly, I'm still unsure of how exactly this works, could you explain please?
[/quote]

When you declare a class member function with the virtual keyword the c++ preprocessor sets up a virtual function table entry. When the base class invokes the pure virtual OnNetworkRead function the derived classes overridden version of the function gets called.

Polymorphism in a nutshell :P
May 20, 2005, 5:10 PM

Search