Main page / CORBA with MinCor.NET page

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: 
	MinCor\Demo\UniEnum

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



Example 

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)


The Implementation (ServerImpl.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: 
    
    Depending on the discriminator of the union passed into the method 'queryByName()' 
    a newly created union instance is filled with data. The newly created union is 
    then pushed back through an IDL 'out' parameter.
    
    
The Client (ClientImpl.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++)
    {
        oPersonal.queryByName("Hans", i, out ouniProperty);
        switch( ouniProperty.discriminator)
        {
            case UniEnu.property.surname:
                srLog += "\n  Surname:" + ouniProperty.strSurname;
                break;
                    
            case UniEnu.property.address:
                srLog += "\n  Address:"+ ouniProperty.strAddress;
                break;
                    
            case UniEnu.property.age:
                srLog += "\n  Age:"+ ouniProperty.iAge;
                break;
                
            case UniEnu.property.female:
                srLog += "\n  Sex:"+ (ouniProperty.bFemale ?"Female":"Male");
                break;
        }
    }