Defining a Struct

From Apache OpenOffice Wiki
Jump to: navigation, search



A struct is a compound type which puts together arbitrary UNOIDL types to form a new data type. Its member data are not encapsulated, rather they are publicly available. Structs are frequently used to handle related data easily, and the event structs broadcast to event listeners.

A plain struct instruction opens with the keyword struct, gives an identifier for the new struct type and has a struct body in braces. It is terminated by a semicolon. The struct body contains a list of struct member declarations that are defined by a known type and an identifier for the struct member. The member declarations must end with a semicolon, as well.

  #ifndef __com_sun_star_reflection_ParamInfo_idl__ 
  #define __com_sun_star_reflection_ParamInfo_idl__ 
  
  #include <com/sun/star/reflection/ParamMode.idl> 
  
  module com { module sun { module star { module reflection {
  
  interface XIdlClass; // forward interface declaration
  
  struct ParamInfo 
  {
      string aName; 
      ParamMode aMode; 
      XIdlClass aType; 
  }; 
  
  }; }; }; }; 
  
  #endif

UNOIDL supports inheritance of struct types. Inheritance is expressed by a colon : followed by the full name of the parent type. A struct type recursively inherits all members of the parent struct and their parents. For instance, derive from the struct com.sun.star.lang.EventObject to put additional information about new events into customized event objects to send to event listeners.

  // com.sun.star.beans.PropertyChangeEvent inherits from com.sun.star.lang.EventObject
  // and adds property-related information to the event object
  struct PropertyChangeEvent : com::sun::star::lang::EventObject 
  {
      string PropertyName;
      boolean Further;
      long PropertyHandle;
      any OldValue;
      any NewValue;
  };

A new feature of OpenOffice.org 2.x were polymorphic struct types. A polymorphic struct type template is similar to a plain struct type, but it has one or more type parameters enclosed in angle brackets <>, and its members can have these parameters as types:

  // A polymorphic struct type template with two type parameters:
  struct Poly<T,U> {
      T member1;
      T member2;
      U member3;
      long member4;
  };

A polymorphic struct type template is not itself a UNO type - it has to be instantiated with actual type arguments to be used as a type:

  // Using an instantiation of Poly as a type in UNOIDL:
  interface XIfc { Poly<boolean, any> fn(); };
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages