user9589156
user9589156

Reputation:

looping over multiple enums

How would i loop over multiple enums.

I kind of want to have the enums inside skeleton

Not really sure on how to construct my enums to have them in a container like this

Then not really sure how i could loop it to get each of the types

Upvotes: 3

Views: 532

Answers (2)

Victor Gubin
Victor Gubin

Reputation: 2937

I got a meta-programming approach based on tuple

#include <tuple>
#include <type_traits>
#include <utility>
#include <iostream>

struct foo_enumerator {

enum class foo {
    ONE = 0 ,
    TWO =  1,
    THREE = 2
};

static constexpr auto reflect = std::make_tuple(
                                            foo::ONE,
                                            foo::TWO,
                                            foo::THREE);

};

struct bar_enumerator {
    enum class bar {
        FOUR = 4,
        FIVE =  5,
        SIX = 6
    };

    static constexpr auto reflect = std::make_tuple(
                                            bar::FOUR,
                                            bar::FIVE,
                                            bar::SIX);

};

// a tuple for_each implementation
// can be replaced with something else, like boost hana for_each for example
namespace detail {

// workaround for default non-type template arguments
template<std::size_t I>
using index_t = std::integral_constant<std::size_t, I>;

// process the `From::value`-th element
template<typename FromIndex,
         typename ToIndex,
         typename Tuple,
         typename UnaryFunction>
struct for_each_t {
    constexpr UnaryFunction&& operator()(Tuple&& t, UnaryFunction&& f) const
    {
        std::forward<UnaryFunction>(f)(
                std::get<FromIndex::value>(std::forward<Tuple>(t)));
        return for_each_t<index_t<FromIndex::value + 1>,
                          ToIndex,
                          Tuple,
                          UnaryFunction>()(
                std::forward<Tuple>(t), std::forward<UnaryFunction>(f));
    }
};

// specialization for empty tuple-likes
template<typename FromIndex, typename Tuple, typename UnaryFunction>
struct for_each_t<FromIndex, index_t<0>, Tuple, UnaryFunction> {
    constexpr UnaryFunction&& operator()(Tuple&&, UnaryFunction&& f) const
    {
        return std::forward<UnaryFunction>(f);
    }
};

// specialization for last element
template<typename ToIndex, typename Tuple, typename UnaryFunction>
struct for_each_t<index_t<ToIndex::value - 1>, ToIndex, Tuple, UnaryFunction> {
    constexpr UnaryFunction&& operator()(Tuple&& t, UnaryFunction&& f) const
    {
        std::forward<UnaryFunction>(f)(
                std::get<ToIndex::value - 1>(std::forward<Tuple>(t)));
        return std::forward<UnaryFunction>(f);
    }
};

}  // namespace detail

template<typename Tuple, typename UnaryFunction>
constexpr UnaryFunction for_each(Tuple&& t, UnaryFunction&& f)
{
    return detail::for_each_t<detail::index_t<0>,
                              detail::index_t<std::tuple_size<
                                  std::remove_reference_t<Tuple>
                              >::value>,
                              Tuple,
                              UnaryFunction>()(
            std::forward<Tuple>(t), std::forward<UnaryFunction>(f));
}


int main(int argc, const char** argv)
{

    constexpr auto all = std::tuple_cat( foo_enumerator::reflect, bar_enumerator::reflect );

    for_each(all, [](auto e_value)  {
            std::cout << "Enumeration value: " << static_cast<unsigned int>(e_value) << std::endl;
    });
}

Upvotes: 1

SimonC
SimonC

Reputation: 1617

The short answer is you can't. At least not in C++ (yet™).

To do what you're describing, you need reflections. Reflections allow you to go over objects at runtime in a dynamic fashion.

C#/.net provides this. Here's an example in C#.

enum MyEnum { Test, Test1, Test2, Another, One, Bites, The, Dust }
foreach (var value in typeof(MyEnum).GetEnumValues()) {
    WriteLine(value.ToString());
}

I'm not sure if C++ will ever adopt reflections, be it in this form or another.

Sorry if this isn't the be-all answer you were wishing for.

Upvotes: 0

Related Questions