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

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

Issue 5070706781978624: Issue #276 - introduce class BSTR_Argument (Closed)
Patch Set: Created July 29, 2014, 4:58 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/COM_Value.h ('k') | src/plugin/PluginClass.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.cpp Support for the values used in COM and Automation.
3 */
4 #include "COM_Value.h"
5 #include <stdexcept>
6
7 using namespace AdblockPlus::COM;
8
9 /*
10 * MSDN SysAllocStringLen function http://msdn.microsoft.com/en-us/library/windo ws/desktop/ms221639%28v=vs.85%29.aspx
11 */
12 BSTR_Argument::BSTR_Argument(const std::wstring& s)
13 {
14 auto length = s.length();
15 if (length == 0)
16 {
17 /*
18 * BSTR makes no distinction between null pointers and non-null pointers to the empty string.
19 * In order to detect allocation failure reliably, we simply don't allocate empty strings.
20 * Compare with the default constructor.
21 */
22 bstr = nullptr;
23 return;
24 }
25 // Assert length >= 1
26 bstr = SysAllocStringLen(s.c_str(), length);
27 /*
28 * Since the string is non-empty,
29 * if the return value of SysAllocStringLen is null it means that it had an allocation failure.
30 */
31 if (!bstr)
32 {
33 throw std::bad_alloc();
34 }
35 }
36
37 /*
38 * MSDN SysFreeString function http://msdn.microsoft.com/en-us/library/windows/d esktop/ms221481%28v=vs.85%29.aspx
39 */
40 BSTR_Argument::~BSTR_Argument()
41 {
42 // There's no need to check for null; SysFreeString already handles that case.
43 SysFreeString(bstr);
44 }
45
46 BSTR* BSTR_Argument::operator&()
47 {
48 // Assign the empty string, if not already empty.
49 if (bstr)
50 {
51 SysFreeString(bstr);
52 bstr = nullptr;
53 }
54 return &bstr;
55 }
56
57 BSTR_Argument::operator std::wstring() const
58 {
59 if (!bstr)
60 {
61 return std::wstring();
62 }
63 return std::wstring(bstr, SysStringLen(bstr));
64 }
OLDNEW
« no previous file with comments | « src/plugin/COM_Value.h ('k') | src/plugin/PluginClass.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld