Mistake on this page? Email us
m2mstringbuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 ARM Limited. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  * Licensed under the Apache License, Version 2.0 (the License); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef __STRING_BUFFER_H__
17 #define __STRING_BUFFER_H__
18 
20 
21 #include <assert.h>
22 #include <stddef.h>
23 
29 template <int SIZE>
31 {
32 public:
36  inline StringBuffer();
37 
38  //
39  // This is not implemented on purpose, as the given string may conflict with
40  // templated size. Otoh, if we used compile time assert, the overflow
41  // could be prevented at compile time.
42  //
43  // inline StringBuffer(const char *initial_string);
44 
50  bool ensure_space(size_t required_size) const;
51 
56  bool append(char data);
57 
66  bool append(const char *data);
67 
76  bool append(const char *data, size_t data_len);
77 
85  bool append_int(uint16_t data);
86 
93  inline size_t get_size() const;
94 
95  // API functionality copied from m2mstring:
96 
97  // find the index of last occurance of given char in string, or negative if not found
98  int find_last_of(char search_char) const;
99 
103  inline const char* c_str() const;
104 
105  // Add this only if needed
106  //inline char* c_str();
107 private:
108  char _buff[SIZE];
109 };
110 
111 template <int SIZE>
113 {
114  // actually a assert_compile() would be better as this is completely a code problem
115  assert(SIZE > 0);
116 
117  _buff[0] = '\0';
118 }
119 
120 template <int SIZE>
121 bool StringBuffer<SIZE>::ensure_space(size_t required_size) const
122 {
123  return StringBufferBase::ensure_space(SIZE, required_size);
124 }
125 
126 template <int SIZE>
127 bool StringBuffer<SIZE>::append(const char *data)
128 {
129  return StringBufferBase::append(_buff, SIZE, data);
130 }
131 
132 template <int SIZE>
133 bool StringBuffer<SIZE>::append(const char *data, size_t data_len)
134 {
135  return StringBufferBase::append(_buff, SIZE, data, data_len);
136 }
137 
138 template <int SIZE>
139 inline bool StringBuffer<SIZE>::append(char data)
140 {
141  return StringBufferBase::append(_buff, SIZE, data);
142 }
143 
144 template <int SIZE>
146 {
147  return StringBufferBase::append_int(_buff, SIZE, data);
148 }
149 
150 template <int SIZE>
151 int StringBuffer<SIZE>::find_last_of(char search_char) const
152 {
153  return StringBufferBase::find_last_of(_buff, search_char);
154 }
155 
156 template <int SIZE>
157 inline const char* StringBuffer<SIZE>::c_str() const
158 {
159  return _buff;
160 }
161 
162 template <int SIZE>
163 inline size_t StringBuffer<SIZE>::get_size() const
164 {
165  return _curr_size;
166 }
167 
168 #endif // !__STRING_BUFFER_H__
const char * c_str() const
Definition: m2mstringbuffer.h:157
StringBuffer()
Definition: m2mstringbuffer.h:112
Definition: m2mstringbuffer.h:30
bool append(char data)
Definition: m2mstringbuffer.h:139
Definition: m2mstringbufferbase.h:26
bool ensure_space(size_t required_size) const
Definition: m2mstringbuffer.h:121
StringBufferBase. This class is the base class for fixed-sized string buffers.
size_t get_size() const
Definition: m2mstringbuffer.h:163
bool append_int(uint16_t data)
Definition: m2mstringbuffer.h:145