OSVR-Core
StringBufferBuilder.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_StringBufferBuilder_h_GUID_D531C5F4_AC8D_4E9D_9682_A38DCBC7DC8C
26 #define INCLUDED_StringBufferBuilder_h_GUID_D531C5F4_AC8D_4E9D_9682_A38DCBC7DC8C
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <vector>
36 #include <string>
37 #include <cstddef>
38 
39 namespace osvr {
40 namespace util {
45  public:
48  char *getBufferOfSize(std::size_t n) {
51  m_buffer.resize(n + 1, '\0');
52  return m_buffer.data();
53  }
59  std::string str() {
61  std::string ret;
62  if (m_buffer.empty()) {
63  return ret;
64  }
68  while (!m_buffer.empty() && m_buffer[m_buffer.size() - 1] == '\0') {
69  m_buffer.pop_back();
70  }
71 
72  if (m_buffer.empty()) {
73  return ret;
74  }
75  ret.assign(m_buffer.begin(), m_buffer.end());
76  return ret;
77  }
78 
79  private:
80  typedef std::vector<char> container;
81  container m_buffer;
82  };
83 }
84 }
85 #endif // INCLUDED_StringBufferBuilder_h_GUID_D531C5F4_AC8D_4E9D_9682_A38DCBC7DC8C
Definition: RunLoopManager.h:42
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
std::string str()
Converts the buffer into a string.
Definition: StringBufferBuilder.h:59
A utility class to adapt APIs that first provide a length, then place a string in a user-allocated bu...
Definition: StringBufferBuilder.h:44
char * getBufferOfSize(std::size_t n)
Allocates a buffer of size n, to include the null terminator, and returns a pointer into it...
Definition: StringBufferBuilder.h:48