OSVR-Core
|
A utility class to adapt APIs that first provide a length, then place a string in a user-allocated buffer, to nearly transparently handle the buffer and convert into a std::string. More...
#include <StringBufferBuilder.h>
Public Member Functions | |
char * | getBufferOfSize (std::size_t n) |
Allocates a buffer of size n, to include the null terminator, and returns a pointer into it. More... | |
std::string | str () |
Converts the buffer into a string. More... | |
A utility class to adapt APIs that first provide a length, then place a string in a user-allocated buffer, to nearly transparently handle the buffer and convert into a std::string.
|
inline |
Allocates a buffer of size n, to include the null terminator, and returns a pointer into it.
Allocate one more character for safety, in case the user's size didn't include the null terminator.
|
inline |
Converts the buffer into a string.
Not const to be able to trim trailing nulls without making a copy or doing things with iterators that felt dangerous. Since this is a class for short-term use, that should be fine.
Create this right away for the NRVO optimization.
Drop trailing nulls - the string constructor doesn't take nulls unless you just hand it a cstr, may as well use the fact we know length.