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.
5 #include "base/win/scoped_bstr.h"
7 #include "base/logging.h"
12 ScopedBstr::ScopedBstr(const char16
* non_bstr
)
13 : bstr_(SysAllocString(non_bstr
)) {
16 ScopedBstr::~ScopedBstr() {
17 COMPILE_ASSERT(sizeof(ScopedBstr
) == sizeof(BSTR
), ScopedBstrSize
);
21 void ScopedBstr::Reset(BSTR bstr
) {
23 // if |bstr_| is NULL, SysFreeString does nothing.
29 BSTR
ScopedBstr::Release() {
35 void ScopedBstr::Swap(ScopedBstr
& bstr2
) {
41 BSTR
* ScopedBstr::Receive() {
42 DCHECK(!bstr_
) << "BSTR leak.";
46 BSTR
ScopedBstr::Allocate(const char16
* str
) {
47 Reset(SysAllocString(str
));
51 BSTR
ScopedBstr::AllocateBytes(size_t bytes
) {
52 Reset(SysAllocStringByteLen(NULL
, static_cast<UINT
>(bytes
)));
56 void ScopedBstr::SetByteLen(size_t bytes
) {
57 DCHECK(bstr_
!= NULL
) << "attempting to modify a NULL bstr";
58 uint32
* data
= reinterpret_cast<uint32
*>(bstr_
);
59 data
[-1] = static_cast<uint32
>(bytes
);
62 size_t ScopedBstr::Length() const {
63 return SysStringLen(bstr_
);
66 size_t ScopedBstr::ByteLength() const {
67 return SysStringByteLen(bstr_
);