| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 | |
|
Oleksandr
2014/06/26 00:48:43
I guess COM_Client.h is not needed?
Eric
2014/06/26 15:13:56
I take it you mean COM_Client.cpp.
Right now, all
| |
| 2 /** | |
| 3 * Wrapper for BSTR values returned by COM components. | |
| 4 * | |
| 5 * The term "values returned" here means "out" parameters to COM interfaces. | |
| 6 * A number of IBrowser functions return allocated BSTR values this way, | |
| 7 * values that require the caller to free them. | |
| 8 * ATL provides CComBSTR for these return values. | |
| 9 * CComBSTR, however, implements a number of general purpose string functions, | |
| 10 * but the present one does not. | |
| 11 * | |
| 12 * The sole responsibility of this class is to manage the allocated resource. | |
| 13 * The supported pattern of use is exactly two steps: | |
| 14 * -# Construct an instance with the BSTR value returned from the COM call | |
| 15 * -# Copy the contents to a std::wstring. | |
| 16 * There generally no reason even to declare a separate variable of this type, | |
| 17 * since the value of the conversion operator can be assigned directly. | |
| 18 * | |
| 19 * This class does no error checking for null pointer values. | |
| 20 * There is, however, no default constructor to initialize one easily. | |
| 21 * The only constructor takes a BSTR argument, | |
| 22 * so the caller has the opportunity to test the value of the "out" argument. | |
| 23 */ | |
| 24 class Returned_BSTR | |
|
Oleksandr
2014/06/26 00:48:43
This multiple classes approach is a bit confusing,
Eric
2014/06/26 15:13:56
The biggest confusion to my eye is the naming. I d
sergei
2014/07/08 11:58:34
I also would like to have only one class.
As well
Eric
2014/07/08 17:46:37
Here's the place for the longer rant about address
| |
| 25 { | |
| 26 /** | |
| 27 * The underlying BSTR variable. | |
| 28 */ | |
| 29 wchar_t * _bstr ; | |
| 30 | |
| 31 public: | |
| 32 /** | |
| 33 * Ordinary constructor. | |
| 34 */ | |
| 35 Returned_BSTR( wchar_t * b ) | |
| 36 : _bstr( b ) | |
| 37 {} | |
| 38 | |
| 39 ~Returned_BSTR() | |
| 40 { | |
| 41 ::SysFreeString( _bstr ); | |
| 42 } | |
| 43 | |
| 44 operator std::wstring() | |
| 45 { | |
| 46 return std::wstring( _bstr, ::SysStringLen( _bstr ) ); | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 class Invoke_Result | |
| 51 { | |
| 52 VARIANT & _result; | |
| 53 | |
| 54 public: | |
| 55 /** | |
| 56 * Ordinary constructor. | |
| 57 * | |
| 58 * The caller has responsibility for checking that the result pointer argument in Invoke() is not null. | |
| 59 * Thus the argument is a reference, not a pointer, to ensure that the caller fulfills its responsibility. | |
| 60 */ | |
| 61 Invoke_Result( VARIANT & result ) | |
| 62 : _result( result ) | |
| 63 {} | |
| 64 | |
| 65 void assign_wstring( std::wstring x ) | |
| 66 { | |
| 67 _result.vt = VT_BSTR; | |
| 68 _result.bstrVal = SysAllocString( x.c_str() ); | |
| 69 } | |
| 70 }; | |
| 71 | |
| 72 inline void invoke_result_wstring( VARIANT & result, std::wstring s ) | |
| 73 { | |
| 74 Invoke_Result ir( result ); | |
| 75 ir.assign_wstring( s ); | |
| 76 } | |
| 77 | |
| 78 class BSTR_Argument | |
| 79 { | |
| 80 BSTR _bstr; | |
| 81 public: | |
| 82 BSTR_Argument( std::wstring s ) | |
| 83 : _bstr( SysAllocStringLen( s.c_str(), s.length() ) ) | |
| 84 { | |
| 85 // Preserve invariant that _bstr is not null | |
| 86 if ( !_bstr ) | |
| 87 { | |
| 88 // Assert SysAllocStringLen return null, meaning out of memory | |
| 89 throw std::bad_alloc(); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 ~BSTR_Argument() | |
| 94 { | |
| 95 SysFreeString( _bstr ); | |
| 96 } | |
| 97 | |
| 98 inline operator BSTR() | |
| 99 { | |
| 100 return _bstr; | |
| 101 } | |
| 102 }; | |
| 103 | |
| 104 /** | |
| 105 * Constructor class for incoming BSTR argument for dispatch entry points. | |
| 106 * | |
| 107 * The BSTR coming in as arguments from dispatch calls are allocated by the call er. | |
| 108 * We don't need to manage the lifetime of these arguments. | |
| 109 * Since we are immediately converting to wstring, we don't need to allocate our own BSTR. | |
| 110 * Thus, even though the constructor pattern looks like BSTR_Argument, | |
| 111 * we don't want the allocate/free behavior of its implementation. | |
| 112 */ | |
| 113 class Incoming_BSTR | |
| 114 : public std::wstring | |
| 115 { | |
| 116 public: | |
| 117 Incoming_BSTR( BSTR bstr ) | |
| 118 : std::wstring( bstr, SysStringLen( bstr ) ) | |
| 119 {} | |
| 120 }; | |
| OLD | NEW |