-
Notifications
You must be signed in to change notification settings - Fork 37
/
wptr.hpp
48 lines (42 loc) · 1.1 KB
/
wptr.hpp
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
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <memory>
namespace kagome {
/**
* `bool operator==(std::weak_ptr<T>, std::weak_ptr<T>)`.
*/
template <typename T>
bool wptrEq(const std::weak_ptr<T> &l, const std::weak_ptr<T> &r) {
return not l.owner_before(r) and not r.owner_before(l);
}
/**
* `wptrEmpty(std::weak_ptr{}) == true`.
* `wptrEmpty(expired) == false`.
*/
template <typename T>
bool wptrEmpty(const std::weak_ptr<T> &w) {
return wptrEq(w, {});
}
/**
* Assert that `std::weak_ptr` is not expired.
*/
template <typename T>
std::shared_ptr<T> wptrLock(const std::weak_ptr<T> &w) {
auto s = w.lock();
[[unlikely]] if (not s and not wptrEmpty(w)) { throw std::bad_weak_ptr{}; }
return s;
}
/**
* Assert that `std::weak_ptr` is neither empty nor expired.
*/
template <typename T>
std::shared_ptr<T> wptrMustLock(const std::weak_ptr<T> &w) {
auto s = w.lock();
[[unlikely]] if (not s) { throw std::bad_weak_ptr{}; }
return s;
}
} // namespace kagome