pjmsg_mcap_wrapper
Loading...
Searching...
No Matches
fixed_size_string.hpp
Go to the documentation of this file.
1// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*!
16 * @file fixed_size_string.hpp
17 *
18 */
19
20#ifndef FASTCDR_UTILS_FIXED_SIZE_STRING_HPP_
21#define FASTCDR_UTILS_FIXED_SIZE_STRING_HPP_
22
23#include <string>
24#include <cstring>
25
26#ifdef _WIN32
27#define MEMCCPY _memccpy
28#else
29#define MEMCCPY memccpy
30#endif // ifdef _WIN32
31
32namespace eprosima {
33namespace fastcdr {
34
35/**
36 * @brief Template class for non-alloc strings.
37 *
38 * Will be truncated when assigned from a longer string.
39 *
40 * @tparam MAX_CHARS Maximum number of characters is specified as the template parameter.
41 * Space for an additional null terminator will be reserved.
42 */
43template <size_t MAX_CHARS>
45{
46public:
47
48 //! @brief Maximum number of characters.
49 static constexpr size_t max_size = MAX_CHARS;
50
51 //! @brief Default constructor.
52 fixed_string() noexcept
53 {
54 memset(string_data, 0, sizeof(string_data));
55 string_len = 0;
56 }
57
58 // We don't need to define copy/move constructors/assignment operators as the default ones would be enough
59
60 /*!
61 * @brief Constructs from a char array
62 * @param[in] c_array Char array to be constructed from.
63 * @param[in] n_chars Number of characters of the Char array
64 */
66 const char* c_array,
67 size_t n_chars) noexcept
68 {
69 assign(c_array, n_chars);
70 }
71
72 /*!
73 * @brief Assigns from a char array
74 * @param[in] c_array Char array to be assigned from.
75 * @param[in] n_chars Number of characters of the Char array.
76 * @return Reference of this instance.
77 */
79 const char* c_array,
80 size_t n_chars) noexcept
81 {
82 string_len = (nullptr == c_array) ? 0 :
83 (MAX_CHARS < n_chars) ? MAX_CHARS : n_chars;
84 if (0 < string_len)
85 {
86 memcpy(string_data, c_array, string_len);
87 }
88 return *this;
89 }
90
91 /*!
92 * @brief Constructs from a C string.
93 * @param[in] c_string Pointer to the C string.
94 */
96 const char* c_string) noexcept
97 : fixed_string()
98 {
99 set(c_string != nullptr ? c_string : "");
100 }
101
102 /*!
103 * @brief Assigns from a C string.
104 * @param[in] c_string Pointer to the C string.
105 * @return Reference of this instance.
106 */
108 const char* c_string) noexcept
109 {
110 set(c_string != nullptr ? c_string : "");
111 return *this;
112 }
113
114 /*!
115 * @brief Constructs from a std::string.
116 * @param[in] str Reference to the std::string.
117 */
119 const std::string& str) noexcept
120 : fixed_string()
121 {
122 set(str.c_str());
123 }
124
125 /*!
126 * @brief Assigns from a std::string.
127 * @param[in] str Reference to the std::string.
128 * return Reference of this instance.
129 */
131 const std::string& str) noexcept
132 {
133 set(str.c_str());
134 return *this;
135 }
136
137 /*!
138 * @brief Assigns from a fixed_string of any size.
139 * @param[in] rhs Reference to the fixed_string.
140 * return Reference of this instance.
141 */
142 template<size_t N> fixed_string& operator = (
143 const fixed_string<N>& rhs) noexcept
144 {
145 set(rhs.c_str());
146 return *this;
147 }
148
149 /*!
150 * @brief Converts to C string.
151 * @return Pointer to the C string.
152 */
153 const char* c_str() const noexcept
154 {
155 return string_data;
156 }
157
158 /*!
159 * @brief Converts to std::string.
160 * @return Reference to the std::string.
161 */
163 {
164 return std::string(string_data);
165 }
166
167 /*!
168 * @brief Compares equality with a C string.
169 * @param[in] rhs C string to be compared with.
170 * @return `true` if strings are equal. `false` otherwise.
171 */
173 const char* rhs) const noexcept
174 {
175 return strncmp(string_data, rhs, MAX_CHARS) == 0;
176 }
177
178 /*!
179 * @brief Compares equality with a std::string.
180 * @param[in] rhs std::string to be compared with.
181 * @return `true` if strings are equal. `false` otherwise.
182 */
184 const std::string& rhs) const noexcept
185 {
186 return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0;
187 }
188
189 /*!
190 * @brief Compares equality with a fixed_string of any size.
191 * @param[in] rhs fixed_string to be compared with.
192 * @return `true` if strings are equal. `false` otherwise.
193 */
194 template<size_t N> bool operator == (
195 const fixed_string<N>& rhs) const noexcept
196 {
197 return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0;
198 }
199
200 /*!
201 * @brief Compares inequality with a C string.
202 * @param[in] rhs C string to be compared with.
203 * @return `true` if strings are not equal. `false` otherwise.
204 */
206 const char* rhs) const noexcept
207 {
208 return !(*this == rhs);
209 }
210
211 /*!
212 * @brief Compares inequality with a std::string.
213 * @param[in] rhs std::string to be compared with.
214 * @return `true` if strings are not equal. `false` otherwise.
215 */
217 const std::string& rhs) const noexcept
218 {
219 return !(*this == rhs);
220 }
221
222 /*!
223 * @brief Compares inequality with a fixed_string of any size.
224 * @param[in] rhs fixed_string to be compared with.
225 * @return `true` if strings are not equal. `false` otherwise.
226 */
227 template<size_t N> bool operator != (
228 const fixed_string<N>& rhs) const noexcept
229 {
230 return !(*this == rhs);
231 }
232
233 /*!
234 * @brief Compares relational less than with a fixed_string of any size.
235 * @param[in] rhs fixed_string to be compared with.
236 * @return `true` if this string is less than the provided one. `false` otherwise.
237 */
238 template<size_t N> bool operator < (
239 const fixed_string<N>& rhs) const noexcept
240 {
241 return 0 > compare(rhs);
242 }
243
244 /*!
245 * @brief Compares relational greater than with a fixed_string of any size.
246 * @param[in] rhs fixed_string to be compared with.
247 * @return `true` if this string is greater than the provided one. `false` otherwise.
248 */
249 template<size_t N> bool operator > (
250 const fixed_string<N>& rhs) const noexcept
251 {
252 return 0 < compare(rhs);
253 }
254
255 /*!
256 * @brief Compares relational less than with a std::string of any size.
257 * @param[in] rhs std::string to be compared with.
258 * @return `true` if this string is less than the provided one. `false` otherwise.
259 */
261 const std::string& rhs) const noexcept
262 {
263 return 0 > compare(rhs);
264 }
265
266 /*!
267 * @brief Compares relational greater than with a std::string of any size.
268 * @param[in] rhs std::string to be compared with.
269 * @return `true` if this string is greater than the provided one. `false` otherwise.
270 */
272 const std::string& rhs) const noexcept
273 {
274 return 0 < compare(rhs);
275 }
276
277 /*!
278 * @brief Casts to a C string.
279 */
280 operator const char* () const noexcept {
281 return c_str();
282 }
283
284 /*!
285 * @brief Returns the size of the string.
286 * @return Length of the string.
287 */
288 size_t size() const noexcept
289 {
290 return string_len;
291 }
292
293 /*!
294 * @brief Compares with a C string.
295 * @param[in] str C string to be compared with.
296 * @return Integer value with the result of the comparison as described in `std::string::compare()`.
297 */
299 const char* str) const noexcept
300 {
301 return strncmp(string_data, str, MAX_CHARS);
302 }
303
304 /*!
305 * @brief Compares with a std::string.
306 * @param[in] str std::string to be compared with.
307 * @return Integer value with the result of the comparison as described in `std::string::compare()`.
308 */
310 const std::string& str) const noexcept
311 {
312 return strncmp(string_data, str.c_str(), MAX_CHARS);
313 }
314
315 /*!
316 * @brief Compares with a fixed_string
317 * @param[in] str fixed_string to be compared with.
318 * @return Integer value with the result of the comparison as described in `std::string::compare()`.
319 */
320 template<size_t N> int compare(
321 const fixed_string<N>& str) const noexcept
322 {
323 return strncmp(string_data, str.c_str(), MAX_CHARS);
324 }
325
326private:
327
328 void set(
329 const char* c_string) noexcept
330 {
331 char* result = static_cast<char*>(MEMCCPY(string_data, c_string, '\0', MAX_CHARS));
332 string_len = (result == nullptr) ? MAX_CHARS : static_cast<size_t>(result - string_data) - 1u;
333 }
334
335 char string_data[MAX_CHARS + 1]; ///< Holds string data, including ending null character.
336 size_t string_len; ///< Holds current string length.
337};
338
340
341} /* namespace fastcdr */
342} /* namespace eprosima */
343
344#endif /* FASTCDR_UTILS_FIXED_SIZE_STRING_HPP_ */
#define MEMCCPY
Definition Cdr.h:49
Template class for non-alloc strings.
std::string to_string() const
Converts to std::string.
fixed_string & assign(const char *c_array, size_t n_chars) noexcept
Assigns from a char array.
void set(const char *c_string) noexcept
size_t string_len
Holds current string length.
int compare(const std::string &str) const noexcept
Compares with a std::string.
bool operator<(const fixed_string< N > &rhs) const noexcept
Compares relational less than with a fixed_string of any size.
fixed_string() noexcept
Default constructor.
bool operator>(const fixed_string< N > &rhs) const noexcept
Compares relational greater than with a fixed_string of any size.
fixed_string & operator=(const char *c_string) noexcept
Assigns from a C string.
bool operator==(const char *rhs) const noexcept
Compares equality with a C string.
char string_data[MAX_CHARS+1]
Holds string data, including ending null character.
fixed_string(const char *c_string) noexcept
Constructs from a C string.
bool operator!=(const char *rhs) const noexcept
Compares inequality with a C string.
fixed_string(const std::string &str) noexcept
Constructs from a std::string.
static constexpr size_t max_size
Maximum number of characters.
int compare(const fixed_string< N > &str) const noexcept
Compares with a fixed_string.
int compare(const char *str) const noexcept
Compares with a C string.
fixed_string(const char *c_array, size_t n_chars) noexcept
Constructs from a char array.
size_t size() const noexcept
Returns the size of the string.
const char * c_str() const noexcept
Converts to C string.