TwDefineEnum (function)

TwType TwDefineEnum(const char *name, const TwEnumVal *enumValues, unsigned int nbValues)

Description

This function creates a new TwType corresponding to a C/C++ enum. Thus it could be used with TwAddVar* functions to control variables of type enum.

Parameters

name

Specify a name for the enum type (must be unique).

enumValues

An array of structures of type TwEnumVal containing integer values and their associated labels (pointers to zero terminated strings) corresponding to the values.

nbValues

Number of elements of the enumValues array.

Return values

  • 0 if an error occurred (call TwGetLastError to retrieve the error).
  • 1 otherwise.

Example

typedef enum { SUMMER, FALL, WINTER, SPRING } Seasons;
Seasons season = WINTER;
 
TwEnumVal seasonsEV[] = { {SUMMER, "Summer"}, {FALL, "Fall"}, {WINTER, "Winter"}, {SPRING, "Spring"} };
TwType seasonType;
 
// ...
 
// Defining season enum type
seasonType = TwDefineEnum("SeasonType", seasonsEV, 4);
// Adding season to bar
TwAddVarRW(bar, "Season", seasonType, &season, NULL);

Note that enum values definition can also be done by TwDefineEnumFromString, or through the enum parameter of the def string of TwAddVar* like this:

typedef enum { SUMMER, FALL, WINTER, SPRING } Seasons;
Seasons season = WINTER;
TwType seasonType;
 
// ...
 
// Defining an empty season enum type
seasonType = TwDefineEnum("SeasonType", NULL, 0);
// Adding season to bar and defining seasonType enum values
TwAddVarRW(bar, "Season", seasonType, &season, " enum='0 {Summer}, 1 {Fall}, 2 {Winter}, 3 {Spring}' ");
// This will affect all variables that are of type seasonType.

See also