Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

167 рядки
5.6 KiB

  1. #pragma once
  2. #include <map>
  3. #include <vector>
  4. #include <cppcore/misc/compare.h>
  5. #include <cppcore/misc/exception.h>
  6. #include <cppcore/misc/type_helper.h>
  7. // #include <cppcore/conversion/string.h>
  8. #define cppcore_define_enum_value_traits(enum, ...) \
  9. namespace cppcore { \
  10. namespace __impl { \
  11. template<> \
  12. struct enum_value_traits<enum, void> \
  13. { \
  14. using enum_type = enum; \
  15. using enum_value_pair_type = std::pair<enum_type, std::string>; \
  16. using enum_value_vector_type = std::vector<enum_value_pair_type>; \
  17. \
  18. static decltype(auto) get_enum_values() \
  19. { \
  20. static const enum_value_vector_type value({ \
  21. __VA_ARGS__ \
  22. }); \
  23. return value; \
  24. } \
  25. }; } }
  26. namespace cppcore
  27. {
  28. namespace __impl
  29. {
  30. /**
  31. * @brief Traits class to strore enum to string pairs and the needed data types.
  32. *
  33. * @tparam T_enum Enum type.
  34. */
  35. template<typename T_enum, typename = void>
  36. struct enum_value_traits
  37. {
  38. using enum_type = T_enum;
  39. using enum_value_pair_type = std::pair<enum_type, std::string>;
  40. using enum_value_vector_type = std::vector<enum_value_pair_type>;
  41. /**
  42. * @brief Returns a vector with all enum to string value paris.
  43. */
  44. static decltype(auto) get_enum_values();
  45. };
  46. /**
  47. * @brief Traits class for enum conersions to store the needed types and data.
  48. *
  49. * @tparam T_enum Enum type.
  50. * @tparam T_traits Traits that stores a list with all enum to string values pairs.
  51. */
  52. template<
  53. typename T_enum,
  54. typename T_traits = enum_value_traits<T_enum>>
  55. struct enum_conversion_traits
  56. {
  57. public:
  58. using enum_type = T_enum;
  59. using traits_type = enum_value_traits<enum_type>;
  60. using enum_to_string_map_type = std::map<enum_type, std::string>;
  61. using string_to_enum_map_type = std::map<std::string, enum_type, op_less_invariant_string>;
  62. /**
  63. * @brief Returns a map with enum to string values.
  64. */
  65. static inline decltype(auto) get_enum_to_string_map();
  66. /**
  67. * @brief Returns a map with string to enum values.
  68. */
  69. static inline decltype(auto) get_string_to_enum_map();
  70. private:
  71. /**
  72. * @brief Helper method to create the enum to string map.
  73. */
  74. static inline decltype(auto) create_enum_to_string_map();
  75. /**
  76. * @brief Helper method to create the string to enum map.
  77. */
  78. static inline decltype(auto) create_string_to_enum_map();
  79. };
  80. }
  81. /**
  82. * @brief Class to convert enum values to and from string.
  83. *
  84. * @tparam T_enum Enum type.
  85. * @tparam T_traits Traits type that stores the enum to value and the value to enum maps.
  86. */
  87. template<
  88. typename T_enum,
  89. typename T_traits = __impl::enum_conversion_traits<T_enum>>
  90. struct enum_conversion
  91. {
  92. using enum_type = T_enum;
  93. using base_type = typename std::underlying_type<enum_type>::type;
  94. using traits_type = T_traits;
  95. /**
  96. * @brief Convert the given enum value to a string.
  97. *
  98. * @param[in] value Enum value to convert to string.
  99. * @param[in] add_numeric_value Append the numeric value to the converted enum value.
  100. *
  101. * @return Enum converted to string.
  102. */
  103. static inline std::string to_string(
  104. enum_type value,
  105. bool add_numeric_value = false);
  106. /**
  107. * @brief Convert the given string to the enum value.
  108. *
  109. * @param[in] str String to convert to enum value.
  110. * @param[out] value Value to store converted
  111. * @param[in] accept_numeric_value Accept numeric values.
  112. *
  113. * @retval true If the conversion was successfull.
  114. * @retval false If the string could not be converted.
  115. */
  116. static inline bool try_to_enum(
  117. const std::string& str,
  118. enum_type& value,
  119. bool accept_numeric_value = true);
  120. /**
  121. * @brief Convert the given string to the enum value. Throws an exception if the conversion failed.
  122. *
  123. * @param[in] str String to convert to enum value.
  124. * @param[in] accept_numeric_value Accept numeric values.
  125. *
  126. * @return Enum value.
  127. */
  128. static inline enum_type to_enum(
  129. const std::string& str,
  130. bool accept_numeric_value = true);
  131. /**
  132. * @brief Convert the given string to the enum value. Returns the passed default value if the conversion failed.
  133. *
  134. * @param[in] str String to convert to enum value.
  135. * @param[in] default_value Default value to pass if the conversion failed.
  136. * @param[in] accept_numeric_value Accept numeric values.
  137. *
  138. * @return Enum value.
  139. */
  140. static inline enum_type to_enum_default(
  141. const std::string& str,
  142. enum_type default_value,
  143. bool accept_numeric_value = true);
  144. };
  145. }
  146. #include "enum.inl"