Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/plugin/COM_Value.h

Issue 5140323101573120: Issue #276 - introduce classes BSTR_ParamArgument and BSTR_ParamResult (Closed)
Patch Set: Created Aug. 4, 2014, 2:41 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/plugin/AdblockPlusDomTraverser.cpp ('k') | src/plugin/COM_Value.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /**
2 * \file COM_Value.h Support for the values used in COM and Automation.
3 *
4 * \par BSTR
5 * MSDN BSTR http://msdn.microsoft.com/en-us/library/windows/desktop/ms221069% 28v=vs.85%29.aspx
6 *
7 * \par VARIANT
8 * MSDN VARIANT structure http://msdn.microsoft.com/en-us/library/windows/desk top/ms221627%28v=vs.85%29.aspx
9 * MSDN VARENUM enumeration http://msdn.microsoft.com/en-us/library/windows/de sktop/ms221170%28v=vs.85%29.aspx
10 */
11 #ifndef COM_VALUE_H
12 #define COM_VALUE_H
13
14 #include <wtypes.h>
15 #include <string>
16
17 namespace AdblockPlus
18 {
19 namespace COM
20 {
21 /**
22 * Base class for BSTR life cycle manager classes, used to pass BSTR to a CO M call.
23 *
24 * There are two different life cycles for BSTR relevant here.
25 * In the first, the BSTR acts as an ordinary argument; in the second, the B STR is a return value.
26 * Both these are implemented as arguments to a COM function;
27 * the difference is that one is an [in] param and the other is an [out] p aram.
28 * The subclass for [in] params is BSTR_ParamArgument.
29 * The subclass for [out] params is BSTR_ParamResult.
30 *
31 * These two life cycles differ in allocation, but not in deallocation.
32 * When used as an argument, the caller must allocate.
33 * BSTR_ParamArgument uses the non-trivial constructor, which allocates a co py of its argument.
34 * When used for a result, the called function allocates.
35 * BSTR_ParamResult uses the default constructor and implements an address-o f operator;
36 * the address-of operator does not allocate and its pointer must be assig ned an allocated value.
37 * In both cases the caller deallocates the BSTR.
38 * Because of this commonality, the present base class implements a destruct or common to its subclasses.
39 *
40 * Copy/move constructor/assignment are all deleted, not because they couldn 't be implemented,
41 * but because these class are meant to be used in tight conjunction with API calls.
42 * Their design use requires that they not leave this proximity.
43 * Deleting copy and move helps to ensure this.
44 *
45 * \par Design Notes
46 * This class is a base class only.
47 * It turns out that it's not felicitous to combine the subclasses with th e base.
48 * The trouble is that having conversions both to BSTR and std::wstring ca uses problems in practice.
49 * Both of these string types define implicit conversions to wchar_t *.
50 * Having both conversions defined on a single class leads to lots of unne cessary explicit conversions.
51 *
52 * \par Reference
53 * MSDN "Allocating and Releasing Memory for a BSTR" http://msdn.microsoft .com/en-us/library/vstudio/xda6xzx7%28v=vs.120%29.aspx
54 * "When you call into a function that expects a BSTR argument, you must a llocate the memory for the BSTR before the call and release it afterwards."
55 * "When you call into a function that returns a BSTR, you must free the s tring yourself."
56 *
57 * \invariant Either bstr == nullptr or bstr is a non-null system-allocated BSTR.
58 */
59 class BSTR_Param
60 {
61 protected:
62 /**
63 * The underlying BSTR pointer.
64 */
65 BSTR bstr;
66
67 /**
68 * Regular constructor has the value of the empty string.
69 *
70 * Subclasses have the responsibility to maintain out class invariant rega rding member `bstr`.
71 */
72 BSTR_Param(BSTR b)
73 : bstr(b)
74 {
75 }
76
77 /**
78 * Destructor
79 *
80 * The destructor frees the BSTR.
81 * Do not use this class when freeing the BSTR is not our responsibility.
82 */
83 ~BSTR_Param();
84
85 private:
86 /**
87 * Copy constructor is deleted
88 */
89 BSTR_Param(const BSTR_Param&); // = delete
90
91 /**
92 * Move constructor is deleted
93 */
94 BSTR_Param(BSTR_Param&&); // = delete
95
96 /**
97 * Copy assignment is deleted
98 */
99 BSTR_Param& operator=(const BSTR_Param&); // = delete
100
101 /**
102 * Move assignment is deleted
103 */
104 BSTR_Param& operator=(BSTR_Param&&); // = delete
105 };
106
107 /**
108 * BSTR life cycle manager.
109 * Used by a caller to pass a BSTR parameter as an argument to a COM functio n.
110 *
111 * This class passes a BSTR value to an [in] parameter of a COM call.
112 * As a conversion class std::wstring --> BSTR.
113 */
114 class BSTR_ParamArgument
115 : protected BSTR_Param
116 {
117 public:
118 /**
119 * Constructor from std::wstring.
120 */
121 BSTR_ParamArgument(const std::wstring& s);
122
123 /**
124 * Conversion operator to BSTR.
125 */
126 operator BSTR() const
127 {
128 return bstr;
129 }
130 };
131
132 /**
133 * BSTR life cycle manager.
134 * Used by a caller to pass a BSTR parameter to receive a result from a COM function.
135 *
136 * This class receives a BSTR value from an [out] parameter of a COM call.
137 * As a conversion class, it converts only in the direction BSTR --> std::ws tring.
138 */
139 class BSTR_ParamResult
140 : protected BSTR_Param
141 {
142 public:
143 /**
144 * As the receiver of an external value, BSTR_ParamResult always has an em pty initial value.
145 */
146 BSTR_ParamResult()
147 : BSTR_Param(nullptr)
148 {
149 }
150
151 /**
152 * Conversion operator to std::wstring
153 */
154 operator std::wstring() const;
155
156 /**
157 * Address-of operator.
158 *
159 * This operator is used for assignment directly into our underlying point er.
160 * In order to avoid leaking memory, this operator also implicitly assigns the null string.
161 * Specifically, this operator is not 'const'.
162 *
163 * \par postcondition
164 * - bstr == nullptr
165 * - return value is not null
166 * - does not throw
167 */
168 BSTR* operator&(); // noexcept
169 };
170
171 }
172 }
173
174 #endif
OLDNEW
« no previous file with comments | « src/plugin/AdblockPlusDomTraverser.cpp ('k') | src/plugin/COM_Value.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld