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. 4 5#include "base/scoped_bstr_win.h" 6 7#include "base/logging.h" 8 9 10ScopedBstr::ScopedBstr(const wchar_t* non_bstr) 11 : bstr_(SysAllocString(non_bstr)) { 12} 13 14ScopedBstr::~ScopedBstr() { 15 COMPILE_ASSERT(sizeof(ScopedBstr) == sizeof(BSTR), ScopedBstrSize); 16 SysFreeString(bstr_); 17} 18 19void ScopedBstr::Reset(BSTR bstr) { 20 if (bstr != bstr_) { 21 // if |bstr_| is NULL, SysFreeString does nothing. 22 SysFreeString(bstr_); 23 bstr_ = bstr; 24 } 25} 26 27BSTR ScopedBstr::Release() { 28 BSTR bstr = bstr_; 29 bstr_ = NULL; 30 return bstr; 31} 32 33void ScopedBstr::Swap(ScopedBstr& bstr2) { 34 BSTR tmp = bstr_; 35 bstr_ = bstr2.bstr_; 36 bstr2.bstr_ = tmp; 37} 38 39BSTR* ScopedBstr::Receive() { 40 DCHECK(bstr_ == NULL) << "BSTR leak."; 41 return &bstr_; 42} 43 44BSTR ScopedBstr::Allocate(const wchar_t* wide_str) { 45 Reset(SysAllocString(wide_str)); 46 return bstr_; 47} 48 49BSTR ScopedBstr::AllocateBytes(int bytes) { 50 Reset(SysAllocStringByteLen(NULL, bytes)); 51 return bstr_; 52} 53 54void ScopedBstr::SetByteLen(uint32 bytes) { 55 DCHECK(bstr_ != NULL) << "attempting to modify a NULL bstr"; 56 uint32* data = reinterpret_cast<uint32*>(bstr_); 57 data[-1] = bytes; 58} 59 60uint32 ScopedBstr::Length() const { 61 return SysStringLen(bstr_); 62} 63 64uint32 ScopedBstr::ByteLength() const { 65 return SysStringByteLen(bstr_); 66} 67