core.hpp 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879
  1. // Copyright (c) 2018-2025 Jean-Louis Leroy
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // See accompanying file LICENSE_1_0.txt
  4. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_OPENMETHOD_CORE_HPP
  6. #define BOOST_OPENMETHOD_CORE_HPP
  7. #include <stdint.h>
  8. #include <algorithm>
  9. #include <cstdlib>
  10. #include <tuple>
  11. #include <type_traits>
  12. #include <utility>
  13. #include <boost/assert.hpp>
  14. #include <boost/config.hpp>
  15. #include <boost/mp11/algorithm.hpp>
  16. #include <boost/mp11/bind.hpp>
  17. #include <boost/mp11/integral.hpp>
  18. #include <boost/mp11/list.hpp>
  19. #include <boost/openmethod/preamble.hpp>
  20. #include <boost/openmethod/default_registry.hpp>
  21. #ifndef BOOST_OPENMETHOD_DEFAULT_REGISTRY
  22. #define BOOST_OPENMETHOD_DEFAULT_REGISTRY ::boost::openmethod::default_registry
  23. #endif
  24. #ifdef _MSC_VER
  25. #pragma warning(push)
  26. #pragma warning(disable : 4100)
  27. #pragma warning(disable : 4646)
  28. #pragma warning(disable : 4702) // unreachable code
  29. #endif
  30. //! Top namespace of the library.
  31. namespace boost::openmethod {
  32. #ifdef __MRDOCS__
  33. #define BOOST_OPENMETHOD_OPEN_NAMESPACE_DETAIL_UNLESS_MRDOCS
  34. #define BOOST_OPENMETHOD_CLOSE_NAMESPACE_DETAIL_UNLESS_MRDOCS
  35. #define BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  36. #else
  37. #define BOOST_OPENMETHOD_OPEN_NAMESPACE_DETAIL_UNLESS_MRDOCS namespace detail {
  38. #define BOOST_OPENMETHOD_CLOSE_NAMESPACE_DETAIL_UNLESS_MRDOCS }
  39. #define BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS detail::
  40. #endif
  41. namespace detail {
  42. using sfinae = void;
  43. }
  44. template<
  45. class Class, class Registry = BOOST_OPENMETHOD_DEFAULT_REGISTRY,
  46. typename = detail::sfinae>
  47. class virtual_ptr;
  48. // =============================================================================
  49. // Helpers
  50. namespace detail {
  51. using macro_default_registry = BOOST_OPENMETHOD_DEFAULT_REGISTRY;
  52. template<typename...>
  53. struct extract_registry;
  54. template<>
  55. struct extract_registry<> {
  56. using registry = BOOST_OPENMETHOD_DEFAULT_REGISTRY;
  57. using others = mp11::mp_list<>;
  58. };
  59. template<typename Type>
  60. struct extract_registry<Type> {
  61. using registry = std::conditional_t<
  62. is_registry<Type>, Type, BOOST_OPENMETHOD_DEFAULT_REGISTRY>;
  63. using others = std::conditional_t<
  64. is_registry<Type>, mp11::mp_list<>, mp11::mp_list<Type>>;
  65. };
  66. template<typename Type1, typename Type2, typename... MoreTypes>
  67. struct extract_registry<Type1, Type2, MoreTypes...> {
  68. static_assert(!is_registry<Type1>, "policy must be the last in the list");
  69. using registry = typename extract_registry<Type2, MoreTypes...>::registry;
  70. using others = mp11::mp_push_front<
  71. typename extract_registry<Type2, MoreTypes...>::others, Type1>;
  72. };
  73. template<class Registry, class... Class>
  74. struct init_type_ids;
  75. template<class Registry, class... Class>
  76. struct init_type_ids<Registry, mp11::mp_list<Class...>> {
  77. static auto fn(type_id* ids) {
  78. (..., (*ids++ = Registry::rtti::template static_type<Class>()));
  79. return ids;
  80. }
  81. };
  82. template<class Base, class Derived>
  83. struct is_unambiguous_accessible_base_of : std::is_base_of<Base, Derived> {
  84. static_assert(
  85. std::is_base_of_v<Base, Derived> ==
  86. std::is_convertible_v<Derived&, Base&>,
  87. "class must be an accessible unambiguous base, repeated inheritance is "
  88. "not "
  89. "supported");
  90. };
  91. // Collect the base classes of a list of classes. The result is a mp11 map that
  92. // associates each class to a list starting with the class itself, followed by
  93. // all its bases, as per std::is_base_of. Thus the list includes the class
  94. // itself at least twice: at the front, and down the list, as its own improper
  95. // base. The direct and indirect bases are all included. The runtime will
  96. // extract the direct proper bases.
  97. template<typename... Cs>
  98. using inheritance_map = mp11::mp_list<boost::mp11::mp_push_front<
  99. boost::mp11::mp_filter_q<
  100. boost::mp11::mp_bind_back<is_unambiguous_accessible_base_of, Cs>,
  101. mp11::mp_list<Cs...>>,
  102. Cs>...>;
  103. // =============================================================================
  104. // optimal_cast
  105. template<typename B, typename D, typename = void>
  106. struct requires_dynamic_cast_ref_aux : std::true_type {};
  107. template<typename B, typename D>
  108. struct requires_dynamic_cast_ref_aux<
  109. B, D, std::void_t<decltype(static_cast<D>(std::declval<B>()))>>
  110. : std::false_type {};
  111. template<class B, class D>
  112. constexpr bool requires_dynamic_cast =
  113. detail::requires_dynamic_cast_ref_aux<B, D>::value;
  114. template<class Registry, class D, class B>
  115. auto optimal_cast(B&& obj) -> decltype(auto) {
  116. if constexpr (requires_dynamic_cast<B, D>) {
  117. return Registry::rtti::template dynamic_cast_ref<D>(
  118. std::forward<B>(obj));
  119. } else {
  120. return static_cast<D>(obj);
  121. }
  122. }
  123. // =============================================================================
  124. // Common details
  125. template<typename T>
  126. struct is_virtual : std::false_type {};
  127. template<typename T>
  128. struct is_virtual<virtual_<T>> : std::true_type {};
  129. template<typename T>
  130. struct remove_virtual_aux {
  131. using type = T;
  132. };
  133. template<typename T>
  134. struct remove_virtual_aux<virtual_<T>> {
  135. using type = T;
  136. };
  137. template<typename T>
  138. using remove_virtual_ = typename remove_virtual_aux<T>::type;
  139. template<typename T, class Registry, typename = void>
  140. struct virtual_type_aux {
  141. using type = void;
  142. };
  143. template<typename T, class Registry>
  144. struct virtual_type_aux<
  145. T, Registry,
  146. std::void_t<typename virtual_traits<T, Registry>::virtual_type>> {
  147. using type = typename virtual_traits<T, Registry>::virtual_type;
  148. };
  149. template<typename T, class Registry>
  150. using virtual_type = typename virtual_type_aux<T, Registry>::type;
  151. template<typename MethodArgList>
  152. using virtual_types = boost::mp11::mp_transform<
  153. remove_virtual_, boost::mp11::mp_filter<detail::is_virtual, MethodArgList>>;
  154. } // namespace detail
  155. BOOST_OPENMETHOD_OPEN_NAMESPACE_DETAIL_UNLESS_MRDOCS
  156. //! Removes the virtual_<> decorator, if present (exposition only).
  157. //!
  158. //! Provides a nested `type` equal to `T`. The template is specialized for
  159. //! `virtual_<T>`.
  160. //!
  161. //! @tparam T A type.
  162. template<typename T>
  163. struct StripVirtualDecorator {
  164. //! Same as `T`
  165. using type = T;
  166. };
  167. //! Removes the virtual_<> decorator (exposition only).
  168. //!
  169. //! Provides a nested `type` equal to `T`.
  170. //!
  171. //! @tparam T A type.
  172. template<typename T>
  173. struct StripVirtualDecorator<virtual_<T>> {
  174. //! Same as `T`.
  175. using type = T;
  176. };
  177. BOOST_OPENMETHOD_CLOSE_NAMESPACE_DETAIL_UNLESS_MRDOCS
  178. // =============================================================================
  179. // virtual_traits
  180. //! Traits for types used as virtual parameters.
  181. //!
  182. //! `virtual_traits` must be specialized for each type that can be used as a
  183. //! virtual parameters. It enables methods to:
  184. //! @li find the type of the object the argument refers to (e.g. `Node` from
  185. //! `Node&`)
  186. //! @li obtain a non-modifiable reference to that object (e.g. a `const Node&` from
  187. //! `Node&`)
  188. //! @li cast the argument to another type (e.g. cast a `Node&` to a `Plus&`)
  189. //!
  190. //! @par Requirements
  191. //!
  192. //! Specializations of `virtual_traits` must provide the members described to
  193. //! the @ref VirtualTraits blueprint.
  194. //!
  195. //! @tparam T A type referring (in the broad sense) to an instance of a class.
  196. //! @tparam Registry A @ref registry.
  197. template<typename T, class Registry>
  198. struct virtual_traits;
  199. //! Specialize virtual_traits for lvalue reference types.
  200. //!
  201. //! @tparam Class A class type, possibly cv-qualified.
  202. //! @tparam Registry A @ref registry.
  203. template<class Class, class Registry>
  204. struct virtual_traits<Class&, Registry> {
  205. //! `Class`, stripped from cv-qualifiers.
  206. using virtual_type = std::remove_cv_t<Class>;
  207. //! Return a reference to a non-modifiable `Class` object.
  208. //! @param arg A reference to a non-modifiable `Class` object.
  209. //! @return A reference to the same object.
  210. static auto peek(const Class& arg) -> const Class& {
  211. return arg;
  212. }
  213. //! Cast to another type.
  214. //!
  215. //! Cast an object to another type. If possible, use `static_cast`.
  216. //! Otherwise, use `Registry::rtti::dynamic_cast_ref`.
  217. //!
  218. //! @tparam Derived A lvalue reference type.
  219. //! @param obj A reference to a `Class` object.
  220. //! @return A reference to the same object, cast to `Derived`.
  221. template<typename Derived>
  222. static auto cast(Class& obj) -> Derived {
  223. static_assert(std::is_lvalue_reference_v<Derived>);
  224. return detail::optimal_cast<Registry, Derived>(obj);
  225. }
  226. };
  227. //! Specialize virtual_traits for xvalue reference types.
  228. //!
  229. //! @tparam T A xvalue reference type.
  230. //! @tparam Registry A @ref registry.
  231. template<class Class, class Registry>
  232. struct virtual_traits<Class&&, Registry> {
  233. //! Same as `Class`.
  234. using virtual_type = Class;
  235. //! Return a reference to a non-modifiable `Class` object.
  236. //! @param arg A reference to a non-modifiable `Class` object.
  237. //! @return A reference to the same object.
  238. static auto peek(const Class& arg) -> const Class& {
  239. return arg;
  240. }
  241. //! Cast to another type.
  242. //!
  243. //! Cast an object to another type. If possible, use `static_cast`.
  244. //! Otherwise, use `Registry::rtti::dynamic_cast_ref`.
  245. //!
  246. //! @tparam Derived A rvalue reference type.
  247. //! @param obj A reference to a `Class` object.
  248. //! @return A reference to the same object, cast to `Derived`.
  249. template<typename Derived>
  250. static auto cast(Class&& obj) -> Derived {
  251. static_assert(std::is_rvalue_reference_v<Derived>);
  252. return detail::optimal_cast<Registry, Derived>(obj);
  253. }
  254. };
  255. //! Specialize virtual_traits for pointer types.
  256. //!
  257. //! @tparam Class A class type, possibly cv-qualified.
  258. //! @tparam Registry A @ref registry.
  259. template<class Class, class Registry>
  260. struct virtual_traits<Class*, Registry> {
  261. //! `Class`, stripped from cv-qualifiers.
  262. using virtual_type = std::remove_cv_t<Class>;
  263. //! Return a reference to a non-modifiable `Class` object.
  264. //! @param arg A pointer to a non-modifiable `Class` object.
  265. //! @return A const reference to the same object.
  266. static auto peek(const Class* arg) -> const Class& {
  267. return *arg;
  268. }
  269. //! Cast to another type.
  270. //!
  271. //! Cast an object to another type. If possible, use `static_cast`.
  272. //! Otherwise, use `Registry::rtti::dynamic_cast_ref`.
  273. //!
  274. //! @tparam Derived A pointer type.
  275. //! @param obj A pointer to a `Class` object.
  276. //! @return A pointer to the same object, cast to `Derived`.
  277. template<typename Derived>
  278. static auto cast(Class* ptr) -> Derived {
  279. static_assert(std::is_pointer_v<Derived>);
  280. if constexpr (detail::requires_dynamic_cast<Class*, Derived>) {
  281. return dynamic_cast<Derived>(ptr);
  282. } else {
  283. return static_cast<Derived>(ptr);
  284. }
  285. }
  286. };
  287. namespace detail {
  288. template<class...>
  289. struct use_class_aux;
  290. template<class Registry, class Class, typename... Bases>
  291. struct use_class_aux<Registry, mp11::mp_list<Class, Bases...>>
  292. : std::conditional_t<
  293. Registry::has_deferred_static_rtti, detail::deferred_class_info,
  294. detail::class_info> {
  295. static type_id bases[sizeof...(Bases)];
  296. use_class_aux() {
  297. this->first_base = bases;
  298. this->last_base = bases + sizeof...(Bases);
  299. this->is_abstract = std::is_abstract_v<Class>;
  300. this->static_vptr = &Registry::template static_vptr<Class>;
  301. if constexpr (!Registry::has_deferred_static_rtti) {
  302. resolve_type_ids();
  303. }
  304. Registry::classes.push_back(*this);
  305. }
  306. void resolve_type_ids() {
  307. this->type = Registry::rtti::template static_type<Class>();
  308. auto iter = bases;
  309. (..., (*iter++ = Registry::rtti::template static_type<Bases>()));
  310. }
  311. ~use_class_aux() {
  312. Registry::classes.remove(*this);
  313. }
  314. };
  315. template<class Registry, class Class, typename... Bases>
  316. type_id use_class_aux<
  317. Registry, mp11::mp_list<Class, Bases...>>::bases[sizeof...(Bases)];
  318. template<class... Classes>
  319. using use_classes_tuple_type = boost::mp11::mp_apply<
  320. std::tuple,
  321. boost::mp11::mp_transform_q<
  322. boost::mp11::mp_bind_front<
  323. detail::use_class_aux,
  324. typename detail::extract_registry<Classes...>::registry>,
  325. boost::mp11::mp_apply<
  326. detail::inheritance_map,
  327. typename detail::extract_registry<Classes...>::others>>>;
  328. } // namespace detail
  329. //! Add classes to a registry
  330. //!
  331. //! `use_classes` is a registrar class that adds one or more classes to a
  332. //! registry.
  333. //!
  334. //! Classes potentially involved in a method definition, an overrider, or a
  335. //! method call must be registered via `use_classes`. A class may be registered
  336. //! multiple times. A class and its direct bases must be listed together in one
  337. //! or more instantiations of `use_classes`.
  338. //!
  339. //! If a class is identified by different type ids in different translation
  340. //! units, it must be registered in as many translation units as necessary for
  341. //! `use_classes` to register all the type ids. This situation can occur when
  342. //! using standard RTTI, because the address of the `type_info` objects are used
  343. //! as type ids, and the standard does not guarantee that there is exactly one
  344. //! such object per class. The only such case known to the author is when using
  345. //! Windows DLLs.
  346. //!
  347. //! Virtual and multiple inheritance are supported, with the exclusion of
  348. //! repeated inheritance.
  349. template<class... Classes>
  350. class use_classes {
  351. detail::use_classes_tuple_type<Classes...> tuple;
  352. };
  353. // =============================================================================
  354. // virtual_ptr
  355. namespace detail {
  356. void boost_openmethod_vptr(...);
  357. template<typename, class, typename = void>
  358. struct is_smart_ptr_aux : std::false_type {};
  359. template<typename Class, class Registry>
  360. struct is_smart_ptr_aux<
  361. Class, Registry,
  362. std::void_t<
  363. typename virtual_traits<Class, Registry>::template rebind<Class>>>
  364. : std::true_type {};
  365. template<class Class, class Other, class Registry, typename = void>
  366. struct same_smart_ptr_aux : std::false_type {};
  367. template<class Class, class Other, class Registry>
  368. struct same_smart_ptr_aux<
  369. Class, Other, Registry,
  370. std::void_t<typename virtual_traits<Class, Registry>::template rebind<
  371. typename Other::element_type>>>
  372. : std::is_same<
  373. Other,
  374. typename virtual_traits<Class, Registry>::template rebind<
  375. typename Other::element_type>> {};
  376. } // namespace detail
  377. BOOST_OPENMETHOD_OPEN_NAMESPACE_DETAIL_UNLESS_MRDOCS
  378. //! Test if argument is polymorphic (exposition only)
  379. //!
  380. //! Evaluates to `true` if `Class` is a polymorphic type, according to the
  381. //! `rtti` policy of `Registry`.
  382. //!
  383. //! If Registry's `rtti` policy is std_rtti`, this is the same as
  384. //! `std::is_polymorphic`. However, other `rtti` policies may have a different
  385. //! view of what is polymorphic.
  386. //!
  387. //! @tparam Class A class type.
  388. //! @tparam Registry A registry.
  389. template<class Class, class Registry>
  390. constexpr bool IsPolymorphic = Registry::rtti::template is_polymorphic<Class>;
  391. //! Test if argument is a smart pointer (exposition only)
  392. //!
  393. //! Evaluates to `true` if `Class` is a smart pointer type, and false otherwise.
  394. //! `Class` is considered a smart pointer if `virtual_traits<Class, Registry>`
  395. //! exists and it defines a nested template `rebind<T>` that can be instantiated
  396. //! with `Class`.
  397. //!
  398. //! @tparam Class A class type.
  399. //! @tparam Registry A registry.
  400. template<typename Class, class Registry>
  401. constexpr bool IsSmartPtr = detail::is_smart_ptr_aux<Class, Registry>::value;
  402. //! Test if arguments are same kind of smart pointers (exposition only)
  403. //!
  404. //! Evaluates to `true` if `Class` and `Other` are both smart pointers of the
  405. //! same type.
  406. //!
  407. //! @tparam Class A class type.
  408. //! @tparam Other Another class type.
  409. //! @tparam Registry A registry.
  410. template<class Class, class Other, class Registry>
  411. constexpr bool SameSmartPtr =
  412. detail::same_smart_ptr_aux<Class, Other, Registry>::value;
  413. BOOST_OPENMETHOD_CLOSE_NAMESPACE_DETAIL_UNLESS_MRDOCS
  414. template<class Registry, typename Arg>
  415. inline auto final_virtual_ptr(Arg&& obj);
  416. namespace detail {
  417. template<class Class, class Registry>
  418. struct is_virtual<virtual_ptr<Class, Registry, void>> : std::true_type {};
  419. template<class Class, class Registry>
  420. struct is_virtual<virtual_ptr<Class, Registry, void>&> : std::true_type {};
  421. template<class Class, class Registry>
  422. struct is_virtual<const virtual_ptr<Class, Registry, void>&> : std::true_type {
  423. };
  424. template<typename>
  425. struct is_virtual_ptr_aux : std::false_type {};
  426. template<class Class, class Registry>
  427. struct is_virtual_ptr_aux<virtual_ptr<Class, Registry, void>> : std::true_type {
  428. };
  429. template<class Class, class Registry>
  430. struct is_virtual_ptr_aux<const virtual_ptr<Class, Registry, void>&>
  431. : std::true_type {};
  432. template<typename T>
  433. constexpr bool is_virtual_ptr = detail::is_virtual_ptr_aux<T>::value;
  434. template<class Class, class Registry>
  435. constexpr bool has_vptr_fn = std::is_same_v<
  436. decltype(boost_openmethod_vptr(
  437. std::declval<const Class&>(), std::declval<Registry*>())),
  438. vptr_type>;
  439. template<class Registry, class ArgType>
  440. decltype(auto) acquire_vptr(const ArgType& arg) {
  441. Registry::require_initialized();
  442. if constexpr (detail::has_vptr_fn<ArgType, Registry>) {
  443. return boost_openmethod_vptr(arg, static_cast<Registry*>(nullptr));
  444. } else {
  445. return Registry::template policy<policies::vptr>::dynamic_vptr(arg);
  446. }
  447. }
  448. template<bool Indirect>
  449. inline auto box_vptr(const vptr_type& vp) {
  450. if constexpr (Indirect) {
  451. return &vp;
  452. } else {
  453. return vp;
  454. }
  455. }
  456. inline auto unbox_vptr(vptr_type vp) {
  457. return vp;
  458. }
  459. inline auto unbox_vptr(const vptr_type* vpp) {
  460. return *vpp;
  461. }
  462. inline vptr_type null_vptr = nullptr;
  463. } // namespace detail
  464. //! Creates a `virtual_ptr` for an object of a known dynamic type.
  465. //!
  466. //! Creates a @ref virtual_ptr to an object, setting its v-table pointer
  467. //! according to the declared type of its argument. Assumes that the static and
  468. //! dynamic types are the same. Sets the v-table pointer to the
  469. //! @ref registry::static_vptr for the class.
  470. //!
  471. //! `Class` is _not_ required to be polymorphic.
  472. //!
  473. //! If runtime checks are enabled, and the argument is polymorphic, checks if
  474. //! the static and dynamic types are the same. If not, calls the error handler
  475. //! with a @ref final_error value, then terminates the program with @ref abort.
  476. //!
  477. //! @par Errors
  478. //!
  479. //! @li @ref final_error The static and dynamic types of the object are
  480. //! different.
  481. //!
  482. //! @tparam Registry A @ref registry.
  483. //! @tparam Arg The type of the argument.
  484. //! @param obj A reference to an object.
  485. //! @return A `virtual_ptr<Class, Registry>` pointing to `obj`.
  486. template<class Registry, typename Arg>
  487. inline auto final_virtual_ptr(Arg&& obj) {
  488. using namespace detail;
  489. using VirtualPtr = virtual_ptr<std::remove_reference_t<Arg>, Registry>;
  490. using Traits = virtual_traits<Arg, Registry>;
  491. using Class = typename Traits::virtual_type;
  492. static_assert(!std::is_const_v<Class>);
  493. static_assert(!std::is_volatile_v<Class>);
  494. static_assert(!std::is_reference_v<Class>);
  495. static_assert(!std::is_pointer_v<Class>);
  496. Registry::require_initialized();
  497. if constexpr (
  498. Registry::has_runtime_checks &&
  499. Registry::rtti::template is_polymorphic<Class>) {
  500. // check that dynamic type == static type
  501. auto static_type = Registry::rtti::template static_type<Class>();
  502. auto dynamic_type = Registry::rtti::dynamic_type(Traits::peek(obj));
  503. if (dynamic_type != static_type) {
  504. if constexpr (is_not_void<typename Registry::error_handler>) {
  505. final_error error;
  506. error.static_type = static_type;
  507. error.dynamic_type = dynamic_type;
  508. Registry::error_handler::error(error);
  509. }
  510. abort();
  511. }
  512. }
  513. const vptr_type& vptr = Registry::template static_vptr<Class>;
  514. BOOST_ASSERT(vptr);
  515. return VirtualPtr(
  516. std::forward<Arg>(obj),
  517. detail::box_vptr<VirtualPtr::use_indirect_vptrs>(vptr));
  518. }
  519. //! Create a `virtual_ptr` for an object of a known dynamic type.
  520. //!
  521. //! This is an overload of `final_virtual_ptr` that uses the default
  522. //! registry as the `Registry` template parameter.
  523. //!
  524. //! @see @ref final_virtual_ptr
  525. // We could give a default value to Registry in the main template, but gcc
  526. // doesn't like it.
  527. template<class Arg>
  528. inline auto final_virtual_ptr(Arg&& obj) {
  529. return final_virtual_ptr<BOOST_OPENMETHOD_DEFAULT_REGISTRY, Arg>(
  530. std::forward<Arg>(obj));
  531. }
  532. //! Wide pointer combining pointers to an object and its v-table
  533. //!
  534. //! A `virtual_ptr` is a wide pointer that combines pointers to an object and
  535. //! its v-table. Calls to methods via `virtual_ptr` are as fast as ordinary
  536. //! virtual function calls (typically two instructions).
  537. //!
  538. //! A `virtual_ptr` can be implicitly constructed from a reference, a pointer,
  539. //! or another `virtual_ptr`, provided that they are type-compatible.
  540. //!
  541. //! `virtual_ptr` has specializations that use a `std::shared_ptr` or a
  542. //! `std::unique_ptr` as the pointer to the object. The mechanism can be
  543. //! extended to other smart pointers by specializing @ref virtual_traits. A
  544. //! "plain" `virtual_ptr` can be constructed from a smart `virtual_ptr`, but not
  545. //! the other way around.
  546. //!
  547. //! The default value for `Registry` can be customized by defining the
  548. //! {{BOOST_OPENMETHOD_DEFAULT_REGISTRY}}
  549. //! preprocessor symbol.
  550. //!
  551. //! @par Requirements
  552. //!
  553. //! @li @ref virtual_traits must be specialized for `Class&`.
  554. //! @li `Class` must be a class type, possibly cv-qualified, registered in
  555. //! `Registry`.
  556. //!
  557. //! @tparam Class The class of the object, possibly cv-qualified
  558. //! @tparam Registry The registry in which `Class` is registered
  559. //! @tparam unnamed Implementation defined, use default
  560. template<class Class, class Registry, typename>
  561. class virtual_ptr {
  562. using traits = virtual_traits<Class&, Registry>;
  563. #ifndef __MRDOCS__
  564. template<class, class, typename>
  565. friend class virtual_ptr;
  566. template<class, typename Arg>
  567. friend auto final_virtual_ptr(Arg&& obj);
  568. #endif
  569. static constexpr bool is_smart_ptr = false;
  570. static constexpr bool use_indirect_vptrs = Registry::has_indirect_vptr;
  571. std::conditional_t<use_indirect_vptrs, const vptr_type*, vptr_type> vp;
  572. Class* obj;
  573. template<
  574. class Other,
  575. typename = std::enable_if_t<std::is_constructible_v<Class*, Other*>>>
  576. virtual_ptr(Other& other, decltype(vp) vp) : vp(vp), obj(&other) {
  577. }
  578. public:
  579. //! Class
  580. //!
  581. //! This is the same as `Class`.
  582. using element_type = Class;
  583. //! Default constructor
  584. //!
  585. //! @note This constructor does nothing. The state of the two pointers
  586. //! inside the object is as specified for uninitialized variables by C++.
  587. virtual_ptr() = default;
  588. //! Construct from `nullptr`
  589. //!
  590. //! Set both object and v-table pointers to `nullptr`.
  591. //!
  592. //! @param value A `nullptr`.
  593. //!
  594. //! @par Example
  595. //!
  596. //! @code
  597. //! struct Animal { virtual ~Animal() { } }; // polymorphic
  598. //! struct Dog : Animal {}; // polymorphic
  599. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  600. //! initialize();
  601. //!
  602. //! virtual_ptr<Dog> p{nullptr};
  603. //! BOOST_TEST(p.get() == nullptr);
  604. //! BOOST_TEST(p.vptr() == nullptr);
  605. //! @endcode
  606. //!
  607. //! @param value A `nullptr`.
  608. explicit virtual_ptr(std::nullptr_t)
  609. : vp(detail::box_vptr<use_indirect_vptrs>(detail::null_vptr)),
  610. obj(nullptr) {
  611. }
  612. //! Construct a `virtual_ptr` from a reference to an object
  613. //!
  614. //! The pointer to the v-table is obtained by calling
  615. //! @ref boost_openmethod_vptr if a suitable overload exists, or the
  616. //! @ref policies::vptr::fn::dynamic_vptr of the registry's
  617. //! `vptr` policy otherwise.
  618. //!
  619. //! @param other A reference to a polymorphic object
  620. //!
  621. //! @par Example
  622. //! @code
  623. //! struct Animal { virtual ~Animal() { } }; // polymorphic
  624. //! struct Dog : Animal {}; // polymorphic
  625. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  626. //! initialize();
  627. //!
  628. //! Dog snoopy;
  629. //! Animal& animal = snoopy;
  630. //!
  631. //! virtual_ptr<Animal> p = animal;
  632. //!
  633. //! BOOST_TEST(p.get() == &snoopy);
  634. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  635. //! @endcode
  636. //!
  637. //! @par Requirements
  638. //! @li `Other` must be a polymorphic class, according to `Registry`'s
  639. //! `rtti` policy.
  640. //! @li `Other\*` must be constructible from `Class\*`.
  641. //!
  642. //! @par Errors
  643. //!
  644. //! The following errors may occur, depending on the policies selected in
  645. //! `Registry`:
  646. //!
  647. //! @li @ref missing_class
  648. template<
  649. class Other,
  650. typename = std::enable_if_t<
  651. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  652. IsPolymorphic<Other, Registry> &&
  653. std::is_constructible_v<Class*, Other*>>>
  654. virtual_ptr(Other& other)
  655. : vp(detail::box_vptr<use_indirect_vptrs>(
  656. detail::acquire_vptr<Registry>(other))),
  657. obj(&other) {
  658. }
  659. //! Construct a `virtual_ptr` from a pointer to an object
  660. //!
  661. //! The pointer to the v-table is obtained by calling
  662. //! @ref boost_openmethod_vptr if a suitable overload exists, or the
  663. //! @ref policies::vptr::fn::dynamic_vptr of the registry's
  664. //! `vptr` policy otherwise.
  665. //!
  666. //! @par Example
  667. //! @code
  668. //! struct Animal { virtual ~Animal() { } }; // polymorphic
  669. //! struct Dog : Animal {}; // polymorphic
  670. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  671. //! initialize();
  672. //!
  673. //! Dog snoopy;
  674. //! Animal* animal = &snoopy;
  675. //!
  676. //! virtual_ptr<Animal> p = animal;
  677. //!
  678. //! BOOST_TEST(p.get() == &snoopy);
  679. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  680. //! @endcode
  681. //!
  682. //! @param other A pointer to a polymorphic object
  683. //!
  684. //! @par Requirements
  685. //!
  686. //! @li `Other` must be a polymorphic class, according to `Registry`'s
  687. //! `rtti` policy.
  688. //!
  689. //! @li `Other\*` must be constructible from `Class\*`.
  690. //!
  691. //! @par Errors
  692. //!
  693. //! The following errors may occur, depending on the policies selected in
  694. //! `Registry`:
  695. //!
  696. //! @li @ref missing_class
  697. template<
  698. class Other,
  699. typename = std::enable_if_t<
  700. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  701. IsPolymorphic<Class, Registry> &&
  702. std::is_constructible_v<Class*, Other*>>>
  703. virtual_ptr(Other* other)
  704. : vp(detail::box_vptr<use_indirect_vptrs>(
  705. detail::acquire_vptr<Registry>(*other))),
  706. obj(other) {
  707. }
  708. //! Construct a `virtual_ptr` from another `virtual_ptr`
  709. //!
  710. //! Copy the object and v-table pointers from `other` to `this.
  711. //!
  712. //! `Other` is _not_ required to be a pointer to a polymorphic class.
  713. //!
  714. //! @par Examples
  715. //!
  716. //! Assigning from a plain virtual_ptr:
  717. //!
  718. //! @code
  719. //! struct Animal {}; // polymorphism not required
  720. //! struct Dog : Animal {}; // polymorphism not required
  721. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  722. //! initialize();
  723. //!
  724. //! Dog snoopy;
  725. //! virtual_ptr<Dog> dog = final_virtual_ptr(snoopy);
  726. //! virtual_ptr<Animal> p{nullptr};
  727. //!
  728. //! p = dog;
  729. //!
  730. //! BOOST_TEST(p.get() == &snoopy);
  731. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  732. //! @endcode
  733. //!
  734. //! Assigning from a smart virtual_ptr:
  735. //!
  736. //! @code
  737. //! struct Animal {}; // polymorphism not required
  738. //! struct Dog : Animal {}; // polymorphism not required
  739. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  740. //! initialize();
  741. //!
  742. //! virtual_ptr<std::shared_ptr<Animal>> snoopy = make_shared_virtual<Dog>();
  743. //! virtual_ptr<Animal> p = snoopy;
  744. //!
  745. //! BOOST_TEST(p.get() == snoopy.get());
  746. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  747. //! @endcode
  748. //!
  749. //! No construction of a smart `virtual_ptr` from a plain `virtual_ptr`:
  750. //!
  751. //! @code
  752. //! static_assert(
  753. //! std::is_constructible_v<
  754. //! shared_virtual_ptr<Animal>, virtual_ptr<Dog>> == false);
  755. //! @endcode
  756. //!
  757. //! @param other A virtual_ptr to a type-compatible object
  758. //!
  759. //! @par Requirements
  760. //! @li `Other`\'s object pointer must be assignable to a `Class\*`.
  761. template<
  762. class Other,
  763. typename = std::enable_if_t<std::is_constructible_v<
  764. Class*, typename virtual_ptr<Other, Registry>::element_type*>>>
  765. virtual_ptr(const virtual_ptr<Other, Registry>& other)
  766. : vp(other.vp), obj(other.get()) {
  767. }
  768. //! Assign a `virtual_ptr` from a reference to an object
  769. //!
  770. //! The pointer to the v-table is obtained by calling
  771. //! @ref boost_openmethod_vptr if a suitable overload exists, or the
  772. //! @ref policies::vptr::fn::dynamic_vptr of the registry's
  773. //! `vptr` policy otherwise.
  774. //!
  775. //! @par Example
  776. //! @code
  777. //! struct Animal { virtual ~Animal() { } }; // polymorphic
  778. //! struct Dog : Animal {}; // polymorphic
  779. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  780. //! initialize();
  781. //!
  782. //! virtual_ptr<Animal> p{nullptr};
  783. //! Dog snoopy;
  784. //! Animal& animal = snoopy;
  785. //!
  786. //! p = animal;
  787. //!
  788. //! BOOST_TEST(p.get() == &snoopy);
  789. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  790. //! @endcode
  791. //!
  792. //! @param other A reference to a polymorphic object
  793. //!
  794. //! @par Requirements
  795. //!
  796. //! @li `Other` must be a polymorphic class, according to `Registry`'s
  797. //! `rtti` policy.
  798. //!
  799. //! @li `Other\*` must be constructible from `Class\*`.
  800. //!
  801. //! @par Errors
  802. //!
  803. //! The following errors may occur, depending on the policies selected in
  804. //! `Registry`:
  805. //!
  806. //! @li @ref missing_class
  807. template<
  808. class Other,
  809. typename = std::enable_if_t<
  810. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  811. IsPolymorphic<Class, Registry> &&
  812. std::is_assignable_v<Class*&, Other*>>>
  813. virtual_ptr& operator=(Other& other) {
  814. obj = &other;
  815. vp = detail::box_vptr<use_indirect_vptrs>(
  816. detail::acquire_vptr<Registry>(other));
  817. return *this;
  818. }
  819. //! Assign a `virtual_ptr` from a pointer to an object
  820. //!
  821. //! The pointer to the v-table is obtained by calling
  822. //! @ref boost_openmethod_vptr if a suitable overload exists, or the
  823. //! @ref policies::vptr::fn::dynamic_vptr of the registry's
  824. //! `vptr` policy otherwise.
  825. //!
  826. //! @par Example
  827. //! @code
  828. //! struct Animal { virtual ~Animal() { } }; // polymorphic
  829. //! struct Dog : Animal {}; // polymorphic
  830. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  831. //! initialize();
  832. //!
  833. //! virtual_ptr<Animal> p{nullptr};
  834. //! Dog snoopy;
  835. //! Animal* animal = &snoopy;
  836. //!
  837. //! p = animal;
  838. //!
  839. //! BOOST_TEST(p.get() == &snoopy);
  840. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  841. //! @endcode
  842. //!
  843. //! @param other A pointer to a polymorphic object
  844. //!
  845. //! @par Requirements
  846. //! @li `Other` must be a polymorphic class, according to `Registry`'s
  847. //! `rtti` policy.
  848. //! @li `Other\*` must be constructible from `Class\*`.
  849. //!
  850. //! @par Errors
  851. //!
  852. //! The following errors may occur, depending on the policies selected in
  853. //! `Registry`:
  854. //!
  855. //! @li @ref missing_class
  856. template<
  857. class Other,
  858. typename = std::enable_if_t<
  859. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  860. IsPolymorphic<Class, Registry> &&
  861. std::is_assignable_v<Class*&, Other*>>>
  862. virtual_ptr& operator=(Other* other) {
  863. obj = other;
  864. vp = detail::box_vptr<use_indirect_vptrs>(
  865. detail::acquire_vptr<Registry>(*other));
  866. return *this;
  867. }
  868. //! Assign a `virtual_ptr` from another `virtual_ptr`
  869. //!
  870. //! Copy the object and v-table pointers from `other` to `this.
  871. //!
  872. //! `Other` is _not_ required to be a pointer to a polymorphic class.
  873. //!
  874. //! @par Examples
  875. //!
  876. //! Assigning from a plain virtual_ptr:
  877. //!
  878. //! @code
  879. //! struct Animal {}; // polymorphism not required
  880. //! struct Dog : Animal {}; // polymorphism not required
  881. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  882. //! initialize();
  883. //!
  884. //! Dog snoopy;
  885. //! virtual_ptr<Dog> dog = final_virtual_ptr(snoopy);
  886. //! virtual_ptr<Animal> p{nullptr};
  887. //!
  888. //! p = dog;
  889. //!
  890. //! BOOST_TEST(p.get() == &snoopy);
  891. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  892. //! @endcode
  893. //!
  894. //! Assigning from a smart virtual_ptr:
  895. //!
  896. //! @code
  897. //! struct Animal {}; // polymorphism not required
  898. //! struct Dog : Animal {}; // polymorphism not required
  899. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  900. //! initialize();
  901. //!
  902. //! virtual_ptr<std::shared_ptr<Animal>> snoopy = make_shared_virtual<Dog>();
  903. //! virtual_ptr<Animal> p;
  904. //!
  905. //! p = snoopy;
  906. //!
  907. //! BOOST_TEST(p.get() == snoopy.get());
  908. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  909. //! @endcode
  910. //!
  911. //! No assignment from a plain `virtual_ptr` to a smart `virtual_ptr`:
  912. //!
  913. //! @code
  914. //! static_assert(
  915. //! std::is_assignable_v<
  916. //! shared_virtual_ptr<Animal>&, virtual_ptr<Dog>> == false);
  917. //! @endcode
  918. //!
  919. //! @param other A virtual_ptr to a type-compatible object
  920. //!
  921. //! @par Requirements
  922. //! @li `Other`\'s object pointer must be assignable to a `Class\*`.
  923. template<
  924. class Other,
  925. typename = std::enable_if_t<std::is_assignable_v<
  926. Class*&, typename virtual_ptr<Other, Registry>::element_type*>>>
  927. virtual_ptr& operator=(const virtual_ptr<Other, Registry>& other) {
  928. obj = other.get();
  929. vp = other.vp;
  930. return *this;
  931. }
  932. //! Set a `virtual_ptr` to `nullptr`
  933. //!
  934. //! Set both object and v-table pointers to `nullptr`.
  935. //!
  936. //! @par Example
  937. //! struct Animal {}; // polymorphism not required
  938. //! struct Dog : Animal {}; // polymorphism not required
  939. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  940. //! initialize();
  941. //!
  942. //! Dog snoopy;
  943. //! virtual_ptr<Animal> p = final_virtual_ptr(snoopy);
  944. //!
  945. //! p = nullptr;
  946. //!
  947. //! BOOST_TEST(p.get() == nullptr);
  948. //! BOOST_TEST(p.vptr() == nullptr);
  949. //! //! @code
  950. //! @endcode
  951. virtual_ptr& operator=(std::nullptr_t) {
  952. obj = nullptr;
  953. vp = detail::box_vptr<use_indirect_vptrs>(detail::null_vptr);
  954. return *this;
  955. }
  956. //! Get a pointer to the object
  957. //!
  958. //! @return A pointer to the object
  959. auto get() const -> Class* {
  960. return obj;
  961. }
  962. //! Get a pointer to the object
  963. //!
  964. //! @return A pointer to the object
  965. auto operator->() const {
  966. return get();
  967. }
  968. //! Get a reference to the object
  969. //!
  970. //! @return A reference to the object
  971. auto operator*() const -> element_type& {
  972. return *get();
  973. }
  974. //! Get a pointer to the object
  975. //!
  976. //! @return A pointer to the object
  977. auto pointer() const -> element_type* {
  978. return obj;
  979. }
  980. //! Cast to another `virtual_ptr` type
  981. //!
  982. //! @par Example
  983. //! @code
  984. //! @endcode
  985. //!
  986. //! @tparam Other The target class of the cast
  987. //! @return A `virtual_ptr<Other, Registry>` pointing to the same object
  988. //! @par Requirements
  989. //! @li `Other` must be a base or derived class of `Class`.
  990. template<
  991. class Other,
  992. typename = std::enable_if_t<
  993. std::is_base_of_v<element_type, Other> ||
  994. std::is_base_of_v<Other, element_type>>>
  995. auto cast() const -> decltype(auto) {
  996. return virtual_ptr<Other, Registry>(
  997. traits::template cast<Other&>(*obj), vp);
  998. }
  999. //! Construct a `virtual_ptr` from a reference to an object
  1000. //!
  1001. //! This function forwards to @ref final_virtual_ptr.
  1002. //!
  1003. //! @tparam Other The type of the argument
  1004. //! @param obj A reference to an object
  1005. //! @return A `virtual_ptr<Class, Registry>` pointing to `obj`
  1006. template<class Other>
  1007. static auto final(Other&& obj) {
  1008. return final_virtual_ptr<Registry>(std::forward<Other>(obj));
  1009. }
  1010. //! Get the v-table pointer
  1011. //! @return The v-table pointer
  1012. auto vptr() const {
  1013. return detail::unbox_vptr(this->vp);
  1014. }
  1015. };
  1016. //! Wide pointer combining a smart pointer to an object and a pointer to its
  1017. //! v-table
  1018. //!
  1019. //! This specialization of `virtual_ptr` uses a smart pointer to track the
  1020. //! object, instead of a plain pointer.
  1021. //!
  1022. //! @tparam SmartPtr A smart pointer type
  1023. //! @tparam Registry The registry in which the underlying class is registered
  1024. template<class SmartPtr, class Registry>
  1025. class virtual_ptr<
  1026. SmartPtr, Registry,
  1027. std::enable_if_t<
  1028. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS IsSmartPtr<SmartPtr, Registry>>> {
  1029. #ifndef __MRDOCS__
  1030. template<class, class, typename>
  1031. friend class virtual_ptr;
  1032. template<class, typename Arg>
  1033. friend auto final_virtual_ptr(Arg&& obj);
  1034. #endif
  1035. static constexpr bool is_smart_ptr = true;
  1036. static constexpr bool use_indirect_vptrs = Registry::has_indirect_vptr;
  1037. using traits = virtual_traits<SmartPtr, Registry>;
  1038. std::conditional_t<use_indirect_vptrs, const vptr_type*, vptr_type> vp;
  1039. SmartPtr obj;
  1040. template<
  1041. class Other,
  1042. typename = std::enable_if_t<std::is_constructible_v<SmartPtr*, Other*>>>
  1043. virtual_ptr(Other& other, decltype(vp) vp) : vp(vp), obj(&other) {
  1044. }
  1045. template<typename Arg>
  1046. virtual_ptr(Arg&& obj, decltype(vp) vp)
  1047. : vp(vp), obj(std::forward<Arg>(obj)) {
  1048. }
  1049. public:
  1050. //! Class pointed to by SmartPtr
  1051. using element_type = typename SmartPtr::element_type;
  1052. //! Default constructor
  1053. //!
  1054. //! Construct the object pointer using its default constructor. Set the
  1055. //! v-table pointer to `nullptr`.
  1056. //!
  1057. //! @par Example
  1058. //! @code
  1059. //! struct Dog {}; // polymorphism not required
  1060. //! BOOST_OPENMETHOD_CLASSES(Dog);
  1061. //! initialize();
  1062. //!
  1063. //! virtual_ptr<std::shared_ptr<Dog>> p;
  1064. //! BOOST_TEST(p.get() == nullptr);
  1065. //! BOOST_TEST(p.vptr() == nullptr);
  1066. //! @par Example
  1067. //! @endcode
  1068. virtual_ptr()
  1069. : vp(detail::box_vptr<use_indirect_vptrs>(detail::null_vptr)) {
  1070. }
  1071. //! Construct from `nullptr`
  1072. //!
  1073. //! Construct the object pointer using its default constructor. Set the
  1074. //! v-table pointer to `nullptr`.
  1075. //!
  1076. //! @par Example
  1077. //! @code
  1078. //! struct Dog {}; // polymorphism not required
  1079. //! BOOST_OPENMETHOD_CLASSES(Dog);
  1080. //! initialize();
  1081. //!
  1082. //! virtual_ptr<std::shared_ptr<Dog>> p{nullptr};
  1083. //! BOOST_TEST(p.get() == nullptr);
  1084. //! BOOST_TEST(p.vptr() == nullptr);
  1085. //! @endcode
  1086. //!
  1087. //! @param value A `nullptr`.
  1088. explicit virtual_ptr(std::nullptr_t)
  1089. : vp(detail::box_vptr<use_indirect_vptrs>(detail::null_vptr)) {
  1090. }
  1091. virtual_ptr(const virtual_ptr& other) = default;
  1092. virtual_ptr(virtual_ptr&& other)
  1093. : vp(std::exchange(
  1094. other.vp,
  1095. detail::box_vptr<use_indirect_vptrs>(detail::null_vptr))),
  1096. obj(std::move(other.obj)) {
  1097. }
  1098. #ifdef __MRDOCS__
  1099. //! Construct from a (const) smart pointer to a derived class
  1100. //!
  1101. //! Set the object pointer with a copy of `other`. Set the v-table pointer
  1102. //! according to the dynamic type of `*other`.
  1103. //!
  1104. //! @par Example
  1105. //! @code
  1106. //! struct Animal { virtual ~Animal() { } }; // polymorphic
  1107. //! struct Dog : Animal {}; // polymorphic
  1108. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  1109. //! initialize();
  1110. //!
  1111. //! const std::shared_ptr<Dog> snoopy = std::make_shared<Dog>();
  1112. //! virtual_ptr<std::shared_ptr<Animal>> p = snoopy;
  1113. //!
  1114. //! BOOST_TEST(p.get() == snoopy.get());
  1115. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  1116. //! @endcode
  1117. //!
  1118. //! @par Requirements
  1119. //! @li `SmartPtr` and `Other` must be instantiated from the same template -
  1120. //! e.g. both `std::shared_ptr` or both `std::unique_ptr`.
  1121. //! @li `Other` must be a smart pointer to a polymorphic class derived from
  1122. //! `element_type`.
  1123. //! @li `SmartPtr` must be constructible from `const Other&`.
  1124. template<
  1125. class Other,
  1126. typename = std::enable_if_t<
  1127. SameSmartPtr<SmartPtr, Other, Registry> &&
  1128. IsPolymorphic<typename Other::element_type, Registry> &&
  1129. std::is_constructible_v<SmartPtr, const Other&>>>
  1130. #else
  1131. template<
  1132. class Other,
  1133. typename = std::enable_if_t<
  1134. detail::SameSmartPtr<SmartPtr, Other, Registry> &&
  1135. detail::IsPolymorphic<typename Other::element_type, Registry> &&
  1136. std::is_constructible_v<SmartPtr, const Other&>>>
  1137. #endif
  1138. virtual_ptr(const Other& other)
  1139. : vp(detail::box_vptr<use_indirect_vptrs>(
  1140. other ? detail::acquire_vptr<Registry>(*other)
  1141. : detail::null_vptr)),
  1142. obj(other) {
  1143. }
  1144. #if __MRDOCS__
  1145. //! Construct from a smart pointer to a derived class
  1146. //!
  1147. //! Copy object pointer from `other` to `this`. Set the v-table pointer
  1148. //! according to the dynamic type of `*other`.
  1149. //!
  1150. //! @par Example
  1151. //! @code
  1152. //! struct Animal { virtual ~Animal() { } }; // polymorphic
  1153. //! struct Dog : Animal {}; // polymorphic
  1154. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  1155. //! initialize();
  1156. //!
  1157. //! std::shared_ptr<Dog> snoopy = std::make_shared<Dog>();
  1158. //! virtual_ptr<std::shared_ptr<Animal>> p = snoopy;
  1159. //!
  1160. //! BOOST_TEST(p.get() == snoopy.get());
  1161. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  1162. //! @endcode
  1163. //!
  1164. //! @par Requirements
  1165. //! @li `SmartPtr` and `Other` must be instantiated from the same template -
  1166. //! e.g. both `std::shared_ptr` or both `std::unique_ptr`.
  1167. //! @li `Other` must be a smart pointer to a polymorphic class derived from
  1168. //! `element_type`.
  1169. //! @li `SmartPtr` must be constructible from `Other&`.
  1170. template<
  1171. class Other,
  1172. typename = std::enable_if_t<
  1173. SameSmartPtr<SmartPtr, Other, Registry> &&
  1174. IsPolymorphic<typename Other::element_type, Registry> &&
  1175. std::is_constructible_v<SmartPtr, Other&>>>
  1176. #else
  1177. template<
  1178. class Other,
  1179. typename = std::enable_if_t<
  1180. detail::SameSmartPtr<SmartPtr, Other, Registry> &&
  1181. detail::IsPolymorphic<typename Other::element_type, Registry> &&
  1182. std::is_constructible_v<SmartPtr, Other&>>>
  1183. #endif
  1184. virtual_ptr(Other& other)
  1185. : vp(detail::box_vptr<use_indirect_vptrs>(
  1186. other ? detail::acquire_vptr<Registry>(*other)
  1187. : detail::null_vptr)),
  1188. obj(other) {
  1189. }
  1190. #ifdef __MRDOCS__
  1191. //! Move-construct from a smart pointer to a derived class
  1192. //!
  1193. //! Move object pointer from `other` to `this`. Set the v-table pointer
  1194. //! according to the dynamic type of `*other`.
  1195. //!
  1196. //! @par Example
  1197. //! @code
  1198. //! struct Animal { virtual ~Animal() { } }; // polymorphic
  1199. //! struct Dog : Animal {}; // polymorphic
  1200. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  1201. //! initialize();
  1202. //!
  1203. //! std::shared_ptr<Dog> snoopy = std::make_shared<Dog>();
  1204. //! Dog* moving = snoopy.get();
  1205. //!
  1206. //! virtual_ptr<std::shared_ptr<Animal>> p = std::move(snoopy);
  1207. //!
  1208. //! BOOST_TEST(p.get() == moving);
  1209. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  1210. //! BOOST_TEST(snoopy.get() == nullptr);
  1211. //! @endcode
  1212. //!
  1213. //! @par Requirements
  1214. //! @li `SmartPtr` and `Other` must be instantiated from the same template -
  1215. //! e.g. both `std::shared_ptr` or both `std::unique_ptr`.
  1216. //! @li `Other` must be a smart pointer to a polymorphic class derived from
  1217. //! `element_type`.
  1218. //! @li `SmartPtr` must be constructible from `Other&&`.
  1219. template<
  1220. class Other,
  1221. typename = std::enable_if_t<
  1222. SameSmartPtr<SmartPtr, Other, Registry> &&
  1223. IsPolymorphic<typename Other::element_type, Registry> &&
  1224. std::is_constructible_v<SmartPtr, Other&&>>>
  1225. #else
  1226. template<
  1227. class Other,
  1228. typename = std::enable_if_t<
  1229. detail::SameSmartPtr<SmartPtr, Other, Registry> &&
  1230. detail::IsPolymorphic<typename Other::element_type, Registry> &&
  1231. std::is_constructible_v<SmartPtr, Other&&>>>
  1232. #endif
  1233. virtual_ptr(Other&& other)
  1234. : vp(detail::box_vptr<use_indirect_vptrs>(
  1235. other ? detail::acquire_vptr<Registry>(*other)
  1236. : detail::null_vptr)),
  1237. obj(std::move(other)) {
  1238. }
  1239. //! Construct from a smart virtual (const) pointer to a derived class
  1240. //!
  1241. //! Copy the object and v-table pointers from `other`.
  1242. //!
  1243. //! `Other` is _not_ required to be a pointer to a polymorphic class.
  1244. //!
  1245. //! @par Example
  1246. //! @code
  1247. //! struct Animal {}; // polymorphism not required
  1248. //! struct Dog : Animal {}; // polymorphism not required
  1249. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  1250. //! initialize();
  1251. //!
  1252. //! const virtual_ptr<std::shared_ptr<Dog>> snoopy = make_shared_virtual<Dog>();
  1253. //! virtual_ptr<std::shared_ptr<Animal>> p = snoopy;
  1254. //!
  1255. //! BOOST_TEST(snoopy.get() != nullptr);
  1256. //! BOOST_TEST(p.get() == snoopy.get());
  1257. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  1258. //! @endcode
  1259. //!
  1260. //! @par Requirements
  1261. //! @li `SmartPtr` and `Other` must be instantiated from the same template -
  1262. //! e.g. both `std::shared_ptr` or both `std::unique_ptr`.
  1263. //! @li `Other` must be a virtual pointer to a class derived from
  1264. //! `element_type`.
  1265. //! @li `SmartPtr` must be constructible from `Other&`.
  1266. template<
  1267. class Other,
  1268. typename = std::enable_if_t<
  1269. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  1270. SameSmartPtr<SmartPtr, Other, Registry> &&
  1271. std::is_constructible_v<SmartPtr, const Other&>>>
  1272. virtual_ptr(const virtual_ptr<Other, Registry>& other)
  1273. : vp(other.vp), obj(other.obj) {
  1274. }
  1275. //! Construct-move from a virtual pointer to a derived class
  1276. //!
  1277. //! Move the object pointer from `other` to `this`. Copy the v-table pointer
  1278. //! from `other`.
  1279. //!
  1280. //! `Other` is _not_ required to be a pointer to a polymorphic class.
  1281. //!
  1282. //! @par Example
  1283. //! @code
  1284. //! struct Animal {}; // polymorphism not required
  1285. //! struct Dog : Animal {}; // polymorphism not required
  1286. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  1287. //! initialize();
  1288. //!
  1289. //! virtual_ptr<std::shared_ptr<Dog>> snoopy = make_shared_virtual<Dog>();
  1290. //! Dog* dog = snoopy.get();
  1291. //!
  1292. //! virtual_ptr<std::shared_ptr<Animal>> p = std::move(snoopy);
  1293. //!
  1294. //! BOOST_TEST(p.get() == dog);
  1295. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  1296. //! BOOST_TEST(snoopy.get() == nullptr);
  1297. //! @endcode
  1298. //!
  1299. //! @par Requirements
  1300. //! @li `SmartPtr` and `Other` must be instantiated from the same template -
  1301. //! e.g. both `std::shared_ptr` or both `std::unique_ptr`.
  1302. //! @li `Other` must be a smart pointer to a class derived from
  1303. //! `element_type`.
  1304. //! @li `SmartPtr` must be constructible from `Other&&`.
  1305. template<
  1306. class Other,
  1307. typename = std::enable_if_t<
  1308. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  1309. SameSmartPtr<SmartPtr, Other, Registry> &&
  1310. std::is_constructible_v<SmartPtr, Other&&>>>
  1311. virtual_ptr(virtual_ptr<Other, Registry>&& other)
  1312. : vp(std::exchange(
  1313. other.vp,
  1314. detail::box_vptr<use_indirect_vptrs>(detail::null_vptr))),
  1315. obj(std::move(other.obj)) {
  1316. }
  1317. //! Assign from `nullptr`
  1318. //!
  1319. //! Reset the object pointer using its default constructor. Set the
  1320. //! v-table pointer to `nullptr`.
  1321. //!
  1322. //! @par Example
  1323. //! @code
  1324. //! struct Dog {}; // polymorphism not required
  1325. //! BOOST_OPENMETHOD_CLASSES(Dog);
  1326. //! initialize();
  1327. //!
  1328. //! virtual_ptr<std::shared_ptr<Dog>> p = make_shared_virtual<Dog>();
  1329. //!
  1330. //! p = nullptr;
  1331. //!
  1332. //! BOOST_TEST(p.get() == nullptr);
  1333. //! BOOST_TEST(p.vptr() == nullptr);
  1334. //! BOOST_TEST((p == virtual_ptr<std::shared_ptr<Dog>>()));
  1335. //! @endcode
  1336. //!
  1337. //! @param value A `nullptr`.
  1338. virtual_ptr& operator=(std::nullptr_t) {
  1339. obj = SmartPtr();
  1340. vp = detail::box_vptr<use_indirect_vptrs>(detail::null_vptr);
  1341. return *this;
  1342. }
  1343. //! Assign from a (const) smart pointer to a derived class
  1344. //!
  1345. //! Copy the object pointer from `other` to `this`. Set the v-table pointer
  1346. //! according to the dynamic type of `*other`.
  1347. //!
  1348. //! @par Example
  1349. //! @code
  1350. //! virtual_ptr<std::shared_ptr<Dog>> snoopy = make_shared_virtual<Dog>();
  1351. //! virtual_ptr<std::shared_ptr<Dog>> p;
  1352. //!
  1353. //! p = snoopy;
  1354. //!
  1355. //! BOOST_TEST(p.get() != nullptr);
  1356. //! BOOST_TEST(p.get() == snoopy.get());
  1357. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  1358. //! BOOST_TEST(snoopy.vptr() == default_registry::static_vptr<Dog>);
  1359. //! @endcode
  1360. //!
  1361. //! @par Requirements
  1362. //! @li `SmartPtr` and `Other` must be instantiated from the same template -
  1363. //! e.g. both `std::shared_ptr` or both `std::unique_ptr`.
  1364. //! @li `Other` must be a smart pointer to a polymorphic class derived from
  1365. //! `element_type`.
  1366. //! @li `SmartPtr` must be constructible from `const Other&`.
  1367. template<
  1368. class Other,
  1369. typename = std::enable_if_t<
  1370. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  1371. SameSmartPtr<SmartPtr, Other, Registry> &&
  1372. std::is_assignable_v<SmartPtr, const Other&> &&
  1373. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  1374. IsPolymorphic<typename Other::element_type, Registry>>>
  1375. virtual_ptr& operator=(const Other& other) {
  1376. obj = other;
  1377. vp = detail::box_vptr<use_indirect_vptrs>(
  1378. detail::acquire_vptr<Registry>(*other));
  1379. return *this;
  1380. }
  1381. //! Move-assign from a smart pointer to a derived class
  1382. //!
  1383. //! Move object pointer from `other` to `this`. Set the v-table pointer
  1384. //! according to the dynamic type of `*other`.
  1385. //!
  1386. //! @par Example
  1387. //! @code
  1388. //! virtual_ptr<std::shared_ptr<Dog>> snoopy = make_shared_virtual<Dog>();
  1389. //! Dog* moving = snoopy.get();
  1390. //! virtual_ptr<std::shared_ptr<Dog>> p;
  1391. //!
  1392. //! p = std::move(snoopy);
  1393. //!
  1394. //! BOOST_TEST(p.get() == moving);
  1395. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  1396. //! BOOST_TEST(snoopy.get() == nullptr);
  1397. //! BOOST_TEST(snoopy.vptr() == nullptr);
  1398. //! @endcode
  1399. //!
  1400. //! @par Requirements
  1401. //! @li `SmartPtr` and `Other` must be instantiated from the same template -
  1402. //! e.g. both `std::shared_ptr` or both `std::unique_ptr`.
  1403. //! @li `Other` must be a smart pointer to a polymorphic class derived from
  1404. //! `element_type`.
  1405. //! @li `SmartPtr` must be constructible from `Other&&`.
  1406. template<
  1407. class Other,
  1408. typename = std::enable_if_t<
  1409. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  1410. SameSmartPtr<SmartPtr, Other, Registry> &&
  1411. std::is_assignable_v<SmartPtr, Other&&> &&
  1412. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  1413. IsPolymorphic<typename Other::element_type, Registry>>>
  1414. virtual_ptr& operator=(Other&& other) {
  1415. vp = detail::box_vptr<use_indirect_vptrs>(
  1416. other ? detail::acquire_vptr<Registry>(*other) : detail::null_vptr);
  1417. obj = std::move(other);
  1418. return *this;
  1419. }
  1420. //! Assign from a smart virtual pointer to a derived class
  1421. //!
  1422. //! Copy the object and v-table pointers from `other` to `this`.
  1423. //!
  1424. //! `Other` is _not_ required to be a pointer to a polymorphic class.
  1425. //!
  1426. //! @par Example
  1427. //! @code
  1428. //! struct Animal {}; // polymorphism not required
  1429. //! struct Dog : Animal {}; // polymorphism not required
  1430. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  1431. //! initialize();
  1432. //!
  1433. //! virtual_ptr<std::shared_ptr<Dog>> snoopy = make_shared_virtual<Dog>();
  1434. //! virtual_ptr<std::shared_ptr<Dog>> p;
  1435. //!
  1436. //! p = snoopy;
  1437. //!
  1438. //! BOOST_TEST(p.get() != nullptr);
  1439. //! BOOST_TEST(p.get() == snoopy.get());
  1440. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  1441. //! BOOST_TEST(snoopy.vptr() == default_registry::static_vptr<Dog>);
  1442. //! @endcode
  1443. //!
  1444. //! @par Requirements
  1445. //! @li `SmartPtr` and `Other` must be instantiated from the same template -
  1446. //! e.g. both `std::shared_ptr` or both `std::unique_ptr`.
  1447. //! @li `Other` must be a virtual pointer to a class derived from
  1448. //! `element_type`.
  1449. //! @li `SmartPtr` must be constructible from `Other&`.
  1450. template<
  1451. class Other,
  1452. typename = std::enable_if_t<
  1453. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  1454. SameSmartPtr<SmartPtr, Other, Registry> &&
  1455. std::is_assignable_v<SmartPtr, Other&>>>
  1456. virtual_ptr& operator=(virtual_ptr<Other, Registry>& other) {
  1457. obj = other.obj;
  1458. vp = other.vp;
  1459. return *this;
  1460. }
  1461. virtual_ptr& operator=(const virtual_ptr& other) = default;
  1462. //! Assign from a smart virtual const pointer to a derived class
  1463. //!
  1464. //! Copy the object and v-table pointers from `other` to `this`.
  1465. //!
  1466. //! `Other` is _not_ required to be a pointer to a polymorphic class.
  1467. //!
  1468. //! @par Example
  1469. //! @code
  1470. //! struct Animal {}; // polymorphism not required
  1471. //! struct Dog : Animal {}; // polymorphism not required
  1472. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  1473. //! initialize();
  1474. //!
  1475. //! const virtual_ptr<std::shared_ptr<Dog>> snoopy = make_shared_virtual<Dog>();
  1476. //! virtual_ptr<std::shared_ptr<Dog>> p;
  1477. //!
  1478. //! p = snoopy;
  1479. //!
  1480. //! BOOST_TEST(p.get() != nullptr);
  1481. //! BOOST_TEST(p.get() == snoopy.get());
  1482. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  1483. //! BOOST_TEST(snoopy.vptr() == default_registry::static_vptr<Dog>);
  1484. //! @endcode
  1485. //!
  1486. //! @par Requirements
  1487. //! @li `SmartPtr` and `Other` must be instantiated from the same template -
  1488. //! e.g. both `std::shared_ptr` or both `std::unique_ptr`.
  1489. //! @li `Other` must be a virtual pointer to a class derived from
  1490. //! `element_type`.
  1491. //! @li `SmartPtr` must be constructible from `Other&`.
  1492. template<
  1493. class Other,
  1494. typename = std::enable_if_t<
  1495. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  1496. SameSmartPtr<SmartPtr, Other, Registry> &&
  1497. std::is_assignable_v<SmartPtr, const Other&>>>
  1498. virtual_ptr& operator=(const virtual_ptr<Other, Registry>& other) {
  1499. obj = other.obj;
  1500. vp = other.vp;
  1501. return *this;
  1502. }
  1503. //! Move from a virtual pointer to a derived class
  1504. //!
  1505. //! Move the object pointer from `other` to `this`. Copy the v-table pointer
  1506. //! from `other`.
  1507. //!
  1508. //! `Other` is _not_ required to be a pointer to a polymorphic class.
  1509. //!
  1510. //! @par Example
  1511. //! @code
  1512. //! struct Animal {}; // polymorphism not required
  1513. //! struct Dog : Animal {}; // polymorphism not required
  1514. //! BOOST_OPENMETHOD_CLASSES(Animal, Dog);
  1515. //! initialize();
  1516. //!
  1517. //! virtual_ptr<std::shared_ptr<Dog>> snoopy =
  1518. //! make_shared_virtual<Dog>();
  1519. //! Dog* moving = snoopy.get();
  1520. //! virtual_ptr<std::shared_ptr<Dog>> p;
  1521. //!
  1522. //! p = std::move(snoopy);
  1523. //!
  1524. //! BOOST_TEST(p.get() == moving);
  1525. //! BOOST_TEST(p.vptr() == default_registry::static_vptr<Dog>);
  1526. //! BOOST_TEST(snoopy.get() == nullptr);
  1527. //! BOOST_TEST(snoopy.vptr() == nullptr);
  1528. //! @endcode
  1529. //!
  1530. //! @par Requirements
  1531. //! @li `SmartPtr` and `Other` must be instantiated from the same template -
  1532. //! e.g. both `std::shared_ptr` or both `std::unique_ptr`.
  1533. //! @li `Other` must be a smart pointer to a class derived from
  1534. //! `element_type`.
  1535. //! @li `SmartPtr` must be constructible from `Other&&`.
  1536. template<
  1537. class Other,
  1538. typename = std::enable_if_t<
  1539. BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  1540. SameSmartPtr<SmartPtr, Other, Registry> &&
  1541. std::is_assignable_v<SmartPtr, Other&&>>>
  1542. virtual_ptr& operator=(virtual_ptr<Other, Registry>&& other) {
  1543. vp = std::exchange(
  1544. other.vp, detail::box_vptr<use_indirect_vptrs>(detail::null_vptr));
  1545. obj = std::move(other.obj);
  1546. return *this;
  1547. }
  1548. //! Get a pointer to the object
  1549. //!
  1550. //! @return A *plain* pointer to the object
  1551. auto get() const -> element_type* {
  1552. return obj.get();
  1553. }
  1554. //! Get a pointer to the object
  1555. //!
  1556. //! @return A *plain* pointer to the object
  1557. auto operator->() const -> element_type* {
  1558. return get();
  1559. }
  1560. //! Get a reference to the object
  1561. //!
  1562. //! @return A reference to the object
  1563. auto operator*() const -> element_type& {
  1564. return *get();
  1565. }
  1566. //! Get a smart pointer to the object
  1567. //!
  1568. //! @return A const reference to the object pointer
  1569. auto pointer() const -> const SmartPtr& {
  1570. return obj;
  1571. }
  1572. //! Cast to another `virtual_ptr` type
  1573. //! @tparam Other The target class of the cast
  1574. //! @return A `virtual_ptr<Other, Registry>` pointing to the same object
  1575. //! @par Requirements
  1576. //! @li `Other` must be a base or a derived class of `Class`.
  1577. template<
  1578. class Other,
  1579. typename = std::enable_if_t<
  1580. std::is_base_of_v<element_type, Other> ||
  1581. std::is_base_of_v<Other, element_type>>>
  1582. auto cast() & -> decltype(auto) {
  1583. using other_smart_ptr = typename traits::template rebind<Other>;
  1584. return virtual_ptr<other_smart_ptr, Registry>(
  1585. traits::template cast<other_smart_ptr>(obj), vp);
  1586. }
  1587. template<
  1588. class Other,
  1589. typename = std::enable_if_t<
  1590. std::is_base_of_v<element_type, Other> ||
  1591. std::is_base_of_v<Other, element_type>>>
  1592. auto cast() const& -> decltype(auto) {
  1593. using other_smart_ptr = typename traits::template rebind<Other>;
  1594. return virtual_ptr<other_smart_ptr, Registry>(
  1595. traits::template cast<other_smart_ptr>(obj), vp);
  1596. }
  1597. template<class Other>
  1598. auto cast() && -> decltype(auto) {
  1599. static_assert(
  1600. std::is_base_of_v<element_type, Other> ||
  1601. std::is_base_of_v<Other, element_type>);
  1602. using other_smart_ptr = typename traits::template rebind<Other>;
  1603. return virtual_ptr<other_smart_ptr, Registry>(
  1604. traits::template cast<other_smart_ptr>(std::move(obj)), vp);
  1605. }
  1606. //! Construct a `virtual_ptr` from a smart pointer to an object
  1607. //!
  1608. //! This function forwards to @ref final_virtual_ptr.
  1609. //!
  1610. //! @tparam Other The type of the argument
  1611. //! @param obj A reference to an object
  1612. //! @return A `virtual_ptr<Class, Registry>` pointing to `obj`
  1613. template<class Other>
  1614. static auto final(Other&& obj) {
  1615. return final_virtual_ptr<Registry>(std::forward<Other>(obj));
  1616. }
  1617. //! Get the v-table pointer
  1618. //! @return The v-table pointer
  1619. auto vptr() const {
  1620. return detail::unbox_vptr(this->vp);
  1621. }
  1622. };
  1623. //! Construct a `virtual_ptr` from a lvalue reference.
  1624. //!
  1625. //! @tparam Class A class type, possibly cv-qualified.
  1626. //! @param obj A lvalue reference to an object.
  1627. //! @return A `virtual_ptr<Class>`.
  1628. template<class Class>
  1629. virtual_ptr(Class& obj)
  1630. -> virtual_ptr<Class, BOOST_OPENMETHOD_DEFAULT_REGISTRY>;
  1631. //! Construct a `virtual_ptr` from a xvalue reference.
  1632. //!
  1633. //! @tparam Class A class type.
  1634. //! @param obj A xvalue reference to an object.
  1635. //! @return A `virtual_ptr<Class>`.
  1636. template<class Class>
  1637. virtual_ptr(Class&& obj)
  1638. -> virtual_ptr<Class, BOOST_OPENMETHOD_DEFAULT_REGISTRY>;
  1639. // Alas this is not allowed:
  1640. // template<class Registry, class Class>
  1641. // virtual_ptr<Registry>(Class&) -> virtual_ptr<Class, Registry>;
  1642. //! Compare two `virtual_ptr`s for equality.
  1643. //!
  1644. //! Compare the underlying object pointers for equality. The v-table pointers
  1645. //! are not compared.
  1646. //!!
  1647. //! @tparam Left The type of the left-hand side argument.
  1648. //! @tparam Right The type of the right-hand side argument.
  1649. //! @tparam Registry A @ref registry.
  1650. //! @param left A reference to a `virtual_ptr`.
  1651. //! @param right A reference to a `virtual_ptr`.
  1652. //! @return `true` if both `virtual_ptr`s point to the same object or both
  1653. //! are `nullptr`, `false` otherwise.
  1654. template<class Left, class Right, class Registry>
  1655. auto operator==(
  1656. const virtual_ptr<Left, Registry>& left,
  1657. const virtual_ptr<Right, Registry>& right) -> bool {
  1658. return left.pointer() == right.pointer();
  1659. }
  1660. //! Compare two `virtual_ptr`s for inequality.
  1661. //!
  1662. //! Compare the underlying object pointers for inequality. The v-table pointers
  1663. //! are not compared.
  1664. //!! @tparam Left The type of the left-hand side argument.
  1665. //! @tparam Right The type of the right-hand side argument.
  1666. //! @tparam Registry A @ref registry.
  1667. //! @param left A reference to a `virtual_ptr`.
  1668. //! @param right A reference to a `virtual_ptr`.
  1669. //! @return `true` if both `virtual_ptr`s point to different objects, or one
  1670. //! is `nullptr` and the other is not, `false` otherwise.
  1671. template<class Left, class Right, class Registry>
  1672. auto operator!=(
  1673. const virtual_ptr<Left, Registry>& left,
  1674. const virtual_ptr<Right, Registry>& right) -> bool {
  1675. return !(left == right);
  1676. }
  1677. //! Specialize virtual_traits for `virtual_ptr`.
  1678. //!
  1679. //! Specialize virtual_traits for `virtual_ptr`\'s passed by value.
  1680. //!
  1681. //! @tparam Class A class type, possibly cv-qualified.
  1682. //! @tparam Registry A @ref registry.
  1683. template<class Class, class Registry>
  1684. struct virtual_traits<virtual_ptr<Class, Registry>, Registry> {
  1685. //! `Class`, stripped from cv-qualifiers.
  1686. using virtual_type =
  1687. std::remove_cv_t<typename virtual_ptr<Class, Registry>::element_type>;
  1688. //! Return a reference to a non-modifiable `Class` object.
  1689. //! @param arg A reference to a non-modifiable `Class` object.
  1690. //! @return A reference to the same object.
  1691. static auto peek(const virtual_ptr<Class, Registry>& ptr)
  1692. -> const virtual_ptr<Class, Registry>& {
  1693. return ptr;
  1694. }
  1695. //! Cast to another type.
  1696. //!
  1697. //! Cast a `virtual_ptr` to another type, using its `cast` member function.
  1698. //!
  1699. //! @param obj A lvalue reference to a `virtual_ptr`.
  1700. //! @return A lvalue reference to a `virtual_ptr` to the same object, cast
  1701. //! to `Derived::element_type`.
  1702. template<typename Derived>
  1703. static auto
  1704. cast(const virtual_ptr<Class, Registry>& ptr) -> decltype(auto) {
  1705. return ptr.template cast<typename Derived::element_type>();
  1706. }
  1707. //! Cast to another type.
  1708. //!
  1709. //! Cast a `virtual_ptr` to another type, using its `cast` member function.
  1710. //!
  1711. //! @param obj A xvalue reference to a `virtual_ptr`.
  1712. //! @return A xvalue reference to a `virtual_ptr` to the same object, cast
  1713. //! to `Derived::element_type`.
  1714. template<typename Derived>
  1715. static auto cast(virtual_ptr<Class, Registry>&& ptr) -> decltype(auto) {
  1716. return std::move(ptr).template cast<typename Derived::element_type>();
  1717. }
  1718. };
  1719. //! Specialize virtual_traits for `virtual_ptr`.
  1720. //!
  1721. //! Specialize virtual_traits for `virtual_ptr`\'s passed by const reference.
  1722. //!
  1723. //! @tparam Class A class type, possibly cv-qualified.
  1724. //! @tparam Registry A @ref registry.
  1725. template<class Class, class Registry>
  1726. struct virtual_traits<const virtual_ptr<Class, Registry>&, Registry> {
  1727. //! `Class`, stripped from cv-qualifiers.
  1728. using virtual_type =
  1729. std::remove_cv_t<typename virtual_ptr<Class, Registry>::element_type>;
  1730. //! Return a reference to a non-modifiable `Class` object.
  1731. //! @param arg A reference to a non-modifiable `Class` object.
  1732. //! @return A reference to the same object.
  1733. static auto peek(const virtual_ptr<Class, Registry>& ptr)
  1734. -> const virtual_ptr<Class, Registry>& {
  1735. return ptr;
  1736. }
  1737. //! Cast to another type.
  1738. //!
  1739. //! Cast a `virtual_ptr` to another type, using its `cast` member function.
  1740. //!
  1741. //! @param obj A lvalue reference to a `virtual_ptr`.
  1742. //! @return A lvalue reference to a `virtual_ptr` to the same object, cast
  1743. //! to `Derived::element_type`.
  1744. template<typename Derived>
  1745. static auto
  1746. cast(const virtual_ptr<Class, Registry>& ptr) -> decltype(auto) {
  1747. return ptr.template cast<
  1748. typename std::remove_reference_t<Derived>::element_type>();
  1749. }
  1750. };
  1751. // =============================================================================
  1752. // Method
  1753. namespace detail {
  1754. template<typename P, typename Q, class Registry>
  1755. struct select_overrider_virtual_type_aux {
  1756. using type = void;
  1757. };
  1758. template<typename P, typename Q, class Registry>
  1759. struct select_overrider_virtual_type_aux<virtual_<P>, Q, Registry> {
  1760. using type = virtual_type<Q, Registry>;
  1761. };
  1762. template<typename P, typename Q, class Registry>
  1763. struct select_overrider_virtual_type_aux<
  1764. virtual_ptr<P, Registry>, virtual_ptr<Q, Registry>, Registry> {
  1765. using type = typename virtual_traits<
  1766. virtual_ptr<Q, Registry>, Registry>::virtual_type;
  1767. };
  1768. template<typename P, typename Q, class Registry>
  1769. struct select_overrider_virtual_type_aux<
  1770. const virtual_ptr<P, Registry>&, const virtual_ptr<Q, Registry>&,
  1771. Registry> {
  1772. using type = typename virtual_traits<
  1773. const virtual_ptr<Q, Registry>&, Registry>::virtual_type;
  1774. };
  1775. template<typename P, typename Q, class Registry>
  1776. using select_overrider_virtual_type =
  1777. typename select_overrider_virtual_type_aux<P, Q, Registry>::type;
  1778. template<
  1779. typename MethodParameters, typename OverriderParameters, class Registry>
  1780. using overrider_virtual_types = boost::mp11::mp_remove<
  1781. boost::mp11::mp_transform_q<
  1782. boost::mp11::mp_bind_back<select_overrider_virtual_type, Registry>,
  1783. MethodParameters, OverriderParameters>,
  1784. void>;
  1785. template<class Method, class Rtti, std::size_t Index>
  1786. struct init_bad_call {
  1787. template<typename Arg, typename... Args>
  1788. static auto fn(bad_call& error, const Arg& arg, const Args&... args) {
  1789. if constexpr (Index == 0u) {
  1790. error.method = Rtti::template static_type<Method>();
  1791. error.arity = sizeof...(args) + 1;
  1792. }
  1793. type_id arg_type_id;
  1794. if constexpr (is_virtual_ptr<Arg>) {
  1795. arg_type_id = Rtti::dynamic_type(*arg);
  1796. } else {
  1797. arg_type_id = Rtti::dynamic_type(arg);
  1798. }
  1799. error.types[Index] = arg_type_id;
  1800. init_bad_call<Method, Rtti, Index + 1>::fn(error, args...);
  1801. }
  1802. static auto fn(bad_call&) {
  1803. }
  1804. };
  1805. template<class Method, class Rtti>
  1806. struct init_bad_call<Method, Rtti, bad_call::max_types> {
  1807. static auto fn(bad_call&) {
  1808. }
  1809. };
  1810. template<class Registry>
  1811. using method_base = std::conditional_t<
  1812. Registry::has_deferred_static_rtti, deferred_method_info, method_info>;
  1813. template<typename T, class Registry>
  1814. struct parameter_traits {
  1815. static auto peek(const T& value) -> const T& {
  1816. return value;
  1817. }
  1818. template<typename>
  1819. static auto cast(T value) -> T {
  1820. return value;
  1821. }
  1822. };
  1823. template<typename T, class Registry>
  1824. struct parameter_traits<virtual_<T>, Registry> : virtual_traits<T, Registry> {};
  1825. template<class Class, class Registry>
  1826. struct parameter_traits<virtual_ptr<Class, Registry, void>, Registry>
  1827. : virtual_traits<virtual_ptr<Class, Registry, void>, Registry> {};
  1828. template<class Class, class Registry>
  1829. struct parameter_traits<const virtual_ptr<Class, Registry, void>&, Registry>
  1830. : virtual_traits<const virtual_ptr<Class, Registry, void>&, Registry> {};
  1831. template<typename...>
  1832. constexpr bool false_t = false; // workaround before CWG2518/P2593R1
  1833. template<typename T, class Registry, typename = void>
  1834. struct validate_method_parameter : std::true_type {};
  1835. template<typename T, class Registry, typename U>
  1836. struct validate_method_parameter<virtual_<T>, Registry, U> : std::false_type {
  1837. static_assert(false_t<T>, "virtual_traits not specialized for type");
  1838. };
  1839. template<typename T, class Registry>
  1840. struct validate_method_parameter<
  1841. virtual_<T>, Registry,
  1842. std::void_t<typename virtual_traits<T, Registry>::virtual_type>>
  1843. : std::bool_constant<
  1844. has_vptr_fn<virtual_type<T, Registry>, Registry> ||
  1845. Registry::rtti::template is_polymorphic<virtual_type<T, Registry>>> {
  1846. static_assert(
  1847. validate_method_parameter::value,
  1848. "virtual_<> parameter is not a polymorphic class and no "
  1849. "boost_openmethod_vptr is applicable");
  1850. };
  1851. template<class Class, class Registry>
  1852. struct validate_method_parameter<virtual_ptr<Class, Registry>, Registry, void>
  1853. : std::true_type {};
  1854. template<class Class, class Registry, class MethodRegistry>
  1855. struct validate_method_parameter<
  1856. virtual_ptr<Class, Registry>, MethodRegistry, void> : std::false_type {
  1857. static_assert(
  1858. false_t<Class, Registry, MethodRegistry>, "registry mismatch");
  1859. };
  1860. } // namespace detail
  1861. //! Implement a method
  1862. //!
  1863. //! Methods are created by specializing the `method` class template with an
  1864. //! identifier, a function type and optionally a registry.
  1865. //!
  1866. //! `Id` is a type, typically an incomplete class declaration named after the
  1867. //! method's purpose. It is used to allow different methods with the same
  1868. //! signature.
  1869. //!
  1870. //! `Fn` is a function type, i.e. a type in the form `ReturnType(Parameters...)`.
  1871. //!
  1872. //! `Registry` is an instantiation of class template @ref registry. Methods may
  1873. //! use only classes that have been registered in the same registry as virtual
  1874. //! parameters and arguments. The registry also contains a set of policies that
  1875. //! influence several aspects of the dispatch mechanism - for example, how to
  1876. //! acquire a v-table pointer for an object, how to report errors, whether to
  1877. //! perform sanity checks, etc.
  1878. //!
  1879. //! The default value for `Registry` is @ref default_registry, but it can be
  1880. //! overridden by defining the preprocessor symbol
  1881. //! {{BOOST_OPENMETHOD_DEFAULT_REGISTRY}}, *before* including
  1882. //! `<boost/openmethod/core.hpp>`. Setting the symbol afterwards has no effect.
  1883. //!
  1884. //! Specializations of `method` have a single instance: the static member `fn`,
  1885. //! which has an `operator()` that forwards to the appropriate overrider. It is
  1886. //! selected in the same way as overloaded function resolution:
  1887. //!
  1888. //! 1. Form the set of all applicable overriders. An overrider is applicable
  1889. //! if it can be called with the arguments passed to the method.
  1890. //!
  1891. //! 2. If the set is empty, call the error handler (if present in the
  1892. //! registry), then terminate the program with `abort`.
  1893. //!
  1894. //! 3. Remove the overriders that are dominated by other overriders in the set.
  1895. //! Overrider A dominates overrider B if at least one of its virtual formal
  1896. //! parameters is more specialized than B's, and if none of B's virtual
  1897. //! parameters is more specialized than A's.
  1898. //!
  1899. //! 4. If the resulting set contains exactly one overrider, call it.
  1900. //!
  1901. //! If a single most specialized overrider does not exist, the program is
  1902. //! terminated via `abort`. If the registry contains an @ref error_handler
  1903. //! policy, its `error` function is called with an object that describes the
  1904. //! error, prior calling `abort`. `error` may prevent termination by throwing an
  1905. //! exception.
  1906. //!
  1907. //! For each virtual argument `arg`, the dispatch mechanism calls
  1908. //! `virtual_traits::peek(arg)` and deduces the v-table pointer from the
  1909. //! `result`, using the first of the following methods that applies:
  1910. //!
  1911. //! 1. If `result` is a `virtual_ptr`, get the pointer to the v-table from it.
  1912. //!
  1913. //! 2. If `boost_openmethod_vptr` can be called with `result` and a `Registry*`,
  1914. //! and it returns a `vptr_type`, call it.
  1915. //!
  1916. //! 3. Call `Registry::rtti::dynamic_vptr(result)`.
  1917. //!
  1918. //! @par N2216 Handling of Ambiguous Calls
  1919. //!
  1920. //! If `Registry` was initialized with the @ref N2216 option, ambiguous calls
  1921. //! are not an error. Instead, the following extra steps are taken to select an
  1922. //! overrider:
  1923. //!
  1924. //! 1. If the return type is a registered polymorphic type, remove all the
  1925. //! overriders that return a less specific type than others.
  1926. //!
  1927. //! 2. If the resulting set contains only one overrider, call it.
  1928. //!
  1929. //! 3. Otherwise, call one of the remaining overriders. Which overrider is
  1930. //! selected is not specified, but it is the same across calls with the
  1931. //! same arguments types.
  1932. //!
  1933. //! @tparam Id A type
  1934. //! @tparam Fn A function type
  1935. //! @tparam Registry The registry in which the method is defined
  1936. template<
  1937. typename Id, typename Fn,
  1938. class Registry = BOOST_OPENMETHOD_DEFAULT_REGISTRY>
  1939. class method;
  1940. //! Method with a specific id, signature and return type
  1941. //!
  1942. //! `method` implements an open-method that takes a parameter list -
  1943. //! `Parameters` - and returns a `ReturnType`.
  1944. //!
  1945. //! `Parameters` must contain at least one virtual parameter, i.e. a parameter
  1946. //! that has a type in the form `virtual_ptr<T, Registry>` or `virtual\_<T>`.
  1947. //! The dynamic types of the virtual arguments are taken into account to select
  1948. //! the overrider to call.
  1949. //!
  1950. //! @see method
  1951. //!
  1952. //! @tparam Id A type representing the method's name
  1953. //! @tparam ReturnType The return type of the method
  1954. //! @tparam Parameters The types of the parameters
  1955. //! @tparam Registry The registry of the method
  1956. template<
  1957. typename Id, typename... Parameters, typename ReturnType, class Registry>
  1958. class method<Id, ReturnType(Parameters...), Registry>
  1959. : public detail::method_base<Registry> {
  1960. template<auto Function, typename FunctionType>
  1961. struct override_aux;
  1962. // Aliases used in implementation only. Everything extracted from template
  1963. // arguments is capitalized like the arguments themselves.
  1964. using RegistryType = Registry;
  1965. using rtti = typename Registry::rtti;
  1966. using DeclaredParameters = mp11::mp_list<Parameters...>;
  1967. using CallParameters =
  1968. boost::mp11::mp_transform<detail::remove_virtual_, DeclaredParameters>;
  1969. using VirtualParameters =
  1970. typename detail::virtual_types<DeclaredParameters>;
  1971. using Signature = auto(Parameters...) -> ReturnType;
  1972. using FunctionPointer = auto (*)(detail::remove_virtual_<Parameters>...)
  1973. -> ReturnType;
  1974. public:
  1975. //! Method singleton
  1976. //!
  1977. //! The only instance of `method`. Its `operator()` is used to call
  1978. //! the method.
  1979. static method fn;
  1980. //! Call the method
  1981. //!
  1982. //! Call the method with `args`. The types of the arguments are the same as
  1983. //! the method `Parameters...`, stripped from any `virtual\_` decorators.
  1984. //!
  1985. //! @param args The arguments for the method call
  1986. //!
  1987. //! @par Errors
  1988. //!
  1989. //! If `Registry` contains an @ref error_handler policy, call its `error`
  1990. //! function with an object of one of the following types:
  1991. //!
  1992. //! @li @ref not_implemented: No overrider is applicable.
  1993. //! @li @ref ambiguous_call: More than one overrider is applicable, and
  1994. //! none is more specialized than all the others.
  1995. //!
  1996. auto operator()(typename BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  1997. StripVirtualDecorator<Parameters>::type... args) const
  1998. -> ReturnType;
  1999. //! Check if a next most specialized overrider exists
  2000. //!
  2001. //! Return `true` if a next most specialized overrider after _Fn_ exists,
  2002. //! and @ref next can be called without causing a @ref bad_call.
  2003. //!
  2004. //! @par Requirements
  2005. //!
  2006. //! `Fn` must be a function that is an overrider of the method.
  2007. //!
  2008. //! @tparam Fn A function that is an overrider of the method.
  2009. //! @return `true` if a next most specialized overrider exists
  2010. template<auto Fn>
  2011. static bool has_next();
  2012. //! The next most specialized overrider
  2013. //!
  2014. //! A pointer to the next most specialized overrider after `Fn`, i.e. the
  2015. //! overrider that would be called for the same tuple of virtual arguments
  2016. //! if `Fn` was not present. Set to `nullptr` if no such overrider exists.
  2017. //! @par Requirements
  2018. //!
  2019. //! `Fn` must be a function that is an overrider of the method.
  2020. //!
  2021. //! @tparam Fn A function that is an overrider of the method.
  2022. template<auto Fn>
  2023. static FunctionPointer next;
  2024. //! Add overriders to method
  2025. //!
  2026. //! `override`, instantiated as a static object, adds one or more overriders
  2027. //! to an open-method.
  2028. //!
  2029. //! @par Requirements
  2030. //!
  2031. //! `Fn` must be a function that fulfills the following requirements:
  2032. //!
  2033. //! @li Have the same number of formal parameters as the method.
  2034. //!
  2035. //! @li Each `virtual_ptr<T>` in the method's parameter list must have a
  2036. //! corresponding `virtual_ptr<U>` parameter in the same position in the
  2037. //! overrider's parameter list, such that `U` is the same as `T`, or has
  2038. //! `T` as an accessible unambiguous base.
  2039. //!
  2040. //! @li Each `virtual_<T>` in the method's parameter list must have a
  2041. //! corresponding `U` parameter in the same position in the overrider's
  2042. //! parameter list, such that `U` is the same as `T`, or has `T` as an
  2043. //! accessible unambiguous base.
  2044. //!
  2045. //! @li All other formal parameters must have the same type as the method's
  2046. //! corresponding parameters.
  2047. //!
  2048. //! @li The return type of the overrider must be the same as the method's
  2049. //! return type or, if it is a polymorphic type, covariant with the method's
  2050. //! return type.
  2051. //!
  2052. //! @tparam Fn One or more functions to the overrider list
  2053. template<auto... Fn>
  2054. class override {
  2055. std::tuple<override_aux<Fn, decltype(Fn)>...> impl;
  2056. };
  2057. private:
  2058. static constexpr auto Arity = boost::mp11::mp_count_if<
  2059. mp11::mp_list<Parameters...>, detail::is_virtual>::value;
  2060. // sanity checks
  2061. static_assert((
  2062. detail::validate_method_parameter<Parameters, Registry>::value && ...));
  2063. static_assert(Arity > 0, "method has no virtual parameters");
  2064. type_id vp_type_ids[Arity];
  2065. std::size_t slots_strides[2 * Arity - 1];
  2066. // Slots followed by strides. No stride for first virtual argument.
  2067. // For 1-method: the offset of the method in the method table, which
  2068. // contains a pointer to a function.
  2069. // For multi-methods: the offset of the first virtual argument in the
  2070. // method table, which contains a pointer to the corresponding cell in
  2071. // the dispatch table, followed by the offset of the second argument and
  2072. // the stride in the second dimension, etc.
  2073. void resolve_type_ids();
  2074. template<typename ArgType>
  2075. auto vptr(const ArgType& arg) const -> vptr_type;
  2076. template<typename MethodArgList, typename ArgType, typename... MoreArgTypes>
  2077. auto resolve_uni(const ArgType& arg, const MoreArgTypes&... more_args) const
  2078. -> detail::word;
  2079. template<typename MethodArgList, typename ArgType, typename... MoreArgTypes>
  2080. auto resolve_multi_first(
  2081. const ArgType& arg,
  2082. const MoreArgTypes&... more_args) const -> detail::word;
  2083. template<
  2084. std::size_t VirtualArg, typename MethodArgList, typename ArgType,
  2085. typename... MoreArgTypes>
  2086. auto resolve_multi_next(
  2087. vptr_type dispatch, const ArgType& arg,
  2088. const MoreArgTypes&... more_args) const -> detail::word;
  2089. template<typename... ArgType>
  2090. FunctionPointer resolve(const ArgType&... args) const;
  2091. template<auto, typename>
  2092. struct thunk;
  2093. template<auto, typename>
  2094. struct thunk;
  2095. method();
  2096. method(const method&) = delete;
  2097. method(method&&) = delete;
  2098. ~method();
  2099. void resolve(); // virtual if Registry contains has_deferred_static_rtti
  2100. static BOOST_NORETURN auto fn_not_implemented(
  2101. detail::remove_virtual_<Parameters>... args) -> ReturnType;
  2102. static BOOST_NORETURN auto
  2103. fn_ambiguous(detail::remove_virtual_<Parameters>... args) -> ReturnType;
  2104. template<
  2105. auto Overrider, typename OverriderReturn,
  2106. typename... OverriderParameters>
  2107. struct thunk<Overrider, OverriderReturn (*)(OverriderParameters...)> {
  2108. static auto
  2109. fn(detail::remove_virtual_<Parameters>... arg) -> ReturnType;
  2110. using OverriderVirtualParameters = detail::overrider_virtual_types<
  2111. DeclaredParameters, mp11::mp_list<OverriderParameters...>,
  2112. Registry>;
  2113. };
  2114. template<auto Function, typename FnReturnType>
  2115. struct override_impl
  2116. : std::conditional_t<
  2117. Registry::has_deferred_static_rtti,
  2118. detail::deferred_overrider_info, detail::overrider_info> {
  2119. explicit override_impl(FunctionPointer* next = nullptr);
  2120. void resolve_type_ids();
  2121. static type_id vp_type_ids[Arity];
  2122. };
  2123. template<auto Function, typename FunctionType>
  2124. struct override_aux;
  2125. template<auto Function, typename FnReturnType, typename... FnParameters>
  2126. struct override_aux<Function, FnReturnType (*)(FnParameters...)> {
  2127. override_aux() {
  2128. (void)&impl;
  2129. }
  2130. static override_impl<Function, FnReturnType> impl;
  2131. };
  2132. };
  2133. template<
  2134. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2135. method<Id, ReturnType(Parameters...), Registry>
  2136. method<Id, ReturnType(Parameters...), Registry>::fn;
  2137. template<
  2138. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2139. template<auto Fn>
  2140. typename method<Id, ReturnType(Parameters...), Registry>::FunctionPointer
  2141. method<Id, ReturnType(Parameters...), Registry>::next;
  2142. template<
  2143. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2144. template<auto Function, typename FnReturnType>
  2145. type_id method<Id, ReturnType(Parameters...), Registry>::override_impl<
  2146. Function, FnReturnType>::vp_type_ids[Arity];
  2147. template<
  2148. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2149. template<auto Function, typename FnReturnType, typename... FnParameters>
  2150. typename method<Id, ReturnType(Parameters...), Registry>::
  2151. template override_impl<Function, FnReturnType>
  2152. method<Id, ReturnType(Parameters...), Registry>::override_aux<
  2153. Function, FnReturnType (*)(FnParameters...)>::impl;
  2154. template<
  2155. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2156. method<Id, ReturnType(Parameters...), Registry>::method() {
  2157. using namespace policies;
  2158. this->slots_strides_ptr = slots_strides;
  2159. if constexpr (!Registry::has_deferred_static_rtti) {
  2160. resolve_type_ids();
  2161. }
  2162. this->vp_begin = vp_type_ids;
  2163. this->vp_end = vp_type_ids + Arity;
  2164. this->not_implemented = reinterpret_cast<void (*)()>(fn_not_implemented);
  2165. this->ambiguous = reinterpret_cast<void (*)()>(fn_ambiguous);
  2166. Registry::methods.push_back(*this);
  2167. }
  2168. template<
  2169. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2170. void method<Id, ReturnType(Parameters...), Registry>::resolve_type_ids() {
  2171. using namespace detail;
  2172. this->method_type_id = rtti::template static_type<method>();
  2173. this->return_type_id =
  2174. rtti::template static_type<virtual_type<ReturnType, Registry>>();
  2175. init_type_ids<
  2176. Registry,
  2177. mp11::mp_transform_q<
  2178. mp11::mp_bind_back<virtual_type, Registry>,
  2179. VirtualParameters>>::fn(this->vp_type_ids);
  2180. }
  2181. template<
  2182. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2183. method<Id, ReturnType(Parameters...), Registry>::~method() {
  2184. Registry::methods.remove(*this);
  2185. }
  2186. // -----------------------------------------------------------------------------
  2187. // method dispatch
  2188. template<
  2189. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2190. BOOST_FORCEINLINE auto
  2191. method<Id, ReturnType(Parameters...), Registry>::operator()(
  2192. typename BOOST_OPENMETHOD_DETAIL_UNLESS_MRDOCS
  2193. StripVirtualDecorator<Parameters>::type... args) const -> ReturnType {
  2194. using namespace detail;
  2195. auto pf = resolve(parameter_traits<Parameters, Registry>::peek(args)...);
  2196. return pf(std::forward<typename StripVirtualDecorator<Parameters>::type>(
  2197. args)...);
  2198. }
  2199. template<
  2200. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2201. template<typename... ArgType>
  2202. BOOST_FORCEINLINE
  2203. typename method<Id, ReturnType(Parameters...), Registry>::FunctionPointer
  2204. method<Id, ReturnType(Parameters...), Registry>::resolve(
  2205. const ArgType&... args) const {
  2206. using namespace detail;
  2207. Registry::require_initialized();
  2208. void (*pf)();
  2209. if constexpr (Arity == 1) {
  2210. pf = resolve_uni<mp11::mp_list<Parameters...>, ArgType...>(args...).pf;
  2211. } else {
  2212. pf = resolve_multi_first<mp11::mp_list<Parameters...>, ArgType...>(
  2213. args...)
  2214. .pf;
  2215. }
  2216. return reinterpret_cast<FunctionPointer>(pf);
  2217. }
  2218. template<
  2219. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2220. template<typename ArgType>
  2221. BOOST_FORCEINLINE auto method<Id, ReturnType(Parameters...), Registry>::vptr(
  2222. const ArgType& arg) const -> vptr_type {
  2223. if constexpr (detail::is_virtual_ptr<ArgType>) {
  2224. return arg.vptr();
  2225. } else {
  2226. return detail::acquire_vptr<Registry>(arg);
  2227. }
  2228. }
  2229. template<
  2230. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2231. template<typename MethodArgList, typename ArgType, typename... MoreArgTypes>
  2232. BOOST_FORCEINLINE auto
  2233. method<Id, ReturnType(Parameters...), Registry>::resolve_uni(
  2234. const ArgType& arg,
  2235. const MoreArgTypes&... more_args) const -> detail::word {
  2236. using namespace detail;
  2237. using namespace policies;
  2238. using namespace boost::mp11;
  2239. if constexpr (is_virtual<mp_first<MethodArgList>>::value) {
  2240. vptr_type vtbl = vptr<ArgType>(arg);
  2241. return vtbl[this->slots_strides[0]];
  2242. } else {
  2243. return resolve_uni<mp_rest<MethodArgList>>(more_args...);
  2244. }
  2245. }
  2246. template<
  2247. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2248. template<typename MethodArgList, typename ArgType, typename... MoreArgTypes>
  2249. BOOST_FORCEINLINE auto
  2250. method<Id, ReturnType(Parameters...), Registry>::resolve_multi_first(
  2251. const ArgType& arg,
  2252. const MoreArgTypes&... more_args) const -> detail::word {
  2253. using namespace detail;
  2254. using namespace boost::mp11;
  2255. if constexpr (is_virtual<mp_first<MethodArgList>>::value) {
  2256. vptr_type vtbl = vptr<ArgType>(arg);
  2257. std::size_t slot = this->slots_strides[0];
  2258. // The first virtual parameter is special. Since its stride is
  2259. // 1, there is no need to store it. Also, the method table
  2260. // contains a pointer into the multi-dimensional dispatch table,
  2261. // already resolved to the appropriate group.
  2262. auto dispatch = vtbl[slot].pw;
  2263. return resolve_multi_next<1, mp_rest<MethodArgList>, MoreArgTypes...>(
  2264. dispatch, more_args...);
  2265. } else {
  2266. return resolve_multi_first<mp_rest<MethodArgList>, MoreArgTypes...>(
  2267. more_args...);
  2268. }
  2269. }
  2270. template<
  2271. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2272. template<
  2273. std::size_t VirtualArg, typename MethodArgList, typename ArgType,
  2274. typename... MoreArgTypes>
  2275. BOOST_FORCEINLINE auto
  2276. method<Id, ReturnType(Parameters...), Registry>::resolve_multi_next(
  2277. vptr_type dispatch, const ArgType& arg,
  2278. const MoreArgTypes&... more_args) const -> detail::word {
  2279. using namespace detail;
  2280. using namespace boost::mp11;
  2281. if constexpr (is_virtual<mp_first<MethodArgList>>::value) {
  2282. vptr_type vtbl = vptr<ArgType>(arg);
  2283. std::size_t slot = this->slots_strides[VirtualArg];
  2284. std::size_t stride = this->slots_strides[Arity + VirtualArg - 1];
  2285. dispatch = dispatch + vtbl[slot].i * stride;
  2286. }
  2287. if constexpr (VirtualArg + 1 == Arity) {
  2288. return *dispatch;
  2289. } else {
  2290. return resolve_multi_next<
  2291. VirtualArg + 1, mp_rest<MethodArgList>, MoreArgTypes...>(
  2292. dispatch, more_args...);
  2293. }
  2294. }
  2295. // -----------------------------------------------------------------------------
  2296. // Error handling
  2297. template<
  2298. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2299. template<auto Fn>
  2300. inline auto
  2301. method<Id, ReturnType(Parameters...), Registry>::has_next() -> bool {
  2302. if (next<Fn> == fn_not_implemented) {
  2303. return false;
  2304. }
  2305. if (next<Fn> == fn_ambiguous) {
  2306. return false;
  2307. }
  2308. return true;
  2309. }
  2310. template<
  2311. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2312. BOOST_NORETURN auto
  2313. method<Id, ReturnType(Parameters...), Registry>::fn_not_implemented(
  2314. detail::remove_virtual_<Parameters>... args) -> ReturnType {
  2315. using namespace policies;
  2316. if constexpr (Registry::has_error_handler) {
  2317. no_overrider error;
  2318. detail::init_bad_call<method, rtti, 0u>::fn(
  2319. error,
  2320. detail::parameter_traits<Parameters, Registry>::peek(args)...);
  2321. Registry::error_handler::error(error);
  2322. }
  2323. abort(); // in case user handler "forgets" to abort
  2324. }
  2325. template<
  2326. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2327. BOOST_NORETURN auto
  2328. method<Id, ReturnType(Parameters...), Registry>::fn_ambiguous(
  2329. detail::remove_virtual_<Parameters>... args) -> ReturnType {
  2330. using namespace policies;
  2331. if constexpr (Registry::has_error_handler) {
  2332. ambiguous_call error;
  2333. detail::init_bad_call<method, rtti, 0u>::fn(
  2334. error,
  2335. detail::parameter_traits<Parameters, Registry>::peek(args)...);
  2336. Registry::error_handler::error(error);
  2337. }
  2338. abort(); // in case user handler "forgets" to abort
  2339. }
  2340. // -----------------------------------------------------------------------------
  2341. // overriders
  2342. namespace detail {
  2343. template<typename T, typename U>
  2344. struct same_reference_category {
  2345. static constexpr bool value = (std::is_lvalue_reference<T>::value ==
  2346. std::is_lvalue_reference<U>::value) &&
  2347. (std::is_rvalue_reference<T>::value ==
  2348. std::is_rvalue_reference<U>::value);
  2349. };
  2350. template<class T1, class T2, typename = void>
  2351. struct validate_overrider_parameter : std::false_type {
  2352. static_assert(
  2353. false_t<T1, T2>, "non-virtual parameter types must match exactly");
  2354. };
  2355. template<class T1, class T2>
  2356. struct validate_overrider_parameter<
  2357. T1, T2,
  2358. std::enable_if_t<
  2359. is_virtual_ptr<T1> && is_virtual_ptr<T2> &&
  2360. !same_reference_category<T1, T2>::value>> : std::false_type {
  2361. static_assert(
  2362. false_t<T1, T2>, "different virtual_ptr<> reference categories");
  2363. };
  2364. template<class T1, class T2>
  2365. struct validate_overrider_parameter<
  2366. T1, T2, std::enable_if_t<is_virtual_ptr<T1> && !is_virtual_ptr<T2>>>
  2367. : std::false_type {
  2368. static_assert(
  2369. false_t<T1, T2>,
  2370. "virtual_ptr<> is required in overrider in same position as in "
  2371. "method");
  2372. };
  2373. template<class T>
  2374. struct validate_overrider_parameter<T, T, void> : std::true_type {};
  2375. template<class T1, class T2>
  2376. struct validate_overrider_parameter<virtual_<T1>, T2, void> : std::true_type {};
  2377. template<class T1, class T2>
  2378. struct validate_overrider_parameter<virtual_<T1>, virtual_<T2>, void>
  2379. : std::false_type {
  2380. static_assert(false_t<T1, T2>, "virtual_<> is not allowed in overriders");
  2381. };
  2382. template<class T, class R>
  2383. struct validate_overrider_parameter<virtual_ptr<T, R>, virtual_ptr<T, R>, void>
  2384. : std::true_type {};
  2385. template<class T1, class R, class T2, class R2>
  2386. struct validate_overrider_parameter<
  2387. virtual_ptr<T1, R>, virtual_ptr<T2, R2>, void> : std::true_type {
  2388. static_assert(std::is_same_v<R, R2>, "registry mismatch");
  2389. using C1 = virtual_type<virtual_ptr<T1, R>, R>;
  2390. using C2 = virtual_type<virtual_ptr<T2, R>, R>;
  2391. static_assert(
  2392. std::is_base_of_v<C1, C2> &&
  2393. std::is_convertible_v<virtual_ptr<T2, R>, virtual_ptr<T1, R>>,
  2394. "method parameter must be an unambiguous accessible base "
  2395. "of corresponding overrider parameter");
  2396. };
  2397. template<class T1, class R, class T2, class R2>
  2398. struct validate_overrider_parameter<
  2399. const virtual_ptr<T1, R>&, const virtual_ptr<T2, R2>&, void>
  2400. : std::true_type {
  2401. static_assert(std::is_same_v<R, R2>, "registry mismatch");
  2402. using C1 = virtual_type<const virtual_ptr<T1, R>&, R>;
  2403. using C2 = virtual_type<const virtual_ptr<T2, R>&, R>;
  2404. static_assert(
  2405. std::is_base_of_v<C1, C2> &&
  2406. std::is_convertible_v<virtual_ptr<T2, R>, virtual_ptr<T1, R>>,
  2407. "method parameter must be an unambiguous accessible base "
  2408. "of corresponding overrider parameter");
  2409. };
  2410. template<class T1, class R, class T2, class R2>
  2411. struct validate_overrider_parameter<
  2412. virtual_ptr<T1, R>&&, virtual_ptr<T2, R2>&&, void> : std::true_type {
  2413. static_assert(std::is_same_v<R, R2>, "registry mismatch");
  2414. using C1 = virtual_type<virtual_ptr<T1, R>&&, R>;
  2415. using C2 = virtual_type<virtual_ptr<T2, R>&&, R>;
  2416. static_assert(
  2417. std::is_base_of_v<C1, C2> &&
  2418. std::is_convertible_v<virtual_ptr<T2, R>, virtual_ptr<T1, R>>,
  2419. "method parameter must be an unambiguous accessible base "
  2420. "of corresponding overrider parameter");
  2421. };
  2422. } // namespace detail
  2423. template<
  2424. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2425. template<
  2426. auto Overrider, typename OverriderReturn, typename... OverriderParameters>
  2427. auto method<Id, ReturnType(Parameters...), Registry>::
  2428. thunk<Overrider, OverriderReturn (*)(OverriderParameters...)>::fn(
  2429. detail::remove_virtual_<Parameters>... arg) -> ReturnType {
  2430. using namespace detail;
  2431. static_assert(
  2432. (validate_overrider_parameter<Parameters, OverriderParameters>::value &&
  2433. ...),
  2434. "virtual_ptr category mismatch");
  2435. return Overrider(
  2436. detail::parameter_traits<Parameters, Registry>::template cast<
  2437. OverriderParameters>(
  2438. std::forward<detail::remove_virtual_<Parameters>>(arg))...);
  2439. }
  2440. template<
  2441. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2442. template<auto Function, typename FnReturnType>
  2443. method<Id, ReturnType(Parameters...), Registry>::override_impl<
  2444. Function, FnReturnType>::override_impl(FunctionPointer* p_next) {
  2445. using namespace detail;
  2446. // static variable this->method below is zero-initialized but gcc and clang
  2447. // don't always see that.
  2448. #ifdef BOOST_CLANG
  2449. #pragma clang diagnostic push
  2450. #pragma clang diagnostic ignored "-Wuninitialized"
  2451. #endif
  2452. #ifdef BOOST_GCC
  2453. #pragma GCC diagnostic push
  2454. #pragma GCC diagnostic ignored "-Wuninitialized"
  2455. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  2456. #endif
  2457. // zero-initalized static variable
  2458. // coverity[uninit_use:FALSE]
  2459. if (overrider_info::method) {
  2460. BOOST_ASSERT(overrider_info::method == &fn);
  2461. return;
  2462. }
  2463. #ifdef BOOST_CLANG
  2464. #pragma clang diagnostic pop
  2465. #endif
  2466. #ifdef BOOST_GCC
  2467. #pragma GCC diagnostic pop
  2468. #endif
  2469. overrider_info::method = &fn;
  2470. if constexpr (!Registry::has_deferred_static_rtti) {
  2471. resolve_type_ids();
  2472. }
  2473. this->next = reinterpret_cast<void (**)()>(
  2474. p_next ? p_next : &method::next<Function>);
  2475. using Thunk = thunk<Function, decltype(Function)>;
  2476. this->pf = reinterpret_cast<void (*)()>(Thunk::fn);
  2477. this->vp_begin = vp_type_ids;
  2478. this->vp_end = vp_type_ids + Arity;
  2479. fn.overriders.push_back(*this);
  2480. }
  2481. template<
  2482. typename Id, typename... Parameters, typename ReturnType, class Registry>
  2483. template<auto Function, typename FnReturnType>
  2484. void method<Id, ReturnType(Parameters...), Registry>::override_impl<
  2485. Function, FnReturnType>::resolve_type_ids() {
  2486. using namespace detail;
  2487. this->return_type = Registry::rtti::template static_type<
  2488. virtual_type<FnReturnType, Registry>>();
  2489. this->type = Registry::rtti::template static_type<decltype(Function)>();
  2490. using Thunk = thunk<Function, decltype(Function)>;
  2491. detail::
  2492. init_type_ids<Registry, typename Thunk::OverriderVirtualParameters>::fn(
  2493. this->vp_type_ids);
  2494. }
  2495. //! Aliases for the most frequently used types in the library.
  2496. namespace aliases {
  2497. using boost::openmethod::final_virtual_ptr;
  2498. using boost::openmethod::virtual_;
  2499. using boost::openmethod::virtual_ptr;
  2500. } // namespace aliases
  2501. // ==============================================================================
  2502. // Exposition only
  2503. #ifdef __MRDOCS__
  2504. //! Blueprint for a specialization of @ref virtual_traits (exposition only).
  2505. //!
  2506. //! Specializations of @ref virtual_traits must implement the members listed
  2507. //! here.
  2508. //!
  2509. //! @tparam T The type of a virtual parameter of a method.
  2510. //! @tparam Registry A @ref registry.
  2511. template<typename T, class Registry>
  2512. struct VirtualTraits {
  2513. //! Class to use for dispatch.
  2514. //!
  2515. //! Aliases to the class to be considered during method dispatch to determine
  2516. //! which overrider to select, and which type_id to use for error reporting.
  2517. //! `virtual_traits<T>::virtual_type` aliases to `Class` if `T` is `Class&`,
  2518. //! `const Class&`, `Class*`, `const Class*`, `virtual_ptr<Class>`,
  2519. //! `virtual_ptr<const Class>`, `std::shared_ptr<Class>`,
  2520. //! `std::shared_ptr<const Class>`, `virtual_ptr<std::shared_ptr<Class>>`,
  2521. //! etc.
  2522. //!
  2523. //! @par Requirements
  2524. //!
  2525. //! `virtual_type` must be an alias to an *unadorned* *class* type, *not*
  2526. //! cv-qualified.
  2527. using virtual_type = detail::unspecified;
  2528. //! Returns a reference to the object to use for dispatch.
  2529. //!
  2530. //! Return a reference to the object to use for dispatch. `arg` may not be
  2531. //! copied, moved or altered in any way.
  2532. //!
  2533. //! @param arg An argument passed to the method call.
  2534. //! @return A reference to an object.
  2535. static auto peek(T arg) -> const virtual_type&;
  2536. //! Casts a virtual argument.
  2537. //!
  2538. //! Casts a virtual argument to the type expected by the overrider.
  2539. //!
  2540. //! @tparam T The type of a virtual parameter of a method.
  2541. //! @tparam U The type of a virtual parameter of an overrider.
  2542. //! @param arg The argument passed to a method call.
  2543. //! @return A reference to the argument, cast to `U`.
  2544. template<typename U>
  2545. static auto cast(T arg) -> U;
  2546. //! Rebind to a another class (smart pointers only).
  2547. //!
  2548. //! If `T` is a smart pointer, `rebind<U>` is the same kind of smart
  2549. //! pointer, but pointing to a `U`.
  2550. //!
  2551. //! @note `rebind` must be implemented @em only for smart pointer classes
  2552. //! that can be used as object pointers by @ref virtual_ptr in place of
  2553. //! plain pointers.
  2554. //!
  2555. //! @tparam U The new element type.
  2556. template<class U>
  2557. using rebind = detail::unspecified;
  2558. };
  2559. #endif
  2560. } // namespace boost::openmethod
  2561. #ifdef _MSC_VER
  2562. #pragma warning(pop)
  2563. #endif
  2564. #endif