visitor.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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_VISITOR_H_
  28. #define RTTR_VISITOR_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/rttr_enable.h"
  31. #include "rttr/type_list.h"
  32. #include "rttr/detail/visitor/visitor_registration.h"
  33. #include <type_traits>
  34. #include <functional>
  35. #include <tuple>
  36. namespace rttr
  37. {
  38. class property;
  39. class method;
  40. class constructor;
  41. /*!
  42. * \brief The class \ref visitor, is used for visiting your registered accessors of a type at compile time.
  43. *
  44. * Sometimes it might be necessary to get access to the underlying
  45. * registered accessor (e.g. function pointers, object pointers, etc...) of a type.
  46. * For example in order to bind a type to a scripting library, which only allows to register concrete types.
  47. * The generic approach of using a visitor will solve this problem. Furthermore, because it is template based,
  48. * you can write one visitor to iterate over all your types in a generic way and have access to the static type information.
  49. *
  50. * Own visitor
  51. * -----------
  52. * In order to implement your own visitor class, you have to derive from the class \ref visitor and reimplement
  53. * the following visitor function templates:
  54. * 1. `void visitor::visit_type_begin(const type_info<T>& info)`
  55. * 2. `void visitor::visit_type_end(const type_info<T>& info);`
  56. * 3. `void visitor::visit_constructor(const constructor_info<T>& info);`
  57. * 4. `void visitor::visit_constructor_function(const constructor_function_info<T>& info);`
  58. * 5. `void visitor::visit_method(const method_info<T>& info);`
  59. * 6. `void visitor::visit_global_method(const method_info<T>& info);`
  60. *
  61. * and add the macro `RTTR_ENABLE(visitor);` in the class body.
  62. *
  63. * Using a visitor
  64. * ---------------
  65. * \code{.cpp}
  66. *
  67. * #include "print_visitor.h" // IMPORTANT: You have to include your visitor always before the include of the `<rttr/registration>`
  68. * #include <rttr/registration>
  69. *
  70. * RTTR_REGISTRATION
  71. * {
  72. * rttr::registration::class_<cat>("cat")
  73. * .method("meow", &cat::meow);
  74. *
  75. * }
  76. * int main()
  77. * {
  78. * print_visitor vi;
  79. *
  80. * type t = type:get_by_name("cat");
  81. * vi.visit(t);
  82. * }
  83. * \endcode
  84. *
  85. * It is very important that the include of the visitor is done before the include of `<rttr/registration>`,
  86. * otherwise the visitor will not be found by rttr. The reason for that is, the visit functions will be created at compile
  87. * time, so the declaration has to be before the instantiation.
  88. * Because of limitations of the c++ preprocessor, it is not possible to include the visitor or the `<rttr/registration>` header in
  89. * a precompiled header and use the \ref visitor functionality. All custom visitors will not be found.
  90. *
  91. */
  92. class RTTR_API visitor
  93. {
  94. public:
  95. /*!
  96. * \brief The constructor of the visitor class.
  97. *
  98. */
  99. visitor();
  100. /*!
  101. * \brief The destructor of the visitor class.
  102. *
  103. */
  104. virtual ~visitor();
  105. /*!
  106. * \brief Calling this function will indirectly call the visit functions
  107. * for all registered types members (constructors, methods, properties, etc...)
  108. * This includes also registered base classes.
  109. */
  110. void visit(type t);
  111. /*!
  112. * \brief Calling this function will indirectly call the function \ref visit_method()
  113. * for the underlying registered type.
  114. */
  115. void visit(method meth);
  116. /*!
  117. * \brief Calling this function will indirectly call the function \ref visit_constructor()
  118. * or \ref visit_constructor_function() for the underlying registered type.
  119. */
  120. void visit(constructor ctor);
  121. /*!
  122. * \brief Calling this function will indirectly call one of the functions:
  123. * - \ref visit_property()
  124. * - \ref visit_readonly_property()
  125. * - \ref visit_global_property()
  126. * - \ref visit_global_readonly_property()
  127. * for the underlying registered type.
  128. */
  129. void visit(property prop);
  130. #ifdef DOXYGEN
  131. /*!
  132. * \brief The \ref type_info class is used to forward all information during registration of a \ref rttr::registration::class_ "class".
  133. * The information will be forwarded when the methods: \ref visit_type_begin() and \ref visit_type_end() are implemented.
  134. */
  135. template<typename T>
  136. struct type_info
  137. {
  138. using declaring_type = T; //!< The compile time type information,
  139. //!< corresponds to template parameter used in: `rttr::type::get<T>()`, or `rttr::registration::class_<T>("...")`
  140. using base_classes = type_list<Base_Classes...>; //!< A list of base classes from type \p T
  141. const type& type_item; //!< The type object
  142. };
  143. /*!
  144. * \brief The \ref constructor_info class is used to forward all information during registration of a constructor.
  145. * The information will be forwarded when the methods: \ref visit_constructor() are implemented.
  146. */
  147. template<typename T>
  148. struct constructor_info
  149. {
  150. using declaring_type = T; //!< The type \p T where this constructor is declared to.
  151. using policy = Policy; //!< The used policy during registration
  152. const constructor& ctor_item; //!< The constructor object
  153. };
  154. /*!
  155. * \brief The \ref constructor_function_info class is used to forward all information during registration of a \ref constructor function.
  156. * The information will be forwarded when the methods: \ref visit_constructor_function() are implemented.
  157. */
  158. template<typename T>
  159. struct constructor_function_info
  160. {
  161. using declaring_type = T; //!< The type \p T where this constructor is declared to.
  162. using policy = Policy; //!< The used policy during registration
  163. const constructor ctor_item; //!< The constructor object
  164. Acc function_ptr; //!< The function pointer to create object T
  165. };
  166. /*!
  167. * \brief The \ref method_info class is used to forward all information during registration of a \ref method.
  168. * The information will be forwarded when the methods: \ref visit_method() are implemented.
  169. */
  170. template<typename T>
  171. struct method_info
  172. {
  173. using declaring_type = T; //!< The type \p T where this method is declared to.
  174. using policy = Policy; //!< The used policy during registration
  175. const method& method_item; //!< The method object
  176. Acc function_ptr; //!< The function pointer of the method (can be a member- or free function)
  177. };
  178. /*!
  179. * \brief The \ref property_info class is used to forward all information during registration of a \ref property.
  180. * The information will be forwarded when the methods: \ref visit_property() are implemented.
  181. */
  182. template<typename T>
  183. struct property_info
  184. {
  185. using declaring_type = T;
  186. using policy = Policy;
  187. const property property_item;
  188. Acc property_accessor;
  189. };
  190. /*!
  191. * \brief The \ref method_info class is used to forward all information during registration of a \ref property.
  192. * The information will be forwarded when the methods: \ref visit_property() are implemented.
  193. */
  194. template<typename T>
  195. struct property_getter_setter_info
  196. {
  197. using declaring_type = T;
  198. using policy = Policy;
  199. const property property_item;
  200. Getter property_getter;
  201. Setter property_setter;
  202. };
  203. #else
  204. template<typename T>
  205. struct type_info;
  206. template<typename T, typename...Base_Classes>
  207. struct type_info<type_list<T, type_list<Base_Classes...>>>
  208. {
  209. using declaring_type = T;
  210. using base_classes = type_list<Base_Classes...>;
  211. const type& type_item;
  212. };
  213. template<typename T>
  214. struct constructor_info;
  215. template<typename T, typename Policy, typename...Ctor_args>
  216. struct constructor_info<type_list<T, Policy, type_list<Ctor_args...>>>
  217. {
  218. using declaring_type = T;
  219. using ctor_args = type_list<Ctor_args...>;
  220. using policy = Policy;
  221. const constructor& ctor_item;
  222. };
  223. template<typename T>
  224. struct constructor_function_info;
  225. template<typename T, typename Policy, typename Acc>
  226. struct constructor_function_info<type_list<T, Policy, Acc>>
  227. {
  228. using declaring_type = T;
  229. using policy = Policy;
  230. const constructor ctor_item;
  231. Acc function_ptr;
  232. };
  233. template<typename T>
  234. struct method_info;
  235. template<typename T, typename Policy, typename Acc>
  236. struct method_info<type_list<T, Policy, Acc>>
  237. {
  238. using declaring_type = T;
  239. using policy = Policy;
  240. const method method_item;
  241. Acc function_ptr;
  242. };
  243. template<typename T>
  244. struct property_info;
  245. template<typename T, typename Policy, typename Acc>
  246. struct property_info<type_list<T, Policy, Acc>>
  247. {
  248. using declaring_type = T;
  249. using policy = Policy;
  250. const property property_item;
  251. Acc property_accessor;
  252. };
  253. template<typename T>
  254. struct property_getter_setter_info;
  255. template<typename T, typename Policy, typename Getter, typename Setter>
  256. struct property_getter_setter_info<type_list<T, Policy, Getter, Setter>>
  257. {
  258. using declaring_type = T;
  259. using policy = Policy;
  260. const property property_item;
  261. Getter property_getter;
  262. Setter property_setter;
  263. };
  264. #endif
  265. /*!
  266. * \brief This function will be called when you visit a type via: \ref visit(type)
  267. * It is the first function that will be invoked, when visiting a type.
  268. * When the visiting type has base classes, it will be first invoked with he most basic type
  269. * and last step with the current type.
  270. * Reimplement this function, when you need the static compile time type information.
  271. *
  272. * \param T Internal template type, do not work with this parameter directly.
  273. * \param Base_Classes... A list of base classes, ascending order
  274. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  275. *
  276. * \remark You have to register your type via: \ref registration::class_
  277. * otherwise the static type information for invoke cannot be retrieved.
  278. * The signature has to match exactly the declaration here, otherwise it will not be invoked.
  279. *
  280. * You normally don't call this function directly. However, make sure this function is declared public,
  281. * otherwise it cannot be invoked.
  282. *
  283. * \see type::get_base_classes()
  284. */
  285. template<typename T, typename...Base_Classes>
  286. void visit_type_begin(const type_info<T>& info);
  287. /*!
  288. * \brief This function will be called when you visit a type via: \ref visit(type).
  289. * It is the last function that will be invoked, when visiting a type.
  290. * When the visiting type has base classes, it will be first invoked with he most basic type
  291. * and last step with the current type.
  292. * Reimplement this function, when you need the static compile time type information.
  293. *
  294. * \param T Internal type type, do not work with this parameter directly.
  295. * \param Base_Classes... A list of base classes, ascending order
  296. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  297. *
  298. * \remark You have to register your type via: \ref registration::class_
  299. * otherwise the static type information for invoke cannot be retrieved.
  300. * The signature has to match exactly the declaration here, otherwise it will not be invoked.
  301. *
  302. * You normally don't call this function directly. However, make sure this function is declared public,
  303. * otherwise it cannot be invoked.
  304. *
  305. * \see type::get_base_classes()
  306. */
  307. template<typename T, typename...Base_Classes>
  308. void visit_type_end(const type_info<T>& info);
  309. /*!
  310. * \brief This function will be called when you visit a constructor via: \ref visit(type)
  311. * or directly\ref visit(constructor).
  312. * Reimplement this function, when you need the static compile time type information.
  313. *
  314. * \param T Internal template type, do not work with this parameter directly.
  315. * \param Ctor_Args The argument of the constructor
  316. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  317. *
  318. * You normally don't call this function directly. However, make sure this function is declared public,
  319. * otherwise it cannot be invoked.
  320. *
  321. * \see visit(constructor)
  322. */
  323. template<typename T, typename...Ctor_Args>
  324. void visit_constructor(const constructor_info<T>& info);
  325. /*!
  326. * \brief This function will be called when you visit a constructor function via: \ref visit(type)
  327. * or \ref visit(constructor).
  328. * Reimplement this function, when you need the static compile time type information.
  329. *
  330. * \param T Internal template type, do not work with this parameter directly.
  331. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  332. *
  333. * You normally don't call this function directly. However, make sure this function is declared public,
  334. * otherwise it cannot be invoked.
  335. *
  336. * \see visit(constructor)
  337. */
  338. template<typename T>
  339. void visit_constructor_function(const constructor_function_info<T>& info);
  340. /*!
  341. * \brief This function will be called when you visit a type method via: \ref visit(type) or \ref visit(method).
  342. * Reimplement this function, when you need the static compile time type information.
  343. *
  344. * \param T Internal template type, do not work with this parameter directly.
  345. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  346. *
  347. * You normally don't call this function directly. However, make sure this function is declared public,
  348. * otherwise it cannot be invoked.
  349. *
  350. * \see visit(method), type::get_method()
  351. */
  352. template<typename T>
  353. void visit_method(const method_info<T>& info);
  354. /*!
  355. * \brief This function will be called when you visit a global method via: \ref visit(method).
  356. * Reimplement this function, when you need the static compile time type information.
  357. *
  358. * \param T Internal template type, do not work with this parameter directly.
  359. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  360. *
  361. * You normally don't call this function directly. However, make sure this function is declared public,
  362. * otherwise it cannot be invoked.
  363. *
  364. * \see visit(method), type::get_global_method()
  365. */
  366. template<typename T>
  367. void visit_global_method(const method_info<T>& info);
  368. /*!
  369. * \brief This function will be called when you visit a property via: \ref visit(property).
  370. * Reimplement this function, when you need the static compile time type information.
  371. *
  372. * \param T Internal template type, do not work with this parameter directly.
  373. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  374. *
  375. * You normally don't call this function directly. However, make sure this function is declared public,
  376. * otherwise it cannot be invoked.
  377. *
  378. * \see visit(property), type::get_property()
  379. */
  380. template<typename T>
  381. void visit_property(const property_info<T>& info);
  382. /*!
  383. * \brief This function will be called when you visit a property via: \ref visit(property).
  384. * Reimplement this function, when you need the static compile time type information.
  385. *
  386. * \param T Internal template type, do not work with this parameter directly.
  387. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  388. *
  389. * You normally don't call this function directly. However, make sure this function is declared public,
  390. * otherwise it cannot be invoked.
  391. *
  392. * \see visit(property), type::get_property()
  393. */
  394. template<typename T>
  395. void visit_getter_setter_property(const property_getter_setter_info<T>& info);
  396. /*!
  397. * \brief This function will be called when you visit a global property via: \ref visit(property).
  398. * Reimplement this function, when you need the static compile time type information.
  399. *
  400. * \param T Internal template type, do not work with this parameter directly.
  401. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  402. *
  403. * You normally don't call this function directly. However, make sure this function is declared public,
  404. * otherwise it cannot be invoked.
  405. *
  406. * \see visit(property), type::get_global_property()
  407. */
  408. template<typename T>
  409. void visit_global_property(const property_info<T>& info);
  410. /*!
  411. * \brief This function will be called when you visit a global property via: \ref visit(property).
  412. * Reimplement this function, when you need the static compile time type information.
  413. *
  414. * \param T Internal template type, do not work with this parameter directly.
  415. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  416. *
  417. * You normally don't call this function directly. However, make sure this function is declared public,
  418. * otherwise it cannot be invoked.
  419. *
  420. * \see visit(property), type::get_global_property()
  421. */
  422. template<typename T>
  423. void visit_global_getter_setter_property(const property_getter_setter_info<T>& info);
  424. /*!
  425. * \brief This function will be called when you visit a read only property via: \ref visit(property).
  426. * Reimplement this function, when you need the static compile time type information.
  427. *
  428. * \param T Internal template type, do not work with this parameter directly.
  429. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  430. *
  431. * You normally don't call this function directly. However, make sure this function is declared public,
  432. * otherwise it cannot be invoked.
  433. *
  434. * \see visit(property), type::get_property()
  435. */
  436. template<typename T>
  437. void visit_readonly_property(const property_info<T>& info);
  438. /*!
  439. * \brief This function will be called when you visit a global read only property via: \ref visit(property).
  440. * Reimplement this function, when you need the static compile time type information.
  441. *
  442. * \param T Internal template type, do not work with this parameter directly.
  443. * \param info This object will be provided by RTTR, use it's public members and the `using's`
  444. *
  445. * You normally don't call this function directly. However, make sure this function is declared public,
  446. * otherwise it cannot be invoked.
  447. *
  448. * \see visit(property), type::get_global_property()
  449. */
  450. template<typename T>
  451. void visit_global_readonly_property(const property_info<T>& info);
  452. private:
  453. void visit_impl(const type& t);
  454. private:
  455. RTTR_ENABLE();
  456. };
  457. } // end namespace rttr
  458. #include "rttr/detail/visitor/visitor_impl.h"
  459. #endif // RTTR_VISITOR_H_