VisiBroker for C++ Developer’s Guide : Using valuetypes

Using valuetypes
This section explains how to use the valuetype IDL type in VisiBroker.
Understanding valuetypes
The valuetype IDL type is used to pass state data over the wire. A valuetype is best thought of as a struct with inheritance and methods. Valuetypes differ from normal interfaces in that they contain properties to describe the valuetype's state, and contain implementation details beyond that of an interface.
Valuetype IDL code sample
The following IDL code declares a simple valuetype:
module Map {
valuetype Point {
public long x;
public long y;
private string label;
factory create (in long x, in long y, in string z);
void print();
};
};
Valuetypes are always local. They are not registered with the VisiBroker ORB, and require no identity, as their value is their identity. They can not be called remotely.
Concrete valuetypes
Concrete valuetypes contain state data. They extend the expressive power of IDL structs by allowing:
Valuetype derivation
You can derive a concrete valuetype from one other concrete valuetype. However, valuetypes can be derived from multiple other abstract valuetypes.
Sharing semantics
Valuetype instances can be shared by other valuetypes across or within other instances. Other IDL data types such as struct, union, or sequence cannot be shared. Valuetypes that are shared are isomorphic between the sending context and the receiving context.
In addition, when the same valuetype is passed into an operation for two or more arguments, the receiving context receives the same valuetype reference for both arguments.
Null semantics
Null valuetypes can be passed over the wire, unlike IDL data types such as structs, unions, and sequences. For instance, by boxing a struct as a boxed valuetype, you can pass a null value struct. For more information, go to “Boxed valuetypes”.
Factories
Factories are methods that can be declared in valuetypes to create valuetypes in a portable way. For more information on Factories, go to “Implementing factories”.
Abstract valuetypes
Abstract valuetypes contain only methods and do not have state. They may not be instantiated. Abstract valuetypes are a bundle of operation signatures with a purely local implementation.
For instance, the following IDL defines an abstract valuetype Account that contains no state, but one method, get_name:
abstract valuetype Account{
string get_name();
}
Now, two valuetypes are defined that inherit the get_name method from the abstract valuetype:
valuetype savingsAccount:Account{
private long balance;
}
valuetype checkingAccount:Account{
private long balance;
}
These two valuetypes contain a variable balance, and they inherit the get_name method from the abstract valuetype Account.
Implementing valuetypes
To implement valuetypes in an application, do the following:
1
2
3
4
Implement the Factory class to implement any factory methods defined in IDL.
5
Implement the create_for_unmarshal method.
6
7
Either implement the _add_ref, _remove_ref, and _ref_countvalue methods or derive from CORBA::DefaultValueRefCountBase.
Defining your valuetypes
In the IDL sample (for more information, see “Valuetype IDL code sample”), you define a valuetype named Point that defines a point on a graph. It contains two public variables, the x and y coordinates, one private variable that is the label of the point, the valuetype's factory, and a print method to print the point.
Compiling your IDL file
When you have defined your IDL, compile it using idl2cpp to create source files. You then modify the source files to implement your valuetypes.
If you compile the IDL shown in “Valuetype IDL code sample”, your output consists of the following files:
Inheriting the valuetype base class
After compiling your IDL, create your implementation of the valuetype. The implementation class will inherit the base class. This class contains the constructor that is called in your ValueFactory, and contains all the variables and methods declared in your IDL.
In the obv\PointImpl.java, the PointImpl class extends the Point class, which is generated from the IDL.
Inheriting the valuetype base class:
class PointImpl : public Map::OBV_Point, public CORBA::DefaultValueRefCountBase {
public:
PointImpl(){}
virtual ~PointImpl(){}
CORBA_ValueBase* _copy_value() {
return new PointImpl(x(), y(), new Map::Label(
CORBA::string_dup(label())));
}

PointImpl( CORBA::Long x, CORBA::Long y, Map::Label_ptr label )
: OBV_Point( x,y,label->_boxed_in())
{}
virtual void print() {
cout << "Point is [" << label() << ": ("
<< x() << ", " << y() << ")]" << endl << endl;
}
};
Implementing the Factory class
When you have created an implementation class, implement the Factory for your valuetype.
In the following example, the generated Point_init class contains the create method declared in your IDL. This class extends CORBA::ValueFactoryBase. The PointDefaultFactory class implements PointValueFactory as shown in the following example.
class PointFactory: public CORBA::ValueFactoryBase {
public:
PointFactory(){}
virtual ~PointFactory(){}
CORBA::ValueBase* create_for_unmarshal() {
return new PointImpl();
}
};
Point_init contains a public method, create_for_unmarshal, that is output as a pure virtual method in Map_c.hh. You must derive a class from Point_init and implement the create_for_unmarshal method to produce the Factory class. When you compile your IDL file, it does not create a skeleton class for this.
Registering your Factory with the VisiBroker ORB
To register your Factory with the VisiBroker ORB, call ORB::register_value_factory. For more information on registering Factories, go to “Registering valuetypes”.
Implementing factories
When the VisiBroker ORB receives a valuetype, it must first be demarshaled, and then the appropriate factory for that type must be found in order to create a new instance of that type. Once the instance has been created, the value data is unmarshaled into the instance. The type is identified by the RepositoryID that is passed as part of the invocation. The mapping between the type and the factory is language specific.
VisiBroker version 4.5 or later version will generate the correct signatures for either the JDK 1.3 or JDK 1.4 default value factory method. Existing (version 4.0) generated code is not designed to run under JDK 1.3, unless you modify the default value factory method signature as shown below. If you use your existing code with JDK 1.3 and do not modify default value factory, the code will not compile or will throw a NO_IMPLEMENT exception. Consequently, we recommend that you regenerate your code to generate the correct signatures.
The following code sample shows how you should modify the default value factory method signature to make sure that it compiles under JDK 1.3:
class PointFactory: public CORBA::ValueFactoryBase
{
public:
PointFactory(){}
virtual ~PointFactory(){}
CORBA::ValueBase* create_for_unmarshal() {
return new PointImpl();
}
};
Factories and valuetypes
When the VisiBroker ORB receives a valuetype, it will look for that type's factory. It will look for a factory named <valuetype>DefaultFactory. For instance, the Point valuetype's factory is called PointDefaultFactory. If the correct factory doesn't conform to this naming schema (<valuetype>DefaultFactory), you must register the correct factory so the VisiBroker ORB can create an instance of the valuetype.
If the VisiBroker ORB cannot find the correct factory for a given valuetype, a MARSHAL exception is raised, with an identified minor code.
Registering valuetypes
Each language mapping specifies how and when registration occurs. If you created a factory with the <valuetype>DefaultFactory naming convention, this is considered implicitly registering that factory, and you do not need to explicitly register your factory with the VisiBroker ORB.
To register a factory that does not conform to the <valuetype>DefaultFactory naming convention, call register_value_factory. To unregister a factory, call unregister_value_factory on the VisiBroker ORB. You can also lookup a registered valuetype factory by calling lookup_value_factory on the VisiBroker ORB.
Boxed valuetypes
Boxed valuetypes allow you to wrap non-value IDL data types as valuetypes. For example, the following IDL boxed valuetype declaration,
valuetype Label string;
is equivalent to this IDL valuetype declaration:
valuetype Label{
public string name;
}
By boxing other data types as valuetypes, it allows you to use valuetype's null semantics and sharing semantics.
Valueboxes are implemented purely with generated code. No user code is required.
Abstract interfaces
Abstract interfaces allow you to choose at runtime whether the object will be passed by value or by reference.
Abstract interfaces differ from IDL interfaces in the following ways:
Abstract interfaces do not implicitly derive from CORBA::Object because they can represent either object references or valuetypes. Valuetypes do not necessarily support common object reference operations. If the abstract interface can be successfully narrowed to an object reference type, you can invoke the operations of CORBA::Object.
For example, examine the following abstract interface.
abstract interface ai{
};
interface itp : ai{
};
valuetype vtp supports ai{
};
interface x {
void m(ai aitp);
};
valuetype y {
void op(ai aitp);
};
For the argument to method m:
itp is always passed as an object reference.
vtp is passed as a value.
Custom valuetypes
By declaring a custom valuetype in IDL, you bypass the default marshaling and unmarshaling model and are responsible for encoding and decoding.
custom valuetype customPoint{
public long x;
public long y;
private string label;
factory create(in long x, in long y, in string z);
};
You must implement the marshal and unmarshal methods from the CustomMarshal interface.
When you declare a custom valuetype, the valuetype extends CORBA::CustomValue, as opposed to CORBA::StreamableValue, as in a regular valuetype. The compiler doesn't generate read or write methods for your valuetype.
You must implement your own read and write methods by using CORBA::DataInputStream and CORBA::DataOutputStream to read and write the values, respectively.
Truncatable valuetypes
Truncatable valuetypes allow you to treat an inherited valuetype as its parent.
The following IDL defines a valuetype checkingAccount that is inherited from the base type Account and can be truncated in the receiving object.
valuetype checkingAccount: truncatable Account{
private long balance;
}
This is useful if the receiving context doesn't need the new data members or methods in the derived valuetype, and if the receiving context isn't aware of the derived valuetype. However, any state data from the derived valuetype that isn't in the parent data type will be lost when the valuetype is passed to the receiving context.
Note
You cannot make a custom valuetype truncatable.