Skip to content

Commit c701e23

Browse files
fix: Add consteval-friendly path
1 parent 99ae1dc commit c701e23

1 file changed

Lines changed: 54 additions & 14 deletions

File tree

src/primitive/impl.cppm

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ struct resolve_concurrency_policy<First, Rest...> {
4343
};
4444

4545
template <policy::policy_type... Policies>
46-
using resolve_concurrency_policy_t =
47-
typename resolve_concurrency_policy<Policies...>::type;
46+
using resolve_concurrency_policy_t = resolve_concurrency_policy<Policies...>::type;
4847

4948
} // namespace details
5049

@@ -58,44 +57,85 @@ public:
5857
"Multiple concurrency policies are not allowed");
5958

6059
constexpr explicit primitive(value_type v) noexcept : value_(v) {}
61-
primitive(primitive const &other) noexcept : value_(other.load()) {}
62-
auto operator=(primitive const &other) noexcept -> primitive & {
60+
61+
constexpr primitive(primitive const &other) noexcept {
62+
if consteval {
63+
value_ = other.value_;
64+
} else {
65+
value_ = other.load();
66+
}
67+
}
68+
69+
constexpr auto operator=(primitive const &other) noexcept -> primitive & {
6370
if (this == &other) {
6471
return *this;
6572
}
6673

67-
store(other.load());
74+
if consteval {
75+
value_ = other.value_;
76+
} else {
77+
store(other.load());
78+
}
6879
return *this;
6980
}
7081

71-
primitive(primitive &&other) noexcept : value_(other.load()) {}
72-
auto operator=(primitive &&other) noexcept -> primitive & {
82+
constexpr primitive(primitive &&other) noexcept {
83+
if consteval {
84+
value_ = other.value_;
85+
} else {
86+
value_ = other.load();
87+
}
88+
}
89+
90+
constexpr auto operator=(primitive &&other) noexcept -> primitive & {
7391
if (this == &other) {
7492
return *this;
7593
}
7694

77-
store(other.load());
95+
if consteval {
96+
value_ = other.value_;
97+
} else {
98+
store(other.load());
99+
}
78100
return *this;
79101
}
80102

81103
constexpr value_type &value() noexcept { return value_; }
104+
82105
[[nodiscard]] constexpr value_type const &value() const noexcept {
83106
return value_;
84107
}
108+
85109
constexpr explicit operator value_type() const noexcept { return value_; }
86110

87-
[[nodiscard]] auto load() const noexcept -> value_type {
111+
[[nodiscard]] constexpr auto load() const noexcept -> value_type {
112+
if consteval {
113+
return value_;
114+
}
88115
require_access_handler_();
89116
return access_handler_t::load(value_);
90117
}
91118

92-
auto store(value_type desired) noexcept -> void {
93-
require_access_handler_();
94-
access_handler_t::store(value_, desired);
119+
constexpr auto store(value_type desired) noexcept -> void {
120+
if consteval {
121+
value_ = desired;
122+
} else {
123+
require_access_handler_();
124+
access_handler_t::store(value_, desired);
125+
}
95126
}
96127

97-
auto compare_exchange(value_type &expected, value_type desired) noexcept
98-
-> bool {
128+
constexpr auto compare_exchange(value_type &expected,
129+
value_type desired) noexcept -> bool {
130+
if consteval {
131+
if (value_ != expected) {
132+
expected = value_;
133+
return false;
134+
}
135+
136+
value_ = desired;
137+
return true;
138+
}
99139
require_access_handler_();
100140
return access_handler_t::compare_exchange(value_, expected, desired);
101141
}

0 commit comments

Comments
 (0)