PreviousNext

Specifying a C++ Class via an IDL Interface

An IDL interface definition and a C++ class are very similar. An IDL interface definition specifies the data structures and operations that an application needs to use a distributed interface. A C++ class specifies the data structures and functions that an application needs to use a type of object. IDL and the IDL compiler blend together the distributed computing capabilities of an interface definition with the object-oriented features of a C++ class to specify distributed objects.

The following example shows an interface definition for a distributed Matrix object:

FILE matrix.idl ---------------------------------------------------
[
uuid(24cb0eda-3eb9-11ce-b1ce-08002bbbf636)
] interface Matrix
{
/* Create a new 2 by 2 Matrix. This operation requires an ACF */
/* to tell the stubs this is a creator operation. */
Matrix * createMatrix(
[in] long v11,
[in] long v12,
[in] long v21,
[in] long v22
);

/* Create a new Matrix of size rows by columns and return TRUE.*/
/* If server does not support the size requested, return FALSE */
/* and the maximum size in rows and columns that it supports.*/
/* This operation requires an ACF to tell the stubs this is a */ /* static operation.
*/ boolean newMatrix(
[in, out] long &rows,
[in, out] long &columns,
[out] Matrix ** m
);

/* The rest of the operations operate on the existing object */
/* that invokes them (this Matrix). */

/* Set a new value in this Matrix. */
void set(
[in] long row,
[in] long col,
[in] long value
);

/* Get a value from this Matrix. */
long get(
[in] long row,
[in] long col
);

/* Return a new Matrix that is the inverse of this Matrix. */
Matrix * inverse();

/* Return a new Matrix that is the product of this Matrix */
/* and m1.*/
Matrix * multiply(
[in] Matrix * m1
);

/* Return in Matrix m2 the sum of this Matrix and m1. */
void add(
[in] Matrix * m1,
[out] Matrix ** m2
);
}

Interface definitions and C++ classes are both specifications, not implementations. An implementation of the IDL interface definition is a server's manager code, and an instance of the class is an object of that class. The operations of the Matrix interface are described as follows:

createMatrix
If an interface designer expects clients to create dynamic objects, at least one operation must be a static function that creates a new object. For example, the createMatrix operation is intended to be a static member function. Static member functions do not require an existing object before they are called.

newMatrix
Parameters are typically passed-by-value in C++. Apply the reference operator (&) to parameters you want to pass by reference. A reference parameter is required if the function changes the value. In this example, the number of the rows and columns are input but the values change if the function cannot create the Matrix requested.

set
The set operation sets an individual value in an existing Matrix object.

get
The get operation obtains an individual value from an existing Matrix object.

inverse
The inverse operation returns a new Matrix object that is the inverse of an existing Matrix object.

add
The add operation does not return a value but has an output parameter that is a new Matrix object. The output is the sum of an existing Matrix object and an input parameter that is a Matrix object.

It is not appropriate to encapsulate data in the definition of a public interface; that would be implementation detail, which does not belong in the interface definition. Therefore, there is no IDL concept of private data as there is in C++. However, the base class from which all IDL interface classes are derived does encapsulated binding data and RPC mechanisms.