Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qbstr_p.h
Go to the documentation of this file.
1
// Copyright (C) 2024 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
QBSTR_P_H
6
#
define
QBSTR_P_H
7
8
//
9
// W A R N I N G
10
// -------------
11
//
12
// This file is not part of the Qt API. It exists purely as an
13
// implementation detail. This header file may change from version to
14
// version without notice, or even be removed.
15
//
16
// We mean it.
17
//
18
19
#
include
<
QtCore
/
private
/
qglobal_p
.
h
>
20
21
#
if
defined
(
Q_OS_WIN
)
||
defined
(
Q_QDOC
)
22
23
#
include
<
QtCore
/
qt_windows
.
h
>
24
#
include
<
QtCore
/
qstring
.
h
>
25
#
include
<
utility
>
26
#
include
<
oaidl
.
h
>
27
28
QT_BEGIN_NAMESPACE
29
30
class
QBStr
31
{
32
public
:
33
QBStr() =
default
;
34
35
~QBStr()
noexcept
36
{
37
free();
38
}
39
40
// Does not take ownership
41
explicit
QBStr(LPCOLESTR str)
noexcept
42
{
43
if
(str)
44
m_str = ::SysAllocString(str);
45
Q_ASSERT(m_str || !str);
46
}
47
48
explicit
QBStr(
const
QString &str)
noexcept
49
{
50
if
(!str.isNull())
51
m_str = ::SysAllocString(
reinterpret_cast
<
const
wchar_t
*>(str.utf16()));
52
Q_ASSERT(m_str || str.isNull());
53
}
54
55
QBStr(
const
QBStr &str)
noexcept
: m_str{ str.copy() } { }
56
57
QBStr(QBStr &&str)
noexcept
: m_str{ std::exchange(str.m_str,
nullptr
) } { }
58
59
// Does not take ownership
60
QBStr &operator=(LPCOLESTR str)
noexcept
61
{
62
free();
63
if
(str)
64
m_str = ::SysAllocString(str);
65
Q_ASSERT(m_str || !str);
66
return
*
this
;
67
}
68
69
QBStr &operator=(
const
QString &str)
noexcept
70
{
71
free();
72
if
(!str.isNull())
73
m_str = ::SysAllocString(
reinterpret_cast
<
const
wchar_t
*>(str.utf16()));
74
Q_ASSERT(m_str || str.isNull());
75
return
*
this
;
76
}
77
78
QBStr &operator=(
const
QBStr &rhs)
noexcept
79
{
80
if
(
this
!= std::addressof(rhs))
81
reset(rhs.copy());
82
83
return
*
this
;
84
}
85
86
QBStr &operator=(QBStr &&rhs)
noexcept
87
{
88
if
(
this
!= std::addressof(rhs))
89
reset(std::exchange(rhs.m_str,
nullptr
));
90
91
return
*
this
;
92
}
93
94
const
BSTR &bstr()
const
noexcept
95
{
96
return
m_str;
97
}
98
99
QString str()
const
100
{
101
return
QString::fromWCharArray(m_str);
102
}
103
104
[[nodiscard]] BSTR release()
noexcept
105
{
106
return
std::exchange(m_str,
nullptr
);
107
}
108
109
[[nodiscard]] BSTR *operator&()
noexcept
// NOLINT(google-runtime-operator)
110
{
111
Q_ASSERT(!m_str);
112
return
&m_str;
113
}
114
115
private
:
116
void
free()
noexcept
117
{
118
if
(m_str)
119
::SysFreeString(m_str);
120
m_str =
nullptr
;
121
}
122
123
void
reset(BSTR str)
noexcept
124
{
125
free();
126
m_str = str;
127
}
128
129
[[nodiscard]] BSTR copy()
const
noexcept
130
{
131
if
(!m_str)
132
return
nullptr
;
133
134
return
::SysAllocStringByteLen(
reinterpret_cast
<
const
char
*>(m_str),
135
::SysStringByteLen(m_str));
136
}
137
138
BSTR m_str =
nullptr
;
139
};
140
141
QT_END_NAMESPACE
142
143
#
endif
// Q_OS_WIN
144
145
#
endif
// QBSTR_P_H
qtbase
src
corelib
platform
windows
qbstr_p.h
Generated on
for Qt by
1.18.0