MAKE_ENUM currently generates functions with custom names that cannot be accessed in templates:
MAKE_ENUM(MyEnum, FOO, BAR, BAZ);
std::vector<MyEnum> values = allValuesMyEnum(); // This is OK
template <typename E>
struct MyClass
{
std::vector<E> values = ??? // No way to access allValues of enum E
}
This PR changes MAKE_ENUM to emit specializations for templated functions under the namespace reflective_enum. These templated functions accept the enum type as an argument and can be used in templates without issue. All enum value arrays and string arrays are evaluated at compile time and can be used in constexpr functions and variables.
MAKE_ENUM(MyEnum, FOO, BAR, BAZ);
static_assert(reflective_enum::is_reflective_enum<FooEnum>::value); // True
constexpr size_t enum_size = reflective_enum::size<MyEnum>(); // Returns num of values in MyEnum
constexpr auto values = reflective_enum::values<MyEnum>(); // Returns array of all MyEnum values
constexpr auto value_names = reflective_enum::valueNames<MyEnum>(); // Returns array of string representations of MyEnum values
constexpr MyEnum value = reflective_enum::fromName<MyEnum>("FOO"); // value holds MyEnum::FOO
LOG(INFO) << value; // Prints "FOO"
We require enums with reflection for the Q-learning PR #3210
Testing Done
Added and updated tests in make_enum_test.cpp
Resolved Issues
Length Justification and Key Files to Review
Review Checklist
It is the reviewers responsibility to also make sure every item here has been covered
[ ] Function & Class comments: All function definitions (usually in the .h file) should have a javadoc style comment at the start of them. For examples, see the functions defined in thunderbots/software/geom. Similarly, all classes should have an associated Javadoc comment explaining the purpose of the class.
[ ] Remove all commented out code
[ ] Remove extra print statements: for example, those just used for testing
[ ] Resolve all TODO's: All TODO (or similar) statements should either be completed or associated with a github issue
Description
MAKE_ENUM
currently generates functions with custom names that cannot be accessed in templates:This PR changes
MAKE_ENUM
to emit specializations for templated functions under the namespacereflective_enum
. These templated functions accept the enum type as an argument and can be used in templates without issue. All enum value arrays and string arrays are evaluated at compile time and can be used inconstexpr
functions and variables.We require enums with reflection for the Q-learning PR #3210
Testing Done
Added and updated tests in
make_enum_test.cpp
Resolved Issues
Length Justification and Key Files to Review
Review Checklist
It is the reviewers responsibility to also make sure every item here has been covered
.h
file) should have a javadoc style comment at the start of them. For examples, see the functions defined inthunderbots/software/geom
. Similarly, all classes should have an associated Javadoc comment explaining the purpose of the class.TODO
(or similar) statements should either be completed or associated with a github issue