OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// Borland WinSys Library
3// Copyright (c) 1994, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Definition of class TString, a flexible universal string envelope class.
7/// Facilitates efficient construction and assignment of many string types
8//----------------------------------------------------------------------------
9
10#if !defined(OWL_STRING_H)
11#define OWL_STRING_H
12
13#include <owl/private/defs.h>
14#if defined(BI_HAS_PRAGMA_ONCE)
15# pragma once
16#endif
17
18#include <owl/private/ole2inc.h>
19#include <owl/private/memory.h>
20#include <owl/module.h>
21#include <owl/lclstrng.h>
22
23namespace owl {
24
25#include <owl/preclass.h>
26
27//
28/// \class TSysStr
29// ~~~~~ ~~~~~~~
30/// System string (BSTR) encapsulation. Also gives BSTRs a unique type
31/// Always assumes ownership of the BSTR, use Relinquish to take BSTR away.
32//
34 public:
35 TSysStr() : P(0) {}
36 TSysStr(const BSTR p) : P(p) {}
37 TSysStr(const TSysStr& src) : P(TOleAuto::SysAllocString(src.P)) {}
38
40
41 int operator !() const {return P == 0;}
42 operator const BSTR() const {return P;}
43 operator BSTR() {return P;}
44
45 TSysStr& operator =(BSTR p) {if (P) TOleAuto::SysFreeString(P); P = p; return *this;}
46 operator BSTR *() {if (P) {TOleAuto::SysFreeString(P); P = 0;} return &P;}
47
48 BSTR Relinquish() {BSTR p = P; P = 0; return p;}
49
50 protected:
51 BSTR P;
52
53 private:
54 void* operator new(size_t) noexcept {return nullptr;} // prohibit use of new, delete, etc
55 void operator delete(void*) {}
56};
57
59
60//
61/// \class TString
62// ~~~~~ ~~~~~~~
63/// Reference to reference counted string object TUString
64/// Lightweight reference object consisting of a pointer to actual object
65/// Facilitates copying and assignment with minimal string reallocations
66//
68 public:
69 /// Construct a TString from any type of string
70 // !CQ the assumption that a const char* is non-volatile is bogus!
71 //
72 TString(const char * s = 0);
73 TString(const wchar_t* s);
74 TString(BSTR s, bool loan);
75 TString(TSysStr& s, bool loan);
76 TString(const tstring& s);
77 TString(TUString* s);
78 TString(const TString& src);
79
80 ~TString();
81
82 // Information
83 //
84 int Length() const; ///< The length in characters of this string
85 bool IsNull() const; ///< Is the string NULL?
86 bool IsWide() const; ///< Are the string contents any kind of wide?
87
88 // Assign any type of string into this TString
89 //
90 TString& operator =(const TString& s);
91 TString& operator =(const tstring& s);
92 TString& operator =(const char * s);
93 TString& operator =(char* s);
94 TString& operator =(const wchar_t* s);
95 TString& operator =(wchar_t* s);
96
97 // Convert this TString into the desired string type & return pointer into
98 // this TString
99 //
100 operator const char *() const;
101 operator char*();
102 operator const wchar_t*() const;
103 operator wchar_t*();
104 BSTR AsBSTR() const;
105
106 // Relinquish ownership and return contents of this TString. Caller then
107 // owns the string & must delete or free it.
108 // !CQ currently returns a copy & then frees its copy on destruction
109 //
110 BSTR RelinquishSysStr() const;
111 wchar_t* RelinquishWide() const;
112 char* RelinquishNarrow() const;
113 tchar* Relinquish() const;
114
115 // Language related
116 //
118 void SetLangId(TLangId id);
119
120 protected:
122};
123
124//
125// Provide ANSI to Wide conversion when OLE requires wide chars
126// Allocate a unicode BSTR from an ANSI char*
127//
128# define OleStr(s) ::owl::TString(s)
129# define OleText(s) L##s
130 inline BSTR SysAllocString(const char * str) {
131 return TOleAuto::SysAllocString((wchar_t*)(const wchar_t*)TString(str));
132 }
133
134//
135/// Allocates a BSTR from a wide string.
136/// Together with the overload above, this allows a BSTR to be allocated from a `tchar` string in
137/// both ANSI and Unicode builds. Do not cast a `tchar` string to `OLECHAR*` instead; in ANSI
138/// builds, that reinterprets the narrow string as a wide string. See [bugs:#656].
139//
140 inline BSTR SysAllocString(const wchar_t* str) {
141 return TOleAuto::SysAllocString(str);
142 }
143
144//----------------------------------------------------------------------------
145
146//
147/// \class TUString
148// ~~~~~ ~~~~~~~~
149/// Privately used by TString to manage string pointers
150/// This is a reference counted union of various string representatons
151/// Constructors/destructors are private to enforce reference count model
152/// Create functions are used to facilitate rapid allocation schemes
153/// Null pointers are never stored; instead a static null object is ref'd
154//
155class _OWLCLASS TUString {
156 public:
157 static TUString* Create(const char * str);
158 static TUString* Create(char* str);
159 static TUString* Create(const wchar_t* str);
160 static TUString* Create(wchar_t* str);
161 static TUString* Create(TSysStr& str, bool loan, TLangId lang = 0);
162 static TUString* Create(BSTR str, bool loan, TLangId lang = 0);
163 static TUString* Create(const tstring& str);
164
165 TUString* Assign(const TUString& s);
166 TUString* Assign(const tstring& s);
167 TUString* Assign(const char * s);
168 TUString* Assign(char* s);
169 TUString* Assign(const wchar_t* s);
170 TUString* Assign(wchar_t* s);
171 TUString* Assign(BSTR str, TLangId lang);
172 operator const char *() const;
173 operator char*();
174 operator const wchar_t*() const;
175 operator wchar_t*();
176
177 TUString& operator ++(); ///< Preincrement operator only
178 TUString& operator --(); ///< Predecrement operator only
179
180 int Length() const; ///< Return appropriate string length
181 bool IsNull() const; ///< Is the string a null string?
182 bool IsWide() const; ///< Are the string contents any kind of wide?
183
185 void RevokeBstr(BSTR s); ///< Used to restore if Created with loan==true
186 void ReleaseBstr(BSTR s); ///< Used to unhook if Created with loan==true
187
188 static wchar_t* ConvertAtoW(const char* src, size_t len = (size_t)-1);
189 static char* ConvertWtoA(const wchar_t* src, size_t len = (size_t)-1);
190 static BSTR ConvertAtoBSTR(const char* src);
191 BSTR ConvertToBSTR();
192
193#if defined(BI_COMP_MSC) // MSC can't handle the dtor being private
194 public:
195#else
196 private:
197#endif
198 ~TUString() {Free();}
199
200 private:
201 // !CQ the assumption that a const char* is non-volatile is bogus!
202 TUString(const char & str);
203 TUString(char& str);
204 TUString(const wchar_t& str);
205 TUString(wchar_t& str);
206 TUString(TSysStr& str, bool loan, TLangId lang);
207 TUString(BSTR str, bool loan, TLangId lang);
208 TUString(const tstring& str);
209
210 void Free();
211 tstring& GetOWLString();
212 const tstring& GetOWLString() const;
213 void AllocOWLString(const tstring& as);
214
215 char* ChangeToCopy();
216 wchar_t* ChangeToWCopy();
217
218 enum TKind {
219 isNull,
220 isConst, isCopy,
221 isWConst, isWCopy,
222 isBstr, isExtBstr,
223 isString,
224#if 0 // if alighn greater then 2
225 } Kind : 16;
226 int16 RefCnt;
227#else
228 } Kind;
229 int RefCnt;
230#endif
231 union {
232 const char * Const; ///< Passed-in string, NOT owned here, read-only
233 char* Copy; ///< Local copy, must be deleted, read-write
234 const wchar_t* WConst; ///< Unicode version of Const (Win32)
235 wchar_t* WCopy; ///< Unicode version of Copy (Win32)
236 BSTR Bstr; ///< Copy of pointer, owned here
237 char StringMem[sizeof(tstring)]; ///< Placeholder for string:: object
238 };
239
240 static TUString Null; // Null TString references this
241 TUString() : Lang(0),Kind(isNull),RefCnt(1),Const(0) {} // for Null object
242
243 friend class TString; // Envelope string class
244};
245
246#include <owl/posclass.h>
247
248//----------------------------------------------------------------------------
249// Inlines
250//
251
252//
253/// Construct a TString from a character array
254//
255inline TString::TString(const char * s)
256:
257 S(TUString::Create(s))
258{
259}
260
261//
262/// Construct a TString from a wide character array
263//
264inline TString::TString(const wchar_t* s)
265:
266 S(TUString::Create(s))
267{
268}
269
270//
271/// Construct a TString from a BSTR (OLE String)
272//
273inline TString::TString(BSTR s, bool loan)
274:
275 S(TUString::Create(s, loan))
276{
277}
278
279//
280/// Construct a TString from a System string (BSTR)
281//
282inline TString::TString(TSysStr& s, bool loan)
283:
284 S(TUString::Create(s, loan))
285{
286}
287
288//
289/// Construct a TString from a string
290//
291inline TString::TString(const tstring& s)
292:
293 S(TUString::Create(s))
294{
295}
296
297//
298/// Construct a TString from a TUString
299//
301:
302 S(s)
303{
304}
305
306//
307/// Construct a TString from a TString (Copy Constructor)
308//
309inline TString::TString(const TString& src)
310:
311 S(src.S)
312{
313 ++*S;
314}
315
316//
317// Destruct a TString (actually decrements a reference counter)
318//
320{
321 --*S;
322}
323
324//
325// Return the length of the string
326//
327inline int TString::Length() const
328{
329 return S->Length();
330}
331
332//
333/// Return true if string is empty
334//
335inline bool TString::IsNull() const
336{
337 return S->IsNull();
338}
339
340//
341/// Return true if string uses wide character set
342//
343inline bool TString::IsWide() const
344{
345 return S->IsWide();
346}
347
348//
349/// Copy contents of TString s into this string
350//
352{
353 S = S->Assign(*s.S); return *this;
354}
355
356//
357/// Copy contents of string s into this string
358//
360{
361 S = S->Assign(s); return *this;
362}
363
364//
365/// Copy contents of const char* s into this string
366//
367inline TString& TString::operator =(const char * s)
368{
369 S = S->Assign(s); return *this;
370}
371
372//
373/// Copy contents of char* s into this string
374//
376{
377 S = S->Assign(s); return *this;
378}
379
380//
381/// Copy contents of const wchar_t* s into this string
382//
383inline TString& TString::operator =(const wchar_t* s)
384{
385 S = S->Assign(s); return *this;
386}
387
388//
389/// Copy contents of wchar_t* s into this string
390//
391inline TString& TString::operator =(wchar_t* s)
392{
393 S = S->Assign(s); return *this;
394}
395
396//
397/// Return string as a const char *
398//
399inline TString::operator const char *() const
400{
401 return S->operator const char *();
402}
403
404//
405/// Return string as a char*
406//
407inline TString::operator char*()
408{
409 return S->operator char*();
410}
411
412//
413/// Return string as a const wchar_t*
414//
415inline TString::operator const wchar_t*() const
416{
417 return S->operator const wchar_t*();
418}
419
420//
421/// Return string as a wchar_t*
422//
423inline TString::operator wchar_t*()
424{
425 return S->operator wchar_t*();
426}
427
428//
429/// Return a BSTR object owned by 'this' (not by the caller)
430//
431inline BSTR TString::AsBSTR() const
432{
433 return S->ConvertToBSTR();
434}
435
436//
437/// Return a pointer (BSTR) to a copy of the string
438//
439inline BSTR TString::RelinquishSysStr() const
440{
441 return TOleAuto::SysAllocString((wchar_t*)(const wchar_t*)*S);
442}
443
444//
445/// Return a pointer (wchar_t*) to a copy of the string
446//
447inline wchar_t* TString::RelinquishWide() const
448{
449 return strnewdup((const wchar_t*)*S);
450}
451
452//
453/// Return a pointer (char*) to a copy of the string
454//
455inline char* TString::RelinquishNarrow() const
456{
457 return strnewdup((const char*)*S);
458}
459
460//
461/// Return a pointer (tchar*) to a copy of the string
462//
464{
465#if !defined(UNICODE)
466 return RelinquishNarrow();
467#else
468 return RelinquishWide();
469#endif
470}
471
472//
473/// Get Language Id of this string
474//
476{
477 return S->Lang;
478}
479
480
481//
482/// Set Language Id of this string
483//
485{
486 S->Lang = id;
487}
488
489//----------------------------------------------------------------------------
490
491//
492/// Increment reference counter for this string
493//
494inline TUString& TUString::operator ++()
495{
496 ++RefCnt;
497 return *this;
498}
499
500//
501/// Decrement reference counter for this string
502//
503inline TUString& TUString::operator --()
504{
505 if (--RefCnt != 0)
506 return *this;
507 delete this;
508 return Null;
509}
510
511//
512/// Return true if string is empty
513//
514inline bool TUString::IsNull() const
515{
516 return Kind == isNull;
517}
518
519//
520/// Return true if string uses wide character set
521//
522inline bool TUString::IsWide() const
523{
524 return Kind == isWConst || Kind == isWCopy || Kind == isBstr || Kind == isExtBstr;
525}
526
527inline tstring& TUString::GetOWLString()
528{
529 CHECK(Kind == isString);
530 return *reinterpret_cast<tstring*>(StringMem);
531}
532
533inline const tstring& TUString::GetOWLString() const
534{
535 CHECK(Kind == isString);
536 return *reinterpret_cast<const tstring*>(StringMem);
537}
538
539inline void TUString::AllocOWLString(const tstring& as)
540{
541 new(StringMem) tstring(as);
542}
543
544} // OWL namespace
545
546
547#endif // OWL_STRING_H
#define CHECK(condition)
Definition checks.h:239
delay loading OLEAUT32.DLL/OLE2DISP.DLL
Definition module.h:1208
static HRESULT SysFreeString(BSTR)
Definition module.cpp:1233
static BSTR SysAllocString(const OLECHAR *)
Definition module.cpp:1256
tchar * Relinquish() const
Return a pointer (tchar*) to a copy of the string.
Definition string.h:463
BSTR RelinquishSysStr() const
Return a pointer (BSTR) to a copy of the string.
Definition string.h:439
char * RelinquishNarrow() const
Return a pointer (char*) to a copy of the string.
Definition string.h:455
wchar_t * RelinquishWide() const
Return a pointer (wchar_t*) to a copy of the string.
Definition string.h:447
int Length() const
The length in characters of this string.
Definition string.h:327
TUString * S
Definition string.h:121
TString(const char *s=0)
Construct a TString from any type of string.
Definition string.h:255
TString & operator=(const TString &s)
Copy contents of TString s into this string.
Definition string.h:351
BSTR AsBSTR() const
Return a BSTR object owned by 'this' (not by the caller).
Definition string.h:431
bool IsWide() const
Are the string contents any kind of wide?
Definition string.h:343
bool IsNull() const
Is the string NULL?
Definition string.h:335
TLangId GetLangId()
Get Language Id of this string.
Definition string.h:475
void SetLangId(TLangId id)
Set Language Id of this string.
Definition string.h:484
System string (BSTR) encapsulation.
Definition string.h:33
BSTR Relinquish()
Definition string.h:48
TSysStr(const BSTR p)
Definition string.h:36
TSysStr(const TSysStr &src)
Definition string.h:37
Privately used by TString to manage string pointers This is a reference counted union of various stri...
Definition string.h:155
static BSTR ConvertAtoBSTR(const char *src)
Definition ustring.cpp:90
static char * ConvertWtoA(const wchar_t *src, size_t len=(size_t) -1)
Definition ustring.cpp:21
static TUString * Create(const char *str)
Definition ustring.cpp:307
TLangId Lang
Definition string.h:184
TUString & operator--()
Predecrement operator only.
Definition string.h:503
TUString & operator++()
Preincrement operator only.
Definition string.h:494
bool IsNull() const
Is the string a null string?
Definition string.h:514
int Length() const
Return appropriate string length.
Definition ustring.cpp:527
TUString * Assign(const TUString &s)
Definition ustring.cpp:357
friend class TString
Definition string.h:243
BSTR ConvertToBSTR()
Definition ustring.cpp:194
void ReleaseBstr(BSTR s)
Used to unhook if Created with loan==true.
Definition ustring.cpp:563
static wchar_t * ConvertAtoW(const char *src, size_t len=(size_t) -1)
Definition ustring.cpp:57
bool IsWide() const
Are the string contents any kind of wide?
Definition string.h:522
void RevokeBstr(BSTR s)
Used to restore if Created with loan==true.
Definition ustring.cpp:548
TLocaleString - localized name support.
Reliable platform independent header for common memory and string functions.
char * strnewdup(const char *s, size_t minAllocSize=0)
Definition memory.cpp:25
Definition of class TModule.
Object Windows Library (OWLNext Core).
Definition animctrl.h:22
owl::uint16 TLangId
Holds a language ID, a predefined number that represents a base language and dialect.
Definition lclstrng.h:26
char tchar
Definition defs.h:77
BSTR SysAllocString(const char *str)
Definition string.h:130
std::string tstring
Definition defs.h:79
#define _OWLCLASS
Definition defs.h:338