Primitive Data Types
|
Strings
|
Constants
|
Enumerations
|
Type Definitions
|
Modules
|
Complex Data Types
|
Primitive Data Types
The basic data types provided by the Interface Definition Language are summarized in the table below. Due to hardware differences between platform, some of the IDL primitive data types have a definition that is marked "platform dependent." On a platform that has 64-bit integral representations, for example, the CORBA::Long
type would still be only 32 bits. You should consult the include file orbtypes.h for an exact mapping of these primitive data types for your particular platform.
Caution The IDL boolean type is defined by the CORBA specification to have only one of two values: 1 or 0. Using other values for a boolean will result in undefined behavior.
char *
. You must use the functions shown ibelow for dynamically allocating strings to ensure that your applications and ISB for C++ use the same memory management facilities. All CORBA string types are null-terminated.
Methods for allocating and freeing memory for strings.
classCORBA
{
...
static char*string_alloc
(CORB::ULong len);
static voidstring_free
(char *data);
...
};
Table 10.2 CORBA string allocation and release methods.
String_var Class
In addition to mapping an IDL string
to a char *
, the IDL to C++ compiler generates a String_var
class that contains a pointer to the memory allocated to hold the string. When a String_var object is destroyed or goes out of scope, the memory allocated to the string is automatically freed. The following code listing shows the String_var
class and the methods it supports. For detailed information on the _var classes, see "The _var Class".
The String_var
class.
class
CORBA
{
class String_var
{
protected:
char *_p
;
...
public:
String_var
();
String_var
(char *p);
~String_var
();
String_var& operator=
(const char *p);
String_var& operator=
(char *p);
String_var& operator=
(const String_var& s);
operator const char *
() const;
operator char *
();
char &operator[]
(CORBA::ULong index);
char operator[]
(CORBA::ULong index) const;
friend ostream& operator<<(ostream&, const String_var& );
inline friend Boolean operator==
(const String_var& s1, const String_var& s2);
...
};
...
}; Constants
The following code listing shows how IDL constants defined outside of any interface specification will be mapped directly to a C++ constant declaration.
Top-level constant definitions in IDL and the resulting C++ code.
// These top-level definitions in IDL
The code listing below shows how constants defined within an interface specification are declared in the include file and assigned a value in the source file.
Constant definitions within an interface specification and the resulting C++ code.
const string str_example = "this is an example";
const long long_example = 100;
const boolean bool_example = TRUE;
...
// Result in the generation of this C++ code
const char * str_example = "this is an example";
const CORBA::Long long_example = 100;
const CORBA::Boolean bool_example = 1;// These definitions are in the IDL file
Under some circumstances, the IDL compiler must generate C++ code containing the value of an IDL constant rather than the name of the constant. The following code example shows how the value of the constant example.idl
interface example {
const string str_example = "this is an example";
const long long_example = 100;
const boolean bool_example = TRUE;
};
...
// Result in the generation of this C++ code in example_client.hh
class example :: public virtual CORBA::Object
{
...
static const char * str_example; /* "this is an example" */
static const CORBA::Long ong_example; /* 100 */
static const CORBA::Boolean bool_example; /* 1 */
...
};
...
// And the generation of this C++ code in example_client.cc
const char * example::str_example = "this is an example";
const CORBA::Long example::long_example = 100;
const CORBA::Boolean example::bool_example = 1;len
must be generated for the typedef V
to allow the C++ code to compile properly.
Sometimes the value of an IDL constant must be generated in C++.
// IDL
interface foo {
const long length = 10;
typedef long V[length];
};
...
// Results in this C++ code being generated by the IDL compiler
class foo : public virtual CORBA::Object
{
const CORBA::Long length;
typedef CORBA::Long V[10];
}; Enumerations
The following code listing shows how enumerations in IDL map directly to C++ enumerations.
Enumerations in IDL map directly to C++.
// IDL
enum enum_type {
first,
second,
third
};
// Results in this C++ code
enum enum_type {
first,
second,
third
}; Type Definitions
The following code listing shows how type definitions in IDL are mapped directly to C++ type definitions. If the original IDL type definition maps to several C++ types, the IDL compiler generates the corresponding aliases for each type in C++.
The mapping of simple type definitions from IDL to C++
// IDL
The code listings below show other type definition mapping examples.
Mapping an IDL interface type definition to C++.
typedef octet example_octet;
typedef enum enum_values {
first,
second,
third
} enum_example;
// Results in the generation of this C++ code
typedef octet example_octet;
enum enum_values {
first,
second,
third
};
typedef enum_values enum_example;// IDL
Mapping an IDL sequence type definition to C++.
interface A1;
typedef A1 A2;
...
// Results in the generation of this C++ code
class A1;
typedef A1 *A1_ptr;
typedef A1_ptr A1Ref;
class A1_var;
typedef A1 A2;
typedef A1_ptr A2_ptr;
typedef A1Ref A2Ref;
typedef A1_var A2_var;// IDL
For more information, see "The _var Class".
typedef sequence<long> S1;
typedef S1 S2;
...
// Results in the generation of this C++ code
class S1;
typedef S1 *S1_ptr;
typedef S1_ptr S1Ref;
class S1_var;
typedef S1 S2;
typedef S1_ptr S2_ptr;
typedef S1Ref S2Ref;
typedef S1_var S2_var;
Modules
The OMG IDL to C++ language mapping specifies that an IDL module
should be mapped to a C++ namespace
with the same name. Since few compilers currently support the namespace,
the C++ language mapping allows the use of class
in its place. The following code example shows how ISB for C++'s IDL compiler maps module
to class
.
Mapping an IDL module
to a C++ class.
// IDL
module ABC
{
// Definitions
...
};
...
// Results in the generation of this C++ codeclass ABC
{
// Definitions
...
}; Complex Data Types
The C++ mappings for IDL structures, unions, sequences and arrays depend on whether or not the data members they contain are of a fixed or variable length. These types are considered to have variable lengths and, consequently, any complex data type that contains them will also have a variable length.
string
type, bounded or unbounded.
sequence
type, bounded or unbounded.
structures
or unions
that contain a variable-length member.
Table 10.3 Summary of the C++ mappings for complex data types.
IDL type | C++ mapping |
---|---|
struct (fixed length) | struct |
struct (variable length) | struct (_var types for variable length members) |
union | class and _var class |
sequence | class and _var class |
array | array |
Fixed-length Structures
The code example below shows how fixed-length structures in IDL are mapped to C++ code. In addition to the structure, ISB for C++'s IDL compiler will also generate an example_var class for the structure. For more information, see "The _var Class".
Mapping fixed-length a IDL structure to C++.
// IDL
The following code example shows how you might use the struct and class.
Use of the
struct example
{
short a;
long b;
};
...
// Results in the generation of this C++ code.
struct example
{
CORBA::Short a;
CORBA::Long b;
};
class example_var
{
...
private:
example
*_ptr;
};example
structure and the example_var
class.
// Declare an example struct and initialize its fields.
To access the fields of the
example ex1
= { 2, 5 };
// Declare a _var class and assign it to a newly created example structure.
// This results in the _ptr pointing to an allocated struct with
// uninitialized fields.
example_var ex2
= new example;
// Initialize the fields of ex2 from ex1ex2->a = ex1.b;
_var
class ex2
, the ->
operator must always be used. When ex2
goes out of scope, the memory allocated to it will be freed automatically.
Variable Length Structures
The following code example shows how you could modify the example
structure, replacing the long
member with a string
and adding an object reference, to change to a variable-length structure.
Mapping a variable-length structure to C++.
// IDL
Notice how the
interface ABC
{
// Definitions ...
};
struct vexample
{
short a;
ABC
c;
string name;
};
...
// Results in the generation of this C++ code
struct vexample
{
CORBA::Short a;
ABC_var
c;
CORBA::String_var
name;
vexample& operator=
(const vexample& s);
};
class vexample_var
{
...
};ABC
object reference is mapped to an ABC_var
class. In a similar fashion, the string name
is mapped to a CORBA::String_var
class. In addition, an assignment operator is also generated for variable-length structures.
Memory Management for Structures
The use of _var classes in variable-length structures ensures that memory allocated to the variable-length members are managed transparently.
union
is mapped to a C++ class
with methods for setting and retrieving the value of the data members. A data member named _d
of the discriminant type is also defined. The value of this discriminant is not set when the union is first created, so an application must set it before using the union. Setting any data member using one of the provided methods automatically sets the discriminant.
Mapping an IDL union to a C++ class.
// IDLThe following table describes some of the methods in the un_ex class.
structst_ex
{
long abc;
};
unionun_ex
switch(long)
{
case 1: long x; // a primitive data type
case 2: string y; // a simple data type
case 3:st_ex
z; // a complex data type
};
...
// Results in the generation of this C++ code
structst_ex
{
CORBA::Long abc;
};
classun_ex
{
private:
CORBA::Long_d
;
CORBA::Long_x
;
CORBA::String_var_y
;
st_ex
_z
;
public:
un_ex
();
~un_ex();
un_ex
(const un_ex& obj);
un_ex&operator=
(const un_ex& obj);
void_d
(CORBA::Long val);
CORBA::Long_d
() const;
voidx
(CORBA::Long val);
CORBA::Longx
() const;
voidy
(char *val);
voidy
(const char *val);
voidy
(const CORBA::String_var& val);
const char*y
() const;
const st_ex&z
() const;
st_ex&z
();
...
};
The IDL to C++ compiler may also generate hash and compare methods for unions, which you can control with compiler options. See the Netscape Internet Service Broker for C++ Reference Guide for more information on compiler options.
Managed Types for Unions
In addition to the un_ex
class shown in the code example above, a un_ex_var
class would also be generated. See "The _var Class" for details on the _var
classes.
Memory Management for Unions
Here are some important points to remember about memory management of complex data types within a union:
char *
accessor method will free any storage before ownership of the passed pointer is assumed.
NOTE: When the length of an unbounded sequence exceeds the maximum length you specify, ISB for C++ will transparently allocate a larger buffer, copy the old buffer to the new buffer and free the memory allocated to the old buffer. However, no attempt will be made to free any unused memory if the maximum length decreases.Mapping an IDL unbounded sequence to a C++ class.
// IDLtypedef sequence<long> LongSeq;
...
// Results in the generation of this C++ code
classLongSeq
{
public:
LongSeq
(CORBA::ULong max=0);
LongSeq
(CORBA::ULong max=0, CORBA::ULong length,
CORBA::Long *data, CORBA::Boolean release = 0);
LongSeq
(const LongSeq&);
~LongSeq
();
LongSeq&operator=
(const LongSeq&);
CORBA::ULongmaximum
() const;
voidlength
(CORBA::ULong len);
CORBA::ULonglength
() const;
const CORBA::ULong&operator[]
(CORBA::ULong index) const;
...
static LongSeq*_duplicate
(LongSeq* ptr);
static void_release
(LongSeq *ptr);
static CORBA::Long*allocbuf
(CORBA::ULong nelems);
static voidfreebuf
(CORBA::Long *data);
private:
CORBA::Long *_contents;
CORBA::ULong _count;
CORBA::ULong _num_allocated:
CORBA::Boolean _release_flag;
CORBA::Long _ref_count;
};
Table 10.5 The LongSeq methods.
Managed Types for Sequences
In addition to the LongSeq
class, a LongSeq_var
class would also be generated. See "The _var Class" for details on the _var
classes. In addition to the usual _var methods, there are two indexing methods defined for sequences.
The two indexing methods added for _var classes representing sequences.
CORBA::Long&
operator[]
(CORBA::ULong index);
const CORBA::Long& operator[]
(CORBA::ULong idex) const ; Memory Management for Sequences
You should carefully consider the memory management issues listed below. The code example below contains sample C++ code that illustrates these points.
allocbuf
and freebuf
to create and free storage used with sequences.// Given this IDL specification for a bounded sequence
typedef sequence<string, 3> String_seq;
...
// Consider this C++ code
char *static_array[] = ("1", "2", "3"};
char *dynamic_array = StringSeq::allocbuf(3);
// Create a sequence, release flag is set to FALSE
by default
StringSeq static_seq(3, static_array);
// Create another sequence, release flag set to TRUE
StringSeq dynamic_seq(3, dynamic_array, 1);
static_seq[1] = "1"; // old memory not
freed, no copying occurs
char *str = string_alloc(2);
dynamic_seq[1] = str; // old memory is freed, no copying occurs
_var
. The following code example shows three arrays with different element types.
Mapping IDL arrays to C++ arrays.
// IDLThe use of the managed type, _var, for strings and object references, allows memory to be managed transparently when array elements are assigned.
interfaceIntf
{
// definitions...
};
typedeflong
L[10];
typedefstring
S[10];
typedefIntf
A[10];
...
// Results in the generation of this C++ code
typedefCORBA::Long
L[10];
typedefCORBA::String_var
S[10];
typedefIntf_var
A[10];
_slice
type for arrays that contains all but the first dimension of the array. The array _slice
type provides a convenient way to pass and return parameters. The following code example shows two examples of the _slice
type.
The _slice
type.
// IDL
typedeflong
L[10];
typedefstring
str[1][2][3];
...
// Results in the generation of these slicestypedef CORBA::Long L_slice[10];
typedef CORBA::String_var str_slice[2][3];
typedef str_slice *str_slice_ptr;
_var
class. This class offers some additional features for array.
operator[]
is overloaded to provide intuitive access to array elements.
_slice
object as an argument._var
class generated for arrays.
// IDL
typedef long L
[10];
...
// Results in the generation of this C++ code
class L_var
{
public:
L_var
();
L_var
(L_slice *slice);
L_var
(const L_var& var);
~L_var
();
L_var& operator=
(L_slice *slice);
L_var& operator=
(const L_var& var);
CORBA::Long& operator[]
(CORBA::ULong index);
operator L_slice *
();
operator L &
() const;
...
private:
L_slice *_ptr;
};
_forany
class is generated to handle arrays with elements mapped to the type any
. As with the _var
class, the _forany
class allows you to access the underlying array type. The _forany
class does not release any memory upon destruction because the any type maintains ownership of the memory. The _forany
class is not implemented as a typedef
because it must be distinguishable from other types for overloading to functions properly.
The _forany
class generated for an IDL array.
// IDL
typedef longL
[10];
...
// Results in the generation of this C++ code
classL_forany
{
public:
L_forany
();
L_forany
(L_slice *slice);
~L_forany
();
CORBA::Long&operator[]
(CORBA::ULong index);
const CORBA::Long&operator[]
(CORBA::ULong index) const;
operator L_slice *
();
operator L &
() const;
operator const L &
() const;
operator const L&
() const;
L_forany& operator=
(const L_forany obj);
...
private:
L_slice *_ptr;
};
new
and delete
operators.
Methods generated for allocating and releasing array memory.
// IDL
typedef long L[10];
...
// Results in the generation of this C++ code
inline L_slice*L_alloc
(); // Dynamically allocates array. Returns
// NULL on failure.
inline voidL_free
(L_slice *data); // Releases array memory allocated with
// L_alloc.
Last Updated: 02/03/98 15:34:25