Folks,
I have a bit of a quandary I was hoping for some expert guidance on. Keep in mind I am not a C++ developer, but I have worked with the language a bit.
I have a native C++ API which I am currently wrapping in managed C++ in order to expose the API to .Net clients. So far, so good, everything is working out just fine. However, I have ventured into the callback portion of the API and I am stumped.
In the native API, the callback functionality is abstract. It is left to the consumer to create their class, inheriting from the abstract and coding the actual implementation of the method. Here is the callback.h file from the native API:
- #ifndef Callback_h
-
#define Callback_h
-
-
namespace NMSP {
-
-
class Conn;
-
class Msg;
-
/** @class Callback
-
* @brief This class is the abstract base class for received message callbacks.
-
* A user created class, derrived from this class, can be passed into
-
* %Subscribe() and %Request() to have user code executed asynchronously
-
* when a message is received.
-
*
-
* Example Callback class:
-
* @code
-
* class PublishCallback : public Callback
-
* {
-
* public:
-
* virtual void CALL_TYPE OnMessage(Conn *conn, Msg *msg)
-
* {
-
* const char *tmp;
-
* msg->ToXML(tmp);
-
* cout << tmp << endl;
-
* }
-
* };
-
* @endcode
-
*
-
* Example Callback registration:
-
* @code
-
* PublishCallback cb;
-
* result = conn->Subscribe("NMSP.*.publish",&cb);
-
* if( result.isError() )
-
* // handle error
-
* @endcode
-
*
-
* @sa Conn::Subscribe(const char *subject, Callback *cb) @n
-
* Conn::Request(Msg *request, long timeout, Callback *cb)
-
*/
-
class API Callback
-
public:
-
virtual void CALL_TYPE OnMessage(Conn *conn, Msg *msg) = 0;
-
};
-
-
} //namespace NMSP {
-
-
#endif // Callback_h
Now, since my C# (or other .Net clients) developers will need to use the callback mechanism, I need to expose this through the managed layer. To do this, do I:
1. just create a class in managed c++ inheriting from callback? e.g.
class ManagedCallback : public Callback? Or,
2. do I create an unmanaged implementation, then create a managed C++ wrapper class for the implementation?
I have been struggling with this for awhile, so any help is greatly appreciated.
Thom
|
|
July 24th, 2008 01:32 PM
# 2
|
Re: Managed/Unmanaged C++ Inheritance
I don't think you can do 1 so I guess you better do 2.
You will need to read up on Delegates and Events which are the names .NET uses for its call back mechanisms. Delegates are the callback procedures and you register them with an event. Your class will then just raise an event and the users delegates will be called.
Not the answer you were looking for? Post your question . . .
190,475 Experts ready to help you find a solution.
Sign up for a free account, or
Login (if you're already a member).