Documentation:BDL
BABEL Definition Language (BDL)
v. 120606
A data definition language for the BABEL development system.
System Engineering and Automation Department.
University of Málaga, Spain.
Contents
Introduction
What is BDL?
BDL (Babel Definition Language) is the medium for specifying, in a programming-language independent fashion, some data types that a BABEL module needs. BDL has been implemented as a restricted subset of one of the most extended definition languages: the OMG CORBA IDL (http://www.omg.org).
Where the BDL types are used?
It is used currently for defining input/output parameters of services and for providing public definitions (data types that the module provides to other modules and to itself). You can use BDL types both in the "Public Definitions" of the module and in the specification of the input/output parameters of the services. You can compose your own types (using "typedef") based on BDL basic or composed types.
What the BDL types are used for?
You can use a variable of a BDL type in the following situations: A) Creating a local variable for the internal use of some logic of the module (auxiliary logic, service logic, start-up logic, etc.). B) Creating a global variable for the internal status of the module (accessible from any logic of the module). C) Using an already defined input/output parameter of a service.
How variables of the BDL types are used?
You should remember that BDL is not a programming language (only a specification for data), and thus, you cannot use directly any BDL type in your codification logics. Rather, BABEL translates automatically for you the BDL types into types understandable by the codification language you have chosen for your module (see for example the Using BDL in the C++ codification language section).
BABEL Definition Language (BDL)
Basic types
Numeric (octet, short, long, ...)
BDL includes the following basic numeric types:
- octet : 8-bit, unsigned (0...2^8-1)
- short : 16-bit, signed (-2^15...2^15-1)
- unsigned short : 16-bit, unsigned (0...2^16-1)
- long : 32-bit, signed (-2^31...2^31-1)
- unsigned long : 32-bit, unsigned (0...2^32-1)
- long long (*) : 64-bit, signed (-2^63...2^63-1)
- unsigned long long (*) : 64-bit, unsigned (0...-2^64-1)
- float : IEEE single-precision floating point numbers.
- double : IEEE double-precision floating point numbers.
(*): The 64-bit integer types (long long and unsigned long long) could not be available in some implementations, being mapped in an undefined way, so its use should be avoided.
Alphanumeric (char)
BDL has only one basic alphanumeric type:
- char : One unsigned 8-bits character.
There is also one composed alphanumeric type: string.
Others (boolean)
BDL includes one basic type for logical operations:
- boolean : Stores a boolean value (TRUE or FALSE)
Composed types
Not-Repeated (struct)
BDL includes the following composed type that does not contain more than one occurrence of an object:
- Structs: They are analogous to C structs, and can contain both basic and composed types fields. For example:
<cpp> struct MyStructName { short FieldOne; double FieldTwo; OtherStructName osn; } MyVarStruct; </cpp>
The union data type is not supported currently in BDL.
Repeated (sequence, array)
BDL includes the following composed types that can hold more than one occurrence of an object:
- Sequences: An ordered list of elements of the same type (maybe composed). A maximum limit of elements can be specified, or not. For example:
<cpp> struct LimitedAccounts { string bankSortCode<10>; sequence<Account> accounts; // No maximum length of sequence. sequence<Account,50> accountsBis; // 50 is the maximum length of sequence. }; </cpp>
NOTE: Do not use sequences of octects to communicate serialized objects from one module to another: that means that you are forcing any receiver to use the same objects definition and software as you, tying other people to your needs. Use only sequences of octects to pack together data that either has no structure or its structure is common knowledge -no additional libraries needed to handle it-.
- Arrays: Fixed-length indexed sets of elements of the same type (maybe composed). For example:
<cpp> struct SomeAccounts { string bankSortCode<10>; // string of length 10 Account accounts[10][2]; // array of dimensions 10x2 }; </cpp>
Others (enum, string)
BDL includes these other composed types:
- Enumerated: The enumeration of several constants. No numeric equivalence should be assumed. For example:
<cpp> enum Currency {pound, dollar, yen, franc}; </cpp>
- Strings: Sequences of characters of 8 bits each. They can be given a fixed maximum length. For example:
<cpp> struct MyStructName { string<10> MyString; // A string that can store no more than 10 characters string MyString2; // A string without maximum length } MyStructVar; </cpp>
Data type alias (typedef)
The typedef keyword has been introduced to define array data types, but it also can be used to define alias for already defined data types.
The syntax is:
<cpp> typedef < an already defined data type > < an alias for that type >; </cpp>
For example:
<cpp> struct MyStruct { long A; string B; };
typedef struct MyStruct MyAliasForMyStruct; typedef unsigned long MyAliasForUnsignedLong;
typedef sequence<short, 50> ShortListType; typedef sequence<long> LongListType;
typedef short ShortArrayType[50][10];
typedef enum MyCurrency {pound, dollar, yen, franc} OtherCurrency;
typedef string<10> MyStringType; typedef string MyString; </cpp>
Constants (const)
The way of defining constants in BDL is: <cpp> const <type> <const name> = <value>; </cpp>
For example:
<cpp> const float PI=3.14159265; const char NUL='\0'; const string LAST_WORDS="My god, it's full of stars!"; </cpp>
Predefined types
A few types are predefined so they can be readily used by any BABEL module. Their translation into the generated source code language follows the same rules as though they were defined by the user.
<cpp> typedef sequence<string> SeqOfStrings; typedef sequence<octet> SeqOfBytes; typedef sequence<boolean> SeqOfBools; typedef sequence<float> SeqOfFloats; typedef sequence<double> SeqOfDoubles;
typedef sequence<short> SeqOfShorts; typedef sequence<unsigned short> SeqOfUShorts; typedef sequence<long> SeqOfLongs; typedef sequence<unsigned long> SeqOfULongs;
typedef sequence<unsigned long long> SeqOfULongLongs;
typedef double TMatrix22[2][2]; typedef double TMatrix33[3][3]; typedef double TMatrix44[4][4]; typedef double TMatrix66[6][6]; </cpp>
Using BDL in C++ codification language
Fixed-length and variable-length data types
The implementation and usage of the BDL data types can be different if the data type is fixed-length or variable-length.
Fixed-length data types
- octet
- short
- unsigned short
- long
- unsigned long
- long long
- unsigned long long
- float
- double
- char
- boolean
- enum
Variable-length data types
- Bounded and unbounded string
- Bounded and unbounded sequence
Data types that can be fixed-length or variable-length
- A struct is fixed-length if all its members are fixed-length. It is variable-length if one or more of its members are variable-length.
- An array is fixed-length if its base type is fixed-length. If its base type is variable-length, then the array is variable-length.
Note: A variable-length array is not an array with a variable number of elements; it is an array with a variable-length element data type.
First consideration: fixed-length and variable-length data types
The first issue when BDL types are translated into C++ is that service output parameters are translated as reference (DataType &) for fixed-length data types, but as a reference to a pointer (DataType *&) for variable-length data types. For variable-length data types, there is also the need to manage the allocation and deallocation of its memory (BABEL does not provide code for that). For specific information, see the documentation for each data type under Usage of BDL in C++.
Examples:
<cpp> // All of this is BDL struct FStrA { // a fixed-length struct (float and long are fixed-length) float f; long c; };
struct VStrB { // a variable-length struct string name; // the struct is variable-length because of this string member unsigned short age; };
struct VStrC { // another variable length struct (the data member contains a string) long x; VStrB data; // the struct is variable-length because of this variable-length struct member };
typedef long FArrA[8][3]; // A fixed-length array (long is fixed-length) typedef FStrA FArrB[8]; // A fixed-length array (FStrA is fixed-length) typedef string<10> VArrC[7]; // A variable-length array (a bounded string is variable-length) typedef VStrB VArrD[12]; // A variable-length array (VStrB is variable-length) </cpp>
Basic types
All the basic types are mapped to types defined in the portable namespace "BABEL" (this is done indepently on the codification language chosen), with functionalities that are indistinguishable from native C++ types. However, octet, boolean, and char may be mapped to the same native C++ type, so any overloaded function that is overloaded solely on these types should be avoided. Also, the 64-bit integer types (long long and unsigned long long) could not be available in some implementations, being mapped in an undefined way.
Translation of basic BDL data types to C++
Numeric:
- octet: BABEL::Octet
- short: BABEL::Short
- unsigned short: BABEL::UShort
- long: BABEL::Long
- unsigned long: BABEL::ULong
- long long: BABEL::LongLong
- unsigned long long: BABEL::ULongLong
- float: BABEL::Float
- double: BABEL::Double
Alphanumeric:
- char: BABEL::Char
Others:
- boolean: BABEL::Boolean.
There are two predefined boolean constants: - BABEL::True - BABEL::False
One can use a BABEL::Boolean variable as a C++ bool type, but it can be defined as another type. You can compare a Boolean value with BABEL::False, but the comparison with BABEL::True should be avoided (use v!=BABEL::False or !v instead of v==BABEL::True).
Using basic data types as service parameters
The basic data types are mapped as BABEL::datatype (as shown above) when used as input parameters in services, and as BABEL::datatype& when used as output. For example, a service "MyOp" with the input parameters "char ch" and "long lg" and the output parameters "boolean bl" and "float fl" can be considered as though it was translated into the following C++ method:
<cpp> void <some class not interesting for the user>::MyOp(BABEL::Char ch, BABEL::Long lg, BABEL::Boolean& bl, BABEL::Float& fl); </cpp>
Input parameter (caller point of view)
Since the data type is passed by value, the caller can use for it a local variable, constant, or expression.
Input parameter (callee point of view)
The callee can use and change the value of the input parameter, but, since it was passed by value, the changes will not be sent back to the caller.
Output parameter (caller point of view)
The output parameter type is a reference to the data type, so the caller must use a variable (defined in the caller) of the corresponding translated data type to receive the output value. The initial value of this variable will be ignored by the callee.
Output parameter (callee point of view)
The callee must ignore the initial value of the output parameter (it can be different from the one passed by the caller), and must set a new value (at least once). If the caller does not set the value, then it will be undefined, and this is an error.
Composed Types
Not-Repeated (struct)
The struct data type is mapped to a C++ struct with equivalent members.
Translation example:
<cpp> // BDL struct FStrA { // a fixed-length struct float f; long c; };
struct VStrB { // a variable-length struct string name; // the struct is variable-length because of this string member unsigned short age; };
struct VStrC { // another variable length struct long x; VStrB data; // the struct is variable-length because of this variable-length struct member }; </cpp>
<cpp> // C++ struct FStrA { // a fixed-length struct BABEL::Float f; BABEL::Long c; }; // Full name: BABEL::MyModule::FStrA
struct VStrB { // a variable-length struct (because of the string member) /* string manager class */ name; // the class name is not defined and should not be used BABEL::UShort age; }; // Full name: BABEL::MyModule::VStrB
struct VStrC { // another variable length struct BABEL::Long x; VStrB data; // the struct is variable-length because of this variable-length struct member }; // Full name: BABEL::MyModule::VStrC </cpp>
Dynamic allocation and deallocation
A struct can be dynamically allocated and deallocated using C++ new and delete operators. If the struct contains any strings (directly or inside another contained data type) then, the strings will be initialized to "" instead of NULL.
Deep copy of struct variables
When you use the assignment operator (=), the struct is deep copied. Any allocated memory will be duplicated, so one can change a copied or original struct without changing the other.
Fixed-length and variable-length struct data types
A struct is variable-length if it contains strings or sequences (directly as a member or recursively). If it does not, the struct is fixed-length. See fixed-length and variable-length data types for a detailed definition of fixed-length and variable length data types.
One must know if a struct is fixed-length or variable-length to know how to pass it as an output parameter of a service (an operation).
A struct as an input (in) parameter (caller point of view):
The struct will be sent as const MyStructDataType&, so the caller needs a variable of that struct to use it as an input parameter of a service.
A struct as an input (in) parameter (callee point of view):
Since the input struct is const MyStructDataType&, the callee cannot change it.
A fixed-length struct as an output (out) parameter (caller point of view):
The parameter type will be MyStructDataType& so the caller needs a variable of that struct to use it as an output parameter of a service. The initial content of the variable is ignored by the callee.
A fixed-length struct as an output (out) parameter (callee point of view):
The parameter type will be MyStructDataType&. The callee must ignore the initial value of the output parameter (it can be different from the one passed by the caller), and must set a new value (at least once). If the caller does not set the value, then it will be undefined, and that is an error. The callee can set the parameter's value assigning another struct of the same type or by setting all of its members. Please, see the documentation of the data type (string, sequence, others...) to know how to do the assignment.
A variable-length struct as an output (out) parameter (caller point of view):
The parameter type will be MyStructDataType*& (not MyStructDataType& used with fixed-length structs). The caller needs a pointer variable to use it as an output parameter of a service. In that pointer variable, the service will store a pointer to the returned struct. The caller must free the memory when the struct is not needed (using the C++ delete operator). The pointer variable value will be overwritten without freeing its content, so, make sure to free its contents before using it as an output parameter.
A variable-length struct as an output (out) parameter (callee point of view):
The parameter type will be MyStructDataType*& (not MyStructDataType& used with fixed-length structs). The callee must allocate a new struct using the C++ operator new, change any member as needed, and store the pointer to the allocated struct in the output parameter. The initial value of the output parameter is undefined and must not be used or freed. The caller must do the assignment to the output parameter once and only once. If you do it more than once, the previous struct will not be freed. If you do not assign any pointer to it, the parameter value will be undefined and that is an error. However, the callee can assign a pointer, then delete it using delete and then assign a new pointer.
Repeated: sequence
NOTE: please read the note on sequences in their definition within the BDL language.
The sequences are mapped to C++ classes.
Examples: See array operators and the get_buffer functions for examples of the sequence usage.
Dynamic allocation and deallocation
To dynamically allocate and deallocate a sequence, one can use the new and delete C++ operators. The buffer used by the sequence is managed automatically.
A sequence as an input (in) parameter (caller point of view):
The sequence will be sent as const MySequenceDataType&, so the caller needs a variable of that sequence to use it as an input parameter of a service.
A sequence as an input (in) parameter (callee point of view):
Since the sequence is const MySequenceDataType&, the callee cannot change it. Note that the sequence could be empty.
A sequence as an output (out) parameter (caller point of view):
The parameter type will be MySequenceDataType*&. The caller needs a pointer variable to use it as an output parameter of a service. In that pointer variable, the service will store a pointer to the returned sequence. The caller must free the memory when the array is not needed, using the C++ delete operator. The pointer variable value will be overwritten without freeing its content, so, make sure to free its contents before using it as an output parameter.
A sequence as an output (out) parameter (callee point of view):
The parameter type will be MySequenceDataType*&. The callee must allocate a new sequence using the C++ new operator. After the allocation, change the content as needed, and store the pointer to the allocated sequence in the output parameter. The initial value of the output parameter is undefined and must not be used or freed. The caller must do the assignment to the output parameter once and only once. If you do it more than once, the previous sequence will not be freed. If you do not assign any pointer to it, the parameter value will be undefined and that is an error. However, the callee can assign a pointer, then delete it using delete and then assign a new pointer.
Warning: The callee can return a empty sequence, but not a NULL pointer (assigning a NULL pointer to the output parameter is not valid).
Using the output parameter:
After assigning a pointer to the output parameter, the callee can use the pointed sequence as usual, but one must remember that the parameter is a pointer, not a sequence. For example:
<cpp> out_seq = new BABEL::MyModule::MyStringSequenceDataType(); out_seq->length(1); // use -> instead of . (*out_seq)[0] = BABEL::string_dup("my string"); // use (*out_seq)[index] instead of out_seq[index] </cpp>
Repeated: array
Translation example:
<cpp> // BDL typedef float MyLongA[8]; typedef string MyStrA[4][12]; </cpp>
<cpp> // C++ typedef BABEL::Float MyLongA[8]; typedef BABEL::Float MyLongA_slice; typedef /* string manager class */ MyStrA[4][12]; typedef /* string manager class */ MyStrA_slice[12]; // Their full names begins with "BABEL::MyModule::" (e.g.: BABEL::MyModule::MyLongA) </cpp>
The T_slice data type
The T_slice data type (e.g.: MyLongA_slice, MyStrA_slice) has the same element type and the same dimensions as the original except the first one. T_slice can be used to declare pointers to an array, since C++ declares array pointers as pointers to the first element of.
For example: <cpp> // C++ BABEL::MyModule::MyLongA a; // an array of MyLongA type BABEL::MyModule::MyLongA_slice* ptr = a; // a pointer to an array of MyLongA type </cpp>
Warning:
The T_slice data type for arrays with different first dimension lengths is the same. For example:
<cpp> // BDL typedef long A[16][8][2]; typedef long B[4][8][2]; // C++ typedef BABEL::Long A[16][8][2]; typedef BABEL::Long A_slice[8][2]; typedef BABEL::Long B[4][8][2]; typedef BABEL::Long B_slice[8][2]; // the same data type as A_slice // Their full names begins with "BABEL::MyModule::" (e.g.: BABEL::MyModule::A) </cpp>
That means that a pointer to A (A_slice*) is compatible with a pointer to B (B_slice*), and the user could use a pointer of one of them as if it were a pointer to the other. That is a grave error (usually leads to core dump), but the C++ compiler will compile the code without issuing a warning message.
Static allocation
A statically allocated array can be created by using its data type name, as any C++ data type. For example:
<cpp> // C++ BABEL::MyModule::MyLongA v1; BABEL::MyModule::MyStrA v2; </cpp>
If the element data type includes strings, they are initialized to "" instead of NULL. When the variable goes out of scope (e.g.: the function that declares it ends) all used memory is freed (strings, sequences, arrays, ...).
Dynamic allocation and deallocation
To dynamically allocate and deallocate an array, one cannot use the new and delete operators or its array versions (new T[] and delete[] ptr). To allocate an array, use the T_alloc function (e.g.: BABEL::MyModule::MyLongA_alloc(), BABEL::MyModule::MyStrA_alloc()). If the element data type includes strings, they are initialized to "" instead of NULL. To deallocate an array, use T_free (e.g.: BABEL::MyModule::MyLongA_free(), BABEL::MyModule::MyStrA_free()).
<cpp> T_slice *T_alloc() </cpp>
Dynamically allocates a T array and returns a pointer to it or NULL if there is not enough memory. Note that the user must free the array when it is not needed anymore. To free the array, use the T_free() function.
<cpp> void T_free(T_slice* p) </cpp>
Frees a T array allocated by T_alloc() or T_dup() (see "Copy and duplication"). p is the array to be freed. It can be NULL (in that case the function does nothing).
Copy and duplication
<cpp> void T_copy(T_slice* to, const T_slice* from) </cpp>
Copies the contents of the from array to the to array. If from or to or both are NULL, the function does nothing. The parameters can be statically or dynamically allocated (or one of each kind). An example of this function could be BABEL::MyModule::MyLongA_copy().
<cpp> T_slice *T_dup(const T_slice* p) </cpp>
Dynamically allocates a new array of the T type, copies the contents of the p array into it and then returns a pointer to it. The caller must free the array using T_free() described in "Dynamic allocation and deallocation". If there is not enough memory or if p is NULL, the returned pointer will be NULL. An example of this function could be BABEL::MyModule::MyLongA_dup()
Array usage
The BDL arrays can be used as C++ arrays except for:
- Dynamic allocation and deallocation must be done using T_alloc() and T_free().
- The arrays can be copied or duplicated using T_copy() and T_dup().
- The usage of the elements stored in the array must follow the element data type usage.
- Any embedded string is initialized to "" (instead of NULL or no initialization at all).
The [ ] operator can be used as usual with BDL arrays.
Example: <cpp> // BDL struct MyStruct { long value; string name; };
typedef MyStruct MyArray[8][2]; // MyArray is variable-length because of the string member "name" of its element data type
// C++ int i; BABEL::MyModule::MyArray a; // a statically allocated array
a[2][1].name = BABEL::string_dup("this member will not be an empty string"); for(i=0; i<8; i++) a[i][0].value=a[i][1].value=i;
BABEL::MyModule::MyArray_slice* b; // a dynamically allocated array BABEL::MyModule::MyArray_slice* c; // another dynamically allocated array
b = BABEL::MyModule::MyArray_alloc(); // dynamically allocates the array BABEL::MyModule::MyArray_copy(b,a); // copy a into b (b=a) c = BABEL::MyModule::MyArray_dup(b); // dynamically allocates an array and copies the contents of b c[2][1].name = BABEL::string_dup("c"); // a and b will not be changed
BABEL::MyModule::MyArray_free(b); // dynamically frees the b array BABEL::MyModule::MyArray_free(c); // dynamically frees the c array </cpp>
Fixed-length and variable-length array data types
An array is variable-length if its element data type is variable-length (contains strings or sequences, directly or recursively). If it does not, the array is fixed-length. See fixed-length and variable-length data types for a detailed definition of fixed-length and variable length data types.
One must know if an array is fixed-length or variable-length to know how to pass it as an output parameter of a service (an operation).
An array as an input (in) parameter (caller point of view):
The array will be sent as const MyArrayDataType, so the caller needs a variable of that array to use it as an input parameter of a service. Note that for C++, the const MyArrayDataType is equivalent to const MyArrayDataType_slice*.
An array as an input (in) parameter (callee point of view):
Since the array is const MyArrayDataType, the callee cannot change it.
A fixed-length array as an output (out) parameter (caller point of view):
The parameter type will be MyArrayDataType_slice*. The caller needs a (dynamically/statically) allocated array of that struct to use it as an output parameter of a service. The initial content of the variable is ignored by the callee, and the caller keeps the ownership (must free the array when not needed). Note that the fixed-length array is allocated by the caller, not by the callee.
A fixed-length array as an output (out) parameter (callee point of view):
The parameter type will be MyArrayDataType_slice*. The callee must ignore the initial value of the output parameter (it can be different from the one passed by the caller), and must set a new value (at least once). If the caller does not set the value, then it will be undefined, and that is an error. Please, see the documentation of the element data type (string, sequence, others...) to know how to do the assignment. Note that the array was allocated by the caller, and the callee cannot reallocate it (the callee does not have a reference to the array pointer).
A variable-length array as an output (out) parameter (caller point of view):
The parameter type will be MyArrayDataType_slice*& (not MyStructDataType_slice* used with fixed-length arrays). The caller needs a pointer variable to use it as an output parameter of a service. In that pointer variable, the service will store a pointer to the returned array. The caller must free the memory when the array is not needed, using T_free() (e.g.: MyArrayDataType_free()). The pointer variable value will be overwritten without freeing its content, so, make sure to free its contents before using it as an output parameter.
A variable-length array as an output (out) parameter (callee point of view):
The parameter type will be a class that behaves as a MyArrayDataType_slice*& (not MyStructDataType_slice* used with fixed-length arrays). The callee must allocate a new array using T_alloc() (e.g.: BABEL::MyModule::MyArrayDataType_alloc()). After the allocation, change the content as needed, and store the pointer to the allocated array in the output parameter. The initial value of the output parameter is undefined and must not be used or freed.
Warning: The caller must do the assignment to the output parameter once and only once. If you do it more than once, the previous array will not be freed. If you do not assign any pointer to it (or if you assign the NULL pointer), the parameter value will be undefined and that is an error. However, the callee can assign a pointer, then delete it using T_free() and then assign a new pointer.
Usage of the variable-length array output parameter:
After assigning a pointer to the output parameter, the callee can use the pointed array, but there are some rules. You can retrieve the stored pointer (a MyArrayDataType_slice*) using the method ptr() of the output parameter class. For example (the BABEL module is called "MyModule", and the variable-length array output parameter is "arr_out"):
<cpp> // Allocate the array and assign it to the output parameter: arr_out = BABEL::MyModule::arr_alloc();
// Get the stored pointer (method 1): BABEL::MyModule::arr_slice* arr_ptr = arr_out.ptr(); // gets the pointer stored in arr_out // arr_out keeps pointing to the array
// Get the stored pointer (method 2): BABEL::MyModule::arr_slice* arr_ptr = arr_out; // gets the pointer without using ptr() // arr_out keeps pointing to the array
// Access to array elements: arr_out.ptr()[4][8] = arr_out.ptr()[1][2] + arr_out.ptr()[3][7]; // Ok
// This is not valid: arr_out[4][8] = arr_out[1][2] + arr_out[3][7]; // Error: use ptr() (*arr_out)[4][8] = (*arr_out)[1][2] + (*arr_out)[3][7]; // Error: use ptr()
// Recommended usage: BABEL::MyModule::arr_slice* arr_ptr = BABEL::MyModule::arr_alloc(); // allocates the array in a temporal variable. // One can duplicate an array instead of allocating a default initialized one. arr_out = arr_ptr; // arr_out and arr_ptr points to the same array (the one to be returned) arr_ptr[1][2] = arr_ptr[3][3] + arr_ptr[4][4]; // use the temporal variable to do any changes to the array // When the function ends, arr_ptr returns the array </cpp>
Others: enum
The enum data type is mapped directly to a C++ enum.
Translation example:
<cpp> // BDL enum MyEnum {one, two, three}; </cpp> |
<cpp> // C++ enum MyEnum {one, two, three}; // the full name for using it will be: BABEL::MyModule::MyEnum </cpp> |
Using enum as service parameter:
The enum data types are mapped as datatype (shown above) when used as input parameters in services, and as datatype& when used as output. For example, a service "MyOp" with an input and output parameters (a_in and b_out) of the enumerated "MyEnum" can be considered as though it is translated into the following C++ method:
<cpp> void <some class uninteresting for the user>::MyOp(BABEL::MyModule::MyEnum a_in, BABEL::MyModule::MyEnum& b_out); </cpp>
Input parameter (caller point of view):
Since the enum is passed by value, the caller can use a variable or a constant enumerator (an enumerated value) of the same translated type.
Input parameter (callee point of view):
The callee can use and change the value of the input parameter as expected, but, since it was passed by value, the changes will not be sent back to the caller.
Output parameter (caller point of view):
The output parameter type is a reference to the enumerated type, so the caller must use a variable of the translated enumerated type to receive the output value. The initial value of this variable will be ignored by the callee.
Output parameter (callee point of view):
The callee must ignore the initial value of the output parameter (it can be different from the one passed by the caller), and must set a new value (at least once). If the caller does not set the value, then it will be undefined, and this is an error.
Others: string
Strings of BDL are translated into (possibly const) char * of C++ if they appear directly as types of some input/output parameter of a service. They can be also defined as variables in the logic of some service in this way: BABEL::String str;. In both cases, the helper functions described below apply.
However, when a BDL string type appears as the type of a field of a composed public type of a module, it is not translated into a BABEL::String type. In that case, it is translated into a special object that can use BABEL::string_dup for assignment, but it cannot use BABEL::string_free for deletion.
Helper functions
<cpp> char * BABEL::string_alloc(BABEL::ULong len) </cpp>
Allocates space for a string. The allocated size will be len+1 (so the allocated buffer has enough space for the trailing 0). Return: The allocated buffer or NULL if there is not enough memory. Note that the user must free the buffer when it is not needed anymore (or assigned to an object that do so). To free the buffer, use the BABEL::string_free() function.
<cpp> char * BABEL::string_dup(const char *c) </cpp>
Duplicates a string. c is the string to be duplicated. The return value is the duplicated string or NULL if there is not enough memory. If c is NULL, the return value will be NULL. The allocated size will be strlen(c)+1 using BABEL::string_alloc(strlen(c)). Note that the user must free the string when it is not needed anymore (or assigned to an object that do so). To free the string, use the BABEL::string_free() function.
<cpp> void BABEL::string_free(char *c) </cpp>
Frees a string allocated by BABEL::string_alloc() or BABEL::string_dup(). c is the string to be freed. It can be NULL (in that case the function does nothing).
A string as an input parameter (caller point of view):
Just use a const char* or a char* (will be treated as a const char*) for passing a string (an embedded string is also valid). Passing a NULL pointer is not allowed and the outcome is undefined. The caller keeps the string ownership.
A string as an input parameter (callee point of view):
Just use the parameter as a const char* (it should not be NULL).
A string as an output parameter (caller point of view):
Use a char* variable as parameter (the parameter is a class that behaves as a char*&). The variable value will be overwritten without freeing its content, so, make sure to free its contents before using it as an output parameter. The returned value can be "" but never NULL. When you do not need the returned string anymore, you must delete it using BABEL::string_free() as described earlier (do not use delete/delete[] to free it).
Also, you can pass as output parameter a string which is part of a composed type. In that case the current string will be automatically freed. For example, if you have a service "MyOp" with an output parameter "string s", you can pass as that parameter the following: "MyStruct.stringfield", provided that MyStruct is a variable of some struct type that contains a field ("stringfield") of type string.
A string as an output parameter (callee point of view):
The parameter will be a class that behaves as a char*&. The callee must do the assignment to a string output parameter once and only once. If you do it more than once, the previous string will not be freed. If you do not assign any string to it, the parameter value will be undefined and that is an error. However, the following is valid:
<cpp> void <some class that is not interesting for the user>::MyOperation(/*out string parameter data type*/ str) { str = BABEL::string_dup("Unknown"); if(/*...*/) { BABEL::string_free(str); // free the previous string str = BABEL::string_dup("Ok"); } } </cpp>
Warning: An output string parameter does not behaves as an embedded string. As stated before, each assignment overwrites the previous string without freeing it.
Assigning a char* value:
When assigning a char* value to an output string parameter, the parameter will own that pointer without making a duplicate of the string, so that string should be created through BABEL::string_dup() or BABEL::string_alloc(), because it will be eventually freed using BABEL::string_free() (if it was created by new or anything else, a typecast to const char* is needed).
Assigning a const char* value:
If you assign a value using a const char*, the string will be duplicated with BABEL::string_dup(). Please, note that some compilers (non ANSI/ISO C++ compliant) treats literal strings as char*, not as const char*, so it is preferred to use str = (const char*)"literal string" or str = BABEL::string_dup("literal string") instead of str = "literal string".
Assigning an embedded string:
The direct assignment of an embedded string to an output string parameter is not allowed. Please, use string_dup() to do the assignment. E.g: out_str_arg = BABEL::string_dup(my_struct.my_embedded_str).
Using the output parameter:
After a string is assigned to the output parameter, you can use the parameter as a char*. The [ ] operator is available for read and write, but one sould take care not to use an index out of the string allocated buffer. The string can be modified and shortened but not enlarged. To enlarge a string you must allocate a larger buffer using BABEL::string_alloc(). If you assign a new buffer to the output parameter, remember to free the previous one using BABEL::string_free().
Examples:
<cpp> // This service has an input string parameter, str_in, and an output string parameter, str_out. void <uninteresting class>::Echo(const char* str_in, /* the string output class */ str_out) { str_out = BABEL::string_dup(str_in); // duplicates str_in and returns the copy in str_out // or: str_out = str_in; // duplicates str_in and returns the copy in str_out (the same as above) // or: str_out = "str"; // duplicates "str" and returns the copy in str_out. However, str_out = (const char*)"str" is preferred.
//str_out = new char[1]; str_out[0]=0; // thar is an error: the string must be allocated using string_dup() or string_alloc(). }
void <uninteresting class>::GetName(const MyContactStruct& contact_in, /* the string output class */ name_out) { name_out = BABEL::string_dup(contact_in.name); // copies contact_in.name and returns the copy in name_out //name_out = contact_in.name; // that is an error (*) } </cpp>
(*): contact_in.name is an embedded string. This assignment is not allowed. The user must use the prior line to do the assignment.
Data type alias (typedef)
An alias for a data type created by the BDL typedef keyword behaves as the original data type. For example, an alias for an array should be treated as if that alias was an original array.
Constants (const)
The BDL constants are mapped to equivalent C++ constants. To use a constant one must add BABEL::MyModule:: before the name of the constant ("MyModule" is the name of the BABEL module). Thus, the constant PI should be referenced in C++ as BABEL::MyModule::PI.
For example:
<cpp> // BDL const float PI=3.14159265; const char NUL='\0'; const string LAST_WORDS="My god, it's full of stars!"; </cpp> |
<cpp> // C++ static const BABEL::Float PI=3.14159265; static const BABEL::Char NUL='\0'; static const char* LAST_WORDS="My god, it's full of stars!"; </cpp> |