FastCDR  Version 2.3.6
FastCDR
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 
32 namespace eprosima {
33 namespace fastcdr {
34 
43 template <size_t MAX_CHARS>
45 {
46 public:
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  string_data[string_len] = '\0';
89  return *this;
90  }
91 
97  const char* c_string) noexcept
98  : fixed_string()
99  {
100  set(c_string != nullptr ? c_string : "");
101  }
102 
109  const char* c_string) noexcept
110  {
111  set(c_string != nullptr ? c_string : "");
112  return *this;
113  }
114 
120  const std::string& str) noexcept
121  : fixed_string()
122  {
123  set(str.c_str());
124  }
125 
132  const std::string& str) noexcept
133  {
134  set(str.c_str());
135  return *this;
136  }
137 
143  template<size_t N> fixed_string& operator = (
144  const fixed_string<N>& rhs) noexcept
145  {
146  set(rhs.c_str());
147  return *this;
148  }
149 
154  const char* c_str() const noexcept
155  {
156  return string_data;
157  }
158 
163  std::string to_string() const
164  {
165  return std::string(string_data);
166  }
167 
174  const char* rhs) const noexcept
175  {
176  return strncmp(string_data, rhs, MAX_CHARS) == 0;
177  }
178 
185  const std::string& rhs) const noexcept
186  {
187  return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0;
188  }
189 
195  template<size_t N> bool operator == (
196  const fixed_string<N>& rhs) const noexcept
197  {
198  return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0;
199  }
200 
207  const char* rhs) const noexcept
208  {
209  return !(*this == rhs);
210  }
211 
218  const std::string& rhs) const noexcept
219  {
220  return !(*this == rhs);
221  }
222 
228  template<size_t N> bool operator != (
229  const fixed_string<N>& rhs) const noexcept
230  {
231  return !(*this == rhs);
232  }
233 
239  template<size_t N> bool operator < (
240  const fixed_string<N>& rhs) const noexcept
241  {
242  return 0 > compare(rhs);
243  }
244 
250  template<size_t N> bool operator > (
251  const fixed_string<N>& rhs) const noexcept
252  {
253  return 0 < compare(rhs);
254  }
255 
262  const std::string& rhs) const noexcept
263  {
264  return 0 > compare(rhs);
265  }
266 
273  const std::string& rhs) const noexcept
274  {
275  return 0 < compare(rhs);
276  }
277 
281  operator const char* () const noexcept {
282  return c_str();
283  }
284 
289  size_t size() const noexcept
290  {
291  return string_len;
292  }
293 
299  int compare(
300  const char* str) const noexcept
301  {
302  return strncmp(string_data, str, MAX_CHARS);
303  }
304 
310  int compare(
311  const std::string& str) const noexcept
312  {
313  return strncmp(string_data, str.c_str(), MAX_CHARS);
314  }
315 
321  template<size_t N> int compare(
322  const fixed_string<N>& str) const noexcept
323  {
324  return strncmp(string_data, str.c_str(), MAX_CHARS);
325  }
326 
327 private:
328 
329  void set(
330  const char* c_string) noexcept
331  {
332  char* result = static_cast<char*>(MEMCCPY(string_data, c_string, '\0', MAX_CHARS));
333  string_len = (result == nullptr) ? MAX_CHARS : static_cast<size_t>(result - string_data) - 1u;
334  }
335 
336  char string_data[MAX_CHARS + 1];
337  size_t string_len;
338 };
339 
341 
342 } /* namespace fastcdr */
343 } /* namespace eprosima */
344 
345 #endif /* FASTCDR_UTILS_FIXED_SIZE_STRING_HPP_ */
std::string to_string() const
Converts to std::string.
Definition: fixed_size_string.hpp:163
fixed_string(const char *c_array, size_t n_chars) noexcept
Constructs from a char array.
Definition: fixed_size_string.hpp:65
int compare(const char *str) const noexcept
Compares with a C string.
Definition: fixed_size_string.hpp:299
int compare(const fixed_string< N > &str) const noexcept
Compares with a fixed_string.
Definition: fixed_size_string.hpp:321
fixed_string(const char *c_string) noexcept
Constructs from a C string.
Definition: fixed_size_string.hpp:96
fixed_string() noexcept
Default constructor.
Definition: fixed_size_string.hpp:52
bool operator!=(const char *rhs) const noexcept
Compares inequality with a C string.
Definition: fixed_size_string.hpp:206
Definition: fixed_size_string.hpp:32
bool operator==(const char *rhs) const noexcept
Compares equality with a C string.
Definition: fixed_size_string.hpp:173
fixed_string & operator=(const char *c_string) noexcept
Assigns from a C string.
Definition: fixed_size_string.hpp:108
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:250
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:239
int compare(const std::string &str) const noexcept
Compares with a std::string.
Definition: fixed_size_string.hpp:310
fixed_string(const std::string &str) noexcept
Constructs from a std::string.
Definition: fixed_size_string.hpp:119
Template class for non-alloc strings.
Definition: fixed_size_string.hpp:44
static constexpr size_t max_size
Maximum number of characters.
Definition: fixed_size_string.hpp:49
size_t size() const noexcept
Returns the size of the string.
Definition: fixed_size_string.hpp:289
const char * c_str() const noexcept
Converts to C string.
Definition: fixed_size_string.hpp:154