Why and when to perform this task
To aid application development, WebSphere Application Server provides a valuetype library that contains C++ valuetype implementations for some commonly used Java classes in the java.lang, java.io, java.util, and javax.ejb packages. For example, Integer, Float, Vector, Exception, and so on. However, you might want to create your own C++ valuetypes.
The following steps describe how to create a C++ valuetype from an existing sample class called vttest.Book
Steps for this task
rmic -idl vttest.Book
idlc -mcpponly -mnohhguards -mdllname=vtlib_name -shh:uc -Iinclude-path vttest/Book.idl
idlc -mcpponly -mnohhguards -mdllname=vtlib_name -eih:ic -Iinclude-path vttest/Book.idl
class vttest_Book_Impl : virtual public ::vttest::Book, virtual public ::CORBA:: DefaultValueRefCountBasebecomes:
class vttest_Book_Impl : virtual public ::vttest::Book, virtual public OBV_vttest::Book, virtual public ::CORBA::DefaultValueRefCountBase
Note: The generated file <class>_C.cpp contains the syntax required for the class OBV_<module>::<class>.
For example:
class <module_<class>_factory : public ::<module>::<class>_init { public: virtual ; ::CORBA::ValueBase* create_for_unmarshal(); ::<module>::<class>* create(); ::CORBA::ValueBase* asValueBase(void* v); };For Book class, this becomes:
class vttest_Book_factory : public ::vttest::Book_init { public: virtual: ::CORBA::ValueBase* create_for_unmarshal(); ::vttest::Book* create(); ::CORBA::ValueBase* asValueBase(void* v); };
class java_lang_Boolean_factory : public java::lang::Boolean_init { public: virtual ::CORBA::ValueBase* create_for_unmarshal(); virtual java::lang::Boolean* create__CORBA_WStringValue(::CORBA::WStringValue* arg0); virtual java::lang::Boolean* create__boolean(::CORBA::Boolean arg0); virtual ::CORBA::ValueBase* asValueBase(void* v); };The <class>_init class defined in the generated <class>.hh file shows the create constructors that must be included in the <class>_factory for your class. (The create consructors above are specific to the Boolean valuetype class.) The class Book_init in Book.hh contains only one create constructor:
virtual Book* create ()=0;Therefore, only that create constructor is added to the class definition for vttest_Book_factory:
virtual ::vttest::Book* create();
virtual CORBA::ValueBase *create_for_unmarshal() { return new java_lang_Boolean_Impl(); } virtual java::lang::Boolean* create__boolean (CORBA::Boolean arg0) { java_lang_Boolean_Impl *ptr = new java_lang_Boolean_Impl(); ptr->value(arg0); return ptr; }The create_for_unmarshal() method creates a new <class>_Impl object and returns it. The create methods create new <class>_Impl classes and initialize them with appropriate values. (Your create methods must match those in your class.) Since the methods in the implementation file are not virtual, the virtual keyword must be deleted. In their completed form, the implementations for the Book class look like this:
::CORBA::ValueBase* vttest_Book_factory::create_for_unmarshal() { return new ::vttest_Book_Impl(); } ::vttest::Book* vttest_Book_factory::create() { return new ::vttest_Book_Impl(); }
::CORBA::ValueBase* <module>_<class>_factory::asValueBase(void* v) { return (::CORBA::ValueBase*)( (::<module>::<class>*) v ); }You must change the class name to match your own class. In the case of the Book class:
::CORBA::ValueBase* vttest_Book_factory::asValueBase(void* v) { return (::CORBA::ValueBase*)((::vttest::Book*)v); }
class vttest_Book_factoryInit { public : vttest_Book_factoryInit() { ::CORBA::ORB_ptr _vtlibOrb; int _argc = 0; ::CORBA::ValueFactoryBase_var retfact; ::CORBA::ValueFactoryBase_var fact; // Get access to the ORB. _vtlibOrb = ::CORBA::ORB_init(_argc, NULL, "DSOM"); // Create a Book factory. fact = new ::vttest_Book_factory(); // Register the factory. retfact = _vtlibOrb->register_value_factory((char *)::vttest::Book::Book_RID, fact.in() ); } }; // Static instantiation of the class. static vttest_Book_factoryInit __vttest_Book_factoryInit;
::<module>::<class>* create<class>() { static <module>_<class>_init *factory = NULL; if ( factory == NULL ) factory = (<module>::<class>_init*) ::com::ibm::ws::VtlUtil::getFactory(::<module>::<class>::<class>_RID); ::<module>::<class>* myPtr = factory->create(); return myPtr; }For the Book class, this is:
::vttest::Book* createBook() { static ::vttest::Book_init *factory = NULL; factory = (::vttest::Book_init*) ::com::ibm::ws::VtlUtil::getFactory(::vttest::Book::Book_RID); ::vttest::Book* bookPtr = factory->create(); return bookPtr; }
What to do next
You have now completed creation of your own C++ valuetype. Instances of this class can be created and used locally by your client. Because this class is a valuetype, it also can be serialized and sent to a server, where it can be used and returned to your client.