Skip to content

Commit d92cdb6

Browse files
committed
refactor(*): applying deducing this language feature
1 parent a821b32 commit d92cdb6

File tree

11 files changed

+136
-81
lines changed

11 files changed

+136
-81
lines changed

include/stdsharp/compilation_config_in.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
#ifdef _MSC_VER
1919
#define STDSHARP_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
20-
#define STDSHARP_INTRINSIC [[msvc::intrinsic]]
20+
#define STDSHARP_INTRINSIC [[msvc::intrinsic]] [[nodiscard]]
2121
#else
2222
#define STDSHARP_NO_UNIQUE_ADDRESS [[no_unique_address]]
23-
#define STDSHARP_INTRINSIC STDSHARP_ALWAYS_INLINE
23+
#define STDSHARP_INTRINSIC STDSHARP_ALWAYS_INLINE [[nodiscard]]
2424
#endif
2525

2626
#if defined(_MSC_VER) || (defined(_WIN32) && defined(__clang__))

include/stdsharp/functional/invocables.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ namespace stdsharp::details
2121
using func = std::tuple_element_t<I, indexed_values>;
2222

2323
public:
24-
template<typename Self, typename... Args, typename Fn = cv_ref_align_t<Self&&, func>>
24+
template<typename Self, typename... Args, typename Fn = forward_cast_t<Self, func>>
2525
requires ::std::invocable<Fn, Args...>
2626
constexpr decltype(auto) operator()(this Self&& self, Args&&... args)
2727
noexcept(nothrow_invocable<Fn, Args...>)
2828
{
2929
return invoke(
30-
cpo::get_element<I>( //
31-
static_cast<cv_ref_align_t<Self&&, indexed_values>>(
32-
static_cast<cv_ref_align_t<Self&&, type>>(cpp_forward(self))
33-
)
30+
cpo::get_element<I>(
31+
stdsharp::forward_cast<Self, indexed_operator, type>(self)
3432
),
3533
cpp_forward(args)...
3634
);
@@ -152,12 +150,15 @@ namespace stdsharp
152150

153151
sequenced_invocables() = default;
154152

155-
template<typename Self, typename... Args, typename Base = cv_ref_align_t<Self&&, base>>
153+
template<typename Self, typename... Args, typename Base = forward_cast_t<Self, base>>
156154
requires std::invocable<sequenced_invoke_fn, Base, Args...>
157155
constexpr decltype(auto) operator()(this Self&& self, Args&&... args)
158156
noexcept(nothrow_invocable<sequenced_invoke_fn, Base, Args...>)
159157
{
160-
return sequenced_invoke(static_cast<Base>(self), cpp_forward(args)...);
158+
return sequenced_invoke(
159+
forward_cast<Self, sequenced_invocables, base>(self),
160+
cpp_forward(args)...
161+
);
161162
}
162163
};
163164

@@ -171,12 +172,12 @@ namespace stdsharp
171172

172173
using base::base;
173174

174-
template<typename Self, typename... Args, typename Base = cv_ref_align_t<Self&&, base>>
175+
template<typename Self, typename... Args, typename Base = forward_cast_t<Self, base>>
175176
requires std ::invocable<Base, Args...>
176177
[[nodiscard]] constexpr decltype(auto) operator()(this Self&& self, Args&&... args)
177178
noexcept(nothrow_invocable<Base, Args...>)
178179
{
179-
return static_cast<Base>(self)(cpp_forward(args)...);
180+
return forward_cast<Self, nodiscard_invocable, base>(self)(cpp_forward(args)...);
180181
}
181182
};
182183

include/stdsharp/lazy.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#include <memory>
44
#include <utility>
55

6-
#include "concepts/concepts.h"
7-
#include "stdsharp/type_traits/core_traits.h"
6+
#include "utility/forward_cast.h"
87

98
namespace stdsharp
109
{
@@ -161,25 +160,19 @@ namespace stdsharp
161160
[[nodiscard]] constexpr bool has_value() const noexcept { return has_value_; }
162161

163162
template<typename Self>
164-
constexpr decltype(auto) get(this Self&& self) noexcept( //
165-
noexcept( //
166-
generate_value(static_cast<cv_ref_align_t<Self&&, lazy_value>>(cpp_forward(self)))
167-
)
168-
)
169-
requires requires {
170-
generate_value(static_cast<cv_ref_align_t<Self&&, lazy_value>>(cpp_forward(self)));
171-
}
163+
constexpr decltype(auto) get(this Self&& self)
164+
noexcept(noexcept(generate_value(forward_cast<Self, lazy_value>(self))))
165+
requires requires { generate_value(forward_cast<Self, lazy_value>(self)); }
172166
{
173-
auto&& this_ = static_cast<cv_ref_align_t<Self&&, lazy_value>>(cpp_forward(self));
167+
auto&& this_ = forward_cast<Self, lazy_value>(self);
174168
generate_value(cpp_forward(this_));
175169
return cpp_forward(this_).value_;
176170
}
177171

178172
template<typename Self>
179-
constexpr decltype(auto) cget(this const Self& self) noexcept
173+
constexpr decltype(auto) cget(this const Self&& self) noexcept
180174
{
181-
auto&& this_ = static_cast<cv_ref_align_t<const Self&, lazy_value>>(cpp_forward(self));
182-
return cpp_forward(this_).value_;
175+
return forward_cast<const Self, lazy_value>(self).value_;
183176
}
184177
}; // NOLINTEND(*-noexcept-*)
185178

include/stdsharp/synchronizer.h

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "mutex/mutex.h"
66
#include "reflection/reflection.h"
7-
#include "stdsharp/type_traits/core_traits.h"
7+
#include "utility/constructor.h"
88

99
namespace stdsharp
1010
{
@@ -58,6 +58,9 @@ namespace stdsharp
5858
{
5959
}
6060

61+
using default_get_shared_lock_fn = constructor<std::shared_lock<lock_type>>;
62+
using default_get_unique_lock_fn = constructor<std::unique_lock<lock_type>>;
63+
6164
public:
6265
template<typename ValueTuple, typename LockableTuple>
6366
requires piecewise_constructible_from<value_type, ValueTuple> &&
@@ -73,39 +76,35 @@ namespace stdsharp
7376
std::make_index_sequence<std::tuple_size_v<std::remove_cvref_t<ValueTuple>>>{},
7477
std::make_index_sequence<std::tuple_size_v<std::remove_cvref_t<LockableTuple>>>{}
7578
)
79+
7680
{
7781
}
7882

79-
template<typename Self, typename... Args, typename SelfT = const Self&>
80-
constexpr void read(
81-
this const Self& self,
82-
std ::invocable<const cv_ref_align_t<SelfT, value_type>> auto&& func,
83-
Args&&... args
84-
)
85-
requires std ::constructible_from<std ::shared_lock<lock_type>, lock_type&, Args...>
83+
template<
84+
typename Self,
85+
typename Fn,
86+
std::invocable<lock_type> GetLock = default_get_shared_lock_fn,
87+
typename SelfT = const Self>
88+
requires std ::invocable<Fn&, forward_cast_t<SelfT, value_type>> &&
89+
lockable<std::invoke_result_t<GetLock>>
90+
constexpr void read(this const Self&& self, Fn func, GetLock get_lock = {})
8691
{
87-
auto&& this_ = static_cast<cv_ref_align_t<SelfT, synchronizer>>(cpp_forward(self));
88-
const std ::shared_lock lock{
89-
cpp_forward(this_).lockable_,
90-
static_cast<decltype(args)>(args)...
91-
};
92-
invoke(cpp_forward(func), cpp_forward(this_).value_);
92+
auto&& this_ = forward_cast<SelfT, synchronizer>(self);
93+
const auto& lock = get_lock(cpp_forward(this_).lockable_);
94+
invoke(func, cpp_forward(this_).value_);
9395
}
9496

95-
template<typename Self, typename... Args>
96-
constexpr void write(
97-
this Self&& self,
98-
std ::invocable<cv_ref_align_t<Self&&, value_type>> auto&& func,
99-
Args&&... args
100-
)
101-
requires std ::constructible_from<std ::unique_lock<lock_type>, lock_type&, Args...>
97+
template<
98+
typename Self,
99+
typename Fn,
100+
std::invocable<lock_type> GetLock = default_get_unique_lock_fn>
101+
requires std::invocable<Fn&, forward_cast_t<Self, value_type>> &&
102+
lockable<std::invoke_result_t<GetLock>>
103+
constexpr void write(this Self&& self, Fn func, GetLock get_lock = {})
102104
{
103-
auto&& this_ = static_cast<cv_ref_align_t<Self&&, synchronizer>>(cpp_forward(self));
104-
const std ::unique_lock lock{
105-
cpp_forward(this_).lockable_,
106-
static_cast<decltype(args)>(args)...
107-
};
108-
invoke(cpp_forward(func), cpp_forward(this_).value_);
105+
auto&& this_ = forward_cast<Self, synchronizer>(self);
106+
const auto& lock = get_lock(cpp_forward(this_).lockable_);
107+
invoke(func, cpp_forward(this_).value_);
109108
}
110109

111110
constexpr const lock_type& lockable() const noexcept { return lockable_; }

include/stdsharp/type_traits/indexed_traits.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,20 @@ namespace stdsharp::details
134134
}
135135

136136
private:
137-
template<std::size_t Index>
138-
using indexed_value_type = std::tuple_element_t<Index, indexed_types>;
137+
template<std::size_t J>
138+
using indexed_value_t = indexed_value<std::tuple_element_t<J, indexed_types>, J>;
139139

140140
public:
141-
template<std ::size_t Index, typename Self>
141+
template<std::size_t J, typename Self>
142142
[[nodiscard]] constexpr decltype(auto) get(this Self&& self) noexcept
143143
{
144-
using indexed = indexed_value<indexed_value_type<Index>, Index>;
145-
return static_cast<cv_ref_align_t<Self&&, indexed>>(
146-
static_cast<cv_ref_align_t<Self&&, impl>>(cpp_forward(self))
147-
)
148-
.get();
144+
return forward_cast<Self, indexed_value_t<J>>(self).get();
145+
}
146+
147+
template<std::size_t J, typename Self>
148+
[[nodiscard]] constexpr decltype(auto) cget(this Self&& self) noexcept
149+
{
150+
return forward_cast<Self, indexed_value_t<J>>(self).cget();
149151
}
150152
};
151153
};

include/stdsharp/utility/cast_to.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,6 @@ namespace stdsharp
1919

2020
template<typename U>
2121
inline constexpr cast_to_fn<U> cast_to{};
22-
23-
template<typename T>
24-
struct cast_fwd_fn
25-
{
26-
template<typename U>
27-
requires explicitly_convertible<U, cv_ref_align_t<U&&, T>>
28-
STDSHARP_INTRINSIC constexpr decltype(auto) operator()(U&& u) const
29-
noexcept(nothrow_explicitly_convertible<U, cv_ref_align_t<U&&, T>>)
30-
{
31-
return static_cast<cv_ref_align_t<U&&, T>>(cpp_forward(u));
32-
}
33-
};
34-
35-
template<typename T>
36-
inline constexpr auto cast_fwd = cast_fwd_fn<T>{};
3722
}
3823

3924
#include "../compilation_config_out.h"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#include "../type_traits/core_traits.h"
4+
#include "../concepts/concepts.h"
5+
6+
#include "../compilation_config_in.h"
7+
8+
namespace stdsharp
9+
{
10+
template<typename From, typename To>
11+
using forward_cast_t = cv_ref_align_t<From&&, To>;
12+
}
13+
14+
namespace stdsharp
15+
{
16+
template<typename...>
17+
struct forward_cast_fn;
18+
19+
template<typename From, typename To>
20+
struct forward_cast_fn<From, To>
21+
{
22+
using from_t = std::remove_cvref_t<From>;
23+
using to_t = std::remove_cvref_t<To>;
24+
25+
[[nodiscard]] constexpr decltype(auto) operator()(From&& from) const noexcept
26+
requires std::derived_from<from_t, to_t> && not_same_as<from_t, to_t>
27+
{ // c-style cast allow us cast derived to inaccessible base
28+
return (forward_cast_t<From, To>)cpp_forward(from); // NOLINT
29+
}
30+
31+
[[nodiscard]] constexpr decltype(auto) operator()(From&& from) const noexcept
32+
requires std::same_as<from_t, to_t>
33+
{
34+
return static_cast<forward_cast_t<From, To>>(cpp_forward(from));
35+
}
36+
};
37+
38+
template<typename From, typename To, typename... Rest>
39+
struct forward_cast_fn<From, To, Rest...>
40+
{
41+
private:
42+
using cur_fn = forward_cast_fn<From, To>;
43+
using next_fn = forward_cast_fn<To, Rest...>;
44+
45+
public:
46+
[[nodiscard]] constexpr decltype(auto) operator()(From&& from) const noexcept
47+
requires requires { next_fn{}(cur_fn{}(from)); }
48+
{
49+
return next_fn{}(cur_fn{}(from));
50+
}
51+
};
52+
53+
template<typename... T>
54+
inline constexpr forward_cast_fn<T...> forward_cast{};
55+
}
56+
57+
#include "../compilation_config_out.h"

include/stdsharp/utility/to_lvalue.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
22

3-
#include "../macros.h"
43
#include "../compilation_config_in.h"
54
#include "../concepts/concepts.h"
65

include/stdsharp/utility/utility.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "cast_to.h"
66
#include "constructor.h"
77
#include "to_lvalue.h"
8+
#include "forward_cast.h"
89

910
#include "../compilation_config_in.h"
1011

include/stdsharp/utility/value_wrapper.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include <utility>
44

5-
#include "../concepts/concepts.h"
5+
#include "forward_cast.h"
6+
67
#include "../compilation_config_in.h"
7-
#include "stdsharp/type_traits/core_traits.h"
88

99
namespace stdsharp::details
1010
{
@@ -40,13 +40,19 @@ namespace stdsharp
4040
template<typename Self>
4141
[[nodiscard]] constexpr decltype(auto) get(this Self&& self) noexcept
4242
{
43-
return static_cast<cv_ref_align_t<Self&&,value_wrapper>>(cpp_forward(self)).v;
43+
return static_cast<forward_cast_t<Self, T>>(forward_cast<Self, value_wrapper>(self).v);
4444
}
4545

4646
template<typename Self>
47-
[[nodiscard]] constexpr explicit operator cv_ref_align_t<Self&&, T>(this Self&& self) noexcept
47+
[[nodiscard]] constexpr decltype(auto) cget(this const Self&& self) noexcept
48+
{
49+
return static_cast<forward_cast_t<const Self, T>>(forward_cast<const Self, value_wrapper>(self).v);
50+
}
51+
52+
template<std::derived_from<value_wrapper> Self>
53+
[[nodiscard]] constexpr explicit operator forward_cast_t<Self, T>(this Self&& self) noexcept
4854
{
49-
return static_cast<cv_ref_align_t<Self&&,value_wrapper>>(cpp_forward(self)).get();
55+
return forward_cast<value_wrapper, Self>(self).get();
5056
}
5157
};
5258

0 commit comments

Comments
 (0)