|
- #pragma once
-
- #include <map>
- #include <vector>
-
- #include <cppcore/misc/compare.h>
- #include <cppcore/misc/exception.h>
- #include <cppcore/misc/type_helper.h>
- // #include <cppcore/conversion/string.h>
-
- #define cppcore_define_enum_value_traits(enum, ...) \
- namespace cppcore { \
- namespace __impl { \
- template<> \
- struct enum_value_traits<enum, void> \
- { \
- using enum_type = enum; \
- using enum_value_pair_type = std::pair<enum_type, std::string>; \
- using enum_value_vector_type = std::vector<enum_value_pair_type>; \
- \
- static decltype(auto) get_enum_values() \
- { \
- static const enum_value_vector_type value({ \
- __VA_ARGS__ \
- }); \
- return value; \
- } \
- }; } }
-
- namespace cppcore
- {
-
- namespace __impl
- {
-
- /**
- * @brief Traits class to strore enum to string pairs and the needed data types.
- *
- * @tparam T_enum Enum type.
- */
- template<typename T_enum, typename = void>
- struct enum_value_traits
- {
- using enum_type = T_enum;
- using enum_value_pair_type = std::pair<enum_type, std::string>;
- using enum_value_vector_type = std::vector<enum_value_pair_type>;
-
- /**
- * @brief Returns a vector with all enum to string value paris.
- */
- static decltype(auto) get_enum_values();
- };
-
- /**
- * @brief Traits class for enum conersions to store the needed types and data.
- *
- * @tparam T_enum Enum type.
- * @tparam T_traits Traits that stores a list with all enum to string values pairs.
- */
- template<
- typename T_enum,
- typename T_traits = enum_value_traits<T_enum>>
- struct enum_conversion_traits
- {
- public:
- using enum_type = T_enum;
- using traits_type = enum_value_traits<enum_type>;
- using enum_to_string_map_type = std::map<enum_type, std::string>;
- using string_to_enum_map_type = std::map<std::string, enum_type, op_less_invariant_string>;
-
- /**
- * @brief Returns a map with enum to string values.
- */
- static inline decltype(auto) get_enum_to_string_map();
-
- /**
- * @brief Returns a map with string to enum values.
- */
- static inline decltype(auto) get_string_to_enum_map();
-
- private:
- /**
- * @brief Helper method to create the enum to string map.
- */
- static inline decltype(auto) create_enum_to_string_map();
-
- /**
- * @brief Helper method to create the string to enum map.
- */
- static inline decltype(auto) create_string_to_enum_map();
- };
-
- }
-
- /**
- * @brief Class to convert enum values to and from string.
- *
- * @tparam T_enum Enum type.
- * @tparam T_traits Traits type that stores the enum to value and the value to enum maps.
- */
- template<
- typename T_enum,
- typename T_traits = __impl::enum_conversion_traits<T_enum>>
- struct enum_conversion
- {
- using enum_type = T_enum;
- using base_type = typename std::underlying_type<enum_type>::type;
- using traits_type = T_traits;
-
- /**
- * @brief Convert the given enum value to a string.
- *
- * @param[in] value Enum value to convert to string.
- * @param[in] add_numeric_value Append the numeric value to the converted enum value.
- *
- * @return Enum converted to string.
- */
- static inline std::string to_string(
- enum_type value,
- bool add_numeric_value = false);
-
- /**
- * @brief Convert the given string to the enum value.
- *
- * @param[in] str String to convert to enum value.
- * @param[out] value Value to store converted
- * @param[in] accept_numeric_value Accept numeric values.
- *
- * @retval true If the conversion was successfull.
- * @retval false If the string could not be converted.
- */
- static inline bool try_to_enum(
- const std::string& str,
- enum_type& value,
- bool accept_numeric_value = true);
-
- /**
- * @brief Convert the given string to the enum value. Throws an exception if the conversion failed.
- *
- * @param[in] str String to convert to enum value.
- * @param[in] accept_numeric_value Accept numeric values.
- *
- * @return Enum value.
- */
- static inline enum_type to_enum(
- const std::string& str,
- bool accept_numeric_value = true);
-
- /**
- * @brief Convert the given string to the enum value. Returns the passed default value if the conversion failed.
- *
- * @param[in] str String to convert to enum value.
- * @param[in] default_value Default value to pass if the conversion failed.
- * @param[in] accept_numeric_value Accept numeric values.
- *
- * @return Enum value.
- */
- static inline enum_type to_enum_default(
- const std::string& str,
- enum_type default_value,
- bool accept_numeric_value = true);
- };
-
- }
-
- #include "enum.inl"
|