FreeCAD C++
Public Member Functions | Protected Member Functions | List of all members
App::Enumeration Class Reference

#include <App/Enumeration.h>

Detailed Description

A bidirectional string-integer mapping.

This is mainly intended for two purposes: working around the difficulty in C++ of sharing enumerations between different source files, namespaces, etc. and as the data type stored by App::PropertyEnumeration

Internally, Enumeration maintains

  1. Either a const pointer to an array of C-style strings, or a vector of C++ std::strings
  2. An integer index into that array/vector representing the string representing the instance's value.

If built with FC_DEBUG defined, some boundaries of passed in pointers will be checked. Otherwise, the caller has the responsibility of checking the limits of given indices.

Public Member Functions

 Enumeration ()
 Constructs an empty Enumeration object.
 
 Enumeration (const Enumeration &other)
 Standard copy constructor.
 
 Enumeration (const char *valStr)
 Constructs an Enumeration with a single element.
 
 Enumeration (const char **list, const char *valStr)
 Constructs an Enumeration using val within list.
 
 ~Enumeration ()
 Standard destructor.
 
void setEnums (const char **plEnums)
 
void setEnums (const std::vector< std::string > &values)
 Set all enum values as vector of strings. More...
 
void setValue (const char *value)
 Set the enum using a C string.
 
void setValue (const std::string &value)
 Overload of setValue(const char *value)
 
void setValue (long value, bool checkRange=false)
 Set the enum using a long. More...
 
bool isValue (const char *value) const
 Checks if the property is set to a certain string value.
 
bool contains (const char *value) const
 Checks if a string is included in the enumeration.
 
const char * getCStr (void) const
 Return the value as C string. More...
 
int getInt (void) const
 Return value as integer. More...
 
std::vector< std::string > getEnumVector (void) const
 get all possible enum values as vector of strings
 
const char ** getEnums (void) const
 get pointer to the enum list
 
bool isValid (void) const
 Returns true if the instance is in a usable state.
 
int maxValue (void) const
 Returns the highest usable integer value for this enum. More...
 
Enumerationoperator= (const Enumeration &other)
 Assignment operator.
 
bool operator== (const Enumeration &other) const
 true iff our string representation matches other's More...
 
bool operator== (const char *other) const
 true iff our string representation matches other More...
 

Protected Member Functions

bool isCustom (void) const
 Returns true if instance was not initialized via static string list.
 
void findMaxVal (void)
 Updates _maxVal.
 
void tearDown (void)
 De-allocates memory used in _EnumArray. More...
 

Member Function Documentation

◆ getCStr()

const char* App::Enumeration::getCStr ( void  ) const

Return the value as C string.

Returns NULL if the enumeration is invalid.

◆ getInt()

int App::Enumeration::getInt ( void  ) const

Return value as integer.

Returns -1 if the Enumeration isn't valid

◆ maxValue()

int App::Enumeration::maxValue ( void  ) const

Returns the highest usable integer value for this enum.

Returns -1 if the enumeration is not valid according to isValid()

◆ operator==() [1/2]

bool App::Enumeration::operator== ( const Enumeration other) const

true iff our string representation matches other's

Returns false if either Enumeration is not valid.

◆ operator==() [2/2]

bool App::Enumeration::operator== ( const char *  other) const

true iff our string representation matches other

Returns false if Enumeration is not valid.

◆ setEnums() [1/2]

void App::Enumeration::setEnums ( const char **  plEnums)

Sets the enumeration string list The list is a NULL terminated array of pointers to const char* strings.

const char enums[] = {"Black","White","Other",NULL}

If Enumeration was already valid, will attempt to preserve the string-representation value of the Enumeration

Enumeration does not take ownership of the passed object

◆ setEnums() [2/2]

void App::Enumeration::setEnums ( const std::vector< std::string > &  values)

Set all enum values as vector of strings.

This method causes the Enumeration to dynamically allocate it's own array of C Strings, which will be deleted by the destructor or subsequent calls to setEnums(). So, it is important to make sure the Enumeration stays in scope as long as values returned by getCStr are in use.

If Enumeration was already valid, will attempt to preserve the string-representation value of the Enumeration

◆ setValue()

void App::Enumeration::setValue ( long  value,
bool  checkRange = false 
)

Set the enum using a long.

if checkRange is set to true, throws Base::ValueError when values are set out of range

Checks for boundaries via assert()

◆ tearDown()

void App::Enumeration::tearDown ( void  )
protected

De-allocates memory used in _EnumArray.

Important to not call this unless this Enumeration owns array.