Skip to content

Commit 39febed

Browse files
Extend custom enumeration API: string_to_enumeration (#44)
* Extend custom enumeration API: string_to_enumeration Signed-off-by: Juan López Fernández <juanlopez@eprosima.com> * Uncrustify Signed-off-by: Juan López Fernández <juanlopez@eprosima.com> --------- Signed-off-by: Juan López Fernández <juanlopez@eprosima.com>
1 parent b8b1148 commit 39febed

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

cpp_utils/include/cpp_utils/macros/custom_enumeration.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,20 @@ namespace utils {
7575
inline std::vector<std::string> string_vector_ ## enumeration_name() \
7676
{ return std::vector<std::string> (NAMES_ ## enumeration_name.begin(), NAMES_ ## enumeration_name.end()); } \
7777
\
78+
/* String to enumeration */ \
79+
inline bool string_to_enumeration(const std::string& s, enumeration_name & e) \
80+
{ \
81+
for (int i = 0; i < COUNT_ARGUMENTS(__VA_ARGS__); i++) \
82+
if (NAMES_ ## enumeration_name[i] == s){e = static_cast<enumeration_name>(i); return true;} \
83+
return false; \
84+
} \
85+
\
7886
/* From string */ \
7987
inline enumeration_name from_string_ ## enumeration_name(const std::string& s) \
8088
{ \
81-
for (int i = 0; i < COUNT_ARGUMENTS(__VA_ARGS__); i++) \
82-
if (NAMES_ ## enumeration_name[i] == s)return static_cast<enumeration_name>(i); \
83-
throw eprosima::utils::InitializationException( \
89+
enumeration_name e; \
90+
if (string_to_enumeration(s, e))return e; \
91+
throw eprosima::utils::InitializationException ( \
8492
STR_ENTRY << "Not correct name " << s << " for Enum " << STRINGIFY(enumeration_name) << "."); \
8593
} \
8694
\
@@ -95,7 +103,7 @@ namespace utils {
95103
inline std::array<enumeration_name, COUNT_ARGUMENTS(__VA_ARGS__)> all_values_ ## enumeration_name() \
96104
{ \
97105
std::array<enumeration_name, COUNT_ARGUMENTS(__VA_ARGS__)> result; \
98-
for (int i = 0; i < COUNT_ARGUMENTS(__VA_ARGS__); i++)result[i] = enumeration_name(i); \
106+
for (int i = 0; i < COUNT_ARGUMENTS(__VA_ARGS__); i++)result[i] = enumeration_name(i); \
99107
return result; \
100108
} \
101109
\

cpp_utils/test/unittest/macros/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ set(TEST_SOURCES
9090

9191
set(TEST_LIST
9292
show_how
93+
string_to_enumeration
9394
from_string
9495
to_string
9596
serializator

cpp_utils/test/unittest/macros/enumerationBuilderMacrosTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ TEST(enumerationBuilderMacrosTest, n_values)
9292
ASSERT_EQ(test::N_VALUES_TestCustomEnum, test::NUMBER_OF_ELEMENTS);
9393
}
9494

95+
/**
96+
* Construct enumeration of type TestCustomEnum from a string.
97+
*
98+
* CASES:
99+
* x each element inside
100+
* - string not in enumeration
101+
*/
102+
TEST(enumerationBuilderMacrosTest, string_to_enumeration)
103+
{
104+
test::TestCustomEnum value;
105+
// x each element inside
106+
for (unsigned int i = 0; i < test::N_VALUES_TestCustomEnum; i++)
107+
{
108+
ASSERT_TRUE(test::string_to_enumeration(test::string_values[i], value));
109+
ASSERT_TRUE(value == test::enum_values[i]);
110+
}
111+
112+
// string not in enumeration
113+
ASSERT_FALSE(test::string_to_enumeration("el_0", value));
114+
}
115+
95116
/**
96117
* Construct enumeration of type TestCustomEnum from a string.
97118
*

0 commit comments

Comments
 (0)