OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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) {}
38
39 ~TSysStr() {if (P) TOleAuto::SysFreeString(P);}
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:
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
58class _OWLCLASS TUString;
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 //
117 TLangId GetLangId();
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
136//
137/// \class TUString
138// ~~~~~ ~~~~~~~~
139/// Privately used by TString to manage string pointers
140/// This is a reference counted union of various string representatons
141/// Constructors/destructors are private to enforce reference count model
142/// Create functions are used to facilitate rapid allocation schemes
143/// Null pointers are never stored; instead a static null object is ref'd
144//
146 public:
147 static TUString* Create(const char * str);
148 static TUString* Create(char* str);
149 static TUString* Create(const wchar_t* str);
150 static TUString* Create(wchar_t* str);
151 static TUString* Create(TSysStr& str, bool loan, TLangId lang = 0);
152 static TUString* Create(BSTR str, bool loan, TLangId lang = 0);
153 static TUString* Create(const tstring& str);
154
155 TUString* Assign(const TUString& s);
156 TUString* Assign(const tstring& s);
157 TUString* Assign(const char * s);
158 TUString* Assign(char* s);
159 TUString* Assign(const wchar_t* s);
160 TUString* Assign(wchar_t* s);
161 TUString* Assign(BSTR str, TLangId lang);
162 operator const char *() const;
163 operator char*();
164 operator const wchar_t*() const;
165 operator wchar_t*();
166
167 TUString& operator ++(); ///< Preincrement operator only
168 TUString& operator --(); ///< Predecrement operator only
169
170 int Length() const; ///< Return appropriate string length
171 bool IsNull() const; ///< Is the string a null string?
172 bool IsWide() const; ///< Are the string contents any kind of wide?
173
175 void RevokeBstr(BSTR s); ///< Used to restore if Created with loan==true
176 void ReleaseBstr(BSTR s); ///< Used to unhook if Created with loan==true
177
178 static wchar_t* ConvertAtoW(const char* src, size_t len = (size_t)-1);
179 static char* ConvertWtoA(const wchar_t* src, size_t len = (size_t)-1);
180 static BSTR ConvertAtoBSTR(const char* src);
181 BSTR ConvertToBSTR();
182
183#if defined(BI_COMP_MSC) // MSC can't handle the dtor being private
184 public:
185#else
186 private:
187#endif
188 ~TUString() {Free();}
189
190 private:
191 // !CQ the assumption that a const char* is non-volatile is bogus!
192 TUString(const char & str);
193 TUString(char& str);
194 TUString(const wchar_t& str);
195 TUString(wchar_t& str);
196 TUString(TSysStr& str, bool loan, TLangId lang);
197 TUString(BSTR str, bool loan, TLangId lang);
198 TUString(const tstring& str);
199
200 void Free();
201 tstring& GetOWLString();
202 const tstring& GetOWLString() const;
203 void AllocOWLString(const tstring& as);
204
205 char* ChangeToCopy();
206 wchar_t* ChangeToWCopy();
207
208 enum TKind {
209 isNull,
210 isConst, isCopy,
211 isWConst, isWCopy,
212 isBstr, isExtBstr,
213 isString,
214#if 0 // if alighn greater then 2
215 } Kind : 16;
216 int16 RefCnt;
217#else
218 } Kind;
219 int RefCnt;
220#endif
221 union {
222 const char * Const; ///< Passed-in string, NOT owned here, read-only
223 char* Copy; ///< Local copy, must be deleted, read-write
224 const wchar_t* WConst; ///< Unicode version of Const (Win32)
225 wchar_t* WCopy; ///< Unicode version of Copy (Win32)
226 BSTR Bstr; ///< Copy of pointer, owned here
227 char StringMem[sizeof(tstring)]; ///< Placeholder for string:: object
228 };
229
230 static TUString Null; // Null TString references this
231 TUString() : Lang(0),Kind(isNull),RefCnt(1),Const(0) {} // for Null object
232
233 friend class TString; // Envelope string class
234};
235
236#include <owl/posclass.h>
237
238//----------------------------------------------------------------------------
239// Inlines
240//
241
242//
243/// Construct a TString from a character array
244//
245inline TString::TString(const char * s)
246:
247 S(TUString::Create(s))
248{
249}
250
251//
252/// Construct a TString from a wide character array
253//
254inline TString::TString(const wchar_t* s)
255:
256 S(TUString::Create(s))
257{
258}
259
260//
261/// Construct a TString from a BSTR (OLE String)
262//
264:
265 S(TUString::Create(s, loan))
266{
267}
268
269//
270/// Construct a TString from a System string (BSTR)
271//
273:
274 S(TUString::Create(s, loan))
275{
276}
277
278//
279/// Construct a TString from a string
280//
281inline TString::TString(const tstring& s)
282:
283 S(TUString::Create(s))
284{
285}
286
287//
288/// Construct a TString from a TUString
289//
291:
292 S(s)
293{
294}
295
296//
297/// Construct a TString from a TString (Copy Constructor)
298//
300:
301 S(src.S)
302{
303 ++*S;
304}
305
306//
307// Destruct a TString (actually decrements a reference counter)
308//
310{
311 --*S;
312}
313
314//
315// Return the length of the string
316//
317inline int TString::Length() const
318{
319 return S->Length();
320}
321
322//
323/// Return true if string is empty
324//
325inline bool TString::IsNull() const
326{
327 return S->IsNull();
328}
329
330//
331/// Return true if string uses wide character set
332//
333inline bool TString::IsWide() const
334{
335 return S->IsWide();
336}
337
338//
339/// Copy contents of TString s into this string
340//
342{
343 S = S->Assign(*s.S); return *this;
344}
345
346//
347/// Copy contents of string s into this string
348//
350{
351 S = S->Assign(s); return *this;
352}
353
354//
355/// Copy contents of const char* s into this string
356//
357inline TString& TString::operator =(const char * s)
358{
359 S = S->Assign(s); return *this;
360}
361
362//
363/// Copy contents of char* s into this string
364//
366{
367 S = S->Assign(s); return *this;
368}
369
370//
371/// Copy contents of const wchar_t* s into this string
372//
373inline TString& TString::operator =(const wchar_t* s)
374{
375 S = S->Assign(s); return *this;
376}
377
378//
379/// Copy contents of wchar_t* s into this string
380//
381inline TString& TString::operator =(wchar_t* s)
382{
383 S = S->Assign(s); return *this;
384}
385
386//
387/// Return string as a const char *
388//
389inline TString::operator const char *() const
390{
391 return S->operator const char *();
392}
393
394//
395/// Return string as a char*
396//
397inline TString::operator char*()
398{
399 return S->operator char*();
400}
401
402//
403/// Return string as a const wchar_t*
404//
405inline TString::operator const wchar_t*() const
406{
407 return S->operator const wchar_t*();
408}
409
410//
411/// Return string as a wchar_t*
412//
413inline TString::operator wchar_t*()
414{
415 return S->operator wchar_t*();
416}
417
418//
419/// Return a BSTR object owned by 'this' (not by the caller)
420//
421inline BSTR TString::AsBSTR() const
422{
423 return S->ConvertToBSTR();
424}
425
426//
427/// Return a pointer (BSTR) to a copy of the string
428//
430{
431 return TOleAuto::SysAllocString((wchar_t*)(const wchar_t*)*S);
432}
433
434//
435/// Return a pointer (wchar_t*) to a copy of the string
436//
437inline wchar_t* TString::RelinquishWide() const
438{
439 return strnewdup((const wchar_t*)*S);
440}
441
442//
443/// Return a pointer (char*) to a copy of the string
444//
445inline char* TString::RelinquishNarrow() const
446{
447 return strnewdup((const char*)*S);
448}
449
450//
451/// Return a pointer (tchar*) to a copy of the string
452//
454{
455#if !defined(UNICODE)
456 return RelinquishNarrow();
457#else
458 return RelinquishWide();
459#endif
460}
461
462//
463/// Get Language Id of this string
464//
466{
467 return S->Lang;
468}
469
470
471//
472/// Set Language Id of this string
473//
475{
476 S->Lang = id;
477}
478
479//----------------------------------------------------------------------------
480
481//
482/// Increment reference counter for this string
483//
485{
486 ++RefCnt;
487 return *this;
488}
489
490//
491/// Decrement reference counter for this string
492//
494{
495 if (--RefCnt != 0)
496 return *this;
497 delete this;
498 return Null;
499}
500
501//
502/// Return true if string is empty
503//
504inline bool TUString::IsNull() const
505{
506 return Kind == isNull;
507}
508
509//
510/// Return true if string uses wide character set
511//
512inline bool TUString::IsWide() const
513{
514 return Kind == isWConst || Kind == isWCopy || Kind == isBstr || Kind == isExtBstr;
515}
516
517inline tstring& TUString::GetOWLString()
518{
519 CHECK(Kind == isString);
520 return *reinterpret_cast<tstring*>(StringMem);
521}
522
523inline const tstring& TUString::GetOWLString() const
524{
525 CHECK(Kind == isString);
526 return *reinterpret_cast<const tstring*>(StringMem);
527}
528
529inline void TUString::AllocOWLString(const tstring& as)
530{
531 new(StringMem) tstring(as);
532}
533
534} // OWL namespace
535
536
537#endif // OWL_STRING_H
#define CHECK(condition)
Definition checks.h:239
delay loading OLEAUT32.DLL/OLE2DISP.DLL
Definition module.h:1205
static BSTR SysAllocString(const OLECHAR *)
Definition module.cpp:1256
Reference to reference counted string object TUString Lightweight reference object consisting of a po...
Definition string.h:67
tchar * Relinquish() const
Return a pointer (tchar*) to a copy of the string.
Definition string.h:453
BSTR RelinquishSysStr() const
Return a pointer (BSTR) to a copy of the string.
Definition string.h:429
char * RelinquishNarrow() const
Return a pointer (char*) to a copy of the string.
Definition string.h:445
wchar_t * RelinquishWide() const
Return a pointer (wchar_t*) to a copy of the string.
Definition string.h:437
int Length() const
The length in characters of this string.
Definition string.h:317
TUString * S
Definition string.h:121
TString(const char *s=0)
Construct a TString from any type of string.
Definition string.h:245
TString & operator=(const TString &s)
Copy contents of TString s into this string.
Definition string.h:341
BSTR AsBSTR() const
Return a BSTR object owned by 'this' (not by the caller)
Definition string.h:421
bool IsWide() const
Are the string contents any kind of wide?
Definition string.h:333
bool IsNull() const
Is the string NULL?
Definition string.h:325
TLangId GetLangId()
Get Language Id of this string.
Definition string.h:465
void SetLangId(TLangId id)
Set Language Id of this string.
Definition string.h:474
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:145
const wchar_t * WConst
Unicode version of Const (Win32)
Definition string.h:224
TLangId Lang
Definition string.h:174
TUString & operator--()
Predecrement operator only.
Definition string.h:493
TUString & operator++()
Preincrement operator only.
Definition string.h:484
bool IsNull() const
Is the string a null string?
Definition string.h:504
int Length() const
Return appropriate string length.
Definition ustring.cpp:527
TUString * Assign(const TUString &s)
Definition ustring.cpp:357
const char * Const
Passed-in string, NOT owned here, read-only.
Definition string.h:222
BSTR ConvertToBSTR()
Definition ustring.cpp:194
bool IsWide() const
Are the string contents any kind of wide?
Definition string.h:512
wchar_t * WCopy
Unicode version of Copy (Win32)
Definition string.h:225
char StringMem[sizeof(tstring)]
Placeholder for string:: object.
Definition string.h:227
BSTR Bstr
Copy of pointer, owned here.
Definition string.h:226
char * Copy
Local copy, must be deleted, read-write.
Definition string.h:223
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
BSTR SysAllocString(const char *str)
Definition string.h:130
std::string tstring
Definition defs.h:79
#define _OWLCLASS
Definition defs.h:338