Mapping of Struct Types

From Apache OpenOffice Wiki
Jump to: navigation, search



A plain UNO struct type is mapped to a C++ struct with the same name. Each member of the UNO struct type is mapped to a public data member with the same name and corresponding type. The C++ struct provides a default constructor which initializes all members with default values, and a constructor which takes explicit values for all members. If a plain struct type inherits from another struct type, the generated C++ struct derives from the C++ struct corresponding to the inherited UNO struct type.

A polymorphic UNO struct type template with a list of type parameters is mapped to a C++ struct template with a corresponding list of type parameters. For example, the C++ template corresponding to com.sun.star.beans.Optional looks something like

  template< typename T > struct Optional { 
      sal_Bool IsPresent;
      T Value;
 
      Optional(): IsPresent(sal_False), Value() {}
 
      Optional(sal_Bool theIsPresent, T const & theValue): IsPresent(theIsPresent), Value(theValue) {}
  };

As can be seen in the example above, the default constructor uses default initialization to give values to any parametric data members. This has a number of consequences:

  • Some compilers do not implement default initialization correctly for all types. For example, Microsoft Visual C++ .NET 2003 leaves objects of primitive types uninitialized, instead of zero-initializing them. (Which means, for example, that after Optional<sal_Int32> o; the expression o.Value has an undefined value, instead of being zero.)
  • The default value of a UNO enum type is its first member. A (deprecated) feature of UNO enum types is to give specific numeric values to individual members. Now, if a UNO enum type whose first member has a numeric value other than zero is used as the type of a parametric member, default-initializing that member will give it the numeric value zero, even if zero does not correspond to the default member of the UNO enum type (it need not even correspond to any member of the UNO enum type).
  • Another pitfall is that a parametric member of type any of a default-constructed polymorphic struct type instance (think Optional<Any> o; o.Value in C++, new Optional<Object>().Value in Java 1.5) has different values in the C++ language binding and the Java language binding. In C++, it contains void, whereas in Java it contains a null reference of type XInterface. To avoid any problems, it is best not to rely on the default constructor in such situations.

On some platforms, the C++ typedef types sal_uInt16 (representing the UNO type unsigned short) and sal_Unicode (representing the UNO type char) are synonyms for the same fundamental C++ type. This could lead to problems when either of those types is used as a type argument of a polymorphic struct type. The chosen solution is to generally forbid the (deprecated) UNO types unsigned short, unsigned long, and unsigned hyper as type arguments of polymorphic struct types.

 getCppuType(static_cast< com::sun::star::beans::Optional< sal_Unicode > >(0))

and

 getCppuType(static_cast< com::sun::star::beans::Optional< sal_uInt16 > >(0))

cannot return different data for the two different UNO types (as the two function calls are to the same identical function on those platforms). The chosen solution is to generally forbid the (deprecated) UNO types unsigned short, unsigned int, and unsigned long as type arguments of polymorphic struct types.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages