michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_SCOPED_BSTR_WIN_H_ michael@0: #define BASE_SCOPED_BSTR_WIN_H_ michael@0: michael@0: #include "base/basictypes.h" // needed to pick up OS_WIN michael@0: michael@0: #include "base/logging.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: // Manages a BSTR string pointer. michael@0: // The class interface is based on scoped_ptr. michael@0: class ScopedBstr { michael@0: public: michael@0: ScopedBstr() : bstr_(NULL) { michael@0: } michael@0: michael@0: // Constructor to create a new BSTR. michael@0: // NOTE: Do not pass a BSTR to this constructor expecting ownership to michael@0: // be transferred - even though it compiles! ;-) michael@0: explicit ScopedBstr(const wchar_t* non_bstr); michael@0: ~ScopedBstr(); michael@0: michael@0: // Give ScopedBstr ownership over an already allocated BSTR or NULL. michael@0: // If you need to allocate a new BSTR instance, use |allocate| instead. michael@0: void Reset(BSTR bstr = NULL); michael@0: michael@0: // Releases ownership of the BSTR to the caller. michael@0: BSTR Release(); michael@0: michael@0: // Creates a new BSTR from a wide string. michael@0: // If you already have a BSTR and want to transfer ownership to the michael@0: // ScopedBstr instance, call |reset| instead. michael@0: // Returns a pointer to the new BSTR, or NULL if allocation failed. michael@0: BSTR Allocate(const wchar_t* wide_str); michael@0: michael@0: // Allocates a new BSTR with the specified number of bytes. michael@0: // Returns a pointer to the new BSTR, or NULL if allocation failed. michael@0: BSTR AllocateBytes(int bytes); michael@0: michael@0: // Sets the allocated length field of the already-allocated BSTR to be michael@0: // |bytes|. This is useful when the BSTR was preallocated with e.g. michael@0: // SysAllocStringLen or SysAllocStringByteLen (call |AllocateBytes|) and michael@0: // then not all the bytes are being used. michael@0: // Note that if you want to set the length to a specific number of characters, michael@0: // you need to multiply by sizeof(wchar_t). Oddly, there's no public API to michael@0: // set the length, so we do this ourselves by hand. michael@0: // michael@0: // NOTE: The actual allocated size of the BSTR MUST be >= bytes. michael@0: // That responsibility is with the caller. michael@0: void SetByteLen(uint32_t bytes); michael@0: michael@0: // Swap values of two ScopedBstr's. michael@0: void Swap(ScopedBstr& bstr2); michael@0: michael@0: // Retrieves the pointer address. michael@0: // Used to receive BSTRs as out arguments (and take ownership). michael@0: // The function DCHECKs on the current value being NULL. michael@0: // Usage: GetBstr(bstr.Receive()); michael@0: BSTR* Receive(); michael@0: michael@0: // Returns number of chars in the BSTR. michael@0: uint32_t Length() const; michael@0: michael@0: // Returns the number of bytes allocated for the BSTR. michael@0: uint32_t ByteLength() const; michael@0: michael@0: operator BSTR() const { michael@0: return bstr_; michael@0: } michael@0: michael@0: protected: michael@0: BSTR bstr_; michael@0: michael@0: private: michael@0: // Forbid comparison of ScopedBstr types. You should never have the same michael@0: // BSTR owned by two different scoped_ptrs. michael@0: bool operator==(const ScopedBstr& bstr2) const; michael@0: bool operator!=(const ScopedBstr& bstr2) const; michael@0: DISALLOW_COPY_AND_ASSIGN(ScopedBstr); michael@0: }; michael@0: michael@0: // Template class to generate a BSTR from a static wide string michael@0: // without touching the heap. Use this class via the StackBstrVar and michael@0: // StackBstr macros. michael@0: template michael@0: class StackBstrT { michael@0: public: michael@0: // Try to stay as const as we can in an attempt to avoid someone michael@0: // using the class incorrectly (e.g. by supplying a variable instead michael@0: // of a verbatim string. We also have an assert in the constructor michael@0: // as an extra runtime check since the const-ness only catches one case. michael@0: explicit StackBstrT(const wchar_t* const str) { michael@0: // The BSTR API uses UINT, but we prefer uint32_t. michael@0: // Make sure we'll know about it if these types don't match. michael@0: COMPILE_ASSERT(sizeof(uint32_t) == sizeof(UINT), UintToUint32); michael@0: COMPILE_ASSERT(sizeof(wchar_t) == sizeof(OLECHAR), WcharToOlechar); michael@0: michael@0: // You shouldn't pass string pointers to this constructor since michael@0: // there's no way for the compiler to calculate the length of the michael@0: // string (string_bytes will be equal to pointer size in those cases). michael@0: DCHECK(lstrlenW(str) == (string_bytes / sizeof(bstr_.str_[0])) - 1) << michael@0: "not expecting a string pointer"; michael@0: memcpy(bstr_.str_, str, string_bytes); michael@0: bstr_.len_ = string_bytes - sizeof(wchar_t); michael@0: } michael@0: michael@0: operator BSTR() { michael@0: return bstr_.str_; michael@0: } michael@0: michael@0: protected: michael@0: struct BstrInternal { michael@0: uint32_t len_; michael@0: wchar_t str_[string_bytes / sizeof(wchar_t)]; michael@0: } bstr_; michael@0: }; michael@0: michael@0: // Use this macro to generate an inline BSTR from a wide string. michael@0: // This is about 6 times faster than using the SysAllocXxx functions to michael@0: // allocate a BSTR and helps with keeping heap fragmentation down. michael@0: // Example: michael@0: // DoBstrStuff(StackBstr(L"This is my BSTR")); michael@0: // Where DoBstrStuff is: michael@0: // HRESULT DoBstrStuff(BSTR bstr) { ... } michael@0: #define StackBstr(str) \ michael@0: static_cast(StackBstrT(str)) michael@0: michael@0: // If you need a named BSTR variable that's based on a fixed string michael@0: // (e.g. if the BSTR is used inside a loop or more than one place), michael@0: // use StackBstrVar to declare a variable. michael@0: // Example: michael@0: // StackBstrVar(L"my_property", myprop); michael@0: // for (int i = 0; i < objects.length(); ++i) michael@0: // ProcessValue(objects[i].GetProp(myprop)); // GetProp accepts BSTR michael@0: #define StackBstrVar(str, var) \ michael@0: StackBstrT var(str) michael@0: michael@0: #endif // BASE_SCOPED_BSTR_WIN_H_