Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: base/scoped_bstr_win.h

Issue 3781009: Move the windows-specific scoped_* stuff from base to base/win and in the bas... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // TODO(brettw) remove this file when all callers are converted to using the
6 #define BASE_SCOPED_BSTR_WIN_H_ 6 // new location/namespace
7 #pragma once 7 #include "base/win/scoped_bstr.h"
8 8
9 #include <windows.h> 9 using base::win::ScopedBstr;
10 #include <oleauto.h>
11
12 #include "base/logging.h"
13
14 // Manages a BSTR string pointer.
15 // The class interface is based on scoped_ptr.
16 class ScopedBstr {
17 public:
18 ScopedBstr() : bstr_(NULL) {
19 }
20
21 // Constructor to create a new BSTR.
22 // NOTE: Do not pass a BSTR to this constructor expecting ownership to
23 // be transferred - even though it compiles! ;-)
24 explicit ScopedBstr(const wchar_t* non_bstr);
25 ~ScopedBstr();
26
27 // Give ScopedBstr ownership over an already allocated BSTR or NULL.
28 // If you need to allocate a new BSTR instance, use |allocate| instead.
29 void Reset(BSTR bstr = NULL);
30
31 // Releases ownership of the BSTR to the caller.
32 BSTR Release();
33
34 // Creates a new BSTR from a wide string.
35 // If you already have a BSTR and want to transfer ownership to the
36 // ScopedBstr instance, call |reset| instead.
37 // Returns a pointer to the new BSTR, or NULL if allocation failed.
38 BSTR Allocate(const wchar_t* wide_str);
39
40 // Allocates a new BSTR with the specified number of bytes.
41 // Returns a pointer to the new BSTR, or NULL if allocation failed.
42 BSTR AllocateBytes(int bytes);
43
44 // 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.
46 // SysAllocStringLen or SysAllocStringByteLen (call |AllocateBytes|) and
47 // then 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 //
52 // NOTE: The actual allocated size of the BSTR MUST be >= bytes.
53 // That responsibility is with the caller.
54 void SetByteLen(uint32 bytes);
55
56 // Swap values of two ScopedBstr's.
57 void Swap(ScopedBstr& bstr2);
58
59 // Retrieves the pointer address.
60 // Used to receive BSTRs as out arguments (and take ownership).
61 // The function DCHECKs on the current value being NULL.
62 // Usage: GetBstr(bstr.Receive());
63 BSTR* Receive();
64
65 // Returns number of chars in the BSTR.
66 uint32 Length() const;
67
68 // Returns the number of bytes allocated for the BSTR.
69 uint32 ByteLength() const;
70
71 operator BSTR() const {
72 return bstr_;
73 }
74
75 protected:
76 BSTR bstr_;
77
78 private:
79 // Forbid comparison of ScopedBstr types. You should never have the same
80 // BSTR owned by two different scoped_ptrs.
81 bool operator==(const ScopedBstr& bstr2) const;
82 bool operator!=(const ScopedBstr& bstr2) const;
83 DISALLOW_COPY_AND_ASSIGN(ScopedBstr);
84 };
85
86 // Template class to generate a BSTR from a static wide string
87 // without touching the heap. Use this class via the StackBstrVar and
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
102 // You shouldn't pass string pointers to this constructor since
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_
OLDNEW
« no previous file with comments | « base/process_util_win.cc ('k') | base/scoped_bstr_win.cc » ('j') | base/win/scoped_comptr.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698