soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SBStr.h
1#ifndef __SBSTR_H__
2#define __SBSTR_H__
3
4#include <windows.h>
5#include <oleauto.h>
6
7typedef WCHAR char16;
8typedef unsigned int uint32;
9
10SNSBEGIN
11
12// Manages a BSTR string pointer.
13// The class interface is based on scoped_ptr.
14class SOUI_EXP sbstr {
15 public:
16 /**
17 * @brief Default constructor
18 * Initializes the BSTR pointer to NULL.
19 */
20 sbstr()
21 : bstr_(NULL)
22 {
23 }
24
25 /**
26 * @brief Constructor to create a new BSTR from a wide character string
27 * @param non_bstr Pointer to the wide character string
28 * NOTE: Do not pass a BSTR to this constructor expecting ownership to be transferred - even though it compiles! ;-)
29 */
30 explicit sbstr(const char16 *non_bstr);
31
32 /**
33 * @brief Constructor to create a new BSTR with a specified size from a wide character string
34 * @param nSize Size of the BSTR in characters
35 * @param non_bstr Pointer to the wide character string
36 */
37 explicit sbstr(int nSize, const char16 *non_bstr);
38
39 /**
40 * @brief Destructor
41 * Releases the BSTR if it is not NULL.
42 */
43 ~sbstr();
44
45 /**
46 * @brief Give sbstr ownership over an already allocated BSTR or NULL
47 * @param bstr Pointer to the BSTR to take ownership of
48 * If you need to allocate a new BSTR instance, use |Allocate| instead.
49 */
50 void Reset(BSTR bstr = NULL);
51
52 /**
53 * @brief Releases ownership of the BSTR to the caller
54 * @return Pointer to the released BSTR
55 */
56 BSTR Release();
57
58 /**
59 * @brief Creates a new BSTR from a wide character string
60 * @param str Pointer to the wide character string
61 * @return Pointer to the new BSTR, or NULL if allocation failed
62 * If you already have a BSTR and want to transfer ownership to the sbstr instance, call |Reset| instead.
63 */
64 BSTR Allocate(const char16 *str);
65
66 /**
67 * @brief Allocates a new BSTR with the specified number of bytes
68 * @param bytes Number of bytes to allocate
69 * @return Pointer to the new BSTR, or NULL if allocation failed
70 */
71 BSTR AllocateBytes(size_t bytes);
72
73 /**
74 * @brief Sets the allocated length field of the already-allocated BSTR to be |bytes|
75 * @param bytes Number of bytes to set as the length
76 * This is useful when the BSTR was preallocated with e.g. SysAllocStringLen or SysAllocStringByteLen (call |AllocateBytes|) and then not all the bytes are being used.
77 * NOTE: The actual allocated size of the BSTR MUST be >= bytes. That responsibility is with the caller.
78 */
79 void SetByteLen(size_t bytes);
80
81 /**
82 * @brief Swap values of two sbstr's
83 * @param bstr2 Reference to the sbstr to swap with
84 */
85 void Swap(sbstr &bstr2);
86
87 /**
88 * @brief Retrieves the pointer address
89 * @return Pointer to the BSTR pointer
90 * Used to receive BSTRs as out arguments (and take ownership).
91 * The function DCHECKs on the current value being NULL.
92 * Usage: GetBstr(bstr.Receive());
93 */
94 BSTR *Receive();
95
96 /**
97 * @brief Returns the number of characters in the BSTR
98 * @return Number of characters in the BSTR
99 */
100 size_t Length() const;
101
102 /**
103 * @brief Returns the number of bytes allocated for the BSTR
104 * @return Number of bytes allocated for the BSTR
105 */
106 size_t ByteLength() const;
107
108 /**
109 * @brief Implicit conversion operator to BSTR
110 * @return Pointer to the BSTR
111 */
112 operator BSTR() const
113 {
114 return bstr_;
115 }
116
117 protected:
118 BSTR bstr_;
119
120 private:
121 // Forbid comparison of sbstr types. You should never have the same BSTR owned by two different sbstr instances.
122 bool operator==(const sbstr &bstr2) const;
123 bool operator!=(const sbstr &bstr2) const;
124 DISALLOW_COPY_AND_ASSIGN(sbstr);
125};
126
127SNSEND
128
129#endif //__SBSTR_H__