[Main]

Fixed Size Element Arrays

Arrays used in IDL should always be type defined and are always a fixed size.  An ORB vendor implements a class based on the type definition that will give the functionality of an array object.  If this type defined class is created on the stack, all the default members are released correctly.  The ORB vendor implements an alloc, free, and dup methods(similar to the string methods) for allocating, freeing, and duplicating arrays on the heap.  The methods are based on the type definition of the array.  For instance, typedef long Vector[10]; declares a new type Vector.  A single member of the array is given a special type definition called a slice. In the Vector instance, the type definition for an individual array element is given generally as typedef long Vector_slice; .  Based on this slice definition, the heap method for allocating an new array is given as  Vector_slice *Vector_alloc(void); .  This method returns 0, if the array could not be allocated.  To free the array, the method void Vector_free(Vector_slice *addr); is used.  The duplication of an array on the heap is done with the Vector_slice * Vector_dup(Vector_slice *vec);

Both the type definition and the slice version have an operator [ ] defined for easy access to the array members.  Indexing outside specified array index range is undefined and will produce arbitrary responses depending on the vendor.

Arrays consisting of fixed size elements do not require special treatment of individual elements.  Like the distinction of fixed sized structures and variable sized structures, arrays of fixed size elements and variable size elements are passed across an IDL interface differently.  The following table shows how in,out, inout and return types are passed across an IDL interface for a fixed size datatype X of a type defined array Array

Data Type in out inout return
typedef  X  Array[5]; const Array arg Array arg Array arg Array_slice *

The first example (#1) is of an array being used as an in parameter for a function called Method in an interface IExample.

// Example #1 : IDL for IN argument
	
typedef long Argument[10];

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


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


/////////////////////////////////////
void Servant::Method(const Argument arg)
	{
	for(int i=0;i<10;++i)
		{  cout<<"index "<<i<<" = "<< arg[i]<< endl; }
	}
	
//  Client Code
	{  IExample_var obj;

	/* Init obj code Here */
	
	Argument arg;
	for(int i=0;i<10;++i)
		{  arg[i]=100*i; }

	obj->Method(arg);
	}
	

 

The next example(#2) demonstrates an IDL out parameter.  Fixed sized structures are passed by reference when the out parameter is used.

	// Example #2 : IDL for OUT argument
	
typedef long Argument[10];

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

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


/////////////////////////////////////
void Servant::Method(Argument arg)
	{
	for(int i=0;i<10;++i)
		{  arg[i]=100*i; }
	}

	
//  Client Code
	{  IExample_var obj;

	/* Init obj code Here */
	
	Argument arg;
	obj->Method(arg);

	for(int i=0;i<10;++i)  // print out returned elements
		{  cout<<"index "<<i<<" = "<< arg[i]<< endl; }
    	}

 

Example #3 demonstrates an inout parameter.

	// Example #3 : IDL for INOUT 
	
typedef long Argument[10];

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


/////////////////////////////////////
void Servant::Method(Argument arg)
	{
	for(int i=0;i<10;++i) // print in array
		{  cout<<"index "<<i<<" = "<< arg[i]<< endl; }


	for(i=0;i<10;++i) // set out array
		{  arg[i]=100*i; }
	}
//  Client Code
	{  IExample_var obj;

	/* Init obj code Here */
	
	Argument arg;

	for(int i=0;i<10;++i)
		{  arg[i]=i; }

	obj->Method(arg);

	for(i=0;i<10;++i)
		{  cout<<"index "<<i<<" = "<< arg[i]<< endl; }

	}
    

 

Example #4 demonstrates the returning of a fixed sized element array.

	// Example #4 : IDL for returning an array
	
typedef long Argument[10];

interface IExample  // IDL Interface
	{
	Argument Method( ); // return an variable sized struct
	};
// Servant Code Implementation
///////////////////////////////////////
class Servant:public virtual POA_IExample
	{
	public:
	Argument_slice * Method(void);
	};


/////////////////////////////////////
Argument_slice * Servant::Method(void)
	{
	Argument_slice *ret=Argument_alloc();

	for(int i=0;i<10;++i) // set out array
		{  ret[i]=100*i; }

	return ret;
	}


	
//  Client Code
	{  IExample_var obj;

	/* Init obj code Here */
	
	Argument_slice *arg;

	arg=obj->Method( );

	for(i=0;i<10;++i)
		{  cout<<"index "<<i<<" = "<< arg[i]<< endl; }

	Argument_free(arg);
	}  

 

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