Difference between revisions of "Documentation/DevGuide/WritingUNO/Defining a Struct"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
Line 11: Line 11:
  
 
A plain struct instruction opens with the keyword <code>struct</code>, 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.
 
A plain struct instruction opens with the keyword <code>struct</code>, 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.
<source lang="idl">
+
<syntaxhighlight lang="idl">
 
   #ifndef __com_sun_star_reflection_ParamInfo_idl__  
 
   #ifndef __com_sun_star_reflection_ParamInfo_idl__  
 
   #define __com_sun_star_reflection_ParamInfo_idl__  
 
   #define __com_sun_star_reflection_ParamInfo_idl__  
Line 31: Line 31:
 
    
 
    
 
   #endif  
 
   #endif  
</source>
+
</syntaxhighlight>
 
UNOIDL supports inheritance of struct types. Inheritance is expressed by a colon <code>:</code> followed by the full name of the parent type. A struct type recursively inherits all members of the parent <code>struct</code> and their parents. For instance, derive from the struct <idl>com.sun.star.lang.EventObject</idl> to put additional information about new events into customized event objects to send to event listeners.
 
UNOIDL supports inheritance of struct types. Inheritance is expressed by a colon <code>:</code> followed by the full name of the parent type. A struct type recursively inherits all members of the parent <code>struct</code> and their parents. For instance, derive from the struct <idl>com.sun.star.lang.EventObject</idl> to put additional information about new events into customized event objects to send to event listeners.
<source lang="idl">
+
<syntaxhighlight lang="idl">
 
   // com.sun.star.beans.PropertyChangeEvent inherits from com.sun.star.lang.EventObject
 
   // com.sun.star.beans.PropertyChangeEvent inherits from com.sun.star.lang.EventObject
 
   // and adds property-related information to the event object
 
   // and adds property-related information to the event object
Line 44: Line 44:
 
       any NewValue;
 
       any NewValue;
 
   };
 
   };
</source>
+
</syntaxhighlight">
A new feature of {{PRODUCTNAME}} {{OOo2.x}} are ''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 <code><></code>, and its members can have these parameters as types:
+
A new feature of OpenOffice.org 2.x are ''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 <code><></code>, and its members can have these parameters as types:
<source lang="idl">
+
<syntaxhighlight lang="idl">
 
   // A polymorphic struct type template with two type parameters:
 
   // A polymorphic struct type template with two type parameters:
 
   struct Poly<T,U> {
 
   struct Poly<T,U> {
Line 54: Line 54:
 
       long member4;
 
       long member4;
 
   };
 
   };
</source>
+
</syntaxhighlight>
 
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:
 
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:
<source lang="idl">
+
<syntaxhighlight lang="idl">
 
   // Using an instantiation of Poly as a type in UNOIDL:
 
   // Using an instantiation of Poly as a type in UNOIDL:
 
   interface XIfc { Poly<boolean, any> fn(); };
 
   interface XIfc { Poly<boolean, any> fn(); };
</source>
+
</syntaxhighlight>
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Writing UNO Components]]
 
[[Category:Documentation/Developer's Guide/Writing UNO Components]]

Revision as of 16:22, 23 December 2020



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;
  };
</syntaxhighlight">
A new feature of OpenOffice.org 2.x are ''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 <code><></code>, and its members can have these parameters as types:
<syntaxhighlight lang="idl">
  // 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