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: Created July 25, 2014, 9:32 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
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 )
Eric 2014/07/30 14:14:42 If you initialize something in the constructor ini
+{
+ 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;
sergei 2014/07/28 09:18:26 The comment should be in the header near the ctr,
Eric 2014/07/29 16:59:27 This is an implementation detail, not some basic p
+ 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.
sergei 2014/07/28 09:18:26 I don't like the changing of the value when the ad
Eric 2014/07/29 16:59:27 Directly assigning to internal members of smart po
+ 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 ) );
+}

Powered by Google App Engine
This is Rietveld