Skip to content

Commit d5f29fb

Browse files
committed
feat(synchronizer): rename concurrent object to
synchronizer
1 parent a676edb commit d5f29fb

File tree

5 files changed

+85
-95
lines changed

5 files changed

+85
-95
lines changed

include/stdsharp/namespace_alias.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ namespace std
66

77
namespace stdsharp
88
{
9-
namespace std = std;
9+
namespace std = ::std;
1010
}

include/stdsharp/concurrent_object.h renamed to include/stdsharp/synchronizer.h

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace stdsharp
1212
{
1313
template<typename T, shared_lockable Lockable = std::shared_mutex>
1414
requires basic_lockable<Lockable>
15-
class concurrent_object
15+
class synchronizer
1616
{
1717
public:
1818
using value_type = std::optional<T>;
@@ -22,7 +22,7 @@ namespace stdsharp
2222
requires copy_assignable<value_type>
2323
static constexpr void assign_value(
2424
value_type& object, //
25-
const concurrent_object<T, OtherLockable>& other //
25+
const synchronizer<T, OtherLockable>& other //
2626
)
2727
{
2828
other.read([&object](const value_type& obj) { object = obj; });
@@ -31,7 +31,7 @@ namespace stdsharp
3131
template<typename OtherLockable>
3232
requires move_assignable<value_type>
3333
static constexpr void
34-
assign_value(value_type& object, concurrent_object<T, OtherLockable>&& other)
34+
assign_value(value_type& object, synchronizer<T, OtherLockable>&& other)
3535
{
3636
cpp_move(other).write([&object](value_type&& obj) { object = cpp_move(obj); });
3737
}
@@ -40,19 +40,19 @@ namespace stdsharp
4040
static constexpr bool other_assignable =
4141
requires(value_type obj) { assign_value(obj, std::declval<Other>()); };
4242

43-
static constexpr bool copy_assignable = other_assignable<const concurrent_object&>;
44-
static constexpr bool move_assignable = other_assignable<concurrent_object>;
43+
static constexpr bool copy_assignable = other_assignable<const synchronizer&>;
44+
static constexpr bool move_assignable = other_assignable<synchronizer>;
4545

4646
template<typename Other>
4747
requires(other_assignable<Other>)
48-
constexpr concurrent_object(const empty_t, Other&& other)
48+
constexpr synchronizer(const empty_t, Other&& other)
4949
{
5050
assign_value(object_, cpp_forward(other));
5151
}
5252

5353
template<typename Other>
5454
requires(other_assignable<Other>)
55-
constexpr concurrent_object& assign_impl(Other&& other)
55+
constexpr synchronizer& assign_impl(Other&& other)
5656
{
5757
write([&other](value_type& obj) { assign_value(obj, cpp_forward(other)); });
5858
return *this;
@@ -71,13 +71,13 @@ namespace stdsharp
7171
struct write_fn
7272
{
7373
template<typename Func>
74-
constexpr void operator()(concurrent_object&& instance, Func&& func) const
74+
constexpr void operator()(synchronizer&& instance, Func&& func) const
7575
{
7676
std::invoke(cpp_forward(func), cpp_move(instance).object_);
7777
}
7878

7979
template<typename Func>
80-
constexpr void operator()(concurrent_object& instance, Func&& func) const
80+
constexpr void operator()(synchronizer& instance, Func&& func) const
8181
{
8282
const std::unique_lock lock{instance.lockable()};
8383
std::invoke(cpp_forward(func), instance.object_);
@@ -87,18 +87,18 @@ namespace stdsharp
8787
public:
8888
using lock_type = Lockable;
8989

90-
concurrent_object() = default;
90+
synchronizer() = default;
9191

9292
template<typename... Args>
9393
requires std::constructible_from<T, Args...> && std::default_initializable<Lockable>
94-
explicit constexpr concurrent_object(Args&&... t_arg): object_(cpp_forward(t_arg)...)
94+
explicit constexpr synchronizer(Args&&... t_arg): object_(cpp_forward(t_arg)...)
9595
{
9696
}
9797

9898
template<typename... TArgs, typename... LockArgs>
9999
requires std::constructible_from<T, TArgs...> &&
100100
std::constructible_from<Lockable, LockArgs...>
101-
explicit constexpr concurrent_object(
101+
explicit constexpr synchronizer(
102102
const std::piecewise_construct_t tag,
103103
std::tuple<TArgs...> t_arg,
104104
std::tuple<LockArgs...> lock_arg
@@ -109,48 +109,48 @@ namespace stdsharp
109109

110110
template<typename Other>
111111
requires(other_assignable<Other>)
112-
constexpr concurrent_object(Other&& other): concurrent_object(empty, cpp_forward(other))
112+
constexpr synchronizer(Other&& other): synchronizer(empty, cpp_forward(other))
113113
{
114114
}
115115

116-
constexpr concurrent_object(const concurrent_object& other)
116+
constexpr synchronizer(const synchronizer& other)
117117
requires copy_assignable
118-
: concurrent_object(empty, other)
118+
: synchronizer(empty, other)
119119
{
120120
}
121121

122-
constexpr concurrent_object(concurrent_object&& other) noexcept(false)
122+
constexpr synchronizer(synchronizer&& other) noexcept(false)
123123
requires move_assignable
124-
: concurrent_object(empty, cpp_move(other))
124+
: synchronizer(empty, cpp_move(other))
125125
{
126126
}
127127

128128
template<typename Other>
129129
requires(other_assignable<Other>)
130-
constexpr concurrent_object& operator=(Other&& other)
130+
constexpr synchronizer& operator=(Other&& other)
131131
{
132132
assign_impl(cpp_forward(other));
133133
return *this;
134134
}
135135

136-
constexpr concurrent_object& operator=(const concurrent_object& other)
136+
constexpr synchronizer& operator=(const synchronizer& other)
137137
requires copy_assignable
138138
{
139139
if(this != &other) assign_impl(other);
140140
return *this;
141141
}
142142

143-
constexpr concurrent_object& operator=(concurrent_object&& other) noexcept(false)
143+
constexpr synchronizer& operator=(synchronizer&& other) noexcept(false)
144144
requires move_assignable
145145
{
146146
if(this != &other) assign_impl(cpp_move(other));
147147
return *this;
148148
}
149149

150-
~concurrent_object() = default;
150+
~synchronizer() = default;
151151

152152
template<typename OtherLockable>
153-
constexpr void swap(concurrent_object<T, OtherLockable>& other) //
153+
constexpr void swap(synchronizer<T, OtherLockable>& other) //
154154
requires std::swappable_with<
155155
value_type,
156156
typename std::remove_reference_t<decltype(other)>::value_type // clang-format off
@@ -171,7 +171,7 @@ namespace stdsharp
171171
template<std::invocable<const value_type> Func>
172172
constexpr void read(Func&& func) const&&
173173
{
174-
read_fn{}(static_cast<const concurrent_object&&>(*this), cpp_forward(func));
174+
read_fn{}(static_cast<const synchronizer&&>(*this), cpp_forward(func));
175175
}
176176

177177
template<std::invocable<value_type&> Func>
@@ -196,40 +196,32 @@ namespace stdsharp
196196
};
197197

198198
template<typename T>
199-
concurrent_object(T&&) -> concurrent_object<std::decay_t<T>>;
199+
synchronizer(T&&) -> synchronizer<std::decay_t<T>>;
200200

201201
namespace reflection
202202
{
203203
template<typename T, typename U>
204-
inline constexpr auto function<concurrent_object<T, U>> =
205-
member<concurrent_object<T, U>>::template func_reflect<"read"_ltr, "write"_ltr>(
204+
inline constexpr auto function<synchronizer<T, U>> =
205+
member<synchronizer<T, U>>::template func_reflect<"read"_ltr, "write"_ltr>(
206206
[](auto&& v, auto&&... args) //
207207
noexcept( //
208-
noexcept(
209-
cast_fwd<concurrent_object<T, U>>(cpp_forward(v)).read(cpp_forward(args)...)
208+
noexcept(cast_fwd<synchronizer<T, U>>(cpp_forward(v)).read(cpp_forward(args)...)
210209
)
211210
) -> //
212211
decltype( //
213-
cast_fwd<concurrent_object<T, U>>(cpp_forward(v)).read(cpp_forward(args)...)
212+
cast_fwd<synchronizer<T, U>>(cpp_forward(v)).read(cpp_forward(args)...)
214213
) //
215-
{
216-
return cast_fwd<concurrent_object<T, U>>(cpp_forward(v))
217-
.read(cpp_forward(args)...);
218-
},
214+
{ return cast_fwd<synchronizer<T, U>>(cpp_forward(v)).read(cpp_forward(args)...); },
219215
[](auto&& v, auto&&... args) //
220216
noexcept( //
221217
noexcept( //
222-
cast_fwd<concurrent_object<T, U>>(cpp_forward(v))
223-
.write(cpp_forward(args)...)
218+
cast_fwd<synchronizer<T, U>>(cpp_forward(v)).write(cpp_forward(args)...)
224219
)
225220
) -> //
226221
decltype( //
227-
cast_fwd<concurrent_object<T, U>>(cpp_forward(v)).write(cpp_forward(args)...)
222+
cast_fwd<synchronizer<T, U>>(cpp_forward(v)).write(cpp_forward(args)...)
228223
) //
229-
{
230-
return cast_fwd<concurrent_object<T, U>>(cpp_forward(v))
231-
.write(cpp_forward(args)...);
232-
}
224+
{ return cast_fwd<synchronizer<T, U>>(cpp_forward(v)).write(cpp_forward(args)...); }
233225
);
234226
}
235227
}

tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ set(src
2626
src/type_traits/type_sequence_test.cpp
2727
src/type_traits/value_sequence_test.cpp
2828
src/utility/utility_test.cpp
29-
src/concurrent_object_test.cpp
30-
src/pattern_match_test.cpp)
29+
src/pattern_match_test.cpp
30+
src/synchronizer_test.cpp)
3131

3232
config_lib(${PROJECT_NAME}Lib INTERFACE)
3333

tests/src/concurrent_object_test.cpp

Lines changed: 0 additions & 52 deletions
This file was deleted.

tests/src/synchronizer_test.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "test.h"
2+
#include "stdsharp/synchronizer.h"
3+
4+
using namespace std;
5+
using namespace fmt;
6+
using namespace stdsharp;
7+
8+
SCENARIO("synchronizer", "[synchronizer]") // NOLINT
9+
{
10+
struct my_mutex
11+
{
12+
static constexpr void lock() {}
13+
14+
static constexpr void unlock() {}
15+
16+
static constexpr void lock_shared() {}
17+
18+
static constexpr bool try_lock_shared() { return true; }
19+
20+
static constexpr void unlock_shared() {}
21+
};
22+
23+
STATIC_REQUIRE(copyable<synchronizer<int>>);
24+
STATIC_REQUIRE(movable<synchronizer<int>>);
25+
26+
STATIC_REQUIRE(default_initializable<synchronizer<int>>);
27+
STATIC_REQUIRE(constructible_from<synchronizer<int>, synchronizer<int, my_mutex>>);
28+
STATIC_REQUIRE(constructible_from<synchronizer<int>, const synchronizer<int, my_mutex>&>);
29+
30+
STATIC_REQUIRE(assignable<synchronizer<int>&, synchronizer<int, my_mutex>>);
31+
STATIC_REQUIRE(assignable<synchronizer<int>&, const synchronizer<int, my_mutex>&>);
32+
}
33+
34+
SCENARIO("synchronizer reflection support", "[synchronizer]") // NOLINT
35+
{
36+
using namespace reflection;
37+
using namespace stdsharp::literals;
38+
39+
using concurrent_object_t = synchronizer<int>;
40+
41+
using function = reflection::function_t<concurrent_object_t>;
42+
43+
STATIC_REQUIRE( //
44+
invocable<
45+
function::member_of_t<to_array("read")>,
46+
concurrent_object_t,
47+
void(const ::std::optional<int>) // clang-format off
48+
> // clang-format on
49+
);
50+
}

0 commit comments

Comments
 (0)