Predefining Values

From Apache OpenOffice Wiki
Jump to: navigation, search



Predefined values can be provided, so that implementers do not have to use cryptic numbers or other literal values. There are two kinds of predefined values, constants and enums. Constants can contain values of any basic UNO type, except void. The enums are automatically numbered long values.

Const and Constants

The constants type is a container for const types. A constants instruction opens with the keyword constants, gives an identifier for the new group of const values and has the body in braces. It terminates with a semicolon. The constants body contains a list of const definitions that define the values of the members starting with the keyword const followed by a known type name and the identifier for the const in uppercase letters. Each const definition must assign a value to the const using an equals sign. The value must match the given type and can be an integer or floating point number, or a character, or a suitable const value or an arithmetic term based on the operators in the table below. The const definitions must end with a semicolon, as well.

  #ifndef __com_sun_star_awt_FontWeight_idl__
  #define __com_sun_star_awt_FontWeight_idl__
 
  module com { module sun { module star { module awt {
 
  constants FontWeight
  {
      const float DONTKNOW = 0.000000; 
      const float THIN = 50.000000; 
      const float ULTRALIGHT = 60.000000; 
      const float LIGHT = 75.000000; 
      const float SEMILIGHT = 90.000000; 
      const float NORMAL = 100.000000; 
      const float SEMIBOLD = 110.000000; 
      const float BOLD = 150.000000; 
      const float ULTRABOLD = 175.000000; 
      const float BLACK = 200.000000; 
  };
 
  }; }; }; };
Operators Allowed in const Meaning
+ addition
- subtraction
* multiplication
/ division
% modulo division
- negative sign
+ positive sign
| bitwise or
^ bitwise xor
& bitwise and
~ bitwise not
>> << bitwise shift right, shift left
Tip.png Use constants to group const types. In the Java language, binding a constants group leads to one class for all const members, whereas a single const is mapped to an entire class.


Enum

An enum type holds a group of predefined long values and maps them to meaningful symbols. It is equivalent to the enumeration type in C++. An enum instruction opens with the keyword enum, gives an identifier for the new group of enum values and has an enum body in braces. It terminates with a semicolon. The enum body contains a comma-separated list of symbols in uppercase letters that are automatically mapped to long values counting from zero, by default.

  #ifndef __com_sun_star_style_ParagraphAdjust_idl__ 
  #define __com_sun_star_style_ParagraphAdjust_idl__ 
 
  module com { module sun { module star { module style {
 
  enum ParagraphAdjust
  {
      LEFT,
      RIGHT,
      BLOCK,
      CENTER,
      STRETCH
  };
 
  }; }; }; };
  #endif

In this example, LEFT corresponds to 0, RIGHT corresponds to 1 and so forth.

An enum member can also be set to a long value using the equals sign. All the following enum values are then incremented starting from this value. If there is another assignment later in the code, the counting starts with that assignment:

  enum Error {
      SYSTEM = 10, // value 10
      RUNTIME, // value 11
      FATAL, // value 12
      USER = 30, // value 30
      SOFT // value 31
  };
Documentation caution.png The explicit use of enum values is deprecated and should not be used. It is a historical characteristic of the enum type but it makes not really sense and makes, for example language bindings unnecessarily complicated.


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