utility.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. #ifndef ENTT_META_UTILITY_HPP
  2. #define ENTT_META_UTILITY_HPP
  3. #include <cstddef>
  4. #include <functional>
  5. #include <type_traits>
  6. #include <utility>
  7. #include "../core/type_traits.hpp"
  8. #include "../locator/locator.hpp"
  9. #include "meta.hpp"
  10. #include "node.hpp"
  11. #include "policy.hpp"
  12. namespace entt {
  13. /**
  14. * @brief Meta function descriptor traits.
  15. * @tparam Ret Function return type.
  16. * @tparam Args Function arguments.
  17. * @tparam Static Function staticness.
  18. * @tparam Const Function constness.
  19. */
  20. template<typename Ret, typename Args, bool Static, bool Const>
  21. struct meta_function_descriptor_traits {
  22. /*! @brief Meta function return type. */
  23. using return_type = Ret;
  24. /*! @brief Meta function arguments. */
  25. using args_type = Args;
  26. /*! @brief True if the meta function is static, false otherwise. */
  27. static constexpr bool is_static = Static;
  28. /*! @brief True if the meta function is const, false otherwise. */
  29. static constexpr bool is_const = Const;
  30. };
  31. /*! @brief Primary template isn't defined on purpose. */
  32. template<typename, typename>
  33. struct meta_function_descriptor;
  34. /**
  35. * @brief Meta function descriptor.
  36. * @tparam Type Reflected type to which the meta function is associated.
  37. * @tparam Ret Function return type.
  38. * @tparam Class Actual owner of the member function.
  39. * @tparam Args Function arguments.
  40. */
  41. template<typename Type, typename Ret, typename Class, typename... Args>
  42. struct meta_function_descriptor<Type, Ret (Class::*)(Args...) const>
  43. : meta_function_descriptor_traits<
  44. Ret,
  45. std::conditional_t<std::is_base_of_v<Class, Type>, type_list<Args...>, type_list<const Class &, Args...>>,
  46. !std::is_base_of_v<Class, Type>,
  47. true> {};
  48. /**
  49. * @brief Meta function descriptor.
  50. * @tparam Type Reflected type to which the meta function is associated.
  51. * @tparam Ret Function return type.
  52. * @tparam Class Actual owner of the member function.
  53. * @tparam Args Function arguments.
  54. */
  55. template<typename Type, typename Ret, typename Class, typename... Args>
  56. struct meta_function_descriptor<Type, Ret (Class::*)(Args...)>
  57. : meta_function_descriptor_traits<
  58. Ret,
  59. std::conditional_t<std::is_base_of_v<Class, Type>, type_list<Args...>, type_list<Class &, Args...>>,
  60. !std::is_base_of_v<Class, Type>,
  61. false> {};
  62. /**
  63. * @brief Meta function descriptor.
  64. * @tparam Type Reflected type to which the meta data is associated.
  65. * @tparam Class Actual owner of the data member.
  66. * @tparam Ret Data member type.
  67. */
  68. template<typename Type, typename Ret, typename Class>
  69. struct meta_function_descriptor<Type, Ret Class::*>
  70. : meta_function_descriptor_traits<
  71. Ret &,
  72. std::conditional_t<std::is_base_of_v<Class, Type>, type_list<>, type_list<Class &>>,
  73. !std::is_base_of_v<Class, Type>,
  74. false> {};
  75. /**
  76. * @brief Meta function descriptor.
  77. * @tparam Type Reflected type to which the meta function is associated.
  78. * @tparam Ret Function return type.
  79. * @tparam MaybeType First function argument.
  80. * @tparam Args Other function arguments.
  81. */
  82. template<typename Type, typename Ret, typename MaybeType, typename... Args>
  83. struct meta_function_descriptor<Type, Ret (*)(MaybeType, Args...)>
  84. : meta_function_descriptor_traits<
  85. Ret,
  86. std::conditional_t<
  87. std::is_same_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type> || std::is_base_of_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type>,
  88. type_list<Args...>,
  89. type_list<MaybeType, Args...>>,
  90. !(std::is_same_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type> || std::is_base_of_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type>),
  91. std::is_const_v<std::remove_reference_t<MaybeType>> && (std::is_same_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type> || std::is_base_of_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type>)> {};
  92. /**
  93. * @brief Meta function descriptor.
  94. * @tparam Type Reflected type to which the meta function is associated.
  95. * @tparam Ret Function return type.
  96. */
  97. template<typename Type, typename Ret>
  98. struct meta_function_descriptor<Type, Ret (*)()>
  99. : meta_function_descriptor_traits<
  100. Ret,
  101. type_list<>,
  102. true,
  103. false> {};
  104. /**
  105. * @brief Meta function helper.
  106. *
  107. * Converts a function type to be associated with a reflected type into its meta
  108. * function descriptor.
  109. *
  110. * @tparam Type Reflected type to which the meta function is associated.
  111. * @tparam Candidate The actual function to associate with the reflected type.
  112. */
  113. template<typename Type, typename Candidate>
  114. class meta_function_helper {
  115. template<typename Ret, typename... Args, typename Class>
  116. static constexpr meta_function_descriptor<Type, Ret (Class::*)(Args...) const> get_rid_of_noexcept(Ret (Class::*)(Args...) const);
  117. template<typename Ret, typename... Args, typename Class>
  118. static constexpr meta_function_descriptor<Type, Ret (Class::*)(Args...)> get_rid_of_noexcept(Ret (Class::*)(Args...));
  119. template<typename Ret, typename Class>
  120. static constexpr meta_function_descriptor<Type, Ret Class::*> get_rid_of_noexcept(Ret Class::*);
  121. template<typename Ret, typename... Args>
  122. static constexpr meta_function_descriptor<Type, Ret (*)(Args...)> get_rid_of_noexcept(Ret (*)(Args...));
  123. template<typename Class>
  124. static constexpr meta_function_descriptor<Class, decltype(&Class::operator())> get_rid_of_noexcept(Class);
  125. public:
  126. /*! @brief The meta function descriptor of the given function. */
  127. using type = decltype(get_rid_of_noexcept(std::declval<Candidate>()));
  128. };
  129. /**
  130. * @brief Helper type.
  131. * @tparam Type Reflected type to which the meta function is associated.
  132. * @tparam Candidate The actual function to associate with the reflected type.
  133. */
  134. template<typename Type, typename Candidate>
  135. using meta_function_helper_t = typename meta_function_helper<Type, Candidate>::type;
  136. /**
  137. * @brief Wraps a value depending on the given policy.
  138. *
  139. * This function always returns a wrapped value in the requested context.<br/>
  140. * Therefore, if the passed value is itself a wrapped object with a different
  141. * context, it undergoes a rebinding to the requested context.
  142. *
  143. * @tparam Policy Optional policy (no policy set by default).
  144. * @tparam Type Type of value to wrap.
  145. * @param ctx The context from which to search for meta types.
  146. * @param value Value to wrap.
  147. * @return A meta any containing the returned value, if any.
  148. */
  149. template<typename Policy = as_is_t, typename Type>
  150. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_dispatch(const meta_ctx &ctx, [[maybe_unused]] Type &&value) {
  151. if constexpr(std::is_same_v<Policy, as_void_t>) {
  152. return meta_any{ctx, std::in_place_type<void>};
  153. } else if constexpr(std::is_same_v<Policy, as_ref_t>) {
  154. return meta_any{ctx, std::in_place_type<Type>, value};
  155. } else if constexpr(std::is_same_v<Policy, as_cref_t>) {
  156. static_assert(std::is_lvalue_reference_v<Type>, "Invalid type");
  157. return meta_any{ctx, std::in_place_type<const std::remove_reference_t<Type> &>, std::as_const(value)};
  158. } else {
  159. return meta_any{ctx, std::forward<Type>(value)};
  160. }
  161. }
  162. /**
  163. * @brief Wraps a value depending on the given policy.
  164. * @tparam Policy Optional policy (no policy set by default).
  165. * @tparam Type Type of value to wrap.
  166. * @param value Value to wrap.
  167. * @return A meta any containing the returned value, if any.
  168. */
  169. template<typename Policy = as_is_t, typename Type>
  170. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_dispatch(Type &&value) {
  171. return meta_dispatch<Policy, Type>(locator<meta_ctx>::value_or(), std::forward<Type>(value));
  172. }
  173. /**
  174. * @brief Returns the meta type of the i-th element of a list of arguments.
  175. * @tparam Type Type list of the actual types of arguments.
  176. * @param ctx The context from which to search for meta types.
  177. * @param index The index of the element for which to return the meta type.
  178. * @return The meta type of the i-th element of the list of arguments.
  179. */
  180. template<typename Type>
  181. [[nodiscard]] static meta_type meta_arg(const meta_ctx &ctx, const std::size_t index) noexcept {
  182. auto &&context = internal::meta_context::from(ctx);
  183. return {ctx, internal::meta_arg_node(context, Type{}, index)};
  184. }
  185. /**
  186. * @brief Returns the meta type of the i-th element of a list of arguments.
  187. * @tparam Type Type list of the actual types of arguments.
  188. * @param index The index of the element for which to return the meta type.
  189. * @return The meta type of the i-th element of the list of arguments.
  190. */
  191. template<typename Type>
  192. [[nodiscard]] static meta_type meta_arg(const std::size_t index) noexcept {
  193. return meta_arg<Type>(locator<meta_ctx>::value_or(), index);
  194. }
  195. /**
  196. * @brief Sets the value of a given variable.
  197. * @tparam Type Reflected type to which the variable is associated.
  198. * @tparam Data The actual variable to set.
  199. * @param instance An opaque instance of the underlying type, if required.
  200. * @param value Parameter to use to set the variable.
  201. * @return True in case of success, false otherwise.
  202. */
  203. template<typename Type, auto Data>
  204. [[nodiscard]] bool meta_setter([[maybe_unused]] meta_handle instance, [[maybe_unused]] meta_any value) {
  205. if constexpr(!std::is_same_v<decltype(Data), Type> && !std::is_same_v<decltype(Data), std::nullptr_t>) {
  206. if constexpr(std::is_member_function_pointer_v<decltype(Data)> || std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>>) {
  207. using descriptor = meta_function_helper_t<Type, decltype(Data)>;
  208. using data_type = type_list_element_t<descriptor::is_static, typename descriptor::args_type>;
  209. if(auto *const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
  210. std::invoke(Data, *clazz, value.cast<data_type>());
  211. return true;
  212. }
  213. } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
  214. using data_type = std::remove_reference_t<typename meta_function_helper_t<Type, decltype(Data)>::return_type>;
  215. if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
  216. if(auto *const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
  217. std::invoke(Data, *clazz) = value.cast<data_type>();
  218. return true;
  219. }
  220. }
  221. } else {
  222. using data_type = std::remove_reference_t<decltype(*Data)>;
  223. if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
  224. if(value.allow_cast<data_type>()) {
  225. *Data = value.cast<data_type>();
  226. return true;
  227. }
  228. }
  229. }
  230. }
  231. return false;
  232. }
  233. /**
  234. * @brief Gets the value of a given variable.
  235. *
  236. * @warning
  237. * The context provided is used only for the return type.<br/>
  238. * It's up to the caller to bind the arguments to the right context(s).
  239. *
  240. * @tparam Type Reflected type to which the variable is associated.
  241. * @tparam Data The actual variable to get.
  242. * @tparam Policy Optional policy (no policy set by default).
  243. * @param ctx The context from which to search for meta types.
  244. * @param instance An opaque instance of the underlying type, if required.
  245. * @return A meta any containing the value of the underlying variable.
  246. */
  247. template<typename Type, auto Data, typename Policy = as_is_t>
  248. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_getter(const meta_ctx &ctx, [[maybe_unused]] meta_handle instance) {
  249. if constexpr(std::is_member_pointer_v<decltype(Data)> || std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>>) {
  250. if constexpr(!std::is_array_v<std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<decltype(Data), Type &>>>>) {
  251. if constexpr(std::is_invocable_v<decltype(Data), Type &>) {
  252. if(auto *clazz = instance->try_cast<Type>(); clazz) {
  253. return meta_dispatch<Policy>(ctx, std::invoke(Data, *clazz));
  254. }
  255. }
  256. if constexpr(std::is_invocable_v<decltype(Data), const Type &>) {
  257. if(auto *fallback = instance->try_cast<const Type>(); fallback) {
  258. return meta_dispatch<Policy>(ctx, std::invoke(Data, *fallback));
  259. }
  260. }
  261. }
  262. return meta_any{meta_ctx_arg, ctx};
  263. } else if constexpr(std::is_pointer_v<decltype(Data)>) {
  264. if constexpr(std::is_array_v<std::remove_pointer_t<decltype(Data)>>) {
  265. return meta_any{meta_ctx_arg, ctx};
  266. } else {
  267. return meta_dispatch<Policy>(ctx, *Data);
  268. }
  269. } else {
  270. return meta_dispatch<Policy>(ctx, Data);
  271. }
  272. }
  273. /**
  274. * @brief Gets the value of a given variable.
  275. * @tparam Type Reflected type to which the variable is associated.
  276. * @tparam Data The actual variable to get.
  277. * @tparam Policy Optional policy (no policy set by default).
  278. * @param instance An opaque instance of the underlying type, if required.
  279. * @return A meta any containing the value of the underlying variable.
  280. */
  281. template<typename Type, auto Data, typename Policy = as_is_t>
  282. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_getter(meta_handle instance) {
  283. return meta_getter<Type, Data, Policy>(locator<meta_ctx>::value_or(), std::move(instance));
  284. }
  285. /**
  286. * @cond TURN_OFF_DOXYGEN
  287. * Internal details not to be documented.
  288. */
  289. namespace internal {
  290. template<typename Policy, typename Candidate, typename... Args>
  291. [[nodiscard]] meta_any meta_invoke_with_args(const meta_ctx &ctx, Candidate &&candidate, Args &&...args) {
  292. if constexpr(std::is_void_v<decltype(std::invoke(std::forward<Candidate>(candidate), args...))>) {
  293. std::invoke(std::forward<Candidate>(candidate), args...);
  294. return meta_any{ctx, std::in_place_type<void>};
  295. } else {
  296. return meta_dispatch<Policy>(ctx, std::invoke(std::forward<Candidate>(candidate), args...));
  297. }
  298. }
  299. template<typename Type, typename Policy, typename Candidate, std::size_t... Index>
  300. [[nodiscard]] meta_any meta_invoke(const meta_ctx &ctx, [[maybe_unused]] meta_handle instance, Candidate &&candidate, [[maybe_unused]] meta_any *args, std::index_sequence<Index...>) {
  301. using descriptor = meta_function_helper_t<Type, std::remove_reference_t<Candidate>>;
  302. if constexpr(std::is_invocable_v<std::remove_reference_t<Candidate>, const Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
  303. if(const auto *const clazz = instance->try_cast<const Type>(); clazz && ((args + Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
  304. return meta_invoke_with_args<Policy>(ctx, std::forward<Candidate>(candidate), *clazz, (args + Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
  305. }
  306. } else if constexpr(std::is_invocable_v<std::remove_reference_t<Candidate>, Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
  307. if(auto *const clazz = instance->try_cast<Type>(); clazz && ((args + Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
  308. return meta_invoke_with_args<Policy>(ctx, std::forward<Candidate>(candidate), *clazz, (args + Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
  309. }
  310. } else {
  311. if(((args + Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
  312. return meta_invoke_with_args<Policy>(ctx, std::forward<Candidate>(candidate), (args + Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
  313. }
  314. }
  315. return meta_any{meta_ctx_arg, ctx};
  316. }
  317. template<typename Type, typename... Args, std::size_t... Index>
  318. [[nodiscard]] meta_any meta_construct(const meta_ctx &ctx, meta_any *const args, std::index_sequence<Index...>) {
  319. if(((args + Index)->allow_cast<Args>() && ...)) {
  320. return meta_any{ctx, std::in_place_type<Type>, (args + Index)->cast<Args>()...};
  321. }
  322. return meta_any{meta_ctx_arg, ctx};
  323. }
  324. } // namespace internal
  325. /**
  326. * Internal details not to be documented.
  327. * @endcond
  328. */
  329. /**
  330. * @brief Tries to _invoke_ an object given a list of erased parameters.
  331. *
  332. * @warning
  333. * The context provided is used only for the return type.<br/>
  334. * It's up to the caller to bind the arguments to the right context(s).
  335. *
  336. * @tparam Type Reflected type to which the object to _invoke_ is associated.
  337. * @tparam Policy Optional policy (no policy set by default).
  338. * @param ctx The context from which to search for meta types.
  339. * @tparam Candidate The type of the actual object to _invoke_.
  340. * @param instance An opaque instance of the underlying type, if required.
  341. * @param candidate The actual object to _invoke_.
  342. * @param args Parameters to use to _invoke_ the object.
  343. * @return A meta any containing the returned value, if any.
  344. */
  345. template<typename Type, typename Policy = as_is_t, typename Candidate>
  346. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_invoke(const meta_ctx &ctx, meta_handle instance, Candidate &&candidate, meta_any *const args) {
  347. return internal::meta_invoke<Type, Policy>(ctx, std::move(instance), std::forward<Candidate>(candidate), args, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<Candidate>>::args_type::size>{});
  348. }
  349. /**
  350. * @brief Tries to _invoke_ an object given a list of erased parameters.
  351. * @tparam Type Reflected type to which the object to _invoke_ is associated.
  352. * @tparam Policy Optional policy (no policy set by default).
  353. * @tparam Candidate The type of the actual object to _invoke_.
  354. * @param instance An opaque instance of the underlying type, if required.
  355. * @param candidate The actual object to _invoke_.
  356. * @param args Parameters to use to _invoke_ the object.
  357. * @return A meta any containing the returned value, if any.
  358. */
  359. template<typename Type, typename Policy = as_is_t, typename Candidate>
  360. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_invoke(meta_handle instance, Candidate &&candidate, meta_any *const args) {
  361. return meta_invoke<Type, Policy>(locator<meta_ctx>::value_or(), std::move(instance), std::forward<Candidate>(candidate), args);
  362. }
  363. /**
  364. * @brief Tries to invoke a function given a list of erased parameters.
  365. *
  366. * @warning
  367. * The context provided is used only for the return type.<br/>
  368. * It's up to the caller to bind the arguments to the right context(s).
  369. *
  370. * @tparam Type Reflected type to which the function is associated.
  371. * @tparam Candidate The actual function to invoke.
  372. * @tparam Policy Optional policy (no policy set by default).
  373. * @param ctx The context from which to search for meta types.
  374. * @param instance An opaque instance of the underlying type, if required.
  375. * @param args Parameters to use to invoke the function.
  376. * @return A meta any containing the returned value, if any.
  377. */
  378. template<typename Type, auto Candidate, typename Policy = as_is_t>
  379. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_invoke(const meta_ctx &ctx, meta_handle instance, meta_any *const args) {
  380. return internal::meta_invoke<Type, Policy>(ctx, std::move(instance), Candidate, args, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<decltype(Candidate)>>::args_type::size>{});
  381. }
  382. /**
  383. * @brief Tries to invoke a function given a list of erased parameters.
  384. * @tparam Type Reflected type to which the function is associated.
  385. * @tparam Candidate The actual function to invoke.
  386. * @tparam Policy Optional policy (no policy set by default).
  387. * @param instance An opaque instance of the underlying type, if required.
  388. * @param args Parameters to use to invoke the function.
  389. * @return A meta any containing the returned value, if any.
  390. */
  391. template<typename Type, auto Candidate, typename Policy = as_is_t>
  392. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_invoke(meta_handle instance, meta_any *const args) {
  393. return meta_invoke<Type, Candidate, Policy>(locator<meta_ctx>::value_or(), std::move(instance), args);
  394. }
  395. /**
  396. * @brief Tries to construct an instance given a list of erased parameters.
  397. *
  398. * @warning
  399. * The context provided is used only for the return type.<br/>
  400. * It's up to the caller to bind the arguments to the right context(s).
  401. *
  402. * @tparam Type Actual type of the instance to construct.
  403. * @tparam Args Types of arguments expected.
  404. * @param ctx The context from which to search for meta types.
  405. * @param args Parameters to use to construct the instance.
  406. * @return A meta any containing the new instance, if any.
  407. */
  408. template<typename Type, typename... Args>
  409. [[nodiscard]] meta_any meta_construct(const meta_ctx &ctx, meta_any *const args) {
  410. return internal::meta_construct<Type, Args...>(ctx, args, std::index_sequence_for<Args...>{});
  411. }
  412. /**
  413. * @brief Tries to construct an instance given a list of erased parameters.
  414. * @tparam Type Actual type of the instance to construct.
  415. * @tparam Args Types of arguments expected.
  416. * @param args Parameters to use to construct the instance.
  417. * @return A meta any containing the new instance, if any.
  418. */
  419. template<typename Type, typename... Args>
  420. [[nodiscard]] meta_any meta_construct(meta_any *const args) {
  421. return meta_construct<Type, Args...>(locator<meta_ctx>::value_or(), args);
  422. }
  423. /**
  424. * @brief Tries to construct an instance given a list of erased parameters.
  425. *
  426. * @warning
  427. * The context provided is used only for the return type.<br/>
  428. * It's up to the caller to bind the arguments to the right context(s).
  429. *
  430. * @tparam Type Reflected type to which the object to _invoke_ is associated.
  431. * @tparam Policy Optional policy (no policy set by default).
  432. * @tparam Candidate The type of the actual object to _invoke_.
  433. * @param ctx The context from which to search for meta types.
  434. * @param candidate The actual object to _invoke_.
  435. * @param args Parameters to use to _invoke_ the object.
  436. * @return A meta any containing the returned value, if any.
  437. */
  438. template<typename Type, typename Policy = as_is_t, typename Candidate>
  439. [[nodiscard]] meta_any meta_construct(const meta_ctx &ctx, Candidate &&candidate, meta_any *const args) {
  440. if constexpr(meta_function_helper_t<Type, Candidate>::is_static || std::is_class_v<std::remove_cv_t<std::remove_reference_t<Candidate>>>) {
  441. return internal::meta_invoke<Type, Policy>(ctx, {}, std::forward<Candidate>(candidate), args, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<Candidate>>::args_type::size>{});
  442. } else {
  443. return internal::meta_invoke<Type, Policy>(ctx, *args, std::forward<Candidate>(candidate), args + 1u, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<Candidate>>::args_type::size>{});
  444. }
  445. }
  446. /**
  447. * @brief Tries to construct an instance given a list of erased parameters.
  448. * @tparam Type Reflected type to which the object to _invoke_ is associated.
  449. * @tparam Policy Optional policy (no policy set by default).
  450. * @tparam Candidate The type of the actual object to _invoke_.
  451. * @param candidate The actual object to _invoke_.
  452. * @param args Parameters to use to _invoke_ the object.
  453. * @return A meta any containing the returned value, if any.
  454. */
  455. template<typename Type, typename Policy = as_is_t, typename Candidate>
  456. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_construct(Candidate &&candidate, meta_any *const args) {
  457. return meta_construct<Type, Policy>(locator<meta_ctx>::value_or(), std::forward<Candidate>(candidate), args);
  458. }
  459. /**
  460. * @brief Tries to construct an instance given a list of erased parameters.
  461. *
  462. * @warning
  463. * The context provided is used only for the return type.<br/>
  464. * It's up to the caller to bind the arguments to the right context(s).
  465. *
  466. * @tparam Type Reflected type to which the function is associated.
  467. * @tparam Candidate The actual function to invoke.
  468. * @tparam Policy Optional policy (no policy set by default).
  469. * @param ctx The context from which to search for meta types.
  470. * @param args Parameters to use to invoke the function.
  471. * @return A meta any containing the returned value, if any.
  472. */
  473. template<typename Type, auto Candidate, typename Policy = as_is_t>
  474. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_construct(const meta_ctx &ctx, meta_any *const args) {
  475. return meta_construct<Type, Policy>(ctx, Candidate, args);
  476. }
  477. /**
  478. * @brief Tries to construct an instance given a list of erased parameters.
  479. * @tparam Type Reflected type to which the function is associated.
  480. * @tparam Candidate The actual function to invoke.
  481. * @tparam Policy Optional policy (no policy set by default).
  482. * @param args Parameters to use to invoke the function.
  483. * @return A meta any containing the returned value, if any.
  484. */
  485. template<typename Type, auto Candidate, typename Policy = as_is_t>
  486. [[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_construct(meta_any *const args) {
  487. return meta_construct<Type, Candidate, Policy>(locator<meta_ctx>::value_or(), args);
  488. }
  489. } // namespace entt
  490. #endif