14
14.1.2 Constructed Data Types
A language mapping must define the means of expressing the constructed data types defined in Section 3.8.2, "Constructed Types," on page 3-22. The ORB defines aggregates of basic data types that are supported, but the language mapping defines how a programmer sees those aggregates. For example, the C mapping might define an OMG IDL struct as a C struct, whereas the LISP mapping might define an OMG IDL struct as a list. The mapping must specify the means to construct and operate on these data types in the programming language.
14.1.3 Constants
OMG IDL definitions may contain named constant values that are useful as parameters for certain operations. The language mapping should provide the means to access these constants by name. 14.1.4 Objects
There are two parts of defining the mapping of ORB objects to a particular language. The first specifies how an object is represented in the program and passed as a parameter to operations. The second is how an object is invoked. The representation of an object reference in a particular language is generally opaque, that is, some language-specific data type is used to represent the object reference, but the program does not interpret the values of that type. The language-specific representation is independent of the ORB representation of an object reference, so that programs are not ORB-dependent. In an object-oriented programming language, it may be convenient to represent an ORB object as a programming language object. Any correspondence between the programming language object types and the OMG IDL types including inheritance, operation names, etc., is up to the language mapping. 14.1.5 Invocation of Operations
An operation invocation requires the specification of the object to be invoked, the operation to be performed, and the parameters to be supplied. There are a variety of possible mappings, depending to a large extent on the procedure mechanism in the particular language. Some possible choices for language mapping of invocation include: interface-specific stub routines, a single general-purpose routine, a set of calls to construct a parameter list and initiate the operation, or mapping ORB operations to operations on objects defined in an object-oriented programming language. 14.1.6 Exceptions
There are two aspects to the mapping of exceptions into a particular language. First is the means for handling an exception when it occurs, including deciding which exception occurred. If the programming language has a model of exceptions that can accommodate ORB exceptions, that would likely be the most convenient choice; if it does not, some other means must be used, for example, passing additional parameters to the operations that receive the exception status. 14.1.7 Attributes
The ORB models attributes as a pair of operations, one to set and one to get the attribute value. The language mapping defines the means of expressing these operations. One reason for distinguishing attributes from pairs of operations is to allow the language mapping to define the most natural way for accessing them. Some possible choices include defining two operations for each attribute, defining two operations that can set or get, respectively, any attribute, defining operations that can set or get groups of attributes, and so forth. 14.1.8 ORB Interfaces
Most of a language mapping is concerned with how the programmer-defined objects and data are accessed. Programmers who use the ORB must also access some interfaces implemented directly by the ORB, for example, to convert an object reference to a string. A language mapping must also specify how these interfaces appear in the particular programming language. 14.2 Scoped Names
The C programmer must always use the global name for a type, constant, exception, or operation. The C global name corresponding to an OMG IDL global name is derived by converting occurrences of "::" to "_" (an underscore) and eliminating the leading underscore.interface example0 {
enum color {red, green, blue};
union bar switch (enum foo {room, bell}) { ... };
· · ·
};
switch (myUnion._d) {interface foo {
typedef short bar; /* A legal OMG IDL statement, but ambiguous in C */
· · ·
}; 14.3 Mapping for Interfaces
All interfaces must be defined at global scope (no nested interfaces). The mapping for an interface declaration is as follows:
};
typedef CORBA_Object example1; /* C */
OMG IDL permits specifications in which arguments, return results, or components of constructed types may be interface references. Consider the following example:
interface example2 {
example1 op2();
}; 14.4 Inheritance and Operation Names
OMG IDL permits the specification of interfaces that inherit operations from other interfaces. Consider the following example: void op3(in long arg3, out long arg4);
}; 14.5 Mapping for Attributes
The mapping for attributes is best explained through example. Consider the following specification: struct position_t {
float x, y;
};
attribute float radius;
readonly attribute position_t position;
}; struct position_t {
float x, y;
};
float _get_radius();
void _set_radius(in float r);
position_t _get_position();
};
14.6 Mapping for Constants
Constant identifiers can be referenced at any point in the user's code where a literal of that type is legal. In C, these constants are #defined. 14.7 Mapping for Basic Data Types
The basic data types have the mappings shown in Table 19 on page 14-8. Implementations are responsible for providing typedefs for CORBA_short, CORBA_long, and so forth. consistent with OMG IDL requirements for the corresponding data types.
The C mapping of the OMG IDL boolean types is unsigned char with only the values 1 (TRUE) and 0 (FALSE) defined; other values produce undefined behavior. CORBA_boolean is provided for symmetry with the other basic data type mappings.
TypeCodes are described in Section 6.7, "TypeCodes," on page 6-33. The _value member for an any is a pointer to the actual value of the datum.
The any type supports the notion of ownership of its _value member. By setting a release flag in the any when a value is installed, programmers can control ownership of the memory pointed to by _value. The location of this release flag is implementation-dependent, so the following two ORB-supplied functions allow for the setting and checking of the any release flag:
CORBA_boolean CORBA_any_get_release(CORBA_any*); 14.8 Mapping Considerations for Constructed Types
The mapping for OMG IDL structured types (structs, unions, arrays, and sequences) can vary slightly depending on whether the data structure is fixed-length or variable-length. A type is variable-length if it is one of the following types:
The mapping of a variable-length type as an out parameter or operation return value is a pointer to the associated class or array, as shown in Table 20 on page 14-19.
For types whose parameter passing modes require heap allocation, an ORB implementation will provide allocation functions. These types include variable-length struct, variable-length union, sequence, any, string, and array of a variable-length type. The return value of these allocation functions must be freed using CORBA_free(). For one of these listed types T, the ORB implementation will provide the following type-specific allocation function:
CORBA_any *CORBA_any_alloc();
char *CORBA_string_alloc();
union Foo switch (long) {
case 1: long x;
case 2: float y;
default: char z;};
This is equivalent to the following struct in C:
typedef struct { /* C */
CORBA_long _d;
union {
CORBA_long x;
CORBA_float y;
CORBA_char z;
} _u;
} Foo;
The discriminator in the struct is always referred to as _d; the union in the struct is always referred to as _u.
Reference to union elements is as in normal C:
Foo *v; /* C */
/* make a call that returns a pointer to a Foo in v */
case 1: printf("x = %ld\n", v->_u.x); break;
case 2: printf("y = %f\n", v->_u.y); break;
default: printf("z = %c\n", v->_u.z); break;
}
An ORB implementation need not use a C union to hold the OMG IDL union elements; a C struct may be used instead. In either case, the programmer accesses the union elements via the _u member.
typedef sequence<long,10> vec10;
In C, this is converted to:
typedef struct { /* C */
CORBA_unsigned_long _maximum;
CORBA_unsigned_long _length;
CORBA_long *_buffer;
} vec10;
An instance of this type is declared as follows:
vec10 x = {10L, 0L, (CORBA_long *)NULL); /* C */
Prior to passing &x as an in parameter, the programmer must set the _buffer member to point to a CORBA_long array of 10 elements, and must set the _length member to the actual number of elements to transmit.
Prior to passing the address of a vec10* as an out parameter (or receiving a vec10* as the function return), the programmer does nothing. The client stub will allocate storage for the returned sequence; for bounded sequences, it also allocates a buffer of the specified size, while for unbounded sequences, it also allocates a buffer big enough to hold what was returned by the object. Upon successful return from the invocation, the _maximum member will contain the size of the allocated array, the _buffer member will point at allocated storage, and the _length member will contain the number of values that were returned in the _buffer member. The client is responsible for freeing the allocated sequence using CORBA_free().
Prior to passing &x as an inout parameter, the programmer must set the _buffer member to point to a CORBA_long array of 10 elements. The _length member must be set to the actual number of elements to transmit. Upon successful return from the invocation, the _length member will contain the number of values that were copied into the buffer pointed to by the _buffer member. If more data must be returned than the original buffer can hold, the callee can deallocate the original _buffer member using CORBA_free() (honoring the release flag) and assign _buffer to point to new storage.
For bounded sequences, it is an error to set the _length or _maximum member to a value larger than the specified bound.
Sequence types support the notion of ownership of their _buffer members. By setting a release flag in the sequence when a buffer is installed, programmers can control ownership of the memory pointed to by _buffer. The location of this release flag is implementation-dependent, so the following two ORB-supplied functions allow for the setting and checking of the sequence release flag:
void CORBA_sequence_set_release(void*, CORBA_boolean); /* C */
CORBA_sequence_set_release can be used to set the state of the release flag. If the flag is set to TRUE, the sequence effectively "owns" the storage pointed to by _buffer; if FALSE, the programmer is responsible for the storage. If, for example, a sequence is returned from an operation with its release flag set to FALSE, calling CORBA_free() on the returned sequence pointer will not deallocate the memory pointed to by _buffer. Before calling CORBA_free() on the _buffer member of a sequence directly, the programmer should check the release flag using CORBA_sequence_get_release. If it returns FALSE, the programmer should not invoke CORBA_free() on the _buffer member; doing so produces undefined behavior. Also, passing a null pointer or a pointer to something other than a sequence type to either CORBA_sequence_set_release or CORBA_sequence_get_release produces undefined behavior.
CORBA_sequence_set_release should only be used by the creator of a sequence. If it is not called for a given sequence instance, then the default value of the release flag for that instance is FALSE.
Two sequence types are the same type if their sequence element type and size arguments are identical. For example,
const long SIZE = 25;
typedef long seqtype;
typedef sequence<long, SIZE> s1;
typedef sequence<long, 25> s2;
typedef sequence<seqtype, SIZE> s3;typedef sequence<seqtype, 25> s4;
declares s1, s2, s3, and s4 to be of the same type.
The OMG IDL type
sequence<type,size>
maps to
#ifndef _CORBA_sequence_type_defined /* C */
#define _CORBA_sequence_type_defined
typedef struct {
CORBA_unsigned_long _maximum;
CORBA_unsigned_long _length;
type *_buffer;
} CORBA_sequence_type;
#endif /* _CORBA_sequence_type_defined */
The ifdef's are needed to prevent duplicate definition where the same type is used more than once. The type name used in the C mapping is the type name of the effective type, e.g. in
typedef CORBA_long FRED; /* C */
typedef sequence<FRED,10> FredSeq;
the sequence is mapped onto struct { ... } CORBA_sequence_long;
If the type in
sequence<type,size>
consists of more than one identifier (e.g. unsigned long), then the generated type name consists of the string "CORBA_sequence_" concatenated to the string consisting of the concatenation of each identifier separated by underscores (e.g. "unsigned_long").
If the type is a string, the string "string" is used to generate the type name. If the type is a sequence, the string "sequence" is used to generate the type name, recursively. For example
sequence<sequence<long> >
generates a type of
CORBA_sequence_sequence_long
These generated type names may be used to declare instances of a sequence type.
In addition to providing a type-specific allocation function for each sequence, an ORB implementation must provide a buffer allocation function for each sequence type. These functions allocate vectors of type T for sequence<T>. They are defined at global scope and are named similarly to sequences:
T *CORBA_sequence_T_allocbuf(CORBA_unsigned_long len); /* C */
Here, "T" refers to the type name. For the type
sequence<sequence<long> >for example, the sequence buffer allocation function is named
T *CORBA_sequence_sequence_long_allocbuf(CORBA_unsigned_long len);
Buffers allocated using these allocation functions are freed using CORBA_free().
typedef string<10> sten;
typedef string sinf;
In C, this is converted to:
typedef CORBA_char *sten; /* C */
typedef CORBA_char *sinf;
Instances of these types are declared as follows:
sten s1 = NULL; /* C */
sinf s2 = NULL;
Two string types are the same type if their size arguments are identical. For example,
const long SIZE = 25; /* C */
typedef string<SIZE> sx;
typedef string<25> sy;
declares sx and sy to be of the same type.
Prior to passing s1 or s2 as an in parameter, the programmer must assign the address of a character buffer containing a 0-byte terminated string to the variable. The caller cannot pass a null pointer as the string argument.
Prior to passing &s1 or &s2 as an out parameter (or receiving an sten or sinf as the return result), the programmer does nothing. The client stub will allocate storage for the returned buffer; for bounded strings, it allocates a buffer of the specified size, while for unbounded strings, it allocates a buffer big enough to hold the returned string. Upon successful return from the invocation, the character pointer will contain the address of the allocated buffer. The client is responsible for freeing the allocated storage using CORBA_free().
Prior to passing &s1 or &s2 as an inout parameter, the programmer must assign the address of a character buffer containing a 0-byte terminated array to the variable. If the returned string is larger than the original buffer, the client stub will call CORBA_free() on the original string and allocate a new buffer for the new string. The client should therefore never pass an inout string parameter that was not allocated using CORBA_string_alloc. The client is responsible for freeing the allocated storage using CORBA_free(), regardless of whether or not a reallocation was necessary.
Strings are dynamically allocated using the following ORB-supplied function:
This function allocates len+1 bytes, enough to hold the string and its terminating NUL character.
Strings allocated in this manner are freed using CORBA_free().
For each named array type in OMG IDL, the mapping provides a C typedef for pointer to the array's slice. A slice of an array is another array with all the dimensions of the original except the first. For example, given the following OMG IDL definition:
typedef long LongArray[4][5];The C mapping provides the following definitions:
typedef CORBA_long LongArray[4][5];
The generated name of the slice typedef is created by appending "_slice" to the original array name.
If the return result, or an out parameter for an array holding a variable-length type, of an operation is an array, the array storage is dynamically allocated by the stub; a pointer to the array slice of the dynamically allocated array is returned as the value of the client stub function. When the data is no longer needed, it is the programmer's responsibility to return the dynamically allocated storage by calling CORBA_free().
For an array T of a variable-length type is dynamically allocated using the following ORB-supplied function:
T_slice *T__alloc(); /* C */
This function is identical to the allocation functions described in Section 14.8, "Mapping Considerations for Constructed Types," on page 14-9, except that the return type is pointer to array slice, not pointer to array.
14.14 Mapping for Exception Types
Each defined exception type is defined as a struct tag and a typedef with the C global name for the exception. An identifier for the exception, in string literal form, is also #defined, as is a type-specific allocation function. For example: long dummy;
};
/* ...may contain additional
* implementation-specific members...
*/
foo *foo__alloc();
The allocation function dynamically allocates an instance of the exception and returns a pointer to it. Each exception type has its own dynamic allocation function. Exceptions allocated using a dynamic allocation function are freed using CORBA_free().
14.15 Implicit Arguments to Operations
From the point of view of the C programmer, all operations declared in an interface have additional leading parameters preceding the operation-specific parameters:
14.16 Interpretation of Functions with Empty Argument Lists
A function declared with an empty argument list is defined to take no operation-specific arguments. 14.17 Argument Passing Considerations
For all OMG IDL types (except arrays), if the OMG IDL signature specifies that an argument is an out or inout parameter, then the caller must always pass the address of a variable of that type (or the value of a pointer to that type); the callee must dereference the parameter to get to the type. For arrays, the caller must pass the address of the first element of the array. typedef long Vector[25];
void bar(out Vector x, out long y);
}; 14.18 Return Result Passing Considerations
When an operation is defined to return a non-void return result, the following rules hold: struct y {
long a;
float b;
};
long op1();
y op2();
}
14.19 Summary of Argument/Result Passing
Table 20 on page 14-19 summarizes what a client passes as an argument to a stub and receives as a result.
1
Including pseudo-object references. 2 A slice is an array with all the dimensions of the original except the first one.
|
A client is responsible for providing storage for all arguments passed as in arguments.
Type
|
Inout Param
|
Out Param
|
Return Result
|
---|---|---|---|
short
|
1
|
1
|
1
|
long
|
1
|
1
|
1
|
unsigned short
|
1
|
1
|
1
|
unsigned long
|
1
|
1
|
1
|
float
|
1
|
1
|
1
|
double
|
1
|
1
|
1
|
boolean
|
1
|
1
|
1
|
char
|
1
|
1
|
1
|
octet
|
1
|
1
|
1
|
enum
|
1
|
1
|
1
|
object reference ptr
|
2
|
2
|
2
|
struct, fixed
|
1
|
1
|
1
|
struct, variable
|
1
|
3
|
3
|
union, fixed
|
1
|
1
|
1
|
union, variable
|
1
|
3
|
3
|
string
|
4
|
3
|
3
|
sequence
|
5
|
3
|
3
|
array, fixed
|
1
|
1
|
6
|
array, variable
|
1
|
6
|
6
|
any
|
5
|
3
|
3
|
1
As listed in Table 21 on page 14-19
|
14.20 Handling Exceptions
The CORBA_Environment type is partially opaque; the C declaration contains at least the following: exception BadCall {
string<80> reason;
};
void op() raises(BadCall);
}; 14.21 Method Routine Signatures
The signatures of the methods used to implement an object depend not only on the language binding, but also on the choice of object adapter. Different object adapters may provide additional parameters to access object adapter-specific features.
14.22 Include Files
Multiple interfaces may be defined in a single source file. By convention, each interface is stored in a separate source file. All OMG IDL compilers will, by default, generate a header file named Foo.h from Foo.idl. This file should be #included by clients and implementations of the interfaces defined in Foo.idl. 14.23 Pseudo-objects
In the C language mapping, there are several interfaces that are defined as pseudo-objects; Table 14 on page A-2 lists the pseudo-objects. A client makes calls on a pseudo-object in the same way as an ordinary ORB object. However, the ORB may implement the pseudo-object directly, and there are restrictions on what a client may do with a pseudo-object.
The ORB itself is a pseudo-object with the following partial definition (see Chapter 7, "ORB Interface" for the complete definition):
interface ORB {
string object_to_string (in Object obj);
Object string_to_object (in string str);
};
Although operations on pseudo-objects are invoked in the usual way defined by the C language mapping, there are restrictions on them. In general, a pseudo-object cannot be specified as a parameter to an operation on an ordinary object. Pseudo-objects are also not accessible using the dynamic invocation interface, and do not have definitions in the interface repository.
Because the programmer uses pseudo-objects in the same way as ordinary objects, some ORB implementations may choose to implement some pseudo-objects as ordinary objects. For example, assuming it could be efficient enough, a context object might be implemented as an ordinary object.
14.24 Mapping of the Dynamic Skeleton Interface to C
For general information about mapping of the Dyanmic Skeleton Interface to programming languages, refer to Section 5.3, "Dynamic Skeleton Interface: Language Mapping," on page 5-3.
CORBA_Identifier CORBA_ServerRequest_op_name (
CORBA_ServerRequest req,
CORBA_Environment *env
);This function returns the name of the operation being performed, as shown in the operation's OMG IDL specification.
CORBA_Context CORBA_ServerRequest_ctx (
CORBA_ServerRequest req,
CORBA_Environment *env
);This function may be used to determine any context values passed as part of the operation. Context will only be available to the extent defined in the operation's OMG IDL definition; for example, attribute operations have none.
void CORBA_ServerRequest_params (
CORBA_ServerRequest req,
CORBA_NVList parameters,
CORBA_Environment *env
);This function is used to retrieve parameters from the ServerRequest, and to find the addresses used to pass pointers to result values to the ORB. It must always be called by each DIR, even when there are no parameters.
The caller passes ownership of the parameters NVList to the ORB. Before this routine is called, that NVList should be initialized with the TypeCodes for each of the parameters to the operation being implemented: in, out, and inout parameters inclusive. When the call returns, the parameters NVList is still usable by the DIR, and all in and inout parameters will have been unmarshaled. Pointers to those parameter values will at that point also be accessible through the parameters NVList.
The implementation routine will then process the call, producing any result values. If the DIR does not need to report an exception, it will replace pointers to inout values in parameters with the values to be returned, and assign pointers to out values in that NVList appropriately as well. When the DIR returns, all the parameter memory is freed as appropriate, and the NVList itself is freed by the ORB.
void CORBA_ServerRequest_result (
CORBA_ServerRequest req,
CORBA_Any value,
CORBA_Environment *env
);This function is used to report any result value for an operation; if the operation has no result, it must not be called. It also must not be called before the parameters have been retrieved, or if an exception is being reported.
void CORBA_ServerRequest_exception (
CORBA_ServerRequest req,
CORBA_exception_type major,
CORBA_Any value,
CORBA_Environment *env
);This function is used to report exceptions, both user and system, to the client who made the original invocation. The parameters are as follows:
major indicates whether the exception is a user exception or system exception
value this is the value of the exception, including an exceptionTypeCode.
typedef void (*DynamicImplementationRoutine) ( /* C */
CORBA_Object target,
CORBA_ServerRequest request,
CORBA_Environment *env
);Such a function will be invoked by the BOA when an invocation is received on an object reference whose implementation has registered a dynamic skeleton.
target is the name object reference to which the invocation is directed.
request is the ServerRequest used to access explicit parameters and report results (and exceptions).
env may be passed to CORBA_BOA_get_principal if desired.
Unlike other BOA object implementations, the CORBA_BOA_set_exception
API is not used. Instead, CORBA_ServerRequest_exception is used; this provides the TypeCode for the exception to the ORB, so it does not need to consult the Interface Repository (or rely on compiled stubs) to marshal the exception value.
interface example4 { // IDL
long op5(in long arg6);};
a method for the op5 routine must have the following function signature:
CORBA_long example4_op5( /* C */
example4 object,
CORBA_Environment *ev,
CORBA_long arg6
);
The object parameter is the object reference that was invoked. The method can identify which object was intended by using the get_id BOA operation. The ev parameter is used for authentication on the get_principal BOA operation, and is used for indicating exceptions.
The method terminates successfully by executing a return statement returning the declared operation value. Prior to returning the result of a successful invocation, the method code must assign legal values to all out and inout parameters.
The method terminates with an error by executing the set_exception BOA operation prior to executing a return statement. The set_exception operation has the following C language definition:
void CORBA_BOA_set_exception ( /* C */
CORBA_Object boa,
CORBA_Environment *ev,
CORBA_exception_type major,
CORBA_char *exceptname,
void *param
);
The ev parameter is the environment parameter passed into the method. The caller must supply a value for the major parameter. The value of the major parameter constrains the other parameters in the call as follows:
For example, the string_to_object ORB operation has the following signature:
CORBA_Object CORBA_ORB_string_to_object ( /* C */
CORBA_Object orb,
CORBA_Environment *ev,
CORBA_char *objectstring
);
The create BOA operation has the following signature:
CORBA_Object CORBA_BOA_create ( /* C */
CORBA_Object boa,
CORBA_Environment *ev,
CORBA_ReferenceData *id,
CORBA_InterfaceDef intf,
CORBA_ImplementationDef impl
);
Although in each example, we are using an "object" that is special (an ORB, an object adapter, or an object reference), the method name is generated as interface_operation in the same way as ordinary objects. Also, the signature contains an CORBA_Environment parameter for error indications.
In the first two cases, the signature calls for an object reference to represent the particular ORB or object adapter being manipulated. Programs may obtain these objects in a variety of ways, for example, in a global variable before program startup if there is only one ORB or BOA that makes sense, or by obtaining them from a name service if more than one is available. In the third case, the object reference being operated on is specified as the first parameter.
Following the same procedure, the C language binding for the remainder of the ORB, BOA, and object reference operations may be determined.
// PIDL
OA/BOA Initialization
The following PIDL specifies the operations (in the ORB interface) that allow applications to get pseudo object references; it is described in detail in Section 7.5, "OA and BOA Initialization," on page 7-8.
14.27 Operations for Obtaining Initial Object References
The following PIDL specifies the operations (in the ORB interface) that allow applications to get pseudo object references for the Interface Repository and Object Services. It is described in detail in Section 7.6, "Obtaining Initial Object References," on page 7-10.
2 Transmissible pseudo-objects are listed as "general arguments" in Table 14 on page A-2 .
3
For brevity, the "CORBA_" prefix is omitted from type names in the tables shown here.