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