-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTR.h
176 lines (139 loc) · 3.57 KB
/
BSTR.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once
//! wrapper class for BSTR values
/*!
This class is for BSTR data manipilation. The main data type is a BSTR.
The BSTR is cleaned up at the destruction of this class.
Please take great care of knowing who owns the content of a BSTR and therefore
who is responsible for freeing the content; this shows from [in], [in, out] and
[out, retval] annotations in the IDL
Implementations in this class take into account that an empty string should
be represented by NULL
*/
class CBSTR
{
private:
BSTR value; /*!< the value contained in this class */
public:
//! Constructor.
/*!
Constructor that creates an empty value
*/
CBSTR()
{value=NULL;
}
//! Copy constructor.
/*!
Constructor used for assignment
\param other Value to copy
*/
CBSTR(const CBSTR &other)
{if (&other!=this)
value=SysAllocString(other.value);
}
//! Constructor.
/*!
Constructor that allocated a BSTR value given a string value
\param value Value to initiaze from
*/
CBSTR(const OLECHAR *value)
{this->value=NULL;
if (value)
if (*value)
this->value=SysAllocString(value);
}
//! Destructor.
/*!
Cleans up the value, if any
*/
~CBSTR()
{if (value) SysFreeString(value);
}
//! Set from a BSTR value
/*!
Set from a valid BSTR value; this class will own the BSTR value afterwards and the caller should not destroy it!
\param b BSTR that we will own
*/
void SetFromBSTR(BSTR b)
{if (value) SysFreeString(value);
value=b;
}
//! Assignment from other CBSTR
/*!
Set from a CBSTR value; the value will be copied
*/
void operator=(const CBSTR other)
{if (&other!=this)
{if (value) SysFreeString(value);
value=NULL;
if (other.value)
if (*other.value)
value=SysAllocString(other.value);
}
}
//! Set from a string value
/*!
Set from a string value; the value will be allocated and owned by this object
*/
void operator=(const OLECHAR *str)
{if (value) SysFreeString(value);
value=NULL;
if (str)
if (*str)
value=SysAllocString(str);
}
//! Check for empty BSTR
/*!
Returns true if value is NULL or empty.
*/
bool operator!()
{if (!value) return true;
if (!lstrlen(value)) return true;
return false;
}
//! String comparison
/*!
Returns true if two BSTR values or string values are equivalent. Takes special care of assuming NULL denoting an empty string.
\param str1 First string in comparison
\param str2 Second string in comparison
*/
static BOOL Same(const OLECHAR *str1,const OLECHAR *str2)
{BOOL res=FALSE;
if (str1)
{if (str2) res=(lstrcmpiW(str1,str2)==0);
else if (*str1==0) res=TRUE;
}
else
if (!str2) res=TRUE;
else if (*str2==0) res=TRUE;
return res;
}
//! String comparison
/*!
Returns true if BSTR value is equivalent to string value. Takes special care of assuming NULL denoting an empty string.
\param str2 String to compare to
*/
BOOL Same(const OLECHAR *str2)
{BOOL res=FALSE;
if (value)
{if (str2) res=(lstrcmpiW(value,str2)==0);
else if (*value==0) res=TRUE;
}
else
if (!str2) res=TRUE;
else if (*str2==0) res=TRUE;
return res;
}
//! Length
/*!
Returns the length of the content
*/
int Length()
{return (value)?lstrlenW(value):0;
}
//! cast to BSTR value
/*!
this object can be used directly as BSTR (as BSTR is defined as OLECHAR *, therefore also as OLECHAR *)
*/
operator BSTR()
{return value;}
};