Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Managed/Unmanaged C++ Inheritance

Question posted by: thedbflow (Newbie) on July 24th, 2008 01:15 PM
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:

Expand|Select|Wrap|Line Numbers
  1. #ifndef Callback_h
  2. #define Callback_h
  3.  
  4. namespace NMSP {
  5.  
  6. class Conn;
  7. class Msg;
  8. /** @class Callback
  9.  * @brief This class is the abstract base class for received message callbacks. 
  10.  * A user created class, derrived from this class, can be passed into 
  11.  * %Subscribe() and %Request() to have user code executed asynchronously
  12.  * when a message is received.
  13.  *
  14.  * Example Callback class:
  15.  * @code
  16.  * class PublishCallback : public Callback
  17.  * {
  18.  *    public:
  19.  *    virtual void CALL_TYPE OnMessage(Conn *conn, Msg *msg)
  20.  *    {
  21.  *        const char *tmp;
  22.  *        msg->ToXML(tmp);
  23.  *        cout << tmp << endl;
  24.  *    }
  25.  * };
  26.  * @endcode
  27.  *
  28.  * Example Callback registration:
  29.  * @code
  30.  * PublishCallback cb;
  31.  * result = conn->Subscribe("NMSP.*.publish",&cb);
  32.  * if( result.isError() )
  33.  *    // handle error
  34.  * @endcode
  35.  *
  36.  * @sa Conn::Subscribe(const char *subject, Callback *cb) @n
  37.  *     Conn::Request(Msg *request, long timeout, Callback *cb)
  38. */          
  39. class API Callback
  40. public:
  41.     virtual void CALL_TYPE OnMessage(Conn *conn, Msg *msg) = 0;
  42. };
  43.  
  44. } //namespace NMSP {
  45.  
  46. #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
Banfa's Avatar
Banfa
AdministratorVoR
5,052 Posts
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.

Reply
Reply
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).

Latest Articles: Read & Comment
Top C / C++ Forum Contributors