| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_SCOPED_BSTR_WIN_H_ | 5 #ifndef BASE_WIN_SCOPED_BSTR_H_ |
| 6 #define BASE_SCOPED_BSTR_WIN_H_ | 6 #define BASE_WIN_SCOPED_BSTR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #include <oleauto.h> | 10 #include <oleauto.h> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/string16.h" |
| 14 |
| 15 namespace base { |
| 16 namespace win { |
| 13 | 17 |
| 14 // Manages a BSTR string pointer. | 18 // Manages a BSTR string pointer. |
| 15 // The class interface is based on scoped_ptr. | 19 // The class interface is based on scoped_ptr. |
| 16 class ScopedBstr { | 20 class ScopedBstr { |
| 17 public: | 21 public: |
| 18 ScopedBstr() : bstr_(NULL) { | 22 ScopedBstr() : bstr_(NULL) { |
| 19 } | 23 } |
| 20 | 24 |
| 21 // Constructor to create a new BSTR. | 25 // Constructor to create a new BSTR. |
| 26 // |
| 22 // NOTE: Do not pass a BSTR to this constructor expecting ownership to | 27 // NOTE: Do not pass a BSTR to this constructor expecting ownership to |
| 23 // be transferred - even though it compiles! ;-) | 28 // be transferred - even though it compiles! ;-) |
| 24 explicit ScopedBstr(const wchar_t* non_bstr); | 29 explicit ScopedBstr(const char16* non_bstr); |
| 25 ~ScopedBstr(); | 30 ~ScopedBstr(); |
| 26 | 31 |
| 27 // Give ScopedBstr ownership over an already allocated BSTR or NULL. | 32 // Give ScopedBstr ownership over an already allocated BSTR or NULL. |
| 28 // If you need to allocate a new BSTR instance, use |allocate| instead. | 33 // If you need to allocate a new BSTR instance, use |allocate| instead. |
| 29 void Reset(BSTR bstr = NULL); | 34 void Reset(BSTR bstr = NULL); |
| 30 | 35 |
| 31 // Releases ownership of the BSTR to the caller. | 36 // Releases ownership of the BSTR to the caller. |
| 32 BSTR Release(); | 37 BSTR Release(); |
| 33 | 38 |
| 34 // Creates a new BSTR from a wide string. | 39 // Creates a new BSTR from a 16-bit C-style string. |
| 40 // |
| 35 // If you already have a BSTR and want to transfer ownership to the | 41 // If you already have a BSTR and want to transfer ownership to the |
| 36 // ScopedBstr instance, call |reset| instead. | 42 // ScopedBstr instance, call |reset| instead. |
| 43 // |
| 37 // Returns a pointer to the new BSTR, or NULL if allocation failed. | 44 // Returns a pointer to the new BSTR, or NULL if allocation failed. |
| 38 BSTR Allocate(const wchar_t* wide_str); | 45 BSTR Allocate(const char16* str); |
| 39 | 46 |
| 40 // Allocates a new BSTR with the specified number of bytes. | 47 // Allocates a new BSTR with the specified number of bytes. |
| 41 // Returns a pointer to the new BSTR, or NULL if allocation failed. | 48 // Returns a pointer to the new BSTR, or NULL if allocation failed. |
| 42 BSTR AllocateBytes(int bytes); | 49 BSTR AllocateBytes(size_t bytes); |
| 43 | 50 |
| 44 // Sets the allocated length field of the already-allocated BSTR to be | 51 // Sets the allocated length field of the already-allocated BSTR to be |
| 45 // |bytes|. This is useful when the BSTR was preallocated with e.g. | 52 // |bytes|. This is useful when the BSTR was preallocated with e.g. |
| 46 // SysAllocStringLen or SysAllocStringByteLen (call |AllocateBytes|) and | 53 // SysAllocStringLen or SysAllocStringByteLen (call |AllocateBytes|) and then |
| 47 // then not all the bytes are being used. | 54 // not all the bytes are being used. |
| 48 // Note that if you want to set the length to a specific number of characters, | |
| 49 // you need to multiply by sizeof(wchar_t). Oddly, there's no public API to | |
| 50 // set the length, so we do this ourselves by hand. | |
| 51 // | 55 // |
| 52 // NOTE: The actual allocated size of the BSTR MUST be >= bytes. | 56 // Note that if you want to set the length to a specific number of |
| 53 // That responsibility is with the caller. | 57 // characters, you need to multiply by sizeof(wchar_t). Oddly, there's no |
| 54 void SetByteLen(uint32 bytes); | 58 // public API to set the length, so we do this ourselves by hand. |
| 59 // |
| 60 // NOTE: The actual allocated size of the BSTR MUST be >= bytes. That |
| 61 // responsibility is with the caller. |
| 62 void SetByteLen(size_t bytes); |
| 55 | 63 |
| 56 // Swap values of two ScopedBstr's. | 64 // Swap values of two ScopedBstr's. |
| 57 void Swap(ScopedBstr& bstr2); | 65 void Swap(ScopedBstr& bstr2); |
| 58 | 66 |
| 59 // Retrieves the pointer address. | 67 // Retrieves the pointer address. |
| 60 // Used to receive BSTRs as out arguments (and take ownership). | 68 // Used to receive BSTRs as out arguments (and take ownership). |
| 61 // The function DCHECKs on the current value being NULL. | 69 // The function DCHECKs on the current value being NULL. |
| 62 // Usage: GetBstr(bstr.Receive()); | 70 // Usage: GetBstr(bstr.Receive()); |
| 63 BSTR* Receive(); | 71 BSTR* Receive(); |
| 64 | 72 |
| 65 // Returns number of chars in the BSTR. | 73 // Returns number of chars in the BSTR. |
| 66 uint32 Length() const; | 74 size_t Length() const; |
| 67 | 75 |
| 68 // Returns the number of bytes allocated for the BSTR. | 76 // Returns the number of bytes allocated for the BSTR. |
| 69 uint32 ByteLength() const; | 77 size_t ByteLength() const; |
| 70 | 78 |
| 71 operator BSTR() const { | 79 operator BSTR() const { |
| 72 return bstr_; | 80 return bstr_; |
| 73 } | 81 } |
| 74 | 82 |
| 75 protected: | 83 protected: |
| 76 BSTR bstr_; | 84 BSTR bstr_; |
| 77 | 85 |
| 78 private: | 86 private: |
| 79 // Forbid comparison of ScopedBstr types. You should never have the same | 87 // Forbid comparison of ScopedBstr types. You should never have the same |
| 80 // BSTR owned by two different scoped_ptrs. | 88 // BSTR owned by two different scoped_ptrs. |
| 81 bool operator==(const ScopedBstr& bstr2) const; | 89 bool operator==(const ScopedBstr& bstr2) const; |
| 82 bool operator!=(const ScopedBstr& bstr2) const; | 90 bool operator!=(const ScopedBstr& bstr2) const; |
| 83 DISALLOW_COPY_AND_ASSIGN(ScopedBstr); | 91 DISALLOW_COPY_AND_ASSIGN(ScopedBstr); |
| 84 }; | 92 }; |
| 85 | 93 |
| 86 // Template class to generate a BSTR from a static wide string | 94 } // namespace win |
| 87 // without touching the heap. Use this class via the StackBstrVar and | 95 } // namespace base |
| 88 // StackBstr macros. | |
| 89 template <uint32 string_bytes> | |
| 90 class StackBstrT { | |
| 91 public: | |
| 92 // Try to stay as const as we can in an attempt to avoid someone | |
| 93 // using the class incorrectly (e.g. by supplying a variable instead | |
| 94 // of a verbatim string. We also have an assert in the constructor | |
| 95 // as an extra runtime check since the const-ness only catches one case. | |
| 96 explicit StackBstrT(const wchar_t* const str) { | |
| 97 // The BSTR API uses UINT, but we prefer uint32. | |
| 98 // Make sure we'll know about it if these types don't match. | |
| 99 COMPILE_ASSERT(sizeof(uint32) == sizeof(UINT), UintToUint32); | |
| 100 COMPILE_ASSERT(sizeof(wchar_t) == sizeof(OLECHAR), WcharToOlechar); | |
| 101 | 96 |
| 102 // You shouldn't pass string pointers to this constructor since | 97 #endif // BASE_SCOPED_BSTR_H_ |
| 103 // there's no way for the compiler to calculate the length of the | |
| 104 // string (string_bytes will be equal to pointer size in those cases). | |
| 105 DCHECK(lstrlenW(str) == (string_bytes / sizeof(bstr_.str_[0])) - 1) << | |
| 106 "not expecting a string pointer"; | |
| 107 memcpy(bstr_.str_, str, string_bytes); | |
| 108 bstr_.len_ = string_bytes - sizeof(wchar_t); | |
| 109 } | |
| 110 | |
| 111 operator BSTR() { | |
| 112 return bstr_.str_; | |
| 113 } | |
| 114 | |
| 115 protected: | |
| 116 struct BstrInternal { | |
| 117 uint32 len_; | |
| 118 wchar_t str_[string_bytes / sizeof(wchar_t)]; | |
| 119 } bstr_; | |
| 120 }; | |
| 121 | |
| 122 // Use this macro to generate an inline BSTR from a wide string. | |
| 123 // This is about 6 times faster than using the SysAllocXxx functions to | |
| 124 // allocate a BSTR and helps with keeping heap fragmentation down. | |
| 125 // Example: | |
| 126 // DoBstrStuff(StackBstr(L"This is my BSTR")); | |
| 127 // Where DoBstrStuff is: | |
| 128 // HRESULT DoBstrStuff(BSTR bstr) { ... } | |
| 129 #define StackBstr(str) \ | |
| 130 static_cast<BSTR>(StackBstrT<sizeof(str)>(str)) | |
| 131 | |
| 132 // If you need a named BSTR variable that's based on a fixed string | |
| 133 // (e.g. if the BSTR is used inside a loop or more than one place), | |
| 134 // use StackBstrVar to declare a variable. | |
| 135 // Example: | |
| 136 // StackBstrVar(L"my_property", myprop); | |
| 137 // for (int i = 0; i < objects.length(); ++i) | |
| 138 // ProcessValue(objects[i].GetProp(myprop)); // GetProp accepts BSTR | |
| 139 #define StackBstrVar(str, var) \ | |
| 140 StackBstrT<sizeof(str)> var(str) | |
| 141 | |
| 142 #endif // BASE_SCOPED_BSTR_WIN_H_ | |
| OLD | NEW |