Index: src/plugin/COM_Value.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/plugin/COM_Value.cpp |
@@ -0,0 +1,64 @@ |
+/** |
+ * \file COM_Value.cpp Support for the values used in COM and Automation. |
+ */ |
+#include "COM_Value.h" |
+#include <stdexcept> |
+ |
+using namespace AdblockPlus::COM; |
+ |
+/* |
+ * MSDN SysAllocStringLen function http://msdn.microsoft.com/en-us/library/windows/desktop/ms221639%28v=vs.85%29.aspx |
+ */ |
+BSTR_Argument::BSTR_Argument(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. |
+ */ |
+ bstr = nullptr; |
+ return; |
+ } |
+ // Assert length >= 1 |
+ bstr = 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 (!bstr) |
+ { |
+ throw std::bad_alloc(); |
+ } |
+} |
+ |
+/* |
+ * MSDN SysFreeString function http://msdn.microsoft.com/en-us/library/windows/desktop/ms221481%28v=vs.85%29.aspx |
+ */ |
+BSTR_Argument::~BSTR_Argument() |
+{ |
+ // There's no need to check for null; SysFreeString already handles that case. |
+ SysFreeString(bstr); |
+} |
+ |
+BSTR* BSTR_Argument::operator&() |
+{ |
+ // Assign the empty string, if not already empty. |
+ if (bstr) |
+ { |
+ SysFreeString(bstr); |
+ bstr = nullptr; |
+ } |
+ return &bstr; |
+} |
+ |
+BSTR_Argument::operator std::wstring() const |
+{ |
+ if (!bstr) |
+ { |
+ return std::wstring(); |
+ } |
+ return std::wstring(bstr, SysStringLen(bstr)); |
+} |