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