Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/plugin/COM_Value.cpp

Issue 5070706781978624: Issue #276 - introduce class BSTR_Argument (Closed)
Patch Set: small correction over patch set 2 Created July 29, 2014, 7:22 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/plugin/COM_Value.h ('k') | src/plugin/PluginClass.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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));
+}
« no previous file with comments | « src/plugin/COM_Value.h ('k') | src/plugin/PluginClass.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld