LeviLamina
Loading...
Searching...
No Matches
function_base.h
1#pragma once
2
3#include <functional>
4
5#include "mc/platform/brstd/detail/DerivedType.h"
6
7namespace brstd::detail::function {
8
9template <DerivedType, class Base>
10class function_base : public Base {
11protected:
12 function_base() { this->construct_empty(); }
14 if (*this) {
15 this->get_vtable().destroy(this->mStorage);
16 }
17 }
19 if (other) {
20 other.get_vtable().move_to(other.mStorage, this->mStorage);
21 this->mStorage.vfptr = std::exchange(other.mStorage.vfptr, nullptr);
22 } else {
23 this->construct_empty();
24 }
25 }
26 function_base& operator=(function_base&& other) {
27 if (this != std::addressof(other)) {
28 if (*this) {
29 this->get_vtable().destroy(this->mStorage);
30 }
31 if (other) {
32 other.get_vtable().move_to(other.mStorage, this->mStorage);
33 this->mStorage.vfptr = std::exchange(other.mStorage.vfptr, nullptr);
34 } else {
35 this->construct_empty();
36 }
37 }
38 return *this;
39 }
40
41public:
42 function_base& operator=(std::nullptr_t) {
43 if (*this) {
44 this->get_vtable().destroy(this->mStorage);
45 this->mStorage.vfptr = nullptr;
46 }
47 return *this;
48 }
49
50 [[nodiscard]] friend bool operator==(function_base const& self, nullptr_t) noexcept {
51 return !static_cast<bool>(self);
52 }
53};
54
55template <class Base>
56class function_base<DerivedType::Copyable, Base> : public function_base<DerivedType::MoveOnly, Base> {
57protected:
58 function_base() = default;
59 function_base(function_base&&) = default;
60 function_base& operator=(function_base&&) = default;
61
62 function_base(function_base const& other) {
63 if (other) {
64 other.get_vtable().copy_to(other.mStorage, this->mStorage);
65 this->mStorage.vfptr = other.mStorage.vfptr;
66 } else {
67 this->construct_empty();
68 }
69 }
70 function_base& operator=(function_base const& other) {
71 if (this != std::addressof(other)) {
72 if (*this) {
73 this->get_vtable().destroy(this->mStorage);
74 }
75 if (other) {
76 other.get_vtable().copy_to(other.mStorage, this->mStorage);
77 this->mStorage.vfptr = other.mStorage.vfptr;
78 } else {
79 this->construct_empty();
80 }
81 }
82 return *this;
83 }
84};
85
86} // namespace brstd::detail::function
Definition function_base.h:10