diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/base_lib.scons | 3 | ||||
-rw-r--r-- | base/base_unittests.scons | 2 | ||||
-rw-r--r-- | base/build/base.vcproj | 8 | ||||
-rw-r--r-- | base/build/base_unittests.vcproj | 4 | ||||
-rw-r--r-- | base/scoped_bstr.cc | 70 | ||||
-rw-r--r-- | base/scoped_bstr.h | 147 | ||||
-rw-r--r-- | base/scoped_bstr_unittest.cc | 98 |
7 files changed, 332 insertions, 0 deletions
diff --git a/base/base_lib.scons b/base/base_lib.scons index 2c0e494..65081dd 100644 --- a/base/base_lib.scons +++ b/base/base_lib.scons @@ -179,8 +179,11 @@ input_files = ChromeFileList([ 'resource_util.h', 'revocable_store.cc', 'revocable_store.h', + 'scoped_bstr.cc', + 'scoped_bstr.h', 'scoped_clipboard_writer.cc', 'scoped_clipboard_writer.h', + 'scoped_comptr.h', 'scoped_handle.h', 'scoped_nsautorelease_pool.h', 'scoped_ptr.h', diff --git a/base/base_unittests.scons b/base/base_unittests.scons index f3ed327..858874a 100644 --- a/base/base_unittests.scons +++ b/base/base_unittests.scons @@ -85,6 +85,7 @@ input_files = ChromeFileList([ 'rand_util_unittest.cc', 'gfx/rect_unittest.cc', 'ref_counted_unittest.cc', + 'scoped_bstr_unittest.cc', 'scoped_comptr_unittest.cc', 'scoped_ptr_unittest.cc', 'sha2_unittest.cc', @@ -154,6 +155,7 @@ if not env.Bit('windows'): 'file_version_info_unittest.cc', 'object_watcher_unittest.cc', 'pe_image_unittest.cc', + 'scoped_bstr_unittest.cc', 'scoped_comptr_unittest.cc', 'system_monitor_unittest.cc', 'sys_string_conversions_unittest.cc', diff --git a/base/build/base.vcproj b/base/build/base.vcproj index 606d530..b2933fd 100644 --- a/base/build/base.vcproj +++ b/base/build/base.vcproj @@ -686,6 +686,14 @@ > </File> <File + RelativePath="..\scoped_bstr.cc" + > + </File> + <File + RelativePath="..\scoped_bstr.h" + > + </File> + <File RelativePath="..\scoped_clipboard_writer.cc" > </File> diff --git a/base/build/base_unittests.vcproj b/base/build/base_unittests.vcproj index 945c8dd..2642c22 100644 --- a/base/build/base_unittests.vcproj +++ b/base/build/base_unittests.vcproj @@ -284,6 +284,10 @@ > </File> <File + RelativePath="..\scoped_bstr_unittest.cc" + > + </File> + <File RelativePath="..\scoped_comptr_unittest.cc" > </File> diff --git a/base/scoped_bstr.cc b/base/scoped_bstr.cc new file mode 100644 index 0000000..908e24e --- /dev/null +++ b/base/scoped_bstr.cc @@ -0,0 +1,70 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/scoped_bstr.h" + +#include "base/logging.h" + + +#if defined(OS_WIN) + +ScopedBstr::ScopedBstr(const wchar_t* non_bstr) + : bstr_(SysAllocString(non_bstr)) { +} + +ScopedBstr::~ScopedBstr() { + COMPILE_ASSERT(sizeof(ScopedBstr) == sizeof(BSTR), ScopedBstrSize); + SysFreeString(bstr_); +} + +void ScopedBstr::Reset(BSTR bstr) { + if (bstr != bstr_) { + // if |bstr_| is NULL, SysFreeString does nothing. + SysFreeString(bstr_); + bstr_ = bstr; + } +} + +BSTR ScopedBstr::Release() { + BSTR bstr = bstr_; + bstr_ = NULL; + return bstr; +} + +void ScopedBstr::Swap(ScopedBstr& bstr2) { + BSTR tmp = bstr_; + bstr_ = bstr2.bstr_; + bstr2.bstr_ = tmp; +} + +BSTR* ScopedBstr::Receive() { + DCHECK(bstr_ == NULL) << "BSTR leak."; + return &bstr_; +} + +BSTR ScopedBstr::Allocate(const wchar_t* wide_str) { + Reset(SysAllocString(wide_str)); + return bstr_; +} + +BSTR ScopedBstr::AllocateBytes(int bytes) { + Reset(SysAllocStringByteLen(NULL, bytes));
+ return bstr_; +} + +void ScopedBstr::SetByteLen(uint32 bytes) { + DCHECK(bstr_ != NULL) << "attempting to modify a NULL bstr"; + uint32* data = reinterpret_cast<uint32*>(bstr_); + data[-1] = bytes; +} + +uint32 ScopedBstr::Length() const { + return SysStringLen(bstr_); +} + +uint32 ScopedBstr::ByteLength() const { + return SysStringByteLen(bstr_); +} + +#endif // defined(OS_WIN) diff --git a/base/scoped_bstr.h b/base/scoped_bstr.h new file mode 100644 index 0000000..00a0484 --- /dev/null +++ b/base/scoped_bstr.h @@ -0,0 +1,147 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_SCOPED_BSTR_H_ +#define BASE_SCOPED_BSTR_H_ + +#include "base/basictypes.h" // needed to pick up OS_WIN + + +#if defined(OS_WIN) +#include "base/logging.h" + +#include <windows.h> +#include <oleauto.h> + +// Manages a BSTR string pointer. +// The class interface is based on scoped_ptr. +class ScopedBstr { + public: + ScopedBstr() : bstr_(NULL) { + } + + // Constructor to create a new BSTR. + // NOTE: Do not pass a BSTR to this constructor expecting ownership to + // be transferred - even though it compiles! ;-) + explicit ScopedBstr(const wchar_t* non_bstr); + ~ScopedBstr(); + + // Give ScopedBstr ownership over an already allocated BSTR or NULL. + // If you need to allocate a new BSTR instance, use |allocate| instead. + void Reset(BSTR bstr = NULL); + + // Releases ownership of the BSTR to the caller. + BSTR Release(); + + // Creates a new BSTR from a wide string. + // If you already have a BSTR and want to transfer ownership to the + // ScopedBstr instance, call |reset| instead. + // Returns a pointer to the new BSTR, or NULL if allocation failed. + BSTR Allocate(const wchar_t* wide_str); + + // Allocates a new BSTR with the specified number of bytes. + // Returns a pointer to the new BSTR, or NULL if allocation failed. + BSTR AllocateBytes(int bytes); + + // Sets the allocated length field of the already-allocated BSTR to be + // |bytes|. This is useful when the BSTR was preallocated with e.g. + // SysAllocStringLen or SysAllocStringByteLen (call |AllocateBytes|) and + // then not all the bytes are being used. + // Note that if you want to set the length to a specific number of characters, + // you need to multiply by sizeof(wchar_t). Oddly, there's no public API to + // set the length, so we do this ourselves by hand. + // + // NOTE: The actual allocated size of the BSTR MUST be >= bytes. + // That responsibility is with the caller. + void SetByteLen(uint32 bytes); + + // Swap values of two ScopedBstr's. + void Swap(ScopedBstr& bstr2); + + // Retrieves the pointer address. + // Used to receive BSTRs as out arguments (and take ownership). + // The function DCHECKs on the current value being NULL. + // Usage: GetBstr(bstr.Receive()); + BSTR* Receive(); + + // Returns number of chars in the BSTR. + uint32 Length() const; + + // Returns the number of bytes allocated for the BSTR. + uint32 ByteLength() const; + + operator BSTR() const { + return bstr_; + } + + protected: + BSTR bstr_; + + private: + // Forbid comparison of ScopedBstr types. You should never have the same + // BSTR owned by two different scoped_ptrs. + bool operator==(const ScopedBstr& bstr2) const; + bool operator!=(const ScopedBstr& bstr2) const; + DISALLOW_COPY_AND_ASSIGN(ScopedBstr); +}; + +// Template class to generate a BSTR from a static wide string +// without touching the heap. Use this class via the StackBstrVar and +// StackBstr macros. +template <uint32 string_bytes> +class StackBstrT { + public: + // Try to stay as const as we can in an attempt to avoid someone + // using the class incorrectly (e.g. by supplying a variable instead + // of a verbatim string. We also have an assert in the constructor + // as an extra runtime check since the const-ness only catches one case. + explicit StackBstrT(const wchar_t* const str) { + // The BSTR API uses UINT, but we prefer uint32. + // Make sure we'll know about it if these types don't match. + COMPILE_ASSERT(sizeof(uint32) == sizeof(UINT), UintToUint32); + COMPILE_ASSERT(sizeof(wchar_t) == sizeof(OLECHAR), WcharToOlechar); + + // You shouldn't pass string pointers to this constructor since + // there's no way for the compiler to calculate the length of the + // string (string_bytes will be equal to pointer size in those cases). + DCHECK(lstrlenW(str) == (string_bytes / sizeof(bstr_.str_[0])) - 1) << + "not expecting a string pointer"; + memcpy(bstr_.str_, str, string_bytes); + bstr_.len_ = string_bytes - sizeof(wchar_t); + } + + operator BSTR() { + return bstr_.str_; + } + + protected: + struct BstrInternal { + uint32 len_; + wchar_t str_[string_bytes / sizeof(wchar_t)]; + } bstr_; +}; + +// Use this macro to generate an inline BSTR from a wide string. +// This is about 6 times faster than using the SysAllocXxx functions to +// allocate a BSTR and helps with keeping heap fragmentation down. +// Example: +// DoBstrStuff(StackBstr(L"This is my BSTR")); +// Where DoBstrStuff is: +// HRESULT DoBstrStuff(BSTR bstr) { ... } +#define StackBstr(str) \ + static_cast<BSTR>(StackBstrT<sizeof(str)>(str)) + +// If you need a named BSTR variable that's based on a fixed string +// (e.g. if the BSTR is used inside a loop or more than one place), +// use StackBstrVar to declare a variable. +// Example: +// StackBstrVar(L"my_property", myprop); +// for (int i = 0; i < objects.length(); ++i) +// ProcessValue(objects[i].GetProp(myprop)); // GetProp accepts BSTR +#define StackBstrVar(str, var) \ + StackBstrT<sizeof(str)> var(str) + +#endif // #if defined(OS_WIN) + +#endif // BASE_SCOPED_BSTR_H_ diff --git a/base/scoped_bstr_unittest.cc b/base/scoped_bstr_unittest.cc new file mode 100644 index 0000000..ed3ab22 --- /dev/null +++ b/base/scoped_bstr_unittest.cc @@ -0,0 +1,98 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/scoped_bstr.h" +#include "testing/gtest/include/gtest/gtest.h" + +#if defined(OS_WIN) +#include <oleauto.h> // VarBstrCmp + +namespace { + +static const wchar_t kTestString1[] = L"123"; +static const wchar_t kTestString2[] = L"456789"; +uint32 test1_len = arraysize(kTestString1) - 1; +uint32 test2_len = arraysize(kTestString2) - 1; + +void DumbBstrTests() { + ScopedBstr b; + EXPECT_TRUE(b == NULL); + EXPECT_TRUE(b.Length() == 0); + EXPECT_TRUE(b.ByteLength() == 0); + b.Reset(NULL); + EXPECT_TRUE(b == NULL); + EXPECT_TRUE(b.Release() == NULL); + ScopedBstr b2; + b.Swap(b2); + EXPECT_TRUE(b2 == NULL); +} + +void GiveMeABstr(BSTR* ret) { + *ret = SysAllocString(kTestString1); +} + +void BasicBstrTests() { + ScopedBstr b1(kTestString1); + EXPECT_TRUE(b1.Length() == test1_len); + EXPECT_TRUE(b1.ByteLength() == test1_len * sizeof(kTestString1[0])); + + ScopedBstr b2; + b1.Swap(b2); + EXPECT_TRUE(b2.Length() == test1_len); + EXPECT_TRUE(b1.Length() == 0); + EXPECT_TRUE(lstrcmpW(b2, kTestString1) == 0); + BSTR tmp = b2.Release(); + EXPECT_TRUE(tmp != NULL); + EXPECT_TRUE(lstrcmpW(tmp, kTestString1) == 0); + EXPECT_TRUE(b2 == NULL); + SysFreeString(tmp); + + GiveMeABstr(b2.Receive()); + EXPECT_TRUE(b2 != NULL); + b2.Reset(); + EXPECT_TRUE(b2.AllocateBytes(100) != NULL); + EXPECT_TRUE(b2.ByteLength() == 100); + EXPECT_TRUE(b2.Length() == 100 / sizeof(kTestString1[0])); + lstrcpyW(static_cast<BSTR>(b2), kTestString1); + EXPECT_TRUE(lstrlen(b2) == test1_len); + EXPECT_TRUE(b2.Length() == 100 / sizeof(kTestString1[0])); + b2.SetByteLen(lstrlen(b2) * sizeof(kTestString2[0])); + EXPECT_TRUE(lstrlen(b2) == b2.Length()); + + EXPECT_TRUE(b1.Allocate(kTestString2) != NULL); + EXPECT_TRUE(b1.Length() == test2_len); + b1.SetByteLen((test2_len - 1) * sizeof(kTestString2[0])); + EXPECT_TRUE(b1.Length() == test2_len - 1); +} + +} // namespace + +TEST(ScopedBstrTest, ScopedBstr) { + DumbBstrTests(); + BasicBstrTests(); +} + +#define kSourceStr L"this is a string" +#define kSourceStrEmpty L"" + +TEST(StackBstrTest, StackBstr) { + ScopedBstr system_bstr(kSourceStr); + StackBstrVar(kSourceStr, stack_bstr); + EXPECT_EQ(VarBstrCmp(system_bstr, stack_bstr, LOCALE_USER_DEFAULT, 0), + VARCMP_EQ); + + StackBstrVar(kSourceStrEmpty, empty); + uint32 l1 = SysStringLen(stack_bstr); + uint32 l2 = SysStringLen(StackBstr(kSourceStr)); + uint32 l3 = SysStringLen(system_bstr); + EXPECT_TRUE(l1 == l2); + EXPECT_TRUE(l2 == l3); + EXPECT_TRUE(SysStringLen(empty) == 0); + + const wchar_t one_more_test[] = L"this is my const string"; + EXPECT_EQ(SysStringLen(StackBstr(one_more_test)), + lstrlenW(one_more_test)); +} + +#endif // defined(OS_WIN) |