blob: 9e08452a03595546c94b0783729b54b8ec26a8cc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#pragma once
#include <memory>
#include <tuple>
template<typename Primary, typename... Others> class ManyPtr : Primary {
public:
using element_type = typename Primary::element_type;
template<typename... Params> ManyPtr(Params &&... params) : Primary {std::forward<Params>(params)...}
{
updatePtrs();
}
using Primary::operator->;
using Primary::operator*;
using Primary::operator bool;
using Primary::get;
template<typename... Params>
void
reset(Params &&... params)
{
Primary::reset(std::forward<Params>(params)...);
updatePtrs();
}
template<typename Other>
[[nodiscard]] consteval static bool
couldBe()
{
return (std::is_convertible_v<Others *, Other *> || ...);
}
template<typename Other>
requires(couldBe<Other>())
[[nodiscard]] auto
getAs() const
{
return std::get<idx<Other>()>(others);
}
template<typename Other>
requires(!couldBe<Other>() && requires { std::dynamic_pointer_cast<Other>(std::declval<Primary>()); })
[[nodiscard]] auto
dynamicCast() const
{
return std::dynamic_pointer_cast<Other>(*this);
}
template<typename Other>
requires(!couldBe<Other>() && !requires { std::dynamic_pointer_cast<Other>(std::declval<Primary>()); })
[[nodiscard]] auto
dynamicCast() const
{
return dynamic_cast<Other *>(get());
}
private:
using OtherPtrs = std::tuple<Others *...>;
template<typename Other>
requires(couldBe<Other>())
[[nodiscard]] consteval static bool
idx()
{
size_t typeIdx = 0;
return ((typeIdx++ && std::is_convertible_v<Others *, Other *>) || ...);
}
void
updatePtrs()
{
if (*this) {
others = {dynamic_cast<Others *>(get())...};
}
else {
others = {};
}
}
OtherPtrs others;
};
template<typename Primary, typename... Others> using ManySharedPtr = ManyPtr<std::shared_ptr<Primary>, Others...>;
template<typename Primary, typename... Others> using ManyUniquePtr = ManyPtr<std::unique_ptr<Primary>, Others...>;
|