userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
struct_subsets.hpp
Go to the documentation of this file.
1
#pragma once
2
6
7
#include <type_traits>
8
#include <utility>
9
10
#include <
userver/utils/forward_like.hpp
>
11
#include <userver/utils/impl/for_each_field.hpp>
12
#include <userver/utils/impl/internal_tag.hpp>
13
14
USERVER_NAMESPACE_BEGIN
15
16
namespace
utils::impl {
17
18
struct
RequireSemicolon;
19
20
struct
NonMovable final {
21
constexpr
explicit
NonMovable(InternalTag)
noexcept
{}
22
};
23
24
template
<
typename
T>
25
constexpr
auto
IsDefinedAndAggregate() ->
decltype
(
static_cast<
void
>
(
sizeof
(T)),
false
) {
26
return
std::is_aggregate_v<T>;
27
}
28
29
template
<
typename
/*T*/
,
typename
... Args>
30
constexpr
auto
IsDefinedAndAggregate(Args...) ->
bool
{
31
return
false
;
32
}
33
34
template
<
typename
TOwner,
typename
TMember>
35
USERVER_IMPL_NODEBUG_INLINE_FUNC
decltype
(
auto
) ForwardLikeExplicit(TMember& member)
noexcept
{
36
if
constexpr
(std::is_lvalue_reference_v<TOwner> || std::is_lvalue_reference_v<TMember>) {
37
return
member;
38
}
else
{
39
return
std::move(member);
40
}
41
}
42
43
template
<
typename
TOwner,
typename
TMember>
44
USERVER_IMPL_NODEBUG_INLINE_FUNC
decltype
(
auto
) ForwardLikeExplicit(
const
TMember& member)
noexcept
{
45
return
member;
46
}
47
48
}
// namespace utils::impl
49
50
USERVER_NAMESPACE_END
51
53
54
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
55
#define USERVER_IMPL_STRUCT_MAP(source, elem) \
56
USERVER_NAMESPACE::utils::impl::ForwardLikeExplicit<OtherDeps, decltype(source.elem)>(source.elem),
57
58
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
59
#define USERVER_IMPL_MAKE_FROM_SUPERSET(Self, ...) \
60
template <typename OtherDeps> \
61
static Self MakeFromSupersetImpl(OtherDeps&& other, USERVER_NAMESPACE::utils::impl::InternalTag) { \
62
return {USERVER_IMPL_FOR_EACH_FIELD(USERVER_IMPL_STRUCT_MAP, other, __VA_ARGS__)}; \
63
}
64
65
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
66
#define USERVER_IMPL_STRUCT_SUBSET_MAP(data, elem) \
67
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */
\
68
decltype(data::elem) elem;
69
70
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
71
#define USERVER_IMPL_STRUCT_SUBSET_REF_MAP(data, elem) \
72
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */
\
73
std::add_const_t<decltype(data::elem)>& elem;
74
76
82
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
83
#define USERVER_ALLOW_CONVERSIONS_TO_SUBSET() \
84
template <typename Other> \
85
requires(USERVER_NAMESPACE::utils::impl::IsDefinedAndAggregate<Other>())
/*implicit*/
operator Other() const& { \
86
return Other::MakeFromSupersetImpl(*this, USERVER_NAMESPACE::utils::impl::InternalTag{}); \
87
} \
88
\
89
template <typename Other> \
90
requires(USERVER_NAMESPACE::utils::impl::IsDefinedAndAggregate<Other>())
/*implicit*/
operator Other()&& { \
91
return Other::MakeFromSupersetImpl(std::move(*this), USERVER_NAMESPACE::utils::impl::InternalTag{}); \
92
} \
93
\
94
friend struct USERVER_NAMESPACE::utils::impl::RequireSemicolon
95
115
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
116
#define USERVER_DEFINE_STRUCT_SUBSET(SubsetStruct, OriginalStruct, ...) \
117
struct SubsetStruct { \
118
USERVER_IMPL_FOR_EACH_FIELD(USERVER_IMPL_STRUCT_SUBSET_MAP, OriginalStruct, __VA_ARGS__) \
119
\
120
USERVER_IMPL_MAKE_FROM_SUPERSET(SubsetStruct, __VA_ARGS__) \
121
\
122
USERVER_ALLOW_CONVERSIONS_TO_SUBSET(); \
123
}
124
150
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
151
#define USERVER_DEFINE_STRUCT_SUBSET_REF(SubsetStructRef, OriginalStruct, ...) \
152
struct SubsetStructRef { \
153
USERVER_IMPL_FOR_EACH_FIELD(USERVER_IMPL_STRUCT_SUBSET_REF_MAP, OriginalStruct, __VA_ARGS__) \
154
\
155
/* Protects against copying into async functions */
\
156
USERVER_NAMESPACE::utils::impl::NonMovable _impl_non_movable{USERVER_NAMESPACE::utils::impl::InternalTag{}}; \
157
\
158
USERVER_IMPL_MAKE_FROM_SUPERSET(SubsetStructRef, __VA_ARGS__) \
159
\
160
USERVER_ALLOW_CONVERSIONS_TO_SUBSET(); \
161
}
userver
utils
struct_subsets.hpp
Generated on
for userver by
Doxygen
1.17.0