Click here to Skip to main content
11,415,711 members (82,758 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi,

Imagine I have some networking class CNetwork which has some method called: makeNetworkRequest(int requestCode). The response from makeNetworkRequest(int requestCode) can of course come some time later this function was called. What I want is that, when that response arrives, to execute such code for example:

void myApp::function1()
{
CMyButton button;
button.SetText(responseFromServer.text);
button.SetTextColor(responseFromServer.color);
}

The thing I want now is that the user from UI may be calling makeNetworkRequest with different request codes -- and based on that I may want to execute different functionality, e.g., for example execute function2 instead of function1 -- and I want to be able to achieve the functionality I mentioned above.
void myApp::function2()
{
CMyEdit edit;
edit.SetText(responseFromServer.text);
}
Posted 13-Feb-13 22:03pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There are really many ways to achieve what you intend to do. For instance:
  • A switch statement with function calls hard-coded for every case
  • A classical, C-like callback mechanism, like the one used in Windows API (see EnumWindowsProc[^]). With this approach, you have to use static methods, though
  • Using member function pointers
  • Using polymorphism, that is pass an object implementing the interface containing the method 'function'. Different objects implement such method in different ways.
  • ...
  Permalink  
Comments
Matra Divat at 15-Feb-13 5:03am
   
>>"Using polymorphism, that is pass an object implementing the interface containing the method 'function'. Different objects implement such method in different ways."

Could you please elaborate on this approach??
Sergey Alexandrovich Kryukov at 12-Mar-13 17:14pm
   
Sure, a 5.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You would probably like to use I/O Completion Ports[^]

Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports[^]

You can use BindIoCompletionCallback[^], and I guess you'll find this article of some interest:IOCPNet - Ultimate IOCP[^]

Best regards
Espen Harlinn
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 12-Mar-13 17:13pm
   
Sure, a 5.
—SA
Espen Harlinn at 12-Mar-13 17:29pm
   
Thank you, Sergey :-D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

give a look to std::function[^] and related header.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

In addition to the ones already mentioned, it's worth considering using the MFC CWnd message pump system, as it looks like you're using MFC.

This especially important as you really shouldn't manipulate GUI elements from another thread.

In this case, you could define a separate message for each request code, or pass the request code on as a message parameter and let the message handling function decide what function to call.

See Meandering Through the Maze of MFC Message and Command Routing[^] for a very good explanation of the MFC messaging system, or just look at How to send a user-defined message with SendMessage, PostMessage or PostThreadMessage[^]
  Permalink  
Comments
Matra Divat at 14-Feb-13 8:51am
   
Hi, no I am not using MFC this was just an example. I might use Blackberry 10 SDK. Indeed manipulating UI is my concern now.. I might need to somehow embed the UI code (e.g., [1]) "somewhere" so that it gets called once the server returns... desirably this UI code should also then be able to use the response data from the server to update it's values etc...

[1]: CSomeUIElement m = new CSomeUIElement(serverresponse.data)... etc. etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,270
1 OriginalGriff 7,512
2 Maciej Los 3,710
3 Abhinav S 3,325
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web01 | 2.8.150427.4 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100