[Main]

Variable Size Structures

Variable sized structures are those structures that have one or more members that cannot be determined to be a fixed size at compile time.  For instance a structure that contains a string member is an example of a variable sized structure.  Stack allocated structures will automatically release all member data when the structure goes out of scope.  Heap allocated structures using the new have to be manually released with the delete.  Any variable sized member of a structure is implemented by an ORB vendor to hide the details of how the member represents data.  But for manipulation, the member acts like a smart pointer.  New data assigned into the variable member will automatically release the memory for the previous value.  For instance a string member of a structure may be implemented by a vendor specific managing class.  When ever the structure is destroyed, the variable sized member will be released correctly.  When a variable sized member is being accessed or is begin modified, one has to be careful to assure that the managing class understands what is expected.  Proper casts may be required for the manager class to perform the correct action.

The table below summarizes the expected parameter types for writing code for in, out, inout, and return types in IDL.

Data Type in out inout return
Variable sized structure MM const MM &arg MM *&arg MM &arg MM *

The first example (#1)is of a variable sized  structure Argument that is passed as an in parameter for a function called Method in an interface IExample.   Notice how the data is accessed with a cast operator so that the variable size member can correctly return the expected data type.

// Example #1 : IDL for fixed size IN structure
struct Argument
	{  
	long Data; // Some Data
	string Info; // A String (variable Size)
	};



interface IExample  // IDL Interface
	{
	void Method(in Argument arg); // in argument
	};

// Servant Code Implementation
///////////////////////////////////////
class Servant:public virtual POA_IExample
	{
	public:
	void Method(const Argument &arg);
	};


/////////////////////////////////////
void Servant::Method(const Argument &arg)
	{
	cout<<"Data:"<<arg.Data<<endl; 
	 // To print the string, cast with (const char *)
	cout<<"String:"<<(const char *)arg.Info<<endl;
	}

	
//  Client Code
	{  IExample_var obj;

	/* Init obj code Here */
	
	Argument arg;
	arg.Data=1234;
	arg.Info=CORBA::string_dup("This is a string");  // place string
	obj->Method(arg);
	}
	

 

The next example(#2) demonstrates an IDL out parameter.  Notice that the returned type is allocated on the heap with a new operator.   The returned type is released with the delete operator.

// Example #2 : IDL for variable sized structure OUT structure
struct Argument
	{  
	long Data; // Some Data
	string Info; // A String (variable Size)
	};



interface IExample  // IDL Interface
	{
	void Method(out Argument arg); // out argument
	};
// Servant Code Implementation
///////////////////////////////////////
class Servant:public virtual POA_IExample
	{
	public:
	void Method(Argument *&arg);
	};


/////////////////////////////////////
void Servant::Method(Argument *&arg)
	{
	arg=new Argument; // heap allocated
	arg->Data=1234;
	arg->Info=CORBA::string_dup("Server Info Data");
	}

	
//  Client Code
	{  IExample_var obj;

	/* Init obj code Here */
	Argument *arg=0;
	obj->Method(arg);
	cout<<"Data: "<<arg->Data<<endl;
	cout<<"Info:"<<(const char *)arg->Info<<endl;
	cout<<endl;
	delete arg;  // release heap object 
	}
    

 

Example #3 demonstrates an inout parameter which is a variable sized structure.

	// Example #3 : IDL for variable sized INOUT structure

struct Argument
	{  
	long Data; // Some Data
	string Info; // A String (variable Size)
	};



interface IExample  // IDL Interface
	{
	void Method(inout Argument arg); // inout argument
	};

// Servant Code Implementation
///////////////////////////////////////
class Servant:public virtual POA_IExample
	{
	public:
	void Method(Argument &arg);
	};


/////////////////////////////////////
void Servant::Method(Argument &arg)
	{
	cout<<"Data: "<<arg.Data<<endl;
	cout<<"Info:"<<(const char *)arg.Info<<endl;
	cout<<endl;

	arg.Data=4444;
	arg.Info=CORBA::string_dup("Server Info Data"); // Old data release
	}

//  Client Code
	{  IExample_var obj;

	/* Init obj code Here */
	
	Argument arg;
	arg.Data=2000;
	arg.Info=CORBA::string_dup("Client String");
	obj->Method(arg);
	cout<<"Data: "<<arg.Data<<endl;
	cout<<"Info:"<<(const char *)arg.Info<<endl;
	cout<<endl;
	}
  

 

Example #4 demonstrates the returning of a variable sized structure.  Notice that the server allocates the structure on the heap, while the client releases the heap allocated structure.

	// Example #4 : IDL for return of a fixed size structure
struct Argument
	{  
	long Data; // Some Data
	string Info; // A String (variable Size)
	};



interface IExample  // IDL Interface
	{
	Argument Method( ); // return an variable sized struct
	};

// Servant Code Implementation
class Servant:public virtual POA_IExample
	{
	public:
	Argument * Method(void);
	};


/////////////////////////////////////
Argument * Servant::Method(void)
	{
	Argument *arg=new Argument;
	arg->Data=1111;
	arg->Info=CORBA::string_dup("Server Info Data"); 
	return arg;
	}
	
//  Client Code
	{  IExample_var obj;

	/* Init obj code Here */
	
	Argument *arg;
	arg=obj->Method();
	cout<<"Data: "<<arg->Data<<endl;
	cout<<"Info:"<<(const char *)arg->Info<<endl;
	cout<<endl; 
	delete arg; // release heap object
	}
    

 

copyright©2001 Prof Devi - mdevi@comine.com, mdevi@liu.edu. All rights reserved