enumeration.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_H_
  28. #define RTTR_ENUMERATION_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/type.h"
  31. #include "rttr/string_view.h"
  32. #include "rttr/detail/misc/class_item_mapper.h"
  33. #include <memory>
  34. #include <string>
  35. namespace rttr
  36. {
  37. class type;
  38. class variant;
  39. class argument;
  40. namespace detail
  41. {
  42. class enumeration_wrapper_base;
  43. }
  44. /*!
  45. * The \ref enumeration class provides several meta information about an enum.
  46. *
  47. * A instance of an enumeration class can only be obtained from the \ref type class or the \ref property class.
  48. * See \ref type::get_enumeration() and \ref property::get_enumeration().
  49. *
  50. * For registration an enum, nested inside a class, see \ref registration::class_<T>::enumeration()
  51. * and for global enums see \ref registration::enumeration().
  52. *
  53. * Meta Information
  54. * ----------------
  55. * An \ref enumeration is described by it's declared name (\ref get_name()), it's enumerator names (\ref get_names())
  56. * and it's corresponding constant values (\ref get_values()).
  57. * The name is represented as \ref string_view and the values are stored as the underlying enum value.
  58. * When the \ref enumeration was declared inside a class, then \ref get_declaring_type() can be used to obtain the type of this class.
  59. *
  60. * The conversion functions \ref name_to_value(), \ref value_to_name() allow conversion between the value representation of an enumeration and its literal representation.
  61. *
  62. * Copying and Assignment
  63. * ----------------------
  64. * A \ref enumeration object is lightweight and can be copied by value. However, each copy will refer to the same underlying enumeration.
  65. *
  66. * Typical Usage
  67. * ----------------------
  68. *
  69. \code{.cpp}
  70. using namespace rttr;
  71. enum class E_Alignment
  72. {
  73. AlignLeft = 0x0001,
  74. AlignRight = 0x0002,
  75. AlignHCenter = 0x0004,
  76. AlignJustify = 0x0008
  77. };
  78. RTTR_REGISTRATION
  79. {
  80. rttr::registration::enumeration<E_Alignment>("E_Alignment")
  81. (
  82. value("AlignLeft", E_Alignment::AlignLeft),
  83. value("AlignRight", E_Alignment::AlignRight),
  84. value("AlignHCenter", E_Alignment::AlignHCenter),
  85. value("AlignJustify", E_Alignment::AlignJustify)
  86. );
  87. }
  88. enumeration enum_align = type::get_by_name("E_Alignment").get_enumeration();
  89. if (enum_align)
  90. {
  91. E_Alignment enum_value = E_Alignment::AlignLeft;
  92. string_view name = enum_align.value_to_name(enum_value);
  93. std::cout << name; // prints "AlignLeft"
  94. variant var = enum_align.name_to_value("AlignJustify");
  95. enum_value = var.get_value<E_Alignment>(); // is now: "AlignJustify"
  96. std::cout << var.to_int(); // prints "8";
  97. std::cout << var.to_string(); // prints "AlignJustify";
  98. }
  99. \endcode
  100. *
  101. * \see method, property, constructor and type
  102. */
  103. class RTTR_API enumeration
  104. {
  105. public:
  106. /*!
  107. * \brief Returns true if this \ref enumeration is valid, otherwise false.
  108. *
  109. * \return True if this \ref enumeration is valid, otherwise false.
  110. */
  111. bool is_valid() const RTTR_NOEXCEPT;
  112. /*!
  113. * \brief Convenience function to check if this \ref enumeration is valid or not.
  114. *
  115. * \return True if this \ref enumeration is valid, otherwise false.
  116. */
  117. explicit operator bool() const RTTR_NOEXCEPT;
  118. /*!
  119. * \brief Returns the declared name of this \ref enumeration.
  120. *
  121. * \return Name of the \ref enumeration.
  122. */
  123. string_view get_name() const RTTR_NOEXCEPT;
  124. /*!
  125. * \brief Returns the underlying type (int, unsigned int, etc.) of this \ref enumeration.
  126. *
  127. * \return Data type of the \ref enumeration.
  128. */
  129. type get_underlying_type() const RTTR_NOEXCEPT;
  130. /*!
  131. * \brief Returns the type object of this \ref enumeration.
  132. *
  133. * \return Data type of the \ref enumeration.
  134. */
  135. type get_type() const RTTR_NOEXCEPT;
  136. /*!
  137. * \brief Returns the \ref type of the class or struct that declares this \ref enumeration.
  138. *
  139. * \remark When this enumeration does not belong to a class (i.e. is a global enumeration) it will return an invalid type object.
  140. * When this enumeration is not valid, this function will return an invalid type object (see \ref type::is_valid).
  141. *
  142. * \return \ref type "Type" of the declaring class/struct for this enumeration.
  143. */
  144. type get_declaring_type() const RTTR_NOEXCEPT;
  145. /*!
  146. * \brief Returns the meta data for the given key \p key.
  147. *
  148. * \remark When no meta data is registered with the given \p key,
  149. * an invalid \ref variant object is returned (see \ref variant::is_valid).
  150. *
  151. * \return A variant object, containing arbitrary data.
  152. */
  153. variant get_metadata(const variant& key) const;
  154. /*!
  155. * \brief Returns all enum names registered for this enumeration.
  156. *
  157. * \remark When the enumeration is invalid then an empty range is returned.
  158. *
  159. * \return A range of enumeration names.
  160. */
  161. array_range<string_view> get_names() const RTTR_NOEXCEPT;
  162. /*!
  163. * \brief Returns all enum values registered for this enumeration.
  164. *
  165. * \remark When the enumeration is invalid then an empty range is returned.
  166. *
  167. * \return A range of enumeration values.
  168. */
  169. array_range<variant> get_values() const RTTR_NOEXCEPT;
  170. /*!
  171. * \brief Returns the string_view that is used as the name of the given enumeration \p value,
  172. * or an empty string_view if the \p value is not defined.
  173. *
  174. * \return A string_view object, containing the name for the given value.
  175. */
  176. string_view value_to_name(argument value) const;
  177. /*!
  178. * \brief Returns the value of the given enumeration \p name, or an empty variant if the name is not defined.
  179. *
  180. * \return A variant object, containing the value for the given \p name.
  181. */
  182. variant name_to_value(string_view name) const;
  183. /*!
  184. * \brief Returns true if this enumeration is the same like the \p other.
  185. *
  186. * \return True if both enumerations are equal, otherwise false.
  187. */
  188. bool operator==(const enumeration& other) const RTTR_NOEXCEPT;
  189. /*!
  190. * Returns true if this enumeration is the not the same like the \p other.
  191. *
  192. * \return True if both enumerations are different, otherwise false.
  193. */
  194. bool operator!=(const enumeration& other) const RTTR_NOEXCEPT;
  195. private:
  196. enumeration(const detail::enumeration_wrapper_base* wrapper) RTTR_NOEXCEPT;
  197. template<typename T>
  198. friend T detail::create_item(const detail::class_item_to_wrapper_t<T>* wrapper);
  199. template<typename T>
  200. friend T detail::create_invalid_item();
  201. private:
  202. const detail::enumeration_wrapper_base* m_wrapper;
  203. };
  204. } // end namespace rttr
  205. #endif // RTTR_ENUMERATION_H_