C components that are wrappers for C++ classes

Since methods in a C component must be implemented as C functions, you must code C wrappers for C++ classes.

NoteEAServer provides direct support for running C++ classes as components, as described in Chapter 14, “Creating CORBA C++ Components.” Sybase supports the technique described here, but recommends that you create a C++ component to run your C++ classes directly.

The procedure for creating a wrapper for a C++ class is as follows:

  1. Code a C create function that instantiates the C++ object and stores the object reference as instance-specific data. For example, if the C++ object is StockTrade, create could be implemented as follows:

    CS_RETCODE CS_PUBLIC create() {
    
      StockTrade *st_ref;
    
      /*
      ** Create an instance of the C++ 
      ** StockTrade object.
      */
      st_ref = new StockTrade();
    
      /* 
      ** Associate it with the EAServer component
      ** instance.
      */
      if (JagSetInstanceData((CS_VOID *)st_ref) 
          != CS_SUCCEED) 
      {
          return CS_FAIL;
      }
    
      return CS_SUCCEED;
    
    }
    
  2. For each C++ method, code a C wrapper function that retrieves the C++ object reference and uses it to call the C++ method. For example, the following shows a C wrapper to call a StockTrade::buyStock C++ method:

    CS_RETCODE CS_PUBLIC buyStock (
          CS_CHAR  *ticker, 
          CS_INT   n_desired, 
          CS_INT   n_bought) 
    {
      StockTrade *st_ref;
      if (JagGetInstanceData((CS_VOID *)&st_ref) 
          != CS_SUCCEED)
      { 
        return CS_FAIL;
      }
      st_ref::buyStock(ticker, n_desired,
                              n_bought);
      return;
    }
    
  3. Code a destroy function that retrieves the C++ object reference and destroys it. For example:

    CS_RETCODE CS_PUBLIC destroy() {
      StockTrade *st_ref;
      if (JagGetInstanceData((CS_VOID *)&st_ref) 
          != CS_SUCCEED)
      { 
        return CS_FAIL;
      }
    
      delete st_ref;
      return CS_SUCCEED;
    
    }
    
  4. Define the EAServer C component to include the C wrapper functions as its methods.