The Backdoor Initializer in C++

In Object Oriented languages there is no _wrapObj method exposed to the user. Instead, the same functionality is achieved simply by calling ``new'' on the Impl class. Interestingly, this means the constructor functionality is NOT placed in a Babel ctor method, but is, instead, actually in the default object constructor.

Here is the private data definition from wrapper_Data_Impl.hxx:

namespace wrapper { 
  class Data_impl : public virtual ::wrapper::Data 

....
  public:
    char* d_string;
    int d_int;
    char* d_ctorTest;

....
  };  // end class Data_impl
} // end namespace wrapper

Here is the client code from wraptest.cxx. Notice wrapper_Data_Impl is included.

#include "wrapper_User.hxx"
#include "wrapper_Data.hxx"
#include "wrapper_Data_Impl.hxx"

int main(int argc, char **argv) { 
  wrapper::Data_impl data;
  wrapper::User user = wrapper::User::_create();
  
  ASSERT( data.d_ctorTest == "ctor was run");
  
  /* Test the data setting*/
  user.accept(data);
  
  ASSERT( data.d_string == "Hello World!");
  ASSERT( data.d_int == 3);
  return 0;
}

Finally, the Impl code from wrapper_Data_Impl.cxx, notice where the constructor code is placed.

// speical constructor, used for data wrapping(required).  
// Do not put code here unless you really know what you're doing!
wrapper::Data_impl::Data_impl() : StubBase(reinterpret_cast< 
  void*>(::wrapper::Data::_wrapObj(this)),false) , _wrapped(true) { 
  // DO-NOT-DELETE splicer.begin(wrapper.Data._ctor2)
  d_ctorTest = "ctor was run";
  // DO-NOT-DELETE splicer.end(wrapper.Data._ctor2)
}

void wrapper::Data_impl::setString_impl (
  /* in */const ::std::string& s ) {
  // DO-NOT-DELETE splicer.begin(wrapper.Data.setString)
  d_string = "Hello World!";
  // DO-NOT-DELETE splicer.end(wrapper.Data.setString)
}

void wrapper::Data_impl::setInt_impl (
  /* in */int32_t i ) 
{
  // DO-NOT-DELETE splicer.begin(wrapper.Data.setInt)
  d_int = 3;
  // DO-NOT-DELETE splicer.end(wrapper.Data.setInt)
}





babel-1.4.0
users_guide Last Modified 2008-10-16

http://www.llnl.gov/CASC/components
components@llnl.gov