Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
scoped_bstr.h
Go to the documentation of this file.
1// Copyright (c) 2011 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
5#ifndef BASE_WIN_SCOPED_BSTR_H_
6#define BASE_WIN_SCOPED_BSTR_H_
7
8#include <windows.h>
9
10#include <oleauto.h>
11
12#include <cstddef>
13
14#include "base/base_export.h"
15#include "base/logging.h"
16
17namespace base {
18namespace win {
19
20// Manages a BSTR string pointer.
21// The class interface is based on unique_ptr.
23 public:
24 ScopedBstr() = default;
25
26 // Constructor to create a new BSTR.
27 //
28 // NOTE: Do not pass a BSTR to this constructor expecting ownership to
29 // be transferred - even though it compiles! ;-)
30 explicit ScopedBstr(std::wstring_view non_bstr);
32
33 BSTR Get() const { return bstr_; }
34
35 // Give ScopedBstr ownership over an already allocated BSTR or null.
36 // If you need to allocate a new BSTR instance, use |allocate| instead.
37 void Reset(BSTR bstr = nullptr);
38
39 // Releases ownership of the BSTR to the caller.
40 BSTR Release();
41
42 // Creates a new BSTR from a 16-bit C-style string.
43 //
44 // If you already have a BSTR and want to transfer ownership to the
45 // ScopedBstr instance, call |reset| instead.
46 //
47 // Returns a pointer to the new BSTR.
48 BSTR Allocate(std::wstring_view str);
49
50 // Allocates a new BSTR with the specified number of bytes.
51 // Returns a pointer to the new BSTR.
52 BSTR AllocateBytes(size_t bytes);
53
54 // Sets the allocated length field of the already-allocated BSTR to be
55 // |bytes|. This is useful when the BSTR was preallocated with e.g.
56 // SysAllocStringLen or SysAllocStringByteLen (call |AllocateBytes|) and then
57 // not all the bytes are being used.
58 //
59 // Note that if you want to set the length to a specific number of
60 // characters, you need to multiply by sizeof(wchar_t). Oddly, there's no
61 // public API to set the length, so we do this ourselves by hand.
62 //
63 // NOTE: The actual allocated size of the BSTR MUST be >= bytes. That
64 // responsibility is with the caller.
65 void SetByteLen(size_t bytes);
66
67 // Swap values of two ScopedBstr's.
68 void Swap(ScopedBstr& bstr2);
69
70 // Retrieves the pointer address.
71 // Used to receive BSTRs as out arguments (and take ownership).
72 // The function DCHECKs on the current value being null.
73 // Usage: GetBstr(bstr.Receive());
74 BSTR* Receive();
75
76 // Returns number of chars in the BSTR.
77 size_t Length() const;
78
79 // Returns the number of bytes allocated for the BSTR.
80 size_t ByteLength() const;
81
82 // Forbid comparison of ScopedBstr types. You should never have the same
83 // BSTR owned by two different scoped_ptrs.
84 bool operator==(const ScopedBstr& bstr2) const = delete;
85 bool operator!=(const ScopedBstr& bstr2) const = delete;
86
87 private:
88 BSTR bstr_ = nullptr;
90};
91
92} // namespace win
93} // namespace base
94
95#endif // BASE_WIN_SCOPED_BSTR_H_
#define BASE_EXPORT
Definition base_export.h:26
bool operator!=(const ScopedBstr &bstr2) const =delete
bool operator==(const ScopedBstr &bstr2) const =delete
#define BASE_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:8