virtual_method.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. namespace base {
  9. template <typename Object, typename ParentObject = void>
  10. class virtual_object;
  11. template <typename ConcreteMethod, typename ReturnType, typename ...Args>
  12. class virtual_method;
  13. template <typename ConcreteMethod, typename BaseMethod>
  14. class virtual_override;
  15. namespace virtual_methods {
  16. struct child_entry;
  17. using is_parent_check = bool(*)(const child_entry &possible_parent);
  18. struct child_entry {
  19. is_parent_check check_is_parent;
  20. int *table_index;
  21. };
  22. using child_entries = std::vector<child_entry>;
  23. // Recursive method to find if some class is a child of some other class.
  24. template <typename ConcreteObject>
  25. struct is_parent {
  26. static inline bool check(const child_entry &possible_parent) {
  27. // Generate a good error message if ConcreteObject is not a child of virtual_object<>.
  28. using all_objects_must_derive_virtual_object = typename ConcreteObject::virtual_object_parent;
  29. using ConcreteObjectParent = all_objects_must_derive_virtual_object;
  30. return (possible_parent.check_is_parent == &is_parent<ConcreteObject>::check)
  31. || is_parent<ConcreteObjectParent>::check(possible_parent);
  32. }
  33. };
  34. template <>
  35. struct is_parent<void> {
  36. static inline bool check(const child_entry &possible_parent) {
  37. return (possible_parent.check_is_parent == &is_parent<void>::check);
  38. }
  39. };
  40. // Just force the compiler not to optimize away the object that "enforce" points at.
  41. inline void dont_optimize_away(void *enforce) {
  42. static volatile void *result = nullptr;
  43. if (result) {
  44. result = enforce;
  45. }
  46. }
  47. template <typename Type, Type Value>
  48. struct dont_optimize_away_struct {
  49. };
  50. inline bool first_dispatch_fired(bool did_fire = false) {
  51. static bool fired = false;
  52. if (did_fire) {
  53. fired = true;
  54. }
  55. return fired;
  56. }
  57. template <typename Object, void (*Creator)(const child_entry &)>
  58. class object_registrator {
  59. public:
  60. inline object_registrator() {
  61. Assert(!first_dispatch_fired());
  62. Creator(child_entry {
  63. &is_parent<Object>::check,
  64. &_index,
  65. });
  66. }
  67. static inline int &Index() {
  68. return _index;
  69. }
  70. private:
  71. static int _index;
  72. };
  73. template <typename Object, void (*Creator)(const child_entry &)>
  74. int object_registrator<Object, Creator>::_index = -1;
  75. class object_base {
  76. protected:
  77. virtual ~object_base() = default;
  78. };
  79. template <typename ...ConcreteArgs>
  80. struct multi_index_collector;
  81. template <int M, typename ...ConcreteArgs>
  82. struct override_key_collector_helper;
  83. template <typename Call, typename ...Args>
  84. struct table_fill_entry_helper;
  85. template <typename ...Args>
  86. struct table_count_size;
  87. } // namespace virtual_methods
  88. // This should be a base class for every child object in your hierarchy.
  89. // It registers this child in the root virtual_objects classes list.
  90. // Also it holds its own index in the classes list that is used for fast
  91. // invoking of methods from the virtual tables in different virtual_methods.
  92. template <typename Object, typename ParentObject>
  93. class virtual_object : public ParentObject {
  94. protected:
  95. virtual ~virtual_object() {
  96. virtual_methods::dont_optimize_away(&_virtual_object_registrator);
  97. }
  98. private:
  99. using virtual_object_parent = ParentObject;
  100. friend struct virtual_methods::is_parent<Object>;
  101. template <typename ...Args>
  102. friend struct virtual_methods::multi_index_collector;
  103. template <int M, typename ...ConcreteArgs>
  104. friend struct virtual_methods::override_key_collector_helper;
  105. template <typename OtherObject, typename OtherParentObject>
  106. friend class virtual_object;
  107. template <typename BaseMethod, typename ReturnType, typename ...Args>
  108. friend class virtual_method;
  109. static inline void virtual_object_register_child(const virtual_methods::child_entry &entry) {
  110. return ParentObject::virtual_object_register_child(entry);
  111. }
  112. using virtual_object_registrator = virtual_methods::object_registrator<Object, &virtual_object::virtual_object_register_child>;
  113. static virtual_object_registrator _virtual_object_registrator;
  114. using virtual_object_dont_optimize_away_registrator = virtual_methods::dont_optimize_away_struct<virtual_object_registrator*, &_virtual_object_registrator>;
  115. static inline int &virtual_object_child_index_static() {
  116. return virtual_object_registrator::Index();
  117. }
  118. int &virtual_object_child_index() override {
  119. return virtual_object_child_index_static();
  120. }
  121. };
  122. template <typename Object, typename ParentObject>
  123. typename virtual_object<Object, ParentObject>::virtual_object_registrator virtual_object<Object, ParentObject>::_virtual_object_registrator = {};
  124. // This should be a base class for the root of the whole hierarchy.
  125. // It holds the table of all child classes in a list.
  126. // This list is used by virtual_methods to generate virtual table.
  127. template <typename Object>
  128. class virtual_object<Object, void> : public virtual_methods::object_base {
  129. protected:
  130. virtual ~virtual_object() {
  131. virtual_methods::dont_optimize_away(&_virtual_object_registrator);
  132. }
  133. private:
  134. using virtual_object_parent = void;
  135. friend struct virtual_methods::is_parent<Object>;
  136. template <typename ...Args>
  137. friend struct virtual_methods::table_count_size;
  138. template <typename ...Args>
  139. friend struct virtual_methods::multi_index_collector;
  140. template <int M, typename ...ConcreteArgs>
  141. friend struct virtual_methods::override_key_collector_helper;
  142. template <typename Call, typename ...Args>
  143. friend struct virtual_methods::table_fill_entry_helper;
  144. template <typename OtherObject, typename OtherParentObject>
  145. friend class virtual_object;
  146. template <typename BaseMethod, typename ReturnType, typename ...Args>
  147. friend class virtual_method;
  148. static inline virtual_methods::child_entries &virtual_object_get_child_entries() {
  149. static virtual_methods::child_entries entries;
  150. return entries;
  151. }
  152. // Registers a new child class.
  153. // After that on the next call to virtual_method::virtual_method_prepare_table() will
  154. // generate a new virtual table for that virtual method.
  155. static inline void virtual_object_register_child(const virtual_methods::child_entry &entry) {
  156. auto &entries = virtual_object_get_child_entries();
  157. for (auto i = entries.begin(), e = entries.end(); i != e; ++i) {
  158. if (entry.check_is_parent(*i)) {
  159. *entry.table_index = (i - entries.begin());
  160. i = entries.insert(i, entry);
  161. for (++i, e = entries.end(); i != e; ++i) {
  162. ++*(i->table_index);
  163. }
  164. return;
  165. }
  166. }
  167. *entry.table_index = entries.size();
  168. entries.push_back(entry);
  169. }
  170. using virtual_object_registrator = virtual_methods::object_registrator<Object, &virtual_object::virtual_object_register_child>;
  171. static virtual_object_registrator _virtual_object_registrator;
  172. using virtual_object_dont_optimize_away_registrator = virtual_methods::dont_optimize_away_struct<virtual_object_registrator*, &_virtual_object_registrator>;
  173. static inline int &virtual_object_child_index_static() {
  174. return virtual_object_registrator::Index();
  175. }
  176. virtual int &virtual_object_child_index() {
  177. return virtual_object_child_index_static();
  178. }
  179. };
  180. template <typename Object>
  181. typename virtual_object<Object, void>::virtual_object_registrator virtual_object<Object, void>::_virtual_object_registrator = {};
  182. namespace virtual_methods {
  183. template <typename Arg>
  184. struct is_virtual_argument : public std::integral_constant<bool,
  185. base::type_traits<Arg>::is_pointer::value
  186. ? std::is_base_of<object_base, typename base::type_traits<Arg>::pointed_type>::value
  187. : false> {
  188. };
  189. template <int N, int Instance>
  190. class multi_int_wrap {
  191. public:
  192. inline multi_int_wrap(int *indices) : _indices(indices) {
  193. }
  194. inline multi_int_wrap<N - 1, Instance> subindex() const {
  195. static_assert(N > 0, "Wrong multi_int_wrap created!");
  196. return multi_int_wrap<N - 1, Instance>(_indices + 1);
  197. }
  198. inline int &current() const {
  199. return *_indices;
  200. }
  201. private:
  202. int *_indices;
  203. };
  204. template <int Instance>
  205. class multi_int_wrap<0, Instance> {
  206. public:
  207. inline multi_int_wrap(int *indices) {
  208. }
  209. inline int current() const {
  210. return 1;
  211. }
  212. };
  213. template <int N>
  214. using multi_index_wrap = multi_int_wrap<N, 0>;
  215. template <int N>
  216. using multi_size_wrap = multi_int_wrap<N, 1>;
  217. template <typename ConcreteArg, typename ...ConcreteArgs>
  218. struct multi_index_collector<ConcreteArg, ConcreteArgs...> {
  219. static constexpr int N = sizeof...(ConcreteArgs) + 1;
  220. static inline void call(multi_index_wrap<N> indices, ConcreteArg arg, ConcreteArgs... args) {
  221. indices.current() = computeIndex(is_virtual_argument<ConcreteArg>(), arg);
  222. multi_index_collector<ConcreteArgs...>::call(indices.subindex(), args...);
  223. }
  224. static inline int computeIndex(std::integral_constant<bool, false>, ConcreteArg arg) {
  225. return 0;
  226. }
  227. static inline int computeIndex(std::integral_constant<bool, true>, ConcreteArg arg) {
  228. return arg->virtual_object_child_index();
  229. }
  230. };
  231. template <>
  232. struct multi_index_collector<> {
  233. static inline void call(multi_index_wrap<0> indices) {
  234. }
  235. };
  236. template <int N>
  237. class override_key;
  238. template <int N, int Instance>
  239. class multi_int {
  240. public:
  241. inline multi_int_wrap<N, Instance> data_wrap() {
  242. return multi_int_wrap<N, Instance>(_indices);
  243. }
  244. template <typename ...ConcreteArgs>
  245. static inline multi_int<N, Instance> collect(ConcreteArgs... args) {
  246. multi_int<N, Instance> result;
  247. multi_index_collector<ConcreteArgs...>::call(result.data_wrap(), args...);
  248. return result;
  249. }
  250. inline void reset() {
  251. memset(_indices, 0, sizeof(_indices));
  252. }
  253. inline int value(int index) const {
  254. return _indices[index];
  255. }
  256. inline void copy(multi_int_wrap<N, Instance> other) {
  257. memcpy(_indices, &other.current(), sizeof(_indices));
  258. }
  259. private:
  260. int _indices[N] = { 0 };
  261. friend class override_key<N>;
  262. };
  263. template <int N>
  264. using multi_index = multi_int<N, 0>;
  265. template <int N>
  266. using multi_size = multi_int<N, 1>;
  267. template <typename Call, int N>
  268. class table_data_wrap {
  269. public:
  270. inline table_data_wrap(Call *data, multi_size_wrap<N> size) : _data(data), _size(size) {
  271. }
  272. inline table_data_wrap<Call, N - 1> operator[](int index) const {
  273. return table_data_wrap<Call, N - 1>(_data + index * _size.subindex().current(), _size.subindex());
  274. }
  275. inline Call &operator[](multi_index_wrap<N> index) const {
  276. return (*this)[index.current()][index.subindex()];
  277. }
  278. inline int size() const {
  279. return count_size(std::integral_constant<int,N>());
  280. }
  281. private:
  282. template <int M>
  283. inline int count_size(std::integral_constant<int,M>) const {
  284. return _size.current() / _size.subindex().current();
  285. }
  286. inline int count_size(std::integral_constant<int,1>) const {
  287. return _size.current();
  288. }
  289. Call *_data;
  290. multi_size_wrap<N> _size;
  291. };
  292. template <typename Call>
  293. class table_data_wrap<Call, 0> {
  294. public:
  295. inline table_data_wrap(Call *data, multi_size_wrap<0> size) : _data(data) {
  296. }
  297. inline Call &operator[](multi_index_wrap<0> index) const {
  298. return *_data;
  299. }
  300. private:
  301. Call *_data;
  302. };
  303. template <typename Call, int N>
  304. class table_data_wrap;
  305. template <typename Arg, typename ...Args>
  306. struct table_count_size<Arg, Args...> {
  307. static constexpr int N = sizeof...(Args) + 1;
  308. static inline void call(multi_size_wrap<N> index) {
  309. auto subindex = index.subindex();
  310. table_count_size<Args...>::call(subindex);
  311. index.current() = count(is_virtual_argument<Arg>()) * subindex.current();
  312. }
  313. static inline int count(std::integral_constant<bool, false>) {
  314. return 1;
  315. }
  316. static inline int count(std::integral_constant<bool, true>) {
  317. return base::type_traits<Arg>::pointed_type::virtual_object_get_child_entries().size();
  318. }
  319. };
  320. template <>
  321. struct table_count_size<> {
  322. static inline void call(multi_size_wrap<0> index) {
  323. }
  324. };
  325. template <typename Call, int N>
  326. class table_data {
  327. public:
  328. inline table_data_wrap<Call, N> data_wrap() {
  329. return table_data_wrap<Call, N>(_data.data(), _size.data_wrap());
  330. }
  331. inline Call &operator[](multi_index<N> index) {
  332. int flat_index = 0;
  333. for (int i = 0; i != N - 1; ++i) {
  334. flat_index += _size.value(i + 1) * index.value(i);
  335. }
  336. flat_index += index.value(N - 1);
  337. return _data[flat_index];
  338. }
  339. template <typename ...Args>
  340. inline bool changed() {
  341. if (!_data.empty()) {
  342. return false;
  343. }
  344. multi_size<N> size;
  345. table_count_size<Args...>::call(size.data_wrap());
  346. _size = size;
  347. _data.resize(_size.value(0), nullptr);
  348. return true;
  349. }
  350. private:
  351. std::vector<Call> _data;
  352. multi_size<N> _size;
  353. };
  354. template <typename Call>
  355. class table_data<Call, 0> {
  356. public:
  357. inline table_data_wrap<Call, 0> data_wrap() {
  358. return table_data_wrap<Call, 0>(&_call, multi_size_wrap<0>(nullptr));
  359. }
  360. inline Call &operator[](multi_index<0> index) {
  361. return _call;
  362. }
  363. inline bool changed() const {
  364. return false;
  365. }
  366. private:
  367. Call _call = nullptr;
  368. };
  369. template <typename Call, typename ...Args>
  370. struct table_fill_entry_helper;
  371. template <typename Call, typename Arg, typename ...Args>
  372. struct table_fill_entry_helper<Call, Arg, Args...> {
  373. static constexpr int N = sizeof...(Args) + 1;
  374. static inline bool call(table_data_wrap<Call, N> table, multi_index_wrap<N> index, Call &fill) {
  375. auto start = index.current();
  376. for (auto i = start, count = table.size(); i != count; ++i) {
  377. auto foundGoodType = good(is_virtual_argument<Arg>(), start, index.current());
  378. if (foundGoodType) {
  379. index.current() = i;
  380. if (table_fill_entry_helper<Call, Args...>::call(table[i], index.subindex(), fill)) {
  381. return true;
  382. }
  383. }
  384. }
  385. index.current() = start;
  386. return false;
  387. }
  388. static inline bool good(std::integral_constant<bool,false>, int start, int current) {
  389. return (start == current);
  390. }
  391. static inline bool good(std::integral_constant<bool,true>, int start, int current) {
  392. using BaseObject = typename base::type_traits<Arg>::pointed_type;
  393. auto &entries = BaseObject::virtual_object_get_child_entries();
  394. return (start == current) || entries[start].check_is_parent(entries[current]);
  395. }
  396. };
  397. template <typename Call>
  398. struct table_fill_entry_helper<Call> {
  399. static inline bool call(table_data_wrap<Call, 0> table, multi_index_wrap<0> index, Call &fill) {
  400. if (auto overrideMethod = table[index]) {
  401. fill = overrideMethod;
  402. return true;
  403. }
  404. return false;
  405. }
  406. };
  407. template <typename Call, int N>
  408. struct table_fill_entry;
  409. template <typename ReturnType, int N, typename BaseMethod, typename ...Args>
  410. struct table_fill_entry<ReturnType(*)(BaseMethod*, Args...), N> {
  411. using Call = ReturnType(*)(BaseMethod*, Args...);
  412. static inline void call(table_data_wrap<Call, N> table, multi_index_wrap<N> index, Call &fill) {
  413. table_fill_entry_helper<Call, Args...>::call(table, index, fill);
  414. }
  415. };
  416. template <typename Call, int N>
  417. inline void fill_entry(table_data_wrap<Call, N> table, multi_index_wrap<N> index, Call &fill) {
  418. return virtual_methods::table_fill_entry<Call, N>::call(table, index, fill);
  419. }
  420. template <int M, typename ...ConcreteArgs>
  421. struct override_key_collector_helper;
  422. template <int M, typename ConcreteArg, typename ...ConcreteArgs>
  423. struct override_key_collector_helper<M, ConcreteArg, ConcreteArgs...> {
  424. static inline void call(int **indices) {
  425. setValue(is_virtual_argument<ConcreteArg>(), indices);
  426. override_key_collector_helper<M + 1, ConcreteArgs...>::call(indices);
  427. }
  428. static inline void setValue(std::integral_constant<bool,false>, int **indices) {
  429. indices[M] = nullptr;
  430. }
  431. static inline void setValue(std::integral_constant<bool,true>, int **indices) {
  432. using ConcreteObject = typename base::type_traits<ConcreteArg>::pointed_type;
  433. using IsParentCheckStruct = is_parent<ConcreteObject>;
  434. using IsParentCheckPointer = decltype(&IsParentCheckStruct::check);
  435. using override_key_collector_dont_optimize_away = dont_optimize_away_struct<IsParentCheckPointer, &IsParentCheckStruct::check>;
  436. override_key_collector_dont_optimize_away dont_optimize_away_object;
  437. (void)dont_optimize_away_object;
  438. // Check that is_parent<> can be instantiated.
  439. // So every ConcreteObject is a valid child of virtual_object<>.
  440. dont_optimize_away(reinterpret_cast<void*>(&IsParentCheckStruct::check));
  441. indices[M] = &ConcreteObject::virtual_object_child_index_static();
  442. }
  443. };
  444. template <int M>
  445. struct override_key_collector_helper<M> {
  446. static inline void call(int **indices) {
  447. }
  448. };
  449. template <typename CallSignature>
  450. struct override_key_collector;
  451. template <typename ReturnType, typename BaseMethod, typename ...ConcreteArgs>
  452. struct override_key_collector<ReturnType(*)(BaseMethod, ConcreteArgs...)> {
  453. static inline void call(int **indices) {
  454. override_key_collector_helper<0, ConcreteArgs...>::call(indices);
  455. }
  456. };
  457. template <int N>
  458. class override_key {
  459. public:
  460. inline multi_index<N> value() const {
  461. multi_index<N> result;
  462. for (int i = 0; i != N; ++i) {
  463. auto pointer = _indices[i];
  464. result._indices[i] = (pointer ? *pointer : 0);
  465. }
  466. return result;
  467. }
  468. friend inline bool operator<(const override_key &k1, const override_key &k2) {
  469. for (int i = 0; i != N; ++i) {
  470. auto pointer1 = k1._indices[i], pointer2 = k2._indices[i];
  471. if (pointer1 < pointer2) {
  472. return true;
  473. } else if (pointer1 > pointer2) {
  474. return false;
  475. }
  476. }
  477. return false;
  478. }
  479. template <typename CallSignature>
  480. inline void collect() {
  481. override_key_collector<CallSignature>::call(_indices);
  482. }
  483. private:
  484. int *_indices[N];
  485. };
  486. template <typename BaseMethod, typename ConcreteMethod, typename CallSignature, typename ...Args>
  487. struct static_cast_helper;
  488. template <typename BaseMethod, typename ConcreteMethod, typename ReturnType, typename ...ConcreteArgs, typename ...Args>
  489. struct static_cast_helper<BaseMethod, ConcreteMethod, ReturnType(*)(BaseMethod *, ConcreteArgs...), Args...> {
  490. static inline ReturnType call(BaseMethod *context, Args ...args) {
  491. return ConcreteMethod::call(context, static_cast<ConcreteArgs>(args)...);
  492. }
  493. };
  494. } // namespace virtual_methods
  495. // This is a base class for all your virtual methods.
  496. // It dispatches a call to one of the registered virtual_overrides
  497. // or calls the fallback method of the BaseMethod class.
  498. template <typename BaseMethod, typename ReturnType, typename ...Args>
  499. class virtual_method {
  500. static constexpr int N = sizeof...(Args);
  501. using virtual_method_call = ReturnType(*)(BaseMethod *context, Args... args);
  502. public:
  503. inline ReturnType call(Args... args) {
  504. auto context = static_cast<BaseMethod*>(this);
  505. auto index = virtual_methods::multi_index<N>::collect(args...);
  506. auto &table = virtual_method_prepare_table();
  507. auto &entry = table[index];
  508. if (!entry) {
  509. virtual_methods::fill_entry(table.data_wrap(), index.data_wrap(), entry);
  510. if (!entry) {
  511. entry = &virtual_method::virtual_method_base_instance;
  512. }
  513. }
  514. return (*entry)(context, args...);
  515. }
  516. private:
  517. // This map of methods contains only the original registered overrides.
  518. using virtual_method_override_key = virtual_methods::override_key<N>;
  519. using virtual_method_override_map = std::map<virtual_method_override_key, virtual_method_call>;
  520. static inline virtual_method_override_map &virtual_method_get_override_map() {
  521. static virtual_method_override_map override_map;
  522. return override_map;
  523. }
  524. // This method generates and returns a virtual table which holds a method
  525. // for any child in the hierarchy or nullptr if none of the virtual_overrides fit.
  526. using virtual_method_table_data = virtual_methods::table_data<virtual_method_call, N>;
  527. static inline virtual_method_table_data &virtual_method_get_table_data() {
  528. static virtual_method_table_data virtual_table;
  529. return virtual_table;
  530. }
  531. static inline virtual_method_table_data &virtual_method_prepare_table() {
  532. auto &virtual_table = virtual_method_get_table_data();
  533. if (virtual_table.template changed<Args...>()) {
  534. virtual_methods::first_dispatch_fired(true);
  535. // The class hierarchy has changed - we need to generate the virtual table once again.
  536. // All other handlers will be placed if they're called.
  537. for (auto &i : virtual_method_get_override_map()) {
  538. virtual_table[i.first.value()] = i.second;
  539. }
  540. }
  541. return virtual_table;
  542. }
  543. static ReturnType virtual_method_base_instance(BaseMethod *context, Args... args) {
  544. return BaseMethod::default_call(context, args...);
  545. }
  546. template <typename ConcreteMethod>
  547. static ReturnType virtual_method_override_instance(BaseMethod *context, Args... args) {
  548. return virtual_methods::static_cast_helper<BaseMethod, ConcreteMethod, decltype(&ConcreteMethod::call), Args...>::call(context, args...);
  549. }
  550. template <typename ConcreteMethod>
  551. static inline void virtual_method_register_override() {
  552. auto call = &virtual_method_override_instance<ConcreteMethod>;
  553. virtual_methods::override_key<N> key;
  554. key.template collect<decltype(&ConcreteMethod::call)>();
  555. virtual_method_get_override_map()[key] = call;
  556. }
  557. template <typename ConcreteMethod, typename OtherBaseMethod>
  558. friend class virtual_override;
  559. };
  560. template <typename ConcreteMethod, typename BaseMethod>
  561. class virtual_override {
  562. protected:
  563. virtual ~virtual_override() {
  564. virtual_methods::dont_optimize_away(&_virtual_override_registrator);
  565. }
  566. private:
  567. class virtual_override_registrator {
  568. public:
  569. inline virtual_override_registrator() {
  570. Assert(!virtual_methods::first_dispatch_fired());
  571. BaseMethod::template virtual_method_register_override<ConcreteMethod>();
  572. }
  573. };
  574. static virtual_override_registrator _virtual_override_registrator;
  575. using virtual_override_dont_optimize_away_registrator = virtual_methods::dont_optimize_away_struct<virtual_override_registrator*, &_virtual_override_registrator>;
  576. };
  577. template <typename ConcreteMethod, typename BaseMethod>
  578. typename virtual_override<ConcreteMethod, BaseMethod>::virtual_override_registrator virtual_override<ConcreteMethod, BaseMethod>::_virtual_override_registrator = {};
  579. } // namespace base