| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 /** | 1 /** |
| 2 * \file COM_Value.h Support for the values used in COM and Automation. | 2 * \file COM_Value.h Support for the values used in COM and Automation. |
| 3 */ | 3 */ |
| 4 #ifndef COM_VALUE_H | 4 #ifndef COM_VALUE_H |
| 5 #define COM_VALUE_H | 5 #define COM_VALUE_H |
| 6 | 6 |
| 7 #include <wtypes.h> | 7 #include <wtypes.h> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 namespace AdblockPlus | 10 namespace AdblockPlus |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 * In both cases, we must release an allocated string, whether or not we all ocated it. | 22 * In both cases, we must release an allocated string, whether or not we all ocated it. |
| 23 * Because of this commonality, it's possible to combine these two uses into a single class. | 23 * Because of this commonality, it's possible to combine these two uses into a single class. |
| 24 * | 24 * |
| 25 * The two life cycles differ in how the allocation is done. | 25 * The two life cycles differ in how the allocation is done. |
| 26 * When used as an argument, the caller must allocate. | 26 * When used as an argument, the caller must allocate. |
| 27 * In this case the caller uses the non-trivial constructor, which allocates a copy of its argument. | 27 * In this case the caller uses the non-trivial constructor, which allocates a copy of its argument. |
| 28 * When used for a result, the called function allocates. | 28 * When used for a result, the called function allocates. |
| 29 * In this case the caller uses the default constructor and the address-of o perator; | 29 * In this case the caller uses the default constructor and the address-of o perator; |
| 30 * the address-of operator does not allocate and its pointer must be assig ned an allocated value. | 30 * the address-of operator does not allocate and its pointer must be assig ned an allocated value. |
| 31 * | 31 * |
| 32 * Copy/move constructor/assignment are all deleted, not because they couldn 't be implemented, | 32 * Copy/move constructor/assignment are all deleted, not because they couldn 't be implemented, |
| 33 * but because these class are meant to be used in tight conjunction with API calls. | 33 * but because these class are meant to be used in tight conjunction with API calls. |
| 34 * Thus their design use requires that they not leave this proximity. | 34 * Thus their design use requires that they not leave this proximity. |
| 35 * | 35 * |
| 36 * \par Reference | 36 * \par Reference |
| 37 * MSDN "Allocating and Releasing Memory for a BSTR" http://msdn.microsoft .com/en-us/library/vstudio/xda6xzx7%28v=vs.120%29.aspx | 37 * MSDN "Allocating and Releasing Memory for a BSTR" http://msdn.microsoft .com/en-us/library/vstudio/xda6xzx7%28v=vs.120%29.aspx |
| 38 * "When you call into a function that expects a BSTR argument, you must a llocate the memory for the BSTR before the call and release it afterwards." | 38 * "When you call into a function that expects a BSTR argument, you must a llocate the memory for the BSTR before the call and release it afterwards." |
| 39 * "When you call into a function that returns a BSTR, you must free the s tring yourself." | 39 * "When you call into a function that returns a BSTR, you must free the s tring yourself." |
| 40 * | 40 * |
| 41 * \invariant Either bstr == nullptr or bstr is a non-null system-allocated BSTR. | 41 * \invariant Either bstr == nullptr or bstr is a non-null system-allocated BSTR. |
| 42 */ | 42 */ |
| 43 class BSTR_Argument | 43 class BSTR_Argument |
| 44 { | 44 { |
| 45 /** | 45 /** |
| 46 * The underlying BSTR pointer. | 46 * The underlying BSTR pointer. |
| 47 */ | 47 */ |
| 48 BSTR bstr; | 48 BSTR bstr; |
| 49 | 49 |
| 50 public: | 50 public: |
| 51 /** | 51 /** |
| 52 * Default constructor has the value of the empty string. | 52 * Default constructor has the value of the empty string. |
| 53 */ | 53 */ |
| 54 BSTR_Argument() | 54 BSTR_Argument() |
| 55 : bstr( nullptr ) | 55 : bstr(nullptr) |
| 56 { | 56 { |
| 57 } | 57 } |
| 58 | 58 |
| 59 /** | 59 /** |
| 60 * Constructor from std::wstring. | 60 * Constructor from std::wstring. |
| 61 */ | 61 */ |
| 62 BSTR_Argument( const std::wstring& s ); | 62 BSTR_Argument(const std::wstring& s); |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * Destructor | 65 * Destructor |
| 66 * | 66 * |
| 67 * The destructor frees the BSTR. | 67 * The destructor frees the BSTR. |
| 68 * Do not use this class when freeing the BSTR is not our responsibility. | 68 * Do not use this class when freeing the BSTR is not our responsibility. |
| 69 */ | 69 */ |
| 70 ~BSTR_Argument(); | 70 ~BSTR_Argument(); |
| 71 | 71 |
| 72 /** | 72 /** |
| 73 * Conversion operator to BSTR. | 73 * Conversion operator to BSTR. |
| 74 */ | 74 */ |
| 75 operator BSTR() const | 75 operator BSTR() const |
| 76 { | 76 { |
| 77 return bstr; | 77 return bstr; |
| 78 } | 78 } |
| 79 | 79 |
| 80 /** | 80 /** |
| 81 * Conversion operator to std::wstring | 81 * Conversion operator to std::wstring |
| 82 */ | 82 */ |
| 83 operator std::wstring() const; | 83 operator std::wstring() const; |
| 84 | 84 |
| 85 /** | 85 /** |
| 86 * Address-of operator. | 86 * Address-of operator. |
| 87 * | 87 * |
| 88 * This operator is used for assignment directly into our underlying point er. | 88 * This operator is used for assignment directly into our underlying point er. |
| 89 * In order to avoid leaking memory, this operator also implicitly assigns the null string. | 89 * In order to avoid leaking memory, this operator also implicitly assigns the null string. |
| 90 * Specifically, this operator is not 'const'. | 90 * Specifically, this operator is not 'const'. |
| 91 * | 91 * |
| 92 * \postcondition | 92 * \par postcondition |
| 93 * bstr == nullptr | 93 * bstr == nullptr |
| 94 */ | 94 */ |
| 95 BSTR* operator&(); | 95 BSTR* operator&(); |
| 96 | 96 |
| 97 private: | 97 private: |
| 98 /** | 98 /** |
| 99 * Copy constructor is deleted | 99 * Copy constructor is deleted |
| 100 */ | 100 */ |
| 101 BSTR_Argument( const BSTR_Argument& ); // = delete | 101 BSTR_Argument(const BSTR_Argument&); // = delete |
| 102 | 102 |
| 103 /** | 103 /** |
| 104 * Move constructor is deleted | 104 * Move constructor is deleted |
| 105 */ | 105 */ |
| 106 BSTR_Argument( BSTR_Argument&& ); // = delete | 106 BSTR_Argument(BSTR_Argument&&); // = delete |
| 107 | 107 |
| 108 /** | 108 /** |
| 109 * Copy assignment is deleted | 109 * Copy assignment is deleted |
| 110 */ | 110 */ |
| 111 BSTR_Argument& operator=( const BSTR_Argument& ); // = delete | 111 BSTR_Argument& operator=(const BSTR_Argument&); // = delete |
| 112 | 112 |
| 113 /** | 113 /** |
| 114 * Move assignment is deleted | 114 * Move assignment is deleted |
| 115 */ | 115 */ |
| 116 BSTR_Argument& operator=( BSTR_Argument&& ); // = delete | 116 BSTR_Argument& operator=(BSTR_Argument&&); // = delete |
| 117 }; | 117 }; |
| 118 | 118 |
| 119 /** | 119 /** |
| 120 * Constructor class for BSTR [in] parameters arriving from our COM entry po ints. | 120 * Constructor class for [in] parameters arriving from our COM entry points. |
| 121 * | 121 * |
| 122 * The life cycle of a BSTR value that comes to us as an [in] parameter is e ntirely managed by the caller. | 122 * The life cycle of a VARIANT value that comes to us as an [in] parameter i s entirely managed by the caller. |
| 123 * Thus this class simply derives from std::wstring and provides an appropri ate constructor. | 123 * Thus the responsbilities of this class are for type checking and value co nversion, |
| 124 * but not life cycle mangement. | |
| 124 * | 125 * |
| 125 * This class is a narrow technique toward a larger goal of eliminating CCom BSTR, | 126 * This class is specifically for use within COM server functions such as fo r IDispatch::Invoke. |
| 126 * which had been used for these parameters. | 127 * |
| 127 * All the uses, however, had been for IDispatch entry points, which are all VARIANT. | 128 * \par Status |
| 128 * In all cases, the caller much first check the type of the VARIANT structu re before constructing an instance. | 129 * This class is, in part, written for issue #1163 "Clean up IDispatch::In voke implementations". |
| 129 * Perhaps better would be a class, say, wstring_Incoming_Param that took a VARIANT as a constructor argument | 130 * |
| 130 * and converted it to a string. | |
| 131 * Such a class, however, is beyond the scope of the work that this class wa s a part of. | |
| 132 */ | 131 */ |
| 133 class Incoming_Param | 132 class IncomingParam |
| 134 : public std::wstring | |
| 135 { | 133 { |
| 134 /** | |
| 135 * The underlying variant value. | |
| 136 * | |
| 137 * It's stored as received and does receive any life cycle treatment. | |
| 138 */ | |
| 139 VARIANT var; | |
| 140 | |
| 136 public: | 141 public: |
| 137 Incoming_Param( BSTR b ); | 142 IncomingParam( VARIANT v ) |
|
sergei
2014/07/28 09:24:31
Instead of commenting all bad things here, I will
Eric
2014/07/29 19:53:08
Oh, that's just awful. If there's one thing I've l
| |
| 143 : var( v ) | |
| 144 { | |
| 145 } | |
| 146 | |
| 147 /** | |
| 148 * Predicate whether the variant is convertible to std::wstring. | |
| 149 * | |
| 150 * This predicate incorporates both type compatibility and implementation support. | |
| 151 * Some types that could be converted are not, simply because they're not used in the present code base. | |
| 152 */ | |
| 153 bool wstringConvertible() const; | |
| 154 | |
| 155 /** | |
| 156 * Convert variant to std::wstring and if not convertible return the empty string. | |
| 157 */ | |
| 158 std::wstring wstringValueNoexcept() const; | |
| 138 }; | 159 }; |
| 139 } | 160 } |
| 140 } | 161 } |
| 141 | 162 |
| 142 #endif | 163 #endif |
| LEFT | RIGHT |