Flutter Engine
The Flutter Engine
scoped_bstr.cc
Go to the documentation of this file.
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <cstdint>
8
9#include "base/logging.h"
11
12namespace base {
13namespace win {
14
15namespace {
16
17BSTR AllocBstrOrDie(std::wstring_view non_bstr) {
18 BSTR result = ::SysAllocStringLen(non_bstr.data(),
19 checked_cast<UINT>(non_bstr.length()));
20 if (!result)
21 std::abort();
22 return result;
23}
24
25BSTR AllocBstrBytesOrDie(size_t bytes) {
26 BSTR result = ::SysAllocStringByteLen(nullptr, checked_cast<UINT>(bytes));
27 if (!result)
28 std::abort();
29 return result;
30}
31
32} // namespace
33
34ScopedBstr::ScopedBstr(std::wstring_view non_bstr)
35 : bstr_(AllocBstrOrDie(non_bstr)) {}
36
38 static_assert(sizeof(ScopedBstr) == sizeof(BSTR), "ScopedBstrSize");
39 ::SysFreeString(bstr_);
40}
41
42void ScopedBstr::Reset(BSTR bstr) {
43 if (bstr != bstr_) {
44 // SysFreeString handles null properly.
45 ::SysFreeString(bstr_);
46 bstr_ = bstr;
47 }
48}
49
51 BSTR bstr = bstr_;
52 bstr_ = nullptr;
53 return bstr;
54}
55
57 BSTR tmp = bstr_;
58 bstr_ = bstr2.bstr_;
59 bstr2.bstr_ = tmp;
60}
61
63 BASE_DCHECK(!bstr_) << "BSTR leak.";
64 return &bstr_;
65}
66
67BSTR ScopedBstr::Allocate(std::wstring_view str) {
68 Reset(AllocBstrOrDie(str));
69 return bstr_;
70}
71
72BSTR ScopedBstr::AllocateBytes(size_t bytes) {
73 Reset(AllocBstrBytesOrDie(bytes));
74 return bstr_;
75}
76
77void ScopedBstr::SetByteLen(size_t bytes) {
78 BASE_DCHECK(bstr_);
79 uint32_t* data = reinterpret_cast<uint32_t*>(bstr_);
80 data[-1] = checked_cast<uint32_t>(bytes);
81}
82
83size_t ScopedBstr::Length() const {
84 return ::SysStringLen(bstr_);
85}
86
87size_t ScopedBstr::ByteLength() const {
88 return ::SysStringByteLen(bstr_);
89}
90
91} // namespace win
92} // namespace base
BSTR Allocate(std::wstring_view str)
Definition: scoped_bstr.cc:67
size_t ByteLength() const
Definition: scoped_bstr.cc:87
void SetByteLen(size_t bytes)
Definition: scoped_bstr.cc:77
void Reset(BSTR bstr=nullptr)
Definition: scoped_bstr.cc:42
void Swap(ScopedBstr &bstr2)
Definition: scoped_bstr.cc:56
BSTR AllocateBytes(size_t bytes)
Definition: scoped_bstr.cc:72
size_t Length() const
Definition: scoped_bstr.cc:83
GAsyncResult * result
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
#define BASE_DCHECK(condition)
Definition: logging.h:63