Difference between revisions of "Documentation/DevGuide/WritingUNO/Predefining Values"
m |
|||
(11 intermediate revisions by 5 users not shown) | |||
Line 6: | Line 6: | ||
|NextPage=Documentation/DevGuide/WritingUNO/Using Comments | |NextPage=Documentation/DevGuide/WritingUNO/Using Comments | ||
}} | }} | ||
+ | {{Documentation/DevGuideLanguages|Documentation/DevGuide/WritingUNO/{{SUBPAGENAME}}}} | ||
{{DISPLAYTITLE:Predefining Values}} | {{DISPLAYTITLE:Predefining Values}} | ||
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. | 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. | ||
Line 12: | Line 13: | ||
The <code>constants</code> type is a container for <code>const</code> types. A <code>constants</code> instruction opens with the keyword constants, gives an identifier for the new group of <code>const</code> values and has the body in braces. It terminates with a semicolon. The <code>constants</code> body contains a list of <code>const</code> definitions that define the values of the members starting with the keyword <code>const</code> followed by a known type name and the identifier for the <code>const</code> in uppercase letters. Each <code>const</code> definition must assign a value to the <code>const</code> 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 <code>const</code> value or an arithmetic term based on the operators in the table below. The <code>const</code> definitions must end with a semicolon, as well. | The <code>constants</code> type is a container for <code>const</code> types. A <code>constants</code> instruction opens with the keyword constants, gives an identifier for the new group of <code>const</code> values and has the body in braces. It terminates with a semicolon. The <code>constants</code> body contains a list of <code>const</code> definitions that define the values of the members starting with the keyword <code>const</code> followed by a known type name and the identifier for the <code>const</code> in uppercase letters. Each <code>const</code> definition must assign a value to the <code>const</code> 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 <code>const</code> value or an arithmetic term based on the operators in the table below. The <code>const</code> definitions must end with a semicolon, as well. | ||
− | < | + | <syntaxhighlight lang="idl"> |
#ifndef __com_sun_star_awt_FontWeight_idl__ | #ifndef __com_sun_star_awt_FontWeight_idl__ | ||
#define __com_sun_star_awt_FontWeight_idl__ | #define __com_sun_star_awt_FontWeight_idl__ | ||
Line 33: | Line 34: | ||
}; }; }; }; | }; }; }; }; | ||
+ | </syntaxhighlight> | ||
− | |||
{|border="1" cellpadding=4 style="border-collapse:collapse;" | {|border="1" cellpadding=4 style="border-collapse:collapse;" | ||
|-bgcolor=#EDEDED | |-bgcolor=#EDEDED | ||
Line 77: | Line 78: | ||
|} | |} | ||
− | {{ | + | {{Tip|Use <tt>constants</tt> to group <tt>const</tt> types. In the Java language, binding a constants group leads to one class for all <tt>const</tt> members, whereas a single <tt>const</tt> is mapped to an entire class.}} |
=== Enum === | === Enum === | ||
An <code>enum</code> type holds a group of predefined long values and maps them to meaningful symbols. It is equivalent to the enumeration type in C++. An <code>enum</code> instruction opens with the keyword <code>enum</code>, gives an identifier for the new group of <code>enum</code> values and has an <code>enum</code> body in braces. It terminates with a semicolon. The <code>enum</code> body contains a comma-separated list of symbols in uppercase letters that are automatically mapped to long values counting from zero, by default. | An <code>enum</code> type holds a group of predefined long values and maps them to meaningful symbols. It is equivalent to the enumeration type in C++. An <code>enum</code> instruction opens with the keyword <code>enum</code>, gives an identifier for the new group of <code>enum</code> values and has an <code>enum</code> body in braces. It terminates with a semicolon. The <code>enum</code> body contains a comma-separated list of symbols in uppercase letters that are automatically mapped to long values counting from zero, by default. | ||
− | < | + | <syntaxhighlight lang="idl"> |
#ifndef __com_sun_star_style_ParagraphAdjust_idl__ | #ifndef __com_sun_star_style_ParagraphAdjust_idl__ | ||
#define __com_sun_star_style_ParagraphAdjust_idl__ | #define __com_sun_star_style_ParagraphAdjust_idl__ | ||
Line 99: | Line 100: | ||
}; }; }; }; | }; }; }; }; | ||
#endif | #endif | ||
− | </ | + | </syntaxhighlight> |
− | In this example, < | + | In this example, <idlm>com.sun.star.style.ParagraphAdjust:LEFT</idlm> corresponds to 0, <idlm>com.sun.star.style.ParagraphAdjust:RIGHT</idlm> corresponds to 1 and so forth. |
An <code>enum</code> member can also be set to a <code>long</code> value using the equals sign. All the following <code>enum</code> values are then incremented starting from this value. If there is another assignment later in the code, the counting starts with that assignment: | An <code>enum</code> member can also be set to a <code>long</code> value using the equals sign. All the following <code>enum</code> values are then incremented starting from this value. If there is another assignment later in the code, the counting starts with that assignment: | ||
− | < | + | <syntaxhighlight lang="idl"> |
enum Error { | enum Error { | ||
SYSTEM = 10, // value 10 | SYSTEM = 10, // value 10 | ||
Line 111: | Line 112: | ||
SOFT // value 31 | SOFT // value 31 | ||
}; | }; | ||
− | </ | + | </syntaxhighlight> |
− | {{ | + | {{Warn|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.}} |
{{PDL1}} | {{PDL1}} | ||
− | [[Category: Writing UNO Components]] | + | |
+ | [[Category:Documentation/Developer's Guide/Writing UNO Components]] |
Latest revision as of 16:27, 23 December 2020
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 |
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 };
Content on this page is licensed under the Public Documentation License (PDL). |