Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtclasshelper_p.h
Go to the documentation of this file.
1
// Copyright (C) 2025 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#
ifndef
QTCLASSHELPER_P_H
6
#
define
QTCLASSHELPER_P_H
7
8
#
include
<
QtCore
/
private
/
qglobal_p
.
h
>
9
10
//
11
// W A R N I N G
12
// -------------
13
//
14
// This file is not part of the Qt API. It exists purely as an
15
// implementation detail. This header file may change from version to
16
// version without notice, or even be removed.
17
//
18
// We mean it.
19
//
20
21
#
include
<
type_traits
>
22
#
include
<
utility
>
23
24
QT_BEGIN_NAMESPACE
25
26
/*
27
Helper for setters overloaded on lvalue/rvalue. If the setter is doing a
28
lot of work around the actual assignment, a common pattern is to create a
29
private helper method
30
31
void doSetFoo(const Foo &lvalue, Foo *rvalue);
32
33
and implement the two setters inline by calling that function:
34
35
void setFoo(const Foo &f) { doSetFoo(f, nullptr); }
36
void setFoo(Foo &&f) { doSetFoo(f, &f); }
37
38
Then, in doSetFoo(), when assigning the argument to the member, use this
39
function:
40
41
q_choose_assign(m_foo, lvalue, rvalue);
42
43
or, when appending to a container,
44
45
q_choose_append(m_container, lvalue, rvalue);
46
47
For all other purposes, you can use
48
49
q_choose_copy_move(lvalue, rvalue);
50
51
though note that this always produces an rvalue (the copy, if any, is done
52
inside the function).
53
54
The functions mandate (in the C++ sense) that all arguments are the same
55
type, so they deduce each argument separately and then static_assert that
56
they're the same. If we need std::exchange()-like mixed types later, it's
57
easy to relax. For now, avoid being overly general.
58
*/
59
template
<
typename
U,
typename
V>
60
U q_choose_copy_move(
const
U &lvalue, V *rvalue)
61
{
62
static_assert
(std::is_same_v<U, V>,
"all arguments must be of the same type"
);
63
if
(rvalue)
64
return
std::move(*rvalue);
65
else
66
return
lvalue;
67
}
68
69
template
<
typename
T,
typename
U,
typename
V>
70
decltype
(
auto
)
q_choose_assign
(T &var,
const
U &lvalue, V *rvalue)
71
{
72
static_assert
(
std
::is_same_v<T, U>,
"all arguments must be of the same type"
);
73
static_assert
(
std
::is_same_v<U, V>,
"all arguments must be of the same type"
);
74
if
(rvalue)
75
return
var =
std
::move(*rvalue);
76
else
77
return
var = lvalue;
78
}
79
80
template
<
typename
Container,
typename
U,
typename
V>
81
decltype
(
auto
)
q_choose_append
(Container &c,
const
U &lvalue, V *rvalue)
82
{
83
static_assert
(
std
::is_same_v<
typename
Container::value_type, U>,
"arguments must match container"
);
84
static_assert
(
std
::is_same_v<U, V>,
"all arguments must be of the same type"
);
85
if
(rvalue)
86
c.push_back(
std
::move(*rvalue));
87
else
88
c.push_back(lvalue);
89
return
c.back();
90
}
91
92
QT_END_NAMESPACE
93
94
#
endif
// QTCLASSHELPER_P_H
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
q_choose_assign
decltype(auto) q_choose_assign(T &var, const U &lvalue, V *rvalue)
Definition
qtclasshelper_p.h:70
q_choose_append
decltype(auto) q_choose_append(Container &c, const U &lvalue, V *rvalue)
Definition
qtclasshelper_p.h:81
qtbase
src
corelib
global
qtclasshelper_p.h
Generated on
for Qt by
1.16.1