Enumerations are a set of named constants with an underlying value associated with them. In some languages, they can either be changed at runtime (mutable) or not changed at runtime (immutable).
Example:
Value
0
1
2
3
Name
“Small”
“Medium”
“Large”
“Xtra Large”
The name is represented by a String. The value can be represented by a value type. In XPS, Enumerations are generated from an Enumeration Class file (SQF) when added to a config file with the file typ set as “enum” - as a result the variable holding the Type Definition will also state “enum” (e.g. XPS_BT_enum_Status) instead of “typ”. The file is preprocessed and built into a Type Definition the same as “typ” SQF files. However, to convert them to proper Enumerations, we will build them using the XPS_fnc_createEnumeration
function instead of the createhashmap0bject
command. This is because from one Type Definition file we create several actual instances as constants and typically only do this once.
The XPS_fnc_createEnumeration
function creates two main things:
An example below inherits from the base class XPS_typ_Enumerations and sets a simple enumeration based on default incremental numbers. Alternatively, numerical values or other values such as a string could be explicitly defined
Note: The type definition XPS_typ_Enumerations is not an “enum” as it in itself doesn't create any Enumerations. Think of it as an abstract class.
Another Note: XPS currently only supports ValueType
to be set to Scalar (Number), String, Text (Structured Text), or Hashmap. Might include future support for any type that hashmaps support as keys.
Note: The ability to specify a hashmap as a value type is now supported. The lookup key on the Helper class is a string conversion of the hashmap itself since hashmaps can’t be used as keys on other hashmaps. Others value types that can be used as keys (Arrays, Booleans, Code, Config, Namespace, Side) are currently not supported at this time.
Pets.sqf File:
[
["#type","TAG_enum_Pets"],
["#base", XPS_typ_Enumeration],
["ValueType","SCALAR"], // Currently Must be either SCALAR, STRING, TEXT, or HASHMAP (default is "SCALAR" if not a supported type).
["Enumerations", [ "None", "Cat", "Dog", "Bird"]]
]
Alternatively, a Pets.sqf File which explicitly defines the values (useful if the values should be exact and/or non-incremental):
[
["#type","TAG_enum_Pets"],
["#base", XPS_typ_Enumeration],
["ValueType","SCALAR"], // Currently Must be either SCALAR, STRING, TEXT, or HASHMAP (default is "SCALAR" if not a supported type).
["Enumerations", [ ["None",0], ["Cat",1], ["Dog",2], ["Bird",3]]]
]
If we call, for example in a preInit function, the following:
["TAG_Pets", TAG_enum_Pets ] call XPS_fnc_createEnumeration;
We will end up with the following hashmap objects assigned to the associated global variables: