C++ bindings for CORBA Strings

The mapping for strings is provided by corba.h, within the CORBA scope. See "IDL name scoping" for more information. The user-visible types are CORBA::String and CORBA::String_var. CORBA::String is a typedef name for char*. The CORBA::String_var class performs storage management of a dynamically allocated CORBA::String. The following functions are for dynamic allocation and deallocation of memory to hold a string:

A String_var object behaves as a char* except when it is assigned or goes out of scope, the memory it points to is automatically freed by CORBA::string_free. When a String_var is constructed or assigned from a char*, the String_var assumes ownership of the string and the caller must no longer access the string directly. If this is not the desired behavior, as when the char* occupies static storage, the caller can use CORBA::string_dup to copy the char* before assigning it. When a String_var is constructed or assigned from a const char*, another String_var, or a String element of a struct, union, array, or sequence, an automatic copy of the source string is done. The String_var class provides subscripting operations to access the characters within the embedded string.

C++ compilers do not treat a string literal (such as "abc") as a const char* upon assignment. Gven both a const and a non-const assignment operator, the compiler chooses the non-const operator. As a result, when a string literal is assigned to a String_var, a copy of the string is not made into dynamically allocated memory. The pointer "owned" by the String_var points to memory that cannot be freed. Thus, string literals must not be assigned to a String_var without an explicit cast to const char*.

Note: If a string type is used as a field or element type, the bindings initialize the field or element to the empty string "" and not null. This rule applies to sequence elements, array elements, struct fields, union fields, and exception fields.

The following is an example using String_var objects:

// first some supporting functions for the examples
char* f1() 
{
   return "abc";
}
char* f2() 
{
   char* s=CORBA::string_alloc(4);strcpy(s,"abc");return s;
}
// then the examples
void main()
{
   CORBA::String_var s1;
   if (0) s1 = f1();// Wrong!! The pointer cannot be freed and
   // no copy is done.
   if (0) s1 = "abc";  // Also wrong, for the same reason.
   const char* const_string = "abcd"; // *const_string cannot be changed 
   s1 = const_string; // OK. A copy of the string is made because
   // it is const, and the copy can be freed.
   CORBA::String_var s3 = f2();// OK. no copy is made, but f2
   // returns a string that can be freed
   CORBA::String_var s4 = CORBA::string_alloc(10); // also OK. no copy
   s4 = s3; // s4 will use string_free followed by string_dup
   long l4 = strlen(s4); // l4 will receive 3
   long l1 = strlen(s1); // l1 will receive 4
   if (l4 >= l1)
   strcpy(s4,s1); // OK, but only because of the condition.
   // note that s4's buffer only has size=4.
   s4 = const_string; // OK. s4 will use string_free followed by
   // string_dup. The copy is made because String_vars
   // must reference a buffer that can be modified.
}
// The s1, s3 and s4 destructors run successfully, freeing their buffers

Related reference
CORBA C++ bindings for data types
IDL name scoping



Searchable topic ID:   rcor_copstr
Last updated: Jun 21, 2007 8:07:48 PM CDT    WebSphere Business Integration Server Foundation, Version 5.0.2
http://publib.boulder.ibm.com/infocenter/wasinfo/index.jsp?topic=/com.ibm.wasee.doc/info/ee/corba/ref/rcor_copstr.html

Library | Support | Terms of Use | Feedback