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 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 } |
OLD | NEW |