PEARL
Parallel Event Access and Replay Library
Public Types | List of all members
pearl::ScopedEnum< base > Class Template Reference

Scoped enumeration class template. More...

#include <pearl/ScopedEnum.h>

Inheritance diagram for pearl::ScopedEnum< base >:
base

Public Types

typedef base::type EnumType
 Enumeration type derived from base. More...
 

Public Member Functions

Constructors & destructor
 ScopedEnum ()
 Default constructor. More...
 
 ScopedEnum (EnumType value)
 Constructor. More...
 
 ScopedEnum (int value)
 Converting constructor. More...
 

Friends

Comparison operators
bool operator< (const ScopedEnum &lhs, const ScopedEnum &rhs)
 Less-than operator. More...
 
bool operator<= (const ScopedEnum &lhs, const ScopedEnum &rhs)
 Less-than-or-equal operator. More...
 
bool operator== (const ScopedEnum &lhs, const ScopedEnum &rhs)
 Equal operator. More...
 
bool operator!= (const ScopedEnum &lhs, const ScopedEnum &rhs)
 Not-equal operator. More...
 
bool operator>= (const ScopedEnum &lhs, const ScopedEnum &rhs)
 Greater-than-or-equal operator. More...
 
bool operator> (const ScopedEnum &lhs, const ScopedEnum &rhs)
 Greater-than operator. More...
 
Value access functions
EnumType native_value (const ScopedEnum &item)
 Get enumeration value. More...
 

Detailed Description

template<typename base>
class pearl::ScopedEnum< base >

The ScopedEnum class template can be used to define enumeration types which behave similar to C++11 scoped enums. That is, enumerators are contained within the scope of the enumeration type and can only be accessed using the scope resolution operator. Moreover, no implicit conversions to/from integral types are performed, which also prevents comparing values from different enumeration types. However, since this class template uses only pre-C++11 language features, some limitations exist. See the Limitations section below for details.

The ScopedEnum class template employs parameterized inheritance to derive the enumerators from a regular (unscoped) enum named type defined in a helper struct provided as the template parameter base. For example, to define a scoped enumeration type Color with the three enumerators RED, GREEN and BLUE, the following code snippet can be used:

struct Color_enum
{
enum type
{
RED,
GREEN,
BLUE
};
};
typedef ScopedEnum<Color_enum> Color;

The newly defined enumeration type Color can then be used much like a C++11 scoped enum, except for some situations which are described in more detail below.

Limitations
Scoped enums cannot be fully emulated using pre-C++11 language features. That is, there are a few limitations that have to be considered when using enumeration types defined using this class template:
  • The defined type is not a C++ enum. That is, its use may prevent certain compiler optimizations and therefore incur a performance penalty.
  • The emulated scoped enum can not be directly used in switch statements. Instead, use the native_value() helper function:
    switch (native_value(color))
    {
    case Color::RED:
    // ...
    }
  • Likewise, direct casting to an integral type such as int is not possible. Again, use the native_value() helper function instead.
  • The emulated scoped enum can not be directly used as a template parameter.
Note
To convert code using emulated scoped enums based on this class template to C++11, the following actions need to be performed:
  • Adjust the enum type definition to use enum class instead of the helper struct and the typedef.
  • Find all uses of the native_value() helper function and either remove the function call (in switch statements) or replace it by a static_cast to the corresponding integral type (for explicit type conversions).

Member Typedef Documentation

◆ EnumType

template<typename base >
typedef base::type pearl::ScopedEnum< base >::EnumType

Constructor & Destructor Documentation

◆ ScopedEnum() [1/3]

template<typename base >
pearl::ScopedEnum< base >::ScopedEnum ( )
inline

Creates an uninitialized scoped enum instance.

◆ ScopedEnum() [2/3]

template<typename base >
pearl::ScopedEnum< base >::ScopedEnum ( EnumType  value)
inline

Creates a scoped enum instance and initializes it with the given enumeration value.

Parameters
valueInitial enumeration value

◆ ScopedEnum() [3/3]

template<typename base >
pearl::ScopedEnum< base >::ScopedEnum ( int  value)
inlineexplicit

Creates a scoped enum instance and initializes it with the given integer value (explicit type cast).

Parameters
valueInitial enumeration value

Friends And Related Function Documentation

◆ native_value

template<typename base >
EnumType native_value ( const ScopedEnum< base > &  item)
friend

Returns the native enumeration value of the given scoped enum item. This helper function has to be used when using an emulated scoped enum in a switch statement or as a replacement for a static_cast to an integral type.

Parameters
itemEnumeration object
Returns
Native enumeration value

◆ operator!=

template<typename base >
bool operator!= ( const ScopedEnum< base > &  lhs,
const ScopedEnum< base > &  rhs 
)
friend

Determines whether the enumeration value of lhs is not equal to the value of rhs.

Parameters
lhsLeft-hand operand
rhsRight-hand operand
Returns
True if lhs is not equal to rhs, false otherwise.

◆ operator<

template<typename base >
bool operator< ( const ScopedEnum< base > &  lhs,
const ScopedEnum< base > &  rhs 
)
friend

Determines whether the enumeration value of lhs is less than the value of rhs.

Parameters
lhsLeft-hand operand
rhsRight-hand operand
Returns
True if lhs is less than rhs, false otherwise.

◆ operator<=

template<typename base >
bool operator<= ( const ScopedEnum< base > &  lhs,
const ScopedEnum< base > &  rhs 
)
friend

Determines whether the enumeration value of lhs is less than or equal to the value of rhs.

Parameters
lhsLeft-hand operand
rhsRight-hand operand
Returns
True if lhs is less than or equal to rhs, false otherwise.

◆ operator==

template<typename base >
bool operator== ( const ScopedEnum< base > &  lhs,
const ScopedEnum< base > &  rhs 
)
friend

Determines whether the enumeration value of lhs is equal to the value of rhs.

Parameters
lhsLeft-hand operand
rhsRight-hand operand
Returns
True if lhs is equal to rhs, false otherwise.

◆ operator>

template<typename base >
bool operator> ( const ScopedEnum< base > &  lhs,
const ScopedEnum< base > &  rhs 
)
friend

Determines whether the enumeration value of lhs is greater than the value of rhs.

Parameters
lhsLeft-hand operand
rhsRight-hand operand
Returns
True if lhs is greater than rhs, false otherwise.

◆ operator>=

template<typename base >
bool operator>= ( const ScopedEnum< base > &  lhs,
const ScopedEnum< base > &  rhs 
)
friend

Determines whether the enumeration value of lhs is greater than or equal to the value of rhs.

Parameters
lhsLeft-hand operand
rhsRight-hand operand
Returns
True if lhs is greater than or equal to rhs, false otherwise.

The documentation for this class was generated from the following file:
pearl::ScopedEnum::native_value
friend EnumType native_value(const ScopedEnum &item)
Get enumeration value.
Definition: ScopedEnum.h:312

Scalasca    Copyright © 1998–2019 Forschungszentrum Jülich GmbH, Jülich Supercomputing Centre
Copyright © 2009–2015 German Research School for Simulation Sciences GmbH, Laboratory for Parallel Programming