UniEnu - Using IDL Unions & Enums

Description:

CORBA IDL enums will be simply mapped into their C# counterparts.

CORBA IDL unions will result in standard C# classes, since C# does not have variant structs (i.e. unions). The corresponding C# class contains a readonly 'discriminator' attribute which is initialized by the first writing access of one of its attributes.

Because a union is a complex data type (and will therefore be passed by value) a complete implementation of the IDL type is generated.

Source:

\Demo\UniEnum

Mapping:

CORBA IDL enum <---> C# enum
CORBA IDL union <---> C# class


Example

Step 1. The IDL (UniEnu.idl):

    module UniEnu 
    {
        enum property { surname, address, age, female};    
        
        union uniProperty switch (property)
        {
            case surname : string  strSurname;
            case address : string  strAddress;
            case age     : long    iAge;
            case female  : boolean bFemale;
        };
        
        interface Personal
        {
            void queryByName( in string strName, in property oPropIn, out uniProperty oPropOut);    
        };
    }; 

(Will be compiled into UniEnu.cs)

Step 2. The Server Implementation (UniEnuSrv.cs):

Since all declared data types are completely generated, we only have to implement the interface 'Personal':

    public class PersonalImpl: UniEnu.PersonalPOA
    {
        public override void queryByName( string a_strName, UniEnu.property a_oProp, out UniEnu.uniProperty  a_oPropOut )     
        {
            a_oPropOut = new UniEnu.uniProperty();
            
            switch( a_oProp )
            {
                case UniEnu.property.surname:
                    a_oPropOut.strSurname = a_strName + ", Dummy";
                    break;
                    
                case UniEnu.property.address:
                    a_oPropOut.strAddress = "Hamburg";
                    break;
                    
                case UniEnu.property.age:
                    a_oPropOut.iAge = 39;
                    break;
                    
                case UniEnu.property.female:
                    a_oPropOut.bFemale = false;
                    break;
            }
        }
    }  

The 'queryByName()' method illustrates the use of unions:

Step 3. The Client (UniEnuClt.cs):

The client retrieves this union instance from the server and evaluates it:

    UniEnu.uniProperty ouniProperty;
    
    for( UniEnu.property i = UniEnu.property.surname; i <= UniEnu.property.female; i++ )
    {
        m_oPersonal.queryByName("Hans", i, out ouniProperty);
        
        switch( ouniProperty.discriminator)
        {
            case UniEnu.property.surname:
                System.Console.WriteLine("Surname:{0}", ouniProperty.strSurname);
                break;
                
            case UniEnu.property.address:
                System.Console.WriteLine("Address:{0}", ouniProperty.strAddress);
                break;
                
            case UniEnu.property.age:
                System.Console.WriteLine("Age:{0}", ouniProperty.iAge);
                break;
                
            case UniEnu.property.female:
                System.Console.WriteLine("Sex:{0}", (ouniProperty.bFemale ?"Female":"Male"));     
                break;
        }
    }

Step 4. Run the example

a.) Start the Server.

b.) Start the Client.