OLD | NEW |
(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 SysFreeString function http://msdn.microsoft.com/en-us/library/windows/d
esktop/ms221481%28v=vs.85%29.aspx |
| 11 */ |
| 12 BSTR_Param::~BSTR_Param() |
| 13 { |
| 14 // There's no need to check for null; SysFreeString already handles that case. |
| 15 SysFreeString(bstr); |
| 16 } |
| 17 |
| 18 namespace { |
| 19 /* |
| 20 * MSDN SysAllocStringLen function http://msdn.microsoft.com/en-us/library/win
dows/desktop/ms221639%28v=vs.85%29.aspx |
| 21 */ |
| 22 BSTR construct_BSTR_ParamArgument(const std::wstring& s) |
| 23 { |
| 24 auto length = s.length(); |
| 25 if (length == 0) |
| 26 { |
| 27 /* |
| 28 * BSTR makes no distinction between null pointers and non-null pointers t
o the empty string. |
| 29 * In order to detect allocation failure reliably, we simply don't allocat
e empty strings. |
| 30 * Compare with the default constructor. |
| 31 */ |
| 32 return nullptr; |
| 33 } |
| 34 // Assert length >= 1 |
| 35 BSTR b = SysAllocStringLen(s.c_str(), length); |
| 36 /* |
| 37 * Since the string is non-empty, |
| 38 * if the return value of SysAllocStringLen is null it means that it had a
n allocation failure. |
| 39 */ |
| 40 if (!b) |
| 41 { |
| 42 throw std::bad_alloc(); |
| 43 } |
| 44 return b; |
| 45 } |
| 46 } |
| 47 |
| 48 BSTR_ParamArgument::BSTR_ParamArgument(const std::wstring& s) |
| 49 : BSTR_Param(construct_BSTR_ParamArgument(s)) |
| 50 {} |
| 51 |
| 52 BSTR* BSTR_ParamResult::operator&() |
| 53 { |
| 54 // Assign the empty string, if not already empty. |
| 55 if (bstr) |
| 56 { |
| 57 SysFreeString(bstr); |
| 58 bstr = nullptr; |
| 59 } |
| 60 return &bstr; |
| 61 } |
| 62 |
| 63 BSTR_ParamResult::operator std::wstring() const |
| 64 { |
| 65 if (!bstr) |
| 66 { |
| 67 return std::wstring(); |
| 68 } |
| 69 return std::wstring(bstr, SysStringLen(bstr)); |
| 70 } |
OLD | NEW |