Project members: Enum

With the enum member you can add a user-defined enumeration data type consisting of uniquely named enumerators. Once defined, this data type can serve in the declaration of any data member within your project. At the runtime, the affected data member can assume exactly one of the enumerators listed within the corresponding enum member. Enum members are represented in Composer by following bricks:

Please note, an enum member can exist only within a unit member.

Add new enum

First switch to the Composer page for the respective unit member you want to add the new enum member.

Then, in the Gallery window, ensure that the folder Chora is opened.

Look in the folder for the template named Enum.

With the mouse, select the template and drag it into the Composer.

Drop the template within the Composer.

Adding a new enum member.

Name the enum

First ensure, that the enum member is selected.

Press the key F2 or select the menu item EDITRename ....

Enter the new name in the Inspector window.

Enum members have a global character - they can be accessed from everywhere within your project. To address an enum member, you must always use its full name, which is composed of the unit name, the member is defined inside, and the name of the member itself, both separated by :: (double colon) signs. For example Core::KeyCode.

Edit the enum

Every enum member can contain any arbitrary number of enumerator members. To add a new or edit existing enumerators open the enum member in a new Composer page:

First ensure, that the enum member is selected.

Press the key ENTER ...

... or double click on the enum member with the mouse.

Please note, newly added enum members contain already few dummy enumerators called per default FirstItem, SecondItem and so far. Feel free to rename or delete them.

Duplicate an existing enum

You can create any number of copies of an already existing enum member.

First ensure, that the enum member is selected.

Press the keys CtrlC and CtrlV in succession ...

... or hold the key Ctrl pressed while you drag and drop the selected enum member.

Finally rename the just duplicated enum member.

You can also Drag & Drop an enum member from the Browser into the Composer window and create so a duplicate of the affected member:

First switch to the Composer page for the respective unit member, where you want to add the duplicate of the enum.

Then ensure that the Browser window is visible.

Within the Browser window locate the desired enum member. This can be done easily with Browser's own filter function.

Select this enum member in the Browser window.

Drag and drop the selected enum member into the Composer:

Duplicating an enum by simply Drag & Drop it between the Browser and Composer windows

Finally rename the just duplicated enum member.

Use the enum member in data member declarations

Every enum member represents an individual user defined enumeration data type, you can use in the declaration of variables, properties, arrays, local variables, local arrays as well as in parameter and return type declaration of a method. The enumeration data type corresponds to the full name of the enum member, composed of the unit name, the member is defined inside, and the name of the member itself, both separated by :: (double colon) signs. For example:

// Declare a local variable of the 'Core::KeyCode' enumeration type var Core::KeyCode keyCode;

Use the enum member in expressions

The name of the enum member can be used within Chora expressions to form a literal of the corresponding enumeration data type. The literal is composed of the enumeration data type name and the name of a particular enumerator member defined inside the enum, both separated by the . (dot) sign. For example:

// Declare a local variable of the 'Core::KeyCode' enumeration type var Core::KeyCode keyCode = ...; // Does the variable store a value corresponding to the enumerator 'Left' // defined within the enumeration data type 'Core::KeyCode'? if ( keyCode == Core::KeyCode.Left ) trace "Evidently, the user has pressed the key 'left'.";

Control the code generation

With the attribute Generator you can selectively control, whether the enum member should be taken in account during the code generation or not. Configuring this attribute with the value false will exclude the member from your project unless the member name is explicitly involved within a Chora expression or data member declaration.

Delete an enum

First ensure, that the enum member is selected.

Press the key DEL or select the menu item EDITDelete.

Attributes of an enum

The following table shows the complete list of attributes provided by the enum member:

Attribute name

Short description

Brick

Determines the position and the size of a enum member brick within the Composer window.

Description

Contains the description for the enum member.

Generator

Controls the code generation for the affected enum member.

Chora syntax of an enum

This section describes the Chora syntax of how enums are stored inside Embedded Wizard project files. The knowledge about the syntax can be helpful when you plan to process the project contents by third party tools. In particular, the general purpose Export and Import functionality introduced in version 14 requires a good understanding of how project members are represented in Chora syntax to correctly interpret the exported members and synthesize the Chora code for members to import. Please note, Embedded Wizard project files (and thus Chora code) are encoded as text in UTF-8 format.

In Chora syntax, enum definition is always introduced with the keyword enum, followed by the name of the enum and a list of enum items enclosed between a pair of curly braces {..}. Concerning the syntax of an enum item, please see Chora syntax of an item.

The definition of an enum can be initiated by a comment block providing optional description for the enum. The content of this comment corresponds to the value of the enum's attribute Description. Furthermore the directives $rect and $output can be specified providing values for the corresponding attributes Brick and Generator. The following example demonstrates the described syntax:

// This is an 'enumeration' containing three items. // // $rect determines the position of the brick inside Composer window. // $output enforces this member to be taken in account during code \ // generation. $rect <10,10,210,50> $output true enum NameOfTheEnum { $rect <10,10,210,50> item FirstItem; $rect <220,10,420,50> item SecondItem = 3; $rect <430,10,630,50> item ThirdItem; }

Each Chora file (or text exchanged with an external Export and/or Import tool) can be (or even should be) prefixed with $version directive. This directive determines the version number of the included Chora code. For details concerning the syntax of the version please see the chapter $version macro. Furthermore, the code can include a $scope directive followed by a path to a project member acting as the original owner of the corresponding code. That means the members described by the Chora code existed inside the member specified in $scope. In case of an enum, $scope would specify the name of the unit the enum belongs to.