1 // Copyright (c) 2006-2008 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/scoped_bstr_win.h"
7 #include "base/logging.h"
10 ScopedBstr::ScopedBstr(const wchar_t* non_bstr
)
11 : bstr_(SysAllocString(non_bstr
)) {
14 ScopedBstr::~ScopedBstr() {
15 COMPILE_ASSERT(sizeof(ScopedBstr
) == sizeof(BSTR
), ScopedBstrSize
);
19 void ScopedBstr::Reset(BSTR bstr
) {
21 // if |bstr_| is NULL, SysFreeString does nothing.
27 BSTR
ScopedBstr::Release() {
33 void ScopedBstr::Swap(ScopedBstr
& bstr2
) {
39 BSTR
* ScopedBstr::Receive() {
40 DCHECK(bstr_
== NULL
) << "BSTR leak.";
44 BSTR
ScopedBstr::Allocate(const wchar_t* wide_str
) {
45 Reset(SysAllocString(wide_str
));
49 BSTR
ScopedBstr::AllocateBytes(int bytes
) {
50 Reset(SysAllocStringByteLen(NULL
, bytes
));
54 void ScopedBstr::SetByteLen(uint32 bytes
) {
55 DCHECK(bstr_
!= NULL
) << "attempting to modify a NULL bstr";
56 uint32
* data
= reinterpret_cast<uint32
*>(bstr_
);
60 uint32
ScopedBstr::Length() const {
61 return SysStringLen(bstr_
);
64 uint32
ScopedBstr::ByteLength() const {
65 return SysStringByteLen(bstr_
);