Mistake on this page?
Report an issue in GitHub or email us
Public Types | Public Member Functions | Protected Member Functions | Friends
SafeEnum< Target, LayoutType > Struct Template Reference

Helper class used to define safe enumerations. More...

#include <SafeEnum.h>

Public Types

typedef LayoutType representation_t
 Type of the representation. More...
 

Public Member Functions

LayoutType value () const
 Explicit access to the inner value of the SafeEnum instance. More...
 
const LayoutType * storage () const
 Return a pointer to the inner storage. More...
 

Protected Member Functions

 SafeEnum (LayoutType value)
 Construction of an enumeration value. More...
 

Friends

bool operator== (Target lhs, Target rhs)
 Equal to operator for Target instances. More...
 
bool operator!= (Target lhs, Target rhs)
 Not equal to operator for Target instances. More...
 
bool operator< (Target lhs, Target rhs)
 Less than operator for Target instances. More...
 
bool operator<= (Target lhs, Target rhs)
 Less than or equal to operator for Target instances. More...
 
bool operator> (Target lhs, Target rhs)
 Greater than operator for Target instances. More...
 
bool operator>= (Target lhs, Target rhs)
 Greater than or equal to operator for Target instances. More...
 

Detailed Description

template<typename Target, typename LayoutType = unsigned int>
struct ble::SafeEnum< Target, LayoutType >

Helper class used to define safe enumerations.

C++ 98 enums expose different security holes:

This template class expose a framework to overcome those issues:

First enum has to be defined in a structure which inherit from this class. The target type is the name of the structure containing the enumeration while LayoutType is the inner type used to stored the enum.

Comparison operator are provided so it is not possible to compare a SafeEnum of a type to another SafeEnum of a different type.

Implicit conversion to integer is not defined, users have to either use the value function which return the integer value stored in an EnumType. Client class can also define their own conversion operation.

Template Parameters
Targetstructure containing the enumeration definition.
LayoutTypeInner type used to store enumeration value.
struct color_t : SafeEnum<color_t> {
enum type {
RED,
GREEN,
BLACK
};
color_t(type) : SafeEnum<color_t>(type) { }
};
// use an uint8_t to store the enumeration value
struct shape_t : SafeEnum<shape_t, uint8_t> {
enum type {
RECTANGLE,
CIRCLE,
TRIANGLE
};
shape_t(type) : SafeEnum<shape_t>(type) { }
};
// shape enumerator is in the shape_t scope.
shape_t shape = shape_t::RECTANGLE;
shape_t shape = color_t::RED; // Compilation error
if (shape == shape_t::CIRCLE) {
}
// compilation error
if (shape == color_t::RED) {
}
void sink(shape_t); (1)
void sink(color_t); (2)
sink(shape); // use overload (1)
sink(color); // use overload (2)
// explicit access to the value is mandatory when a SafeEnum value is used
// as the condition in a switch statement
switch(shape.value()) {
case shape_t::RECTANGLE:
break;
}

Definition at line 111 of file common/SafeEnum.h.

Member Typedef Documentation

typedef LayoutType representation_t

Type of the representation.

Definition at line 115 of file common/SafeEnum.h.

Constructor & Destructor Documentation

SafeEnum ( LayoutType  value)
explicitprotected

Construction of an enumeration value.

Definition at line 121 of file common/SafeEnum.h.

Member Function Documentation

const LayoutType* storage ( ) const

Return a pointer to the inner storage.

Definition at line 211 of file common/SafeEnum.h.

LayoutType value ( ) const

Explicit access to the inner value of the SafeEnum instance.

Definition at line 204 of file common/SafeEnum.h.

Friends And Related Function Documentation

bool operator!= ( Target  lhs,
Target  rhs 
)
friend

Not equal to operator for Target instances.

Parameters
lhsleft hand side of the comparison
rhsright hand side of the comparison
Returns
true if the inner value of lhs and rhs are not equal and false otherwise.

Definition at line 146 of file common/SafeEnum.h.

bool operator< ( Target  lhs,
Target  rhs 
)
friend

Less than operator for Target instances.

Parameters
lhsleft hand side of the comparison
rhsright hand side of the comparison
Returns
true if the inner value of lhs is less than rhs and false otherwise.

Definition at line 158 of file common/SafeEnum.h.

bool operator<= ( Target  lhs,
Target  rhs 
)
friend

Less than or equal to operator for Target instances.

Parameters
lhsleft hand side of the comparison
rhsright hand side of the comparison
Returns
true if the inner value of lhs is less than or equal to rhs and false otherwise.

Definition at line 171 of file common/SafeEnum.h.

bool operator== ( Target  lhs,
Target  rhs 
)
friend

Equal to operator for Target instances.

Parameters
lhsleft hand side of the comparison
rhsright hand side of the comparison
Returns
true if the inner value of lhs and rhs are equal and false otherwise.

Definition at line 133 of file common/SafeEnum.h.

bool operator> ( Target  lhs,
Target  rhs 
)
friend

Greater than operator for Target instances.

Parameters
lhsleft hand side of the comparison
rhsright hand side of the comparison
Returns
true if the inner value of lhs is greater than rhs; false otherwise.

Definition at line 184 of file common/SafeEnum.h.

bool operator>= ( Target  lhs,
Target  rhs 
)
friend

Greater than or equal to operator for Target instances.

Parameters
lhsleft hand side of the comparison
rhsright hand side of the comparison
Returns
true if the inner value of lhs is greater than or equal to rhs; false otherwise.

Definition at line 197 of file common/SafeEnum.h.

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.