Gromacs  2024.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
enumerationhelpers.h File Reference
#include <cstddef>
#include <iterator>
#include <type_traits>
#include <gromacs/external/boost/stl_interfaces/iterator_interface.hpp>
#include "gromacs/utility/gmxassert.h"
+ Include dependency graph for enumerationhelpers.h:
+ This graph shows which files directly or indirectly include this file:


Defines helper types for class enumerations.

These helper types facilitate iterating over class enums, and maintaining a type-safe and value-safe matching list of names. The code is closely based on the public-domain code by Guilherme R. Lampert, found in commit c94c18a of Thanks Guilherme!

NOTE This functionality only works for enumerations of monotonically increasing values, starting with the value zero.

Usage examples:

enum class Foo : int { Bar, Baz, Fooz, Count };

for (Foo c : EnumerationWrapper<Foo>{}) { // 'c' is a constant from Foo }

const EnumerationArray<Foo, std::string> fooStrings = { { "Bar", "Baz", "Fooz" } }; std::cout << fooStrings[Foo::Baz]; std::cout << fooStrings[Foo::Count]; // Triggers an assertion

for (Foo c : keysOf(fooStrings)) { print(fooStrings[c]); }

ArrayRef<const std::string> namesRef(fooStrings);

Mark Abraham


template<typename EnumerationArrayType >
EnumerationArrayType::EnumerationWrapperType gmx::keysOf (const EnumerationArrayType &)
 Returns an object that provides iterators over the keys associated with EnumerationArrayType. More...