2

We publish the IDL files to represent the interface and would have a similar structure with C++/C code so that we can use to map them when the interface is invoked. So some conversion (equivalent) representation would be required.
Like a sequence in IDL is represented by list in C++ and etc.

Recently, I came across a complex (unique), valid IDL file like -

union HostAddress switch(EAddType)
 {  
      case E_IPV_4:  
          char ipv4Addr[4];  
      case E_IPV_6:  
           char ipv6Addr[16]; 
 };

  • How does this structure help us? What is the meaning of having such a union?
  • How do we represent the above IDL structure in C/C++?
kumar_m_kiran
  • 3,982
  • 4
  • 47
  • 72

3 Answers3

0

A union structure gives you an opaque data block that is large enough to contain the largest of the members. in this case you will have an object with the type field and then 16 bytes to contain the ipv6 address and it will also store the ipv4 address, this allows you to make simple code that can process both ipv4 and v6 addresses without the expense of a full object. Unions are normally used for small objects like this where you will waste space to have a unified object that can handle all the cases. see wikipedia article on Unions and wikibook page on Unions

hope that helps.

h4ck3rm1k3
  • 2,060
  • 22
  • 33
  • Yes, but what if the structure contains a different complex structures like strings? – kumar_m_kiran Aug 20 '12 at 07:57
  • In C, strings are stored in separate objects, you would need to make fixed sized strings to put them into a union. It seems that in IDL this is not a problem http://www.linuxjournal.com/article/6584 – h4ck3rm1k3 Aug 20 '12 at 12:44
0

I did find a good explanation at Websphere documentation, CORBA documentation. I particularly liked the detailed explanation in CORBA documentation. Any additional input, please answer to my post!

So the below example

union HostAddress switch(EAddType)
 {  
      case E_IPV_4:  
          char ipv4Addr[4];  
      case E_IPV_6:  
           char ipv6Addr[16]; 
 };

would be implemented as -

struct HostAddress
{
    EIPVSET _discriminant;
    long _d() { return _discriminant; }

    HostAddress() { _discriminant = E_IPV_INVALID; }
    string addressIpv6()   //get function overloaded
    {
       return string(this.ipv6Addr);
    }

    void addressIpv6(const string& ipv6Addr) //set function overloaded
    {
        strncpy(this.ipv6Addr,ipv6Addr.c_str(),16);
        return;
    }

    //similarly for other member variables.

}
kumar_m_kiran
  • 3,982
  • 4
  • 47
  • 72
0

An IDL union is similar to a union in C or C++. The fields share the same memory in order to save space. You can find the complete set of rules for how the IDL union maps to C++ in the IDL to C++ Mapping Document.

In general my advice would be to avoid them. They are confusing and difficult to use in the C++ mapping.

Brian Neal
  • 31,821
  • 7
  • 55
  • 59
  • I have to agree that they are difficult to use in the IDL to C++ mapping, but they are much easier to use in the new IDL to C++11 language mapping which got recently recommended for adoption by the OMG, see http://www.omg.org for the specification and http://osportal.remedy.nl for some examples. – Johnny Willemsen Aug 18 '12 at 19:28
  • @JohnnyWillemsen, yes I think the new mapping is great. Still I'd would never use a union, which is a very low-level construct, in something high-level like CORBA. – Brian Neal Aug 19 '12 at 03:56
  • @BrianNeal: In case you are using the IDL published by another entity, you will have little scope in "avoiding" them :) – kumar_m_kiran Aug 20 '12 at 07:58
  • @kumar_m_kiran that is very true! Unfortunately it is hard to educate everyone on how difficult unions are. People have to learn by experience it seems. :) – Brian Neal Aug 20 '12 at 13:02