-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbox.cc
More file actions
156 lines (135 loc) · 3.41 KB
/
box.cc
File metadata and controls
156 lines (135 loc) · 3.41 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <utility>
template <typename T>
class Box
{
public:
using element_type = T;
constexpr Box() noexcept : m_ptr{nullptr} {}
constexpr explicit Box(T *ptr) noexcept : m_ptr{ptr} {}
~Box() { delete m_ptr; }
Box(const Box &other) : m_ptr{other.m_ptr->clone()} {}
Box(Box &&other) noexcept : m_ptr{other.release()} {}
template <typename U,
typename = std::enable_if_t<std::is_convertible<U *, T *>::value>>
Box(Box<U> other) noexcept : m_ptr{other.release()}
{
}
Box &operator=(Box other) noexcept
{
swap(other);
return *this;
}
T &operator*() const noexcept { return *m_ptr; }
T *operator->() const noexcept { return m_ptr; }
operator bool() const noexcept { return m_ptr != nullptr; }
T *get() const noexcept { return m_ptr; }
T *release() noexcept { return std::exchange(m_ptr, nullptr); }
void swap(Box &other) noexcept { std::swap(m_ptr, other.m_ptr); }
Box *clone() const { return new Box{*this}; }
private:
T *m_ptr;
};
template <typename T>
struct std::hash<Box<T>>
{
std::size_t operator()(const Box<T> &box) const noexcept
{
return std::hash<T *>{}(box.get());
}
};
template <typename T>
bool operator==(const Box<T> &a, std::nullptr_t) noexcept
{
return a.get() == nullptr;
}
template <typename T>
bool operator!=(const Box<T> &a, std::nullptr_t) noexcept
{
return a.get() != nullptr;
}
template <typename T>
bool operator==(std::nullptr_t, const Box<T> &b) noexcept
{
return nullptr == b.get();
}
template <typename T>
bool operator!=(std::nullptr_t, const Box<T> &b) noexcept
{
return nullptr != b.get();
}
template <typename T>
bool operator==(const Box<T> &a, const Box<T> &b) noexcept
{
return a.get() == b.get();
}
template <typename T>
bool operator!=(const Box<T> &a, const Box<T> &b) noexcept
{
return a.get() != b.get();
}
template <typename T>
bool operator<(const Box<T> &a, const Box<T> &b) noexcept
{
return a.get() < b.get();
}
template <typename T>
bool operator>(const Box<T> &a, const Box<T> &b) noexcept
{
return a.get() > b.get();
}
template <typename T>
bool operator<=(const Box<T> &a, const Box<T> &b) noexcept
{
return a.get() <= b.get();
}
template <typename T>
bool operator>=(const Box<T> &a, const Box<T> &b) noexcept
{
return a.get() >= b.get();
}
template <typename CharT, typename Traits, typename T>
std::basic_ostream<CharT, Traits> &
operator<<(std::basic_ostream<CharT, Traits> &os, const Box<T> &box) noexcept
{
return os << box.get();
}
template <typename T>
void swap(Box<T> &a, Box<T> &b) noexcept
{
a.swap(b);
}
template <typename T, typename... Args>
Box<T> make_box(Args &&... args)
{
return Box<T>{new T{std::forward<Args>(args)...}};
}
// --- example ---
#include <string>
#include <unordered_set>
class Base
{
public:
virtual ~Base() {}
virtual Base *clone() const = 0;
virtual void print() const = 0;
};
class Derived : public Base
{
public:
explicit Derived(std::string val) : m_val{std::move(val)} {}
Derived *clone() const override { return new Derived{*this}; }
void print() const override { std::cout << m_val << '\n'; }
private:
std::string m_val;
};
int main()
{
Box<Base> p{make_box<Derived>("hello")};
auto copy = p;
std::cout << copy << '\n';
swap(p, copy);
std::unordered_set<Box<Base>> s{p};
for (const auto &ptr : s)
ptr->print();
}