Fast RTPS  Version 2.14.1
Fast RTPS
Loading...
Searching...
No Matches
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
33namespace eprosima {
34namespace fastrtps {
35
42template <size_t MAX_CHARS>
44{
45public:
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 template<size_t N> bool operator < (
165 const fixed_string<N>& rhs) const noexcept
166 {
167 return 0 > compare(rhs);
168 }
169
170 template<size_t N> bool operator > (
171 const fixed_string<N>& rhs) const noexcept
172 {
173 return 0 < compare(rhs);
174 }
175
177 const std::string& rhs) const noexcept
178 {
179 return 0 > compare(rhs);
180 }
181
183 const std::string& rhs) const noexcept
184 {
185 return 0 < compare(rhs);
186 }
187
188 operator const char* () const noexcept {
189 return c_str();
190 }
191
192 size_t size() const noexcept
193 {
194 return string_len;
195 }
196
205 const char* str) const noexcept
206 {
207 return strncmp(string_data, str, MAX_CHARS);
208 }
209
218 const std::string& str) const noexcept
219 {
220 return strncmp(string_data, str.c_str(), MAX_CHARS);
221 }
222
230 template<size_t N> int compare(
231 const fixed_string<N>& str) const noexcept
232 {
233 return strncmp(string_data, str.c_str(), MAX_CHARS);
234 }
235
236private:
237
238 void set(
239 const char* c_string) noexcept
240 {
241 char* result = (char*) MEMCCPY(string_data, c_string, '\0', MAX_CHARS);
242 string_len = (result == nullptr) ? MAX_CHARS : (size_t)(result - string_data) - 1u;
243 }
244
245 char string_data[MAX_CHARS + 1];
246 size_t string_len;
247};
248
250
251} /* namespace fastrtps */
252} /* namespace eprosima */
253#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
254
255#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 & operator=(const char *c_string) noexcept
Definition fixed_size_string.hpp:87
fixed_string() noexcept
Default constructor.
Definition fixed_size_string.hpp:50
size_t size() const noexcept
Definition fixed_size_string.hpp:192
fixed_string(const std::string &str) noexcept
Definition fixed_size_string.hpp:95
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
int compare(const std::string &str) const noexcept
Compare with a std::string.
Definition fixed_size_string.hpp:217
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
int compare(const fixed_string< N > &str) const noexcept
Compare with a fixed_string.
Definition fixed_size_string.hpp:230
bool operator>(const fixed_string< N > &rhs) const noexcept
Definition fixed_size_string.hpp:170
fixed_string & assign(const char *c_array, size_t n_chars) noexcept
Definition fixed_size_string.hpp:66
bool operator<(const fixed_string< N > &rhs) const noexcept
Definition fixed_size_string.hpp:164
int compare(const char *str) const noexcept
Compare with a C string.
Definition fixed_size_string.hpp:204
bool operator!=(const char *rhs) const noexcept
Definition fixed_size_string.hpp:146