Fast CDR  Version 2.2.6
Fast CDR
Loading...
Searching...
No Matches
fixed_size_string.hpp
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
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
43template <size_t MAX_CHARS>
45{
46public:
47
49 static constexpr size_t max_size = MAX_CHARS;
50
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
66 const char* c_array,
67 size_t n_chars) noexcept
68 {
69 assign(c_array, n_chars);
70 }
71
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
96 const char* c_string) noexcept
97 : fixed_string()
98 {
99 set(c_string != nullptr ? c_string : "");
100 }
101
108 const char* c_string) noexcept
109 {
110 set(c_string != nullptr ? c_string : "");
111 return *this;
112 }
113
119 const std::string& str) noexcept
120 : fixed_string()
121 {
122 set(str.c_str());
123 }
124
131 const std::string& str) noexcept
132 {
133 set(str.c_str());
134 return *this;
135 }
136
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
153 const char* c_str() const noexcept
154 {
155 return string_data;
156 }
157
162 std::string to_string() const
163 {
164 return std::string(string_data);
165 }
166
173 const char* rhs) const noexcept
174 {
175 return strncmp(string_data, rhs, MAX_CHARS) == 0;
176 }
177
184 const std::string& rhs) const noexcept
185 {
186 return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0;
187 }
188
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
206 const char* rhs) const noexcept
207 {
208 return !(*this == rhs);
209 }
210
217 const std::string& rhs) const noexcept
218 {
219 return !(*this == rhs);
220 }
221
227 template<size_t N> bool operator != (
228 const fixed_string<N>& rhs) const noexcept
229 {
230 return !(*this == rhs);
231 }
232
238 template<size_t N> bool operator < (
239 const fixed_string<N>& rhs) const noexcept
240 {
241 return 0 > compare(rhs);
242 }
243
249 template<size_t N> bool operator > (
250 const fixed_string<N>& rhs) const noexcept
251 {
252 return 0 < compare(rhs);
253 }
254
261 const std::string& rhs) const noexcept
262 {
263 return 0 > compare(rhs);
264 }
265
272 const std::string& rhs) const noexcept
273 {
274 return 0 < compare(rhs);
275 }
276
280 operator const char* () const noexcept {
281 return c_str();
282 }
283
288 size_t size() const noexcept
289 {
290 return string_len;
291 }
292
299 const char* str) const noexcept
300 {
301 return strncmp(string_data, str, MAX_CHARS);
302 }
303
310 const std::string& str) const noexcept
311 {
312 return strncmp(string_data, str.c_str(), MAX_CHARS);
313 }
314
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];
336 size_t string_len;
337};
338
340
341} /* namespace fastcdr */
342} /* namespace eprosima */
343
344#endif /* FASTCDR_UTILS_FIXED_SIZE_STRING_HPP_ */
Definition Cdr.h:49
Template class for non-alloc strings.
Definition fixed_size_string.hpp:45
fixed_string & operator=(const char *c_string) noexcept
Assigns from a C string.
Definition fixed_size_string.hpp:107
fixed_string() noexcept
Default constructor.
Definition fixed_size_string.hpp:52
size_t size() const noexcept
Returns the size of the string.
Definition fixed_size_string.hpp:288
fixed_string(const std::string &str) noexcept
Constructs from a std::string.
Definition fixed_size_string.hpp:118
bool operator==(const char *rhs) const noexcept
Compares equality with a C string.
Definition fixed_size_string.hpp:172
static constexpr size_t max_size
Maximum number of characters.
Definition fixed_size_string.hpp:49
int compare(const std::string &str) const noexcept
Compares with a std::string.
Definition fixed_size_string.hpp:309
std::string to_string() const
Converts to std::string.
Definition fixed_size_string.hpp:162
fixed_string(const char *c_array, size_t n_chars) noexcept
Constructs from a char array.
Definition fixed_size_string.hpp:65
const char * c_str() const noexcept
Converts to C string.
Definition fixed_size_string.hpp:153
fixed_string(const char *c_string) noexcept
Constructs from a C string.
Definition fixed_size_string.hpp:95
int compare(const fixed_string< N > &str) const noexcept
Compares with a fixed_string.
Definition fixed_size_string.hpp:320
bool operator>(const fixed_string< N > &rhs) const noexcept
Compares relational greater than with a fixed_string of any size.
Definition fixed_size_string.hpp:249
fixed_string & assign(const char *c_array, size_t n_chars) noexcept
Assigns from a char array.
Definition fixed_size_string.hpp:78
bool operator<(const fixed_string< N > &rhs) const noexcept
Compares relational less than with a fixed_string of any size.
Definition fixed_size_string.hpp:238
int compare(const char *str) const noexcept
Compares with a C string.
Definition fixed_size_string.hpp:298
bool operator!=(const char *rhs) const noexcept
Compares inequality with a C string.
Definition fixed_size_string.hpp:205