Contents Up Previous Next

wxStringBufferLength

This tiny class allows to conveniently access the wxString internal buffer as a writable pointer without any risk of forgetting to restore the string to the usable state later, and allows the user to set the internal length of the string.

For example, assuming you have a low-level OS function called int GetMeaningOfLifeAsString(char *) copying the value in the provided buffer (which must be writable, of course), and returning the actual length of the string, you might call it like this:

    wxString theAnswer;
    wxStringBuffer theAnswerBuffer(theAnswer, 1024);
    int nLength = GetMeaningOfLifeAsString(theAnswerBuffer);
    theAnswerBuffer.SetLength(nLength);
    if ( theAnswer != "42" )
    {
        wxLogError("Something is very wrong!");
    }
Note that the exact usage of this depends on whether on not wxUSE_STL is enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from wxString, keeping the same buffer wxString uses intact. In other words, relying on wxStringBuffer containing the old wxString data is probably not a good idea if you want to build your program in both with and without wxUSE_STL.

Note that SetLength must be called before wxStringBufferLength destructs.

Derived from

None

Include files

<wx/string.h>

Members

wxStringBufferLength::wxStringBufferLength
wxStringBufferLength::~wxStringBufferLength
wxStringBufferLength::SetLength
wxStringBufferLength::operator wxChar *


wxStringBufferLength::wxStringBufferLength

wxStringBufferLength(const wxString& str, size_t len)

Constructs a writable string buffer object associated with the given string and containing enough space for at least len characters. Basically, this is equivalent to calling GetWriteBuf and saving the result.


wxStringBufferLength::~wxStringBufferLength

~wxStringBufferLength()

Restores the string passed to the constructor to the usable state by calling UngetWriteBuf on it.


wxStringBufferLength::SetLength

void SetLength(size_t nLength)

Sets the internal length of the string referred to by wxStringBufferLength to nLength characters.

Must be called before wxStringBufferLength destructs.


wxStringBufferLength::operator wxChar *

wxChar * operator wxChar *()

Returns the writable pointer to a buffer of the size at least equal to the length specified in the constructor.