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 25, 2014, 9:32 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
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 )
Eric 2014/07/30 14:14:42 If you initialize something in the constructor ini
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;
sergei 2014/07/28 09:18:26 The comment should be in the header near the ctr,
Eric 2014/07/29 16:59:27 This is an implementation detail, not some basic p
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.
sergei 2014/07/28 09:18:26 I don't like the changing of the value when the ad
Eric 2014/07/29 16:59:27 Directly assigning to internal members of smart po
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

Powered by Google App Engine
This is Rietveld