Fast RTPS  Version 2.6.0
Fast RTPS
fixed_size_string.hpp
1 // Copyright 2018 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 FASTRTPS_UTILS_FIXED_SIZE_STRING_HPP_
21 #define FASTRTPS_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 #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
33 namespace eprosima {
34 namespace fastrtps {
35 
42 template <size_t MAX_CHARS>
44 {
45 public:
46 
47  static constexpr size_t max_size = MAX_CHARS;
48 
50  fixed_string() noexcept
51  {
52  memset(string_data, 0, sizeof(string_data));
53  string_len = 0;
54  }
55 
56  // We don't need to define copy/move constructors/assignment operators as the default ones would be enough
57 
58  // Construct / assign from a char array
60  const char* c_array,
61  size_t n_chars) noexcept
62  {
63  assign(c_array, n_chars);
64  }
65 
67  const char* c_array,
68  size_t n_chars) noexcept
69  {
70  string_len = (nullptr == c_array) ? 0 :
71  (MAX_CHARS < n_chars) ? MAX_CHARS : n_chars;
72  if (0 < string_len)
73  {
74  memcpy(string_data, c_array, string_len);
75  }
76  return *this;
77  }
78 
79  // Construct / assign from a C string
81  const char* c_string) noexcept
82  : fixed_string()
83  {
84  set(c_string != nullptr ? c_string : "");
85  }
86 
88  const char* c_string) noexcept
89  {
90  set(c_string != nullptr ? c_string : "");
91  return *this;
92  }
93 
94  // Construct / assign from a std::string
96  const std::string& str) noexcept
97  : fixed_string()
98  {
99  set(str.c_str());
100  }
101 
103  const std::string& str) noexcept
104  {
105  set(str.c_str()); return *this;
106  }
107 
108  // Assign from fixed_string of any size
109  template<size_t N> fixed_string& operator = (
110  const fixed_string<N>& rhs) noexcept
111  {
112  set(rhs.c_str()); return *this;
113  }
114 
115  // Converters to standard types
116  const char* c_str() const noexcept
117  {
118  return string_data;
119  }
120 
121  std::string to_string() const
122  {
123  return std::string(string_data);
124  }
125 
126  // Equality comparisons
128  const char* rhs) const noexcept
129  {
130  return strncmp(string_data, rhs, MAX_CHARS) == 0;
131  }
132 
134  const std::string& rhs) const noexcept
135  {
136  return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0;
137  }
138 
139  template<size_t N> bool operator == (
140  const fixed_string<N>& rhs) const noexcept
141  {
142  return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0;
143  }
144 
145  // Inequality comparisons
147  const char* rhs) const noexcept
148  {
149  return strncmp(string_data, rhs, MAX_CHARS) != 0;
150  }
151 
153  const std::string& rhs) const noexcept
154  {
155  return strncmp(string_data, rhs.c_str(), MAX_CHARS) != 0;
156  }
157 
158  template<size_t N> bool operator != (
159  const fixed_string<N>& rhs) const noexcept
160  {
161  return strncmp(string_data, rhs.c_str(), MAX_CHARS) != 0;
162  }
163 
164  operator const char* () const noexcept {
165  return c_str();
166  }
167 
168  size_t size() const noexcept
169  {
170  return string_len;
171  }
172 
173 private:
174 
175  void set(
176  const char* c_string) noexcept
177  {
178  char* result = (char*) MEMCCPY(string_data, c_string, '\0', MAX_CHARS);
179  string_len = (result == nullptr) ? MAX_CHARS : (size_t)(result - string_data) - 1u;
180  }
181 
182  char string_data[MAX_CHARS + 1];
183  size_t string_len;
184 };
185 
187 
188 } /* namespace fastrtps */
189 } /* namespace eprosima */
190 #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
191 
192 #endif /* FASTRTPS_UTILS_FIXED_SIZE_STRING_HPP_ */
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23
Template class for non-alloc strings.
Definition: fixed_size_string.hpp:44
fixed_string() noexcept
Default constructor.
Definition: fixed_size_string.hpp:50
fixed_string & operator=(const char *c_string) noexcept
Definition: fixed_size_string.hpp:87
size_t size() const noexcept
Definition: fixed_size_string.hpp:168
fixed_string(const std::string &str) noexcept
Definition: fixed_size_string.hpp:95
fixed_string & assign(const char *c_array, size_t n_chars) noexcept
Definition: fixed_size_string.hpp:66
bool operator==(const char *rhs) const noexcept
Definition: fixed_size_string.hpp:127
static constexpr size_t max_size
Definition: fixed_size_string.hpp:47
std::string to_string() const
Definition: fixed_size_string.hpp:121
fixed_string(const char *c_array, size_t n_chars) noexcept
Definition: fixed_size_string.hpp:59
const char * c_str() const noexcept
Definition: fixed_size_string.hpp:116
fixed_string(const char *c_string) noexcept
Definition: fixed_size_string.hpp:80
bool operator!=(const char *rhs) const noexcept
Definition: fixed_size_string.hpp:146