Skip to content

Commit 286a44f

Browse files
feat: Add copy constructor/assignment operator and move constructor/assignment operator for primitive
1 parent a38c425 commit 286a44f

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

src/primitive/impl.cppm

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,50 +58,61 @@ public:
5858
"Multiple concurrency policies are not allowed");
5959

6060
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 & {
63+
if (this == &other) {
64+
return *this;
65+
}
66+
67+
store(other.load());
68+
return *this;
69+
}
70+
71+
primitive(primitive &&other) noexcept : value_(other.load()) {}
72+
auto operator=(primitive &&other) noexcept -> primitive & {
73+
if (this == &other) {
74+
return *this;
75+
}
76+
77+
store(other.load());
78+
return *this;
79+
}
80+
6181
constexpr value_type &value() noexcept { return value_; }
6282
[[nodiscard]] constexpr value_type const &value() const noexcept {
6383
return value_;
6484
}
6585
constexpr explicit operator value_type() const noexcept { return value_; }
6686

6787
[[nodiscard]] auto load() const noexcept -> value_type {
68-
using access_handler_t =
69-
policy::concurrency::handler<concurrency_policy, void, value_type,
70-
policy::error::kind>;
71-
static_assert(
72-
policy::concurrency::handler_access_available<concurrency_policy,
73-
value_type>,
74-
"Selected concurrency policy does not provide primitive "
75-
"load/store/CAS support");
88+
require_access_handler_();
7689
return access_handler_t::load(value_);
7790
}
7891

7992
auto store(value_type desired) noexcept -> void {
80-
using access_handler_t =
81-
policy::concurrency::handler<concurrency_policy, void, value_type,
82-
policy::error::kind>;
83-
static_assert(
84-
policy::concurrency::handler_access_available<concurrency_policy,
85-
value_type>,
86-
"Selected concurrency policy does not provide primitive "
87-
"load/store/CAS support");
93+
require_access_handler_();
8894
access_handler_t::store(value_, desired);
8995
}
9096

9197
auto compare_exchange(value_type &expected, value_type desired) noexcept
9298
-> bool {
93-
using access_handler_t =
94-
policy::concurrency::handler<concurrency_policy, void, value_type,
95-
policy::error::kind>;
99+
require_access_handler_();
100+
return access_handler_t::compare_exchange(value_, expected, desired);
101+
}
102+
103+
private:
104+
using access_handler_t =
105+
policy::concurrency::handler<concurrency_policy, void, value_type,
106+
policy::error::kind>;
107+
108+
static constexpr auto require_access_handler_() noexcept -> void {
96109
static_assert(
97110
policy::concurrency::handler_access_available<concurrency_policy,
98111
value_type>,
99112
"Selected concurrency policy does not provide primitive "
100113
"load/store/CAS support");
101-
return access_handler_t::compare_exchange(value_, expected, desired);
102114
}
103115

104-
private:
105116
value_type value_;
106117
};
107118

0 commit comments

Comments
 (0)