constructor.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_CONSTRUCTOR_H_
  28. #define RTTR_CONSTRUCTOR_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/detail/misc/class_item_mapper.h"
  31. #include "rttr/parameter_info.h"
  32. #include "rttr/access_levels.h"
  33. #include "rttr/array_range.h"
  34. #include "rttr/string_view.h"
  35. #include <string>
  36. #include <vector>
  37. namespace rttr
  38. {
  39. class variant;
  40. class type;
  41. class argument;
  42. class visitor;
  43. namespace detail
  44. {
  45. class constructor_wrapper_base;
  46. }
  47. /*!
  48. * The \ref constructor class provides several meta information about a constructor and can be invoked.
  49. *
  50. * A instance of a constructor class can only be obtained from the \ref type class.
  51. * See \ref type::get_constructor() and \ref type::get_constructors().
  52. *
  53. * For registration a constructor of a class, see \ref registration::class_<T>::constructor().
  54. *
  55. * Meta Information
  56. * ----------------
  57. * A \ref constructor has a signature (\ref get_signature()) and instantiate exactly one type (\ref get_instantiated_type()).
  58. * With \ref get_parameter_infos() you retrieve all information of the parameters for this constructor.
  59. * When the \ref constructor was declared inside a class, then \ref get_declaring_type() can be used to obtain the type of this class.
  60. *
  61. * The constructor can be invoked with \ref invoke() or via the \ref type class through \ref type::create().
  62. * The created object will be copied into a variant and returned. Depending on the used policy during the registration process,
  63. * it can be an object with automatic or dynamic storage.
  64. *
  65. * Copying and Assignment
  66. * ----------------------
  67. * A \ref constructor object is lightweight and can be copied by value. However, each copy will refer to the same underlying constructor.
  68. *
  69. * Typical Usage
  70. * ----------------------
  71. *
  72. * \code{.cpp}
  73. * constructor ctor = type::get_by_name("std::string").get_constructor({type::get<std::string>()}); // retrieve the copy ctor
  74. *
  75. * variant var = ctor.invoke(std::string("Hello World")); // returns an object with automatic storage
  76. *
  77. * std::cout << var.get_value<std::string>().c_str() << std::endl; // prints 'Hello World'
  78. * \endcode
  79. *
  80. * \see method, property, enumeration, destructor and type
  81. */
  82. class RTTR_API constructor
  83. {
  84. public:
  85. /*!
  86. * \brief Returns true if this constructor is valid, otherwise false.
  87. *
  88. * \return True if this constructor is valid, otherwise false.
  89. */
  90. bool is_valid() const RTTR_NOEXCEPT;
  91. /*!
  92. * \brief Convenience function to check if this constructor is valid or not.
  93. *
  94. * \return True if this constructor is valid, otherwise false.
  95. */
  96. explicit operator bool() const RTTR_NOEXCEPT;
  97. /*!
  98. * \brief Returns access level with which this constructor was
  99. * \ref registration::class_<T>::constructor() "registered".
  100. *
  101. * \remark When the constructor is not valid, this function will return level \ref access_levels::public_access.
  102. *
  103. * \return \ref access_levels of the method.
  104. */
  105. access_levels get_access_level() const RTTR_NOEXCEPT;
  106. /*!
  107. * Returns the type object of the instantiated type.
  108. *
  109. * \return The instantiated type.
  110. */
  111. type get_instantiated_type() const RTTR_NOEXCEPT;
  112. /*!
  113. * \brief Returns the \ref type of the class or struct that declares this \ref constructor.
  114. *
  115. * \remark When this constructor does not belong to a class (i.e. is a primitive type) it will return an invalid type object.
  116. * When this constructor is not valid, this function will return an invalid type object (see \ref type::is_valid).
  117. *
  118. * \return \ref type "Type" of the declaring class/struct for this enumeration.
  119. */
  120. type get_declaring_type() const RTTR_NOEXCEPT;
  121. /*!
  122. * \brief Returns the signature of this constructor as readable string.
  123. *
  124. * \return The signature as readable string.
  125. */
  126. string_view get_signature() const RTTR_NOEXCEPT;
  127. /*!
  128. * \brief Returns an ordered range of \ref parameter_info objects, which matches the signature of the constructor.
  129. *
  130. * \return A range of parameter_info objects of the constructor signature.
  131. */
  132. array_range<parameter_info> get_parameter_infos() const RTTR_NOEXCEPT;
  133. /*!
  134. * \brief Returns the meta data for the given key \p key.
  135. *
  136. * \remark When no meta data is registered with the given \p key,
  137. * an invalid \ref variant object is returned (see \ref variant::is_valid).
  138. *
  139. * \return A variant object, containing arbitrary data.
  140. */
  141. variant get_metadata(const variant& key) const;
  142. /*!
  143. * \brief Invokes the constructor of type returned by \ref get_instantiated_type().
  144. * The instance will always be created on the heap and will be returned as variant object.
  145. *
  146. * \remark Returns an invalid \ref variant object (see \ref variant::is_valid), when the arguments does
  147. * not match the parameters of the underlying constructor.
  148. *
  149. * \see get_parameter_infos()
  150. *
  151. * \return An instance of the type \ref get_instantiated_type().
  152. */
  153. variant invoke() const;
  154. /*!
  155. * \brief Invokes the constructor of type returned by \ref get_instantiated_type().
  156. * The instance will always be created on the heap and will be returned as variant object.
  157. *
  158. * \remark The given argument type has to match **exactly** the type of the underling constructor parameter,
  159. * otherwise the constructor cannot be invoked and an invalid \ref variant object (see \ref variant::is_valid)
  160. * will be returned.
  161. *
  162. * \see get_parameter_infos()
  163. *
  164. * \return An instance of the type \ref get_instantiated_type().
  165. */
  166. variant invoke(argument arg1) const;
  167. /*!
  168. * \brief Invokes the constructor of type returned by \ref get_instantiated_type().
  169. * The instance will always be created on the heap and will be returned as variant object.
  170. *
  171. * \remark The given argument type has to match **exactly** the type of the underling constructor parameter,
  172. * otherwise the constructor cannot be invoked and an invalid \ref variant object (see \ref variant::is_valid)
  173. * will be returned.
  174. *
  175. * \see get_parameter_infos()
  176. *
  177. * \return An instance of the type \ref get_instantiated_type().
  178. */
  179. variant invoke(argument arg1, argument arg2) const;
  180. /*!
  181. * \brief Invokes the constructor of type returned by \ref get_instantiated_type().
  182. * The instance will always be created on the heap and will be returned as variant object.
  183. *
  184. * \remark The given argument type has to match **exactly** the type of the underling constructor parameter,
  185. * otherwise the constructor cannot be invoked and an invalid \ref variant object (see \ref variant::is_valid)
  186. * will be returned.
  187. *
  188. * \see get_parameter_infos()
  189. *
  190. * \return An instance of the type \ref get_instantiated_type().
  191. */
  192. variant invoke(argument arg1, argument arg2, argument arg3) const;
  193. /*!
  194. * \brief Invokes the constructor of type returned by \ref get_instantiated_type().
  195. * The instance will always be created on the heap and will be returned as variant object.
  196. *
  197. * \remark The given argument type has to match **exactly** the type of the underling constructor parameter,
  198. * otherwise the constructor cannot be invoked and an invalid \ref variant object (see \ref variant::is_valid)
  199. * will be returned.
  200. *
  201. * \see get_parameter_infos()
  202. *
  203. * \return An instance of the type \ref get_instantiated_type().
  204. */
  205. variant invoke(argument arg1, argument arg2, argument arg3, argument arg4) const;
  206. /*!
  207. * \brief Invokes the constructor of type returned by \ref get_instantiated_type().
  208. * The instance will always be created on the heap and will be returned as variant object.
  209. *
  210. * \remark The given argument type has to match **exactly** the type of the underling constructor parameter,
  211. * otherwise the constructor cannot be invoked and an invalid \ref variant object (see \ref variant::is_valid)
  212. * will be returned.
  213. *
  214. * \see get_parameter_infos()
  215. *
  216. * \return An instance of the type \ref get_instantiated_type().
  217. */
  218. variant invoke(argument arg1, argument arg2, argument arg3, argument arg4,
  219. argument arg5) const;
  220. /*!
  221. * \brief Invokes the constructor of type returned by \ref get_instantiated_type().
  222. * The instance will always be created on the heap and will be returned as variant object.
  223. *
  224. * \remark The given argument type has to match **exactly** the type of the underling constructor parameter,
  225. * otherwise the constructor cannot be invoked and an invalid \ref variant object (see \ref variant::is_valid)
  226. * will be returned.
  227. *
  228. * \see get_parameter_infos()
  229. *
  230. * \return An instance of the type \ref get_instantiated_type().
  231. */
  232. variant invoke(argument arg1, argument arg2, argument arg3, argument arg4,
  233. argument arg5, argument arg6) const;
  234. /*!
  235. * \brief Invokes the constructor of type returned by \ref get_instantiated_type().
  236. * The instance will always be created on the heap and will be returned as variant object.
  237. * Use this method when you need to instantiate a constructor with more then 6 parameters.
  238. *
  239. * \remark The given argument type has to match **exactly** the type of the underling constructor parameter,
  240. * otherwise the constructor cannot be invoked and an invalid \ref variant object (see \ref variant::is_valid)
  241. * will be returned.
  242. * Using this invoke function is slower, then specifying the arguments directly.
  243. *
  244. * \see get_parameter_infos()
  245. *
  246. * \return An instance of the type \ref get_instantiated_type().
  247. */
  248. variant invoke_variadic(std::vector<argument> args) const;
  249. /*!
  250. * \brief Returns true if this constructor is the same like the \p other.
  251. *
  252. * \return True if both constructors are equal, otherwise false.
  253. */
  254. bool operator==(const constructor& other) const RTTR_NOEXCEPT;
  255. /*!
  256. * Returns true if this constructor is the not the same like the \p other.
  257. *
  258. * \return True if both constructors are different, otherwise false.
  259. */
  260. bool operator!=(const constructor& other) const RTTR_NOEXCEPT;
  261. private:
  262. constructor(const detail::constructor_wrapper_base* wrapper) RTTR_NOEXCEPT;
  263. void visit(visitor& visitor) const RTTR_NOEXCEPT;
  264. template<typename T>
  265. friend T detail::create_item(const detail::class_item_to_wrapper_t<T>* wrapper);
  266. template<typename T>
  267. friend T detail::create_invalid_item();
  268. friend class visitor;
  269. private:
  270. const detail::constructor_wrapper_base* m_wrapper;
  271. };
  272. } // end namespace rttr
  273. #endif // RTTR_CONSTRUCTOR_H_