Skip to content

Commit dd327d3

Browse files
authored
Build Enumeration Dev Utils (#17)
* Implement dev-utils Build Enumeration Signed-off-by: jparisu <javierparis@eprosima.com> * Add vector to custom enumeration builder Signed-off-by: jparisu <javierparis@eprosima.com> * Add fixes to enumeration builder Signed-off-by: jparisu <javierparis@eprosima.com> * apply suggestions Signed-off-by: jparisu <javierparis@eprosima.com> * remove ... in file path Signed-off-by: jparisu <javierparis@eprosima.com> Signed-off-by: jparisu <javierparis@eprosima.com>
1 parent f97515a commit dd327d3

4 files changed

Lines changed: 372 additions & 37 deletions

File tree

cpp_utils/include/cpp_utils/macros/custom_enumeration.hpp

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ namespace utils {
3535
/**
3636
* @brief This macro creates a Custom Enumeration with auxiliary functions and variables.
3737
*
38-
* An enumeration built with ENUEMERATION_BUILDER has:
38+
* An enumeration built with ENUMERATION_BUILDER has:
3939
* - enum class with name \c enumeration_name and N values, one for each extra argument, and with that exact name.
40-
* - array called \c nammes_<enumeration_name> with the names of each element of the enumeration
40+
* - array called \c NAMES_<enumeration_name> with the names of each element of the enumeration
4141
* as strings of the enum value.
4242
* - \c to_string method to get the string associated with an enumeration value.
4343
* - \c from_string_<enumeration_name> method that gives enumeration value from string name.
@@ -55,42 +55,41 @@ namespace utils {
5555
* my_value = from_string_CustomEnum("el2"); // Set my_value as el2 = 1
5656
* to_string(my_value); // = "el2"
5757
*/
58-
#define ENUMERATION_BUILDER(enumeration_name, ...) \
59-
\
60-
/* Forbid empty enumerations */ \
61-
static_assert( COUNT_ARGUMENTS(__VA_ARGS__), "Empty Enumerations are not allowed."); \
62-
\
63-
/* Declare enumeration */ \
64-
enum class enumeration_name {__VA_ARGS__ \
65-
}; \
66-
\
67-
/* Initialize name arrays */ \
68-
const std::array<std::string, COUNT_ARGUMENTS(__VA_ARGS__)> names_ ## enumeration_name = \
69-
{ APPLY_MACRO_FOR_EACH(STRINGIFY_WITH_COMMA, __VA_ARGS__) }; \
70-
\
71-
/* To string method */ \
72-
inline const std::string& to_string(const enumeration_name& e) \
73-
{ return names_ ## enumeration_name[static_cast<int>(e)]; } \
74-
\
75-
inline std::vector<std::string> string_vector_ ## enumeration_name() \
76-
{ return std::vector<std::string> (names_ ## enumeration_name.begin(), names_ ## enumeration_name.end()); } \
77-
\
78-
/* From string */ \
79-
inline enumeration_name from_string_ ## enumeration_name(const std::string& s) \
80-
{ \
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( \
84-
STR_ENTRY << "Not correct name " << s << " for Enum " << STRINGIFY(enumeration_name) << "."); \
85-
} \
86-
\
87-
/* Serialization operation */ \
88-
inline std::ostream& operator <<(std::ostream& os, const enumeration_name& e) \
89-
{ os << to_string(e); return os; } \
90-
\
91-
/* Number of elements in enumeration */ \
58+
#define ENUMERATION_BUILDER(enumeration_name, ...) \
59+
\
60+
/* Forbid empty enumerations */ \
61+
static_assert( COUNT_ARGUMENTS(__VA_ARGS__), "Empty Enumerations are not allowed."); \
62+
\
63+
/* Declare enumeration */ \
64+
enum class enumeration_name {__VA_ARGS__ \
65+
}; \
66+
\
67+
/* Initialize name arrays */ \
68+
const std::array<std::string, COUNT_ARGUMENTS(__VA_ARGS__)> NAMES_ ## enumeration_name = \
69+
{ APPLY_MACRO_FOR_EACH(STRINGIFY_WITH_COMMA, __VA_ARGS__) }; \
70+
\
71+
/* To string method */ \
72+
inline const std::string& to_string(const enumeration_name& e) \
73+
{ return NAMES_ ## enumeration_name[static_cast<int>(e)]; } \
74+
\
75+
inline std::vector<std::string> string_vector_ ## enumeration_name() \
76+
{ return std::vector<std::string> (NAMES_ ## enumeration_name.begin(), NAMES_ ## enumeration_name.end()); } \
77+
\
78+
/* From string */ \
79+
inline enumeration_name from_string_ ## enumeration_name(const std::string& s) \
80+
{ \
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( \
84+
STR_ENTRY << "Not correct name " << s << " for Enum " << STRINGIFY(enumeration_name) << "."); \
85+
} \
86+
\
87+
/* Serialization operation */ \
88+
inline std::ostream& operator <<(std::ostream& os, const enumeration_name& e) \
89+
{ os << to_string(e); return os; } \
90+
\
91+
/* Number of elements in enumeration */ \
9292
constexpr const unsigned int N_VALUES_ ## enumeration_name = COUNT_ARGUMENTS(__VA_ARGS__)
9393

94-
9594
} /* namespace utils */
9695
} /* namespace eprosima */

dev_utils/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# eProsima Developers Utils Module
2+
3+
This is a package that contains certain scripts to help in the implementation of new projects.
4+
These scripts are meant to generate code or certain utils in a fast and optimal way.
5+
6+
> :warning: This is not a linkable package, it is only for utils focus on developing faster or better code.
7+
8+
## Automatic Code Generator
9+
10+
These tools are meant to autogenerate general code in different languages.
11+
12+
### Enumeration builder
13+
14+
Extend in a new file the whole `ENUMERATION_BUILDER` macro.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ENUMERATION BUILDER
2+
3+
This auto generates the code that would be generated by macro `ENUMERATION_BUILDER` in
4+
`dev-utils/cpp_utils/include/cpp_utils/macros/custom_enumeration.hpp`.
5+
6+
## Motivation
7+
8+
This script is useful because the use of this macro could have problems depending on its use and/or architecture:
9+
10+
- Windows does extend macros in a different order.
11+
- SWIG does not create classes or enums that are extended from a macro.
12+
13+
## Usage
14+
15+
This is a standalone script. In order to execute it:
16+
17+
```sh
18+
python3 enumeration_builder.py --output include/output.hpp --enum CustomEnum --values "value1;value2" --namespaces "eprosima;utils"
19+
```

0 commit comments

Comments
 (0)