Index: src/plugin/COM_Value.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/plugin/COM_Value.cpp |
@@ -0,0 +1,70 @@ |
+/** |
+ * \file COM_Value.cpp Support for the values used in COM and Automation. |
+ */ |
+#include "COM_Value.h" |
+#include <stdexcept> |
+ |
+using namespace AdblockPlus::COM; |
+ |
+/* |
+ * MSDN SysFreeString function http://msdn.microsoft.com/en-us/library/windows/desktop/ms221481%28v=vs.85%29.aspx |
+ */ |
+BSTR_Param::~BSTR_Param() |
+{ |
+ // There's no need to check for null; SysFreeString already handles that case. |
+ SysFreeString(bstr); |
+} |
+ |
+namespace { |
+ /* |
+ * MSDN SysAllocStringLen function http://msdn.microsoft.com/en-us/library/windows/desktop/ms221639%28v=vs.85%29.aspx |
+ */ |
+ BSTR construct_BSTR_ParamArgument(const std::wstring& s) |
+ { |
+ auto length = s.length(); |
+ if (length == 0) |
+ { |
+ /* |
+ * BSTR makes no distinction between null pointers and non-null pointers to the empty string. |
+ * In order to detect allocation failure reliably, we simply don't allocate empty strings. |
+ * Compare with the default constructor. |
+ */ |
+ return nullptr; |
+ } |
+ // Assert length >= 1 |
+ BSTR b = SysAllocStringLen(s.c_str(), length); |
+ /* |
+ * Since the string is non-empty, |
+ * if the return value of SysAllocStringLen is null it means that it had an allocation failure. |
+ */ |
+ if (!b) |
+ { |
+ throw std::bad_alloc(); |
+ } |
+ return b; |
+ } |
+} |
+ |
+BSTR_ParamArgument::BSTR_ParamArgument(const std::wstring& s) |
+ : BSTR_Param(construct_BSTR_ParamArgument(s)) |
+{} |
+ |
+BSTR* BSTR_ParamResult::operator&() |
+{ |
+ // Assign the empty string, if not already empty. |
+ if (bstr) |
+ { |
+ SysFreeString(bstr); |
+ bstr = nullptr; |
+ } |
+ return &bstr; |
+} |
+ |
+BSTR_ParamResult::operator std::wstring() const |
+{ |
+ if (!bstr) |
+ { |
+ return std::wstring(); |
+ } |
+ return std::wstring(bstr, SysStringLen(bstr)); |
+} |