parameter_info.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_PARAMETER_INFO_H_
  28. #define RTTR_PARAMETER_INFO_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/string_view.h"
  31. #include <string>
  32. #include <vector>
  33. #include <cstdint>
  34. namespace rttr
  35. {
  36. class variant;
  37. class type;
  38. class parameter_info;
  39. namespace detail
  40. {
  41. class parameter_info_wrapper_base;
  42. template<typename T>
  43. static parameter_info create_param_info(const T&);
  44. }
  45. /*!
  46. * The \ref parameter_info class provides several meta information about a parameter.
  47. *
  48. * A instance of a parameter_info class can be obtained from the \ref method or \ref constructor class.
  49. * See \ref method::get_parameter_infos() and \ref constructor::get_parameter_infos().
  50. *
  51. * For registration additional parameter infos, see \ref parameter_names().
  52. *
  53. * Meta Information
  54. * ----------------
  55. *
  56. * Copying and Assignment
  57. * ----------------------
  58. * A \ref parameter_info object is lightweight and can be copied by value. However, each copy will refer to the same underlying parameter_info.
  59. *
  60. * Typical Usage
  61. * ----------------------
  62. *
  63. * \code{.cpp}
  64. * using namespace rttr;
  65. *
  66. * void set_window_geometry(const char* name, int w, int h) {...}
  67. *
  68. * RTTR_REGISTRATION
  69. * {
  70. * registration::method("set_window_geometry", &set_window_geometry)
  71. * (
  72. * parameter_names("window name", "width", "height")
  73. * );
  74. * }
  75. *
  76. * //...
  77. *
  78. * method meth = type::get_global_method("set_window_geometry");
  79. * std::vector<parameter_info> param_list = meth.get_parameter_infos();
  80. * for (const auto& info : param_list)
  81. * {
  82. * // print all names of the parameter types and its position in the paramter list
  83. * std::cout << " name: '" << info.get_type().get_name() << "'\n"
  84. * << "index: " << info.get_index()
  85. * << std::endl;
  86. * }
  87. * \endcode
  88. *
  89. * Output:
  90. *
  91. * name: 'window name'
  92. * index: 0
  93. * name: 'width'
  94. * index: 1
  95. * name: 'height'
  96. * index: 2
  97. *
  98. * \see method and constructor
  99. */
  100. class RTTR_API parameter_info
  101. {
  102. public:
  103. /*!
  104. * \brief Returns type of this parameter.
  105. *
  106. * \return Parameter type.
  107. */
  108. type get_type() const RTTR_NOEXCEPT;
  109. /*!
  110. * \brief Returns true when this parameter has a default value; otherwise false.
  111. *
  112. * \return Bool to indicate that this parameter has a default value.
  113. */
  114. bool has_default_value() const RTTR_NOEXCEPT;
  115. /*!
  116. * \brief Returns the default value as \ref variant for this parameter;
  117. * or an \ref variant::is_valid() "invalid" variant to indicate that no default value is available.
  118. *
  119. * \return Default value as variant.
  120. */
  121. variant get_default_value() const;
  122. /*!
  123. * \brief Returns the name of this parameter.
  124. * When no name was provided during registration via \ref parameter_names(),
  125. * then an empty string is returned.
  126. *
  127. * \return The name of the parameter.
  128. */
  129. string_view get_name() const RTTR_NOEXCEPT;
  130. /*!
  131. * \brief Returns the zero-based position of the parameter in the formal parameter list.
  132. *
  133. * \return An integer representing the position this parameter occupies in the parameter list.
  134. */
  135. uint32_t get_index() const RTTR_NOEXCEPT;
  136. /*!
  137. * \brief Returns true if this property is the same like the \p other.
  138. *
  139. * \return True if both properties are equal, otherwise false.
  140. */
  141. bool operator==(const parameter_info& other) const RTTR_NOEXCEPT;
  142. /*!
  143. * Returns true if this property is the not the same like the \p other.
  144. *
  145. * \return True if both properties are different, otherwise false.
  146. */
  147. bool operator!=(const parameter_info& other) const RTTR_NOEXCEPT;
  148. private:
  149. template<typename T>
  150. friend parameter_info detail::create_param_info(const T&);
  151. parameter_info(const detail::parameter_info_wrapper_base* wrapper) RTTR_NOEXCEPT;
  152. private:
  153. const detail::parameter_info_wrapper_base* m_wrapper;
  154. };
  155. } // end namespace rttr
  156. #endif // RTTR_PARAMETER_INFO_H_