TReK C++  5.3.3
Telemetry/Command API
trek::Enumerator Class Reference

This class maps unsigned integer values to strings for enumeration. More...

#include <enumerator.h>

Inherits BaseEnumerator.

Public Member Functions

Constructors, Destructor, and Other Basic Methods
 Enumerator ()
 Default constructor of the class.
 
 Enumerator (Enumerator &input)
 Copy constructor of the class.
 
virtual ~Enumerator ()
 Class destructor. More...
 
void operator= (Enumerator &right_side)
 Provides the equal operator.
 
bool operator== (Enumerator &right_side)
 Provides the == operator.
 
virtual EnumeratorClone ()
 Returns an exact copy of the object;.
 
virtual void Init ()
 Initializes the object. More...
 
Setup Methods

These methods control setting up the API.

int32_t Add (uint32_t int_value, const char *str_value_ptr, uint32_t end_value=0)
 Adds the string values as enumeration for the unsigned integer value. More...
 
int32_t Remove (const char *input)
 Removes the enumeration specifed by the string. More...
 
int32_t Remove (uint32_t input)
 Removes the enumeration specifed by the integer. More...
 
void RemoveAll ()
 Removes all enumerations.
 
Get Methods
int32_t Get (const char *str_value, uint32_t *int_value)
 Finds the corresponding unsigned integer value for the specified string. More...
 
int32_t Get (uint32_t int_value, char *output_str, uint32_t *size_of_output)
 Finds the corresponding string value for the specified unsigned integer. More...
 
uint32_t GetNumberOfEnumerations ()
 Returns the current number of enumerations.
 
void GetEnumerations (StringArray &str_array)
 Gets a list of the string enumerations. More...
 
int32_t GetEnumerations (uint32_t *uint_array, uint32_t *num_available)
 Gets a list of the integer values. More...
 
int32_t GetEnumerations (uint32_t *start_array, uint32_t *end_array, uint32_t *num_available)
 Gets a list of the start and end values for which enumerators exist. For an Enumerator, the start and end values will always be the same. More...
 
Serialization Methods

These methods help read and write the class to disk or memory. Most users will not be interested in these. Those that are can continue reading.

int32_t LoadFile (const char *filename)
 Loads the Enumerator definition from the specified file. More...
 
int32_t SaveFile (const char *filename)
 Saves the Enumerator definition to the specified file. More...
 

Detailed Description

This class maps unsigned integer values to strings for enumeration.

The unsigned integer values is limited to 32 bits.

Constructor & Destructor Documentation

◆ ~Enumerator()

trek::Enumerator::~Enumerator ( )
virtual

Class destructor.

Removes all resources created with this instance of the class.

Member Function Documentation

◆ Add()

int32_t trek::Enumerator::Add ( uint32_t  int_value,
const char *  str_value,
uint32_t  end_value = 0 
)

Adds the string values as enumeration for the unsigned integer value.

Parameters
[in]int_valueThe integer value.
[in]str_valueThe string value.
[in]end_valueThis parameter defaults to zero and is ignored by the method.
Returns
SUCCESS
TREK_DATA_ALREADY_EXISTS
TREK_DATA_NULL_PTR

Example:

int32_t ret_value;
// don't forget to check return values
ret_value = e.Add( 0, "OFF" );
ret_value = e.Add( 1, "ON" );
This class maps unsigned integer values to strings for enumeration.
Definition: enumerator.h:21
int32_t Add(uint32_t int_value, const char *str_value_ptr, uint32_t end_value=0)
Adds the string values as enumeration for the unsigned integer value.
Definition: enumerator.cpp:159

◆ Get() [1/2]

int32_t trek::Enumerator::Get ( const char *  str_value,
uint32_t *  int_value 
)

Finds the corresponding unsigned integer value for the specified string.

If return code is SUCCESS, then the int_value contains a valid value.

Parameters
[in]str_valueString value to find.
[out]int_valueCorresponding integer value.
Returns
SUCCESS
TREK_DATA_DOES_NOT_EXIST
TREK_DATA_NULL_PTR

Example:

uint32_t value;
ret_value = e.Get( "OFF", &value );
if( ret_value )
; // process error
int32_t Get(const char *str_value, uint32_t *int_value)
Finds the corresponding unsigned integer value for the specified string.
Definition: enumerator.cpp:296

◆ Get() [2/2]

int32_t trek::Enumerator::Get ( uint32_t  int_value,
char *  output_str,
uint32_t *  size_of_output 
)

Finds the corresponding string value for the specified unsigned integer.

If return code is SUCCESS, then the output_str contains a valid value.

Parameters
[in]int_valueInteger value to find.
[out]output_strCorresponding string value.
[in,out]size_of_outputThe size of the output buffer. If return code is TREK_DATA_NOT_ENOUGH_SPACE, then this value is reset to the number of bytes required for the given enumerated value.
Returns
SUCCESS
TREK_DATA_DOES_NOT_EXIST
TREK_DATA_NOT_ENOUGH_SPACE
TREK_DATA_NULL_PTR

Example:

char str_value[10];
uint32_t size_of_output = 10;
ret_value = e.Get( 0, str_value, &size_of_output );
if( ret_value )
; // process error
else
printf("%s\n", str_value);

◆ GetEnumerations() [1/3]

void trek::Enumerator::GetEnumerations ( StringArray str_array)

Gets a list of the string enumerations.

Deletes any items in the provided array prior to insertion.

Parameters
[out]str_arrayContains all of the current string enumerations.

Example:

StringArray str_values;
e.GetEnumerations( str_values );
for( uint32_t ii = 0; ii < str_values.Size(); ii++ )
printf("index %u = %s\n", ii, str_values.GetAt(ii) );
void GetEnumerations(StringArray &str_array)
Gets a list of the string enumerations.
Definition: enumerator.cpp:382
This class provides a wrapper for std::vector and std::string so different versions of the standard t...
Definition: string_array.h:19
uint32_t Size()
Returns the number of items in the string array.
Definition: string_array.cpp:67
const char * GetAt(uint32_t index)
Returns the string at the given location.
Definition: string_array.cpp:89

◆ GetEnumerations() [2/3]

int32_t trek::Enumerator::GetEnumerations ( uint32_t *  start_array,
uint32_t *  end_array,
uint32_t *  num_available 
)

Gets a list of the start and end values for which enumerators exist. For an Enumerator, the start and end values will always be the same.

The number of items needed can be found by a call to GetNumberOfEnumerations().

Parameters
[out]start_arrayContains all of the start integer values.
[out]end_arrayContains all of the end integer values.
[in,out]num_availableThe number of available slots in the array on input. Set to the number of valid items in the array on output.
Returns
SUCCESS
TREK_DATA_NOT_ENOUGH_SPACE
TREK_DATA_NULL_PTR

Example:

int32_t ret_value;
uint32_t int_values[10], end_values[10];
uint32_t num_available = 10;
ret_value = e.GetEnumerations( start_values, end_values, &num_available );
if( ret_value == SUCCESS )
{
// start and end are same for this type of enumerator
uint32_t ii = 0;
for( ii = 0; ii < num_available; ii++ )
printf( "Enumerated Value: %u\n", start_values[ii] );
}
#define SUCCESS
The function completed successfully.
Definition: trek_error.h:8

◆ GetEnumerations() [3/3]

int32_t trek::Enumerator::GetEnumerations ( uint32_t *  uint_array,
uint32_t *  num_available 
)

Gets a list of the integer values.

The number of items needed can be found by a call to GetNumberOfEnumerations().

Parameters
[out]uint_arrayContains all of the current integer values.
[in,out]num_availableThe number of available slots in the array on input. Set to the number of valid items in the array on output.
Returns
SUCCESS
TREK_DATA_NOT_ENOUGH_SPACE
TREK_DATA_NULL_PTR

Example:

int32_t ret_value;
uint32_t int_values[10];
uint32_t num_available = 10;
ret_value = e.GetEnumerations( int_values, &num_available );
if( ret_value == SUCCESS )
{
uint32_t ii = 0;
for( ii = 0; ii < num_available; ii++ )
printf( "Enumerated Value: %u\n", int_values[ii] );
}

◆ Init()

void trek::Enumerator::Init ( )
virtual

Initializes the object.

Removes all current enumerations.

◆ LoadFile()

int32_t trek::Enumerator::LoadFile ( const char *  filename)

Loads the Enumerator definition from the specified file.

Reads an XML format for the Enumerator. It can be written in with SaveFile.

Parameters
[in]filenameThe file to load the Enumerator object from.
Returns
SUCCESS
TREK_DATA_NULL_PTR
Other return codes

Example:

ret_value = e.LoadFile( "my_file.xml" );
int32_t LoadFile(const char *filename)
Loads the Enumerator definition from the specified file.
Definition: enumerator.cpp:653

◆ Remove() [1/2]

int32_t trek::Enumerator::Remove ( const char *  input)

Removes the enumeration specifed by the string.

Parameters
[in]inputString value of enumeration to remove.
Returns
SUCCESS
TREK_DATA_DOES_NOT_EXIST
TREK_DATA_NULL_PTR

Example:

int32_t ret_value;
ret_value = e.Remove( "Off" );
if( ret_value )
; // process error
int32_t Remove(const char *input)
Removes the enumeration specifed by the string.
Definition: enumerator.cpp:194

◆ Remove() [2/2]

int32_t trek::Enumerator::Remove ( uint32_t  input)

Removes the enumeration specifed by the integer.

Parameters
[in]inputInteger value of enumeration to remove.
Returns
SUCCESS
TREK_DATA_DOES_NOT_EXIST

Example:

ret_value = e.Remove( 1 );
if( ret_value )
; // process error

◆ SaveFile()

int32_t trek::Enumerator::SaveFile ( const char *  filename)

Saves the Enumerator definition to the specified file.

Writes an XML format for the Enumerator. It can be read in with LoadFile.

Parameters
[in]filenameThe file to save the Enumerator object to.
Returns
SUCCESS
TREK_DATA_NULL_PTR
Other return codes

Example:

ret_value = e.SaveFile( "my_file.xml" );
int32_t SaveFile(const char *filename)
Saves the Enumerator definition to the specified file.
Definition: enumerator.cpp:687