]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/anystr.h
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxAnyStrPtr class declaration
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
13 #include "wx/string.h"
15 // ----------------------------------------------------------------------------
18 // Notice that this is an internal and intentionally not documented class. It
19 // is only used by wxWidgets itself to ensure compatibility with previous
20 // versions and shouldn't be used by user code. When you see a function
21 // returning it you should just know that you can treat it as a string pointer.
22 // ----------------------------------------------------------------------------
24 // This is a helper class convertible to either narrow or wide string pointer.
25 // It is similar to wxCStrData but, unlike it, can be NULL which is required to
26 // represent the return value of wxDateTime::ParseXXX() methods for example.
28 // NB: this class is fully inline and so doesn't need to be DLL-exported
32 // ctors: this class must be created from the associated string or using
33 // its default ctor for an invalid NULL-like object; notice that it is
34 // immutable after creation.
36 // ctor for invalid pointer
42 // ctor for valid pointer into the given string (whose lifetime must be
43 // greater than ours and which should remain constant while we're used)
44 wxAnyStrPtr(const wxString
& str
, const wxString::const_iterator
& iter
)
50 // default copy ctor is ok and so is default dtor, in particular we do not
54 // various operators meant to make this class look like a superposition of
57 // this one is needed to allow boolean expressions involving these objects,
58 // e.g. "if ( FuncReturningAnyStrPtr() && ... )" (unfortunately using
59 // unspecified_bool_type here wouldn't help with ambiguity between all the
60 // different conversions to pointers)
61 operator bool() const { return m_str
!= NULL
; }
63 // at least VC6 and VC7 also need this one or they complain about ambiguity
64 // for !anystr expressions
65 bool operator!() const { return !((bool)*this); }
68 // and these are the conversions operator which allow to assign the result
69 // of FuncReturningAnyStrPtr() to either char* or wxChar* (i.e. wchar_t*)
70 operator const char *() const
75 // check if the string is convertible to char at all
77 // notice that this pointer points into wxString internal buffer
78 // containing its char* representation and so it can be kept for as
79 // long as wxString is not modified -- which is long enough for our
81 const char *p
= m_str
->c_str().AsChar();
84 // find the offset of the character corresponding to this iterator
85 // position in bytes: we don't have any direct way to do it so we
86 // need to redo the conversion again for the part of the string
87 // before the iterator to find its length in bytes in current
90 // NB: conversion won't fail as it succeeded for the entire string
91 p
+= strlen(wxString(m_str
->begin(), m_iter
).mb_str());
93 //else: conversion failed, return "" as we can't do anything else
98 operator const wchar_t *() const
103 // no complications with wide strings (as long as we discount
104 // surrogates as we do for now)
106 // just remember that this works as long as wxString keeps an internal
107 // buffer with its wide wide char representation, just as with AsChar()
109 return m_str
->c_str().AsWChar() + (m_iter
- m_str
->begin());
112 // Because the objects of this class are only used as return type for
113 // functions which can return NULL we can skip providing dereferencing
114 // operators: the code using this class must test it for NULL first and if
115 // it does anything else with it it has to assign it to either char* or
116 // wchar_t* itself, before dereferencing.
120 // if ( *FuncReturningAnyStrPtr() )
122 // is invalid because it could crash. And this
124 // const char *p = FuncReturningAnyStrPtr();
127 // already works fine.
130 // the original string and the position in it we correspond to, if the
131 // string is NULL this object is NULL pointer-like
132 const wxString
* const m_str
;
133 const wxString::const_iterator m_iter
;
135 wxDECLARE_NO_ASSIGN_CLASS(wxAnyStrPtr
);
138 #endif // _WX_ANYSTR_H_