Table of Contents Previous Next
Logo
Client-Side Slice-to-C# Mapping : 14.13 Mapping for Classes
Copyright © 2003-2008 ZeroC, Inc.

14.13 Mapping for Classes

Slice classes are mapped to C# classes with the same name. By default, the generated class contains a public data member for each Slice data member (just as for structures and exceptions), and a member function for each operation.
Note that classes also support the "clr:property" metadata directive, so you can generate classes with virtual properties instead of data members. (See page 412 for details on "clr:property".)
Consider the following class definition:
class TimeOfDay {
    short hour;         // 0  23
    short minute;       // 0  59
    short second;       // 0  59
    string format();    // Return time as hh:mm:ss
};
The Slice compiler generates the following code for this definition:
public interface TimeOfDayOperations_
{
    string format(Ice.Current __current);
}

public interface TimeOfDayOperationsNC_
{
    string format();
}

public abstract class TimeOfDay
    : Ice.ObjectImpl,
      TimeOfDayOperations_,
      TimeOfDayOperationsNC_
{
    public short hour;
    public short minute;
    public short second;

    public TimeOfDay()
    {
    }

    public TimeOfDay(short hour, short minute, short second)
    {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    public string format()
    {
        return format(new Ice.Current());
    }

    public abstract string format(Ice.Current __current);
}
There are a number of things to note about the generated code:
1. The compiler generates "operations interfaces" called TimeOfDayOperations_ and TimeOfDayOperationsNC_. These interfaces contain a method for each Slice operation of the class.
2. The generated class TimeOfDay inherits (indirectly) from Ice.Object. This means that all classes implicitly inherit from Ice.Object, which is the ultimate ancestor of all classes. Note that Ice.Object is not the same as Ice.ObjectPrx. In other words, you cannot pass a class where a proxy is expected and vice versa.
If a class has only data members, but no operations, the compiler generates a non-abstract class.
3. The generated class contains a public member for each Slice data member.
4. The generated class inherits member functions for each Slice operation from the operations interfaces.
5. The generated class contains two constructors.
There is quite a bit to discuss here, so we will look at each item in turn.

14.13.1 Operations Interfaces

The methods in the <interfacename>Operations_ interface have an additional trailing parameter of type Ice.Current, whereas the methods in the <interfacename>OperationsNC_ interface lack this additional trailing parameter. The methods without the Current parameter simply forward to the methods with a Current parameter, supplying a default Current. For now, you can ignore this parameter and pretend it does not exist. (We look at it in more detail in Section 28.6.)
If a class has only data members, but no operations, the compiler omits generating the <interfacename>Operations_ and <interfacename>OperationsNC_ interfaces.

14.13.2 Inheritance from Ice.Object

Like interfaces, classes implicitly inherit from a common base class, Ice.Object. However, as shown in Figure 14.2, classes inherit from Ice.Object instead of Ice.ObjectPrx (which is at the base of the inheritance hierarchy for proxies). As a result, you cannot pass a class where a proxy is expected (and vice versa) because the base types for classes and proxies are not compatible.
Figure 14.2. Inheritance from Ice.ObjectPrx and Ice.Object.
Ice.Object contains a number of member functions:
namespace Ice
{
    public interface Object : System.ICloneable
    {
        int ice_hash();

        bool ice_isA(string s);
        bool ice_isA(string s, Current current);

        void ice_ping();
        void ice_ping(Current current);

        string[] ice_ids();
        string[] ice_ids(Current current);

        string ice_id();
        string ice_id(Current current);

        void ice_preMarshal();
        void ice_postUnmarshal();

        DispatchStatus ice_dispatch(
            Request request,
            DispatchInterceptorAsyncCallback cb);
    }
}
The member functions of Ice.Object behave as follows:
• ice_hash
This function returns a hash value for the class, allowing you to easily place classes into hash tables. The implementation returns the value of System.Object.GetHashCode.
• ice_isA
This function returns true if the object supports the given type ID, and false otherwise.
• ice_ping
As for interfaces, ice_ping provides a basic reachability test for the class.
• ice_ids
This function returns a string sequence representing all of the type IDs supported by this object, including ::Ice::Object.
• ice_id
This function returns the actual run-time type ID for a class. If you call ice_id through a reference to a base instance, the returned type id is the actual (possibly more derived) type ID of the instance.
• ice_preMarshal
The Ice run time invokes this function prior to marshaling the object’s state, providing the opportunity for a subclass to validate its declared data members.
• ice_postUnmarshal
The Ice run time invokes this function after unmarshaling an object’s state. A subclass typically overrides this function when it needs to perform additional initialization using the values of its declared data members.
• ice_dispatch
This function dispatches an incoming request to a servant. It is used in the implementation of dispatch interceptors (see Section 28.22).
Note that the generated class does not override GetHashCode and Equals. This means that classes are compared using shallow reference equality, not value equality (as is used for structures).
The class also provides a Clone method (whose implementation is inherited from Ice.ObjectImpl); the Clone method returns a shallow memberwise copy.

14.13.3 Data Members of Classes

By default, data members of classes are mapped exactly as for structures and exceptions: for each data member in the Slice definition, the generated class contains a corresponding public data member.
If you wish to restrict access to a data member, you can modify its visibility using the protected metadata directive. The presence of this directive causes the Slice compiler to generate the data member with protected visibility. As a result, the member can be accessed only by the class itself or by one of its subclasses. For example, the TimeOfDay class shown below has the protected metadata directive applied to each of its data members:
class TimeOfDay {
    ["protected"] short hour;   // 0  23
    ["protected"] short minute; // 0  59
    ["protected"] short second; // 0  59
    string format();    // Return time as hh:mm:ss
};
The Slice compiler produces the following generated code for this definition:
public abstract class TimeOfDay
    : Ice.ObjectImpl,
      TimeOfDayOperations_,
      TimeOfDayOperationsNC_
{
    protected short hour;
    protected short minute;
    protected short second;

    public TimeOfDay()
    {
    }

    public TimeOfDay(short hour, short minute, short second)
    {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    // ...
}
For a class in which all of the data members are protected, the metadata directive can be applied to the class itself rather than to each member individually. For example, we can rewrite the TimeOfDay class as follows:
["protected"] class TimeOfDay {
    short hour;         // 0  23
    short minute;       // 0  59
    short second;       // 0  59
    string format();    // Return time as hh:mm:ss
};
If a protected data member also has the clr:property directive, the generated property has protected visibility. Consider the TimeOfDay class once again:
["protected", "clr:property"] class TimeOfDay {
    short hour;         // 0  23
    short minute;       // 0  59
    short second;       // 0  59
    string format();    // Return time as hh:mm:ss
};
The effects of combining these two metadata directives are shown in the generated code below:
public abstract class TimeOfDay
    : Ice.ObjectImpl,
      TimeOfDayOperations_,
      TimeOfDayOperationsNC_
{
    private short hour_prop;
    protected short hour {
        get {
            return hour_prop;
        }
        set {
            hour_prop = value;
        }
    }

    // ...
}
See page 412 for more information on the property mapping for data members.

14.13.4 Operations of Classes

Operations of classes are mapped to abstract member functions in the generated class. This means that, if a class contains operations (such as the format operation of our TimeOfDay class), you must provide an implementation of the operation in a class that is derived from the generated class. For example:
public class TimeOfDayI : TimeOfDay
{
    public string format(Ice.Current current)
    {
        return   hour.ToString("D2") + ":"
               + minute.ToString("D2") + ":"
               + second.ToString("D2");
    }
}

Class Factories

Having created a class such as TimeOfDayI, we have an implementation and we can instantiate the TimeOfDayI class, but we cannot receive it as the return value or as an out-parameter from an operation invocation. To see why, consider the following simple interface:
interface Time {
    TimeOfDay get();
};
When a client invokes the get operation, the Ice run time must instantiate and return an instance of the TimeOfDay class. However, TimeOfDay is an abstract class that cannot be instantiated. Unless we tell it, the Ice run time cannot magically know that we have created a TimeOfDayI class that implements the abstract format operation of the TimeOfDay abstract class. In other words, we must provide the Ice run time with a factory that knows that the TimeOfDay abstract class has a TimeOfDayI concrete implementation. The Ice::Communicator interface provides us with the necessary operations:
module Ice {
    local interface ObjectFactory {
        Object create(string type);
        void destroy();
    };

    local interface Communicator {
        void addObjectFactory(ObjectFactory factory, string id);
        ObjectFactory findObjectFactory(string id);
        // ...
    };
};
To supply the Ice run time with a factory for our TimeOfDayI class, we must implement the ObjectFactory interface:
class ObjectFactory : Ice.ObjectFactory
{
    public Ice.Object create(string type)
    {
        if (type.Equals("::M::TimeOfDay"))
            return new TimeOfDayI();
        System.Diagnostics.Debug.Assert(false);
        return null;
    }

    public void destroy()
    {
        // Nothing to do
    }
}
The object factory’s create method is called by the Ice run time when it needs to instantiate a TimeOfDay class. The factory’s destroy method is called by the Ice run time when its communicator is destroyed.
The create method is passed the type ID (see Section 4.13) of the class to instantiate. For our TimeOfDay class, the type ID is "::M::TimeOfDay". Our implementation of create checks the type ID: if it is "::M::TimeOfDay", it instantiates and returns a TimeOfDayI object. For other type IDs, it asserts because it does not know how to instantiate other types of objects.
Given a factory implementation, such as our ObjectFactory, we must inform the Ice run time of the existence of the factory:
Ice.Communicator ic = ...;
ic.addObjectFactory(new ObjectFactory(), "::M::TimeOfDay");
Now, whenever the Ice run time needs to instantiate a class with the type ID "::M::TimeOfDay", it calls the create method of the registered ObjectFactory instance, which returns a TimeOfDayI instance to the Ice run time.
The destroy operation of the object factory is invoked by the Ice run time when the communicator is destroyed. This gives you a chance to clean up any resources that may be used by your factory. Do not call destroy on the factory while it is registered with the communicator—if you do, the Ice run time has no idea that this has happened and, depending on what your destroy implementation is doing, may cause undefined behavior when the Ice run time tries to next use the factory.
The run time guarantees that destroy will be the last call made on the factory, that is, create will not be called concurrently with destroy, and create will not be called once destroy has been called. However, calls to create can be made concurrently.
Note that you cannot register a factory for the same type ID twice: if you call addObjectFactory with a type ID for which a factory is registered, the Ice run time throws an AlreadyRegisteredException.
Finally, keep in mind that if a class has only data members, but no operations, you need not create and register an object factory to transmit instances of such a class. Only if a class has operations do you have to define and register an object factory.

14.13.5 Class Constructors

The generated class contains both a default constructor, and a constructor that accepts one argument for each member of the class. This allows you to create and initialize a class in a single statement, for example:
TimeOfDayI tod = new TimeOfDayI(14, 45, 00); // 2:45pm
For derived classes, the constructor requires one argument of all of the members of the class, including members of the base class(es). For example, consider the the definition from Section 4.11.2 once more:
class TimeOfDay {
    short hour;         // 0  23
    short minute;       // 0  59
    short second;       // 0  59
};

class DateTime extends TimeOfDay {
    short day;          // 1  31
    short month;        // 1  12
    short year;         // 1753 onwards
};
The constructors for the generated classes are as follows:
public class TimeOfDay : Ice.ObjectImpl
{
    public TimeOfDay() {}

    public TimeOfDay(short hour, short minute, short second)
    {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    // ...
}

public class DateTime : TimeOfDay
{
    public DateTime() : base() {}

    public DateTime(short hour,
                    short minute,
                    short second,
                    short day,
                    short month,
                    short year) : base(hour, minute, second)
    {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    // ...
}
In other words, if you want to instantiate and initialize a DateTime instance, you must either use the default constructor or provide data for all of the data members of the instance, including data members of any base classes.
Table of Contents Previous Next
Logo