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
3
/// @file userver/utils/struct_subsets.hpp
4
/// @brief Utilities for creating a struct with a subset of fields of the
5
/// original struct, with conversions between them.
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
52
/// @cond
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
75
/// @endcond
76
77
/// @brief Should be invoked inside a manually defined "root" struct to enable
78
/// conversions from it to subset structs created by
79
/// @ref USERVER_DEFINE_STRUCT_SUBSET and @ref USERVER_DEFINE_STRUCT_SUBSET_REF.
80
///
81
/// @hideinitializer
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
96
/// @brief Defines a struct containing a subset of data members
97
/// from @a OriginalDependencies.
98
///
99
/// Implicit conversions (by copy and by move) are allowed from any superset
100
/// struct to the @a SubsetStruct, as long as the names of the data members
101
/// match, and the superset struct is either defined using
102
/// @ref USERVER_DEFINE_STRUCT_SUBSET or @ref USERVER_DEFINE_STRUCT_SUBSET_REF,
103
/// or it contains @ref USERVER_ALLOW_CONVERSIONS_TO_SUBSET.
104
///
105
/// Usage example:
106
/// @snippet universal/src/utils/struct_subsets_test.cpp deps definitions
107
/// @snippet universal/src/utils/struct_subsets_test.cpp deps usage
108
///
109
/// @param SubsetStruct the name of the subset struct to define
110
/// @param OriginalStruct the name of the superset struct, including its
111
/// namespace if needed
112
/// @param ... names of the data members to copy
113
///
114
/// @hideinitializer
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
125
/// @brief Defines a struct containing a subset of data members
126
/// from @a OriginalDependencies. Appends `const&` to types of all non-reference
127
/// data members.
128
///
129
/// Implicit conversions (by copy and by move) are allowed from any superset
130
/// struct to the @a SubsetStruct, as long as the names of the data members
131
/// match, and the superset struct is either defined using
132
/// @ref USERVER_DEFINE_STRUCT_SUBSET or @ref USERVER_DEFINE_STRUCT_SUBSET_REF,
133
/// or it contains @ref USERVER_ALLOW_CONVERSIONS_TO_SUBSET.
134
///
135
/// `*Ref` structs can be used for parameters of utility functions to avoid
136
/// copying non-reference data members.
137
///
138
/// Usage example:
139
/// @snippet universal/src/utils/struct_subsets_test.cpp ref definitions
140
/// @snippet universal/src/utils/struct_subsets_test.cpp ref usage
141
///
142
/// @param SubsetStructRef the name of the subset struct to define, it should
143
/// typically contain `*Ref` suffix to underline that it needs the original
144
/// backing struct to work
145
/// @param OriginalStruct the name of the superset struct, including its
146
/// namespace if needed
147
/// @param ... names of the data members to copy
148
///
149
/// @hideinitializer
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