enumeration_wrapper.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /************************************************************************************
  2. * *
  3. * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
  4. * *
  5. * This file is part of RTTR (Run Time Type Reflection) *
  6. * License: MIT License *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining *
  9. * a copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
  24. * SOFTWARE. *
  25. * *
  26. *************************************************************************************/
  27. #ifndef RTTR_ENUMERATION_WRAPPER_H_
  28. #define RTTR_ENUMERATION_WRAPPER_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/detail/enumeration/enumeration_wrapper_base.h"
  31. #include "rttr/detail/enumeration/enum_data.h"
  32. #include "rttr/argument.h"
  33. #include "rttr/variant.h"
  34. #include "rttr/string_view.h"
  35. #include <utility>
  36. #include <type_traits>
  37. namespace rttr
  38. {
  39. namespace detail
  40. {
  41. template<typename Enum_Type, std::size_t N, std::size_t Metadata_Count>
  42. class enumeration_wrapper : public enumeration_wrapper_base, public metadata_handler<Metadata_Count>
  43. {
  44. public:
  45. enumeration_wrapper(std::array< enum_data<Enum_Type>, N > data,
  46. std::array<metadata, Metadata_Count> metadata_list) RTTR_NOEXCEPT
  47. : metadata_handler<Metadata_Count>(std::move(metadata_list))
  48. {
  49. int index = 0;
  50. for (const auto& item : data)
  51. {
  52. m_enum_names[index] = item.get_name();
  53. m_enum_values[index] = item.get_value();
  54. m_enum_variant_values[index] = item.get_value();
  55. ++index;
  56. }
  57. static_assert(std::is_enum<Enum_Type>::value, "No enum type provided, please create an instance of this class only for enum types!");
  58. }
  59. bool is_valid() const RTTR_NOEXCEPT { return true; }
  60. type get_type() const RTTR_NOEXCEPT { return type::get<Enum_Type>(); }
  61. type get_underlying_type() const RTTR_NOEXCEPT { return type::get<typename std::underlying_type<Enum_Type>::type>(); }
  62. array_range<string_view> get_names() const RTTR_NOEXCEPT
  63. {
  64. return array_range<string_view>(m_enum_names.data(), N);
  65. }
  66. array_range<variant> get_values() const RTTR_NOEXCEPT
  67. {
  68. return array_range<variant>(m_enum_variant_values.data(), N);
  69. }
  70. string_view value_to_name(argument& value) const
  71. {
  72. if (!value.is_type<Enum_Type>() &&
  73. !value.is_type<typename std::underlying_type<Enum_Type>::type>())
  74. {
  75. return string_view();
  76. }
  77. const Enum_Type enum_value = value.get_value<Enum_Type>();
  78. int index = 0;
  79. for (const auto& item : m_enum_values)
  80. {
  81. if (item == enum_value)
  82. return m_enum_names[index];
  83. ++index;
  84. }
  85. return string_view();
  86. }
  87. variant name_to_value(string_view name) const
  88. {
  89. int index = 0;
  90. for (const auto& item : m_enum_names)
  91. {
  92. if (item == name)
  93. return m_enum_values[index];
  94. ++index;
  95. }
  96. return variant();
  97. }
  98. variant get_metadata(const variant& key) const { return metadata_handler<Metadata_Count>::get_metadata(key); }
  99. private:
  100. std::array< string_view, N > m_enum_names;
  101. std::array< Enum_Type, N > m_enum_values;
  102. std::array< variant, N > m_enum_variant_values;
  103. };
  104. /////////////////////////////////////////////////////////////////////////////////////////
  105. } // end namespace detail
  106. } // end namespace rttr
  107. #endif // RTTR_ENUMERATION_WRAPPER_H_