
From: Martin <adrianm@touchdown.se>
How do I update the length after using buffer() ?
Only way I see is:
FuncThatFillString(str.buffer(), str.capacity()); str.resize(strlen(str.c_str())); // c_str to avoid fixed_string overload
That was the reason for a previous suggestion for ReleaseBuffer() (IIRC). It was a way for the client to have write access to the buffer and then tell the object it had control again. At that point, the object could recompute the length. I think MFC's CString has an overload of ReleaseBuffer() that permits the client to state the length resulting from its modifications of the buffer. (fixed_string would only need to constrain that length so as not to exceed the capacity() and this could be supported, too.) If the computed (or supplied) length exceeds the capacity, then fixed_string should throw an exception because that suggests the buffer was overrun. I previously suggested (or did I just think of it, but not mail it?) a proxy class for write access to the buffer. The proxy would be accessible via a buffer() mf, say, and would provide managed access to the buffer in the fixed_string from whence it came. The proxy can provide conversion operators to char * and char const *, and its dtor can recompute the length -- if not set by the client -- before returning control to the fixed_string. That would automate length verification, but you really don't want the proxy's dtor to throw an exception. (The only way around that, that I can think of, is to set a flag in fixed_string such that the next operation -- other than destruction -- performed on it would result in throwing an exception. That's unsatisfying for many reasons.) So, I'm thinking you'd do something like this: size_t character_count(FuncThatFillString( str.release_buffer(), str.capacity())); str.reclaim_buffer(); // invokes strlen() for you // or: str.reclaim_buffer(character_count); // you provide new length I chose the "release_buffer" and "reclaim_buffer" names to be a little more explicit. Either way, you'd need reciprocal terms. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;