OSVR-Core
Public Member Functions | List of all members
osvr::util::StringBufferBuilder Class Reference

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...
 

Detailed Description

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.

Member Function Documentation

§ getBufferOfSize()

char* osvr::util::StringBufferBuilder::getBufferOfSize ( std::size_t  n)
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.

§ str()

std::string osvr::util::StringBufferBuilder::str ( )
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.


The documentation for this class was generated from the following file: