Fast RTPS  Version 2.1.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
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 C string
59  fixed_string (const char* c_string) noexcept : fixed_string()
60  {
61  set(c_string != nullptr ? c_string : "");
62  }
63 
64  fixed_string& operator = (const char* c_string) noexcept
65  {
66  set(c_string != nullptr ? c_string : "");
67  return *this;
68  }
69 
70  // Construct / assign from a std::string
71  fixed_string (const std::string& str) noexcept : fixed_string() { set(str.c_str()); }
72  fixed_string& operator = (const std::string& str) noexcept { set(str.c_str()); return *this; }
73 
74  // Assign from fixed_string of any size
75  template<size_t N> fixed_string& operator = (const fixed_string<N> & rhs) noexcept { set(rhs.c_str()); return *this; }
76 
77  // Converters to standard types
78  const char* c_str() const noexcept { return string_data; }
79  std::string to_string() const { return std::string(string_data); }
80 
81  // Equality comparisons
82  bool operator == (const char* rhs) const noexcept { return strncmp(string_data, rhs, MAX_CHARS) == 0; }
83  bool operator == (const std::string& rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0; }
84  template<size_t N> bool operator == (const fixed_string<N> & rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0; }
85 
86  // Inequality comparisons
87  bool operator != (const char* rhs) const noexcept { return strncmp(string_data, rhs, MAX_CHARS) != 0; }
88  bool operator != (const std::string& rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) != 0; }
89  template<size_t N> bool operator != (const fixed_string<N> & rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) != 0; }
90 
91  operator const char* () const noexcept { return c_str(); }
92 
93  size_t size() const noexcept { return string_len; }
94 
95  private:
96  void set(const char* c_string) noexcept
97  {
98  char* result = (char*) MEMCCPY(string_data, c_string, '\0', MAX_CHARS);
99  string_len = (result == nullptr) ? MAX_CHARS : (size_t)(result - string_data) - 1u;
100  }
101 
102  char string_data[MAX_CHARS + 1];
103  size_t string_len;
104 };
105 
107 
108 } /* namespace fastrtps */
109 } /* namespace eprosima */
110 #endif
111 
112 #endif /* FASTRTPS_UTILS_FIXED_SIZE_STRING_HPP_ */
eprosima::fastrtps::fixed_string::operator!=
bool operator!=(const char *rhs) const noexcept
Definition: fixed_size_string.hpp:87
eprosima::fastrtps::fixed_string::operator==
bool operator==(const char *rhs) const noexcept
Definition: fixed_size_string.hpp:82
eprosima::fastrtps::fixed_string::max_size
static constexpr size_t max_size
Definition: fixed_size_string.hpp:47
eprosima::fastrtps::fixed_string::size
size_t size() const noexcept
Definition: fixed_size_string.hpp:93
eprosima::fastrtps::fixed_string::operator=
fixed_string & operator=(const char *c_string) noexcept
Definition: fixed_size_string.hpp:64
eprosima::fastrtps::fixed_string::fixed_string
fixed_string(const char *c_string) noexcept
Definition: fixed_size_string.hpp:59
eprosima::fastrtps::fixed_string::c_str
const char * c_str() const noexcept
Definition: fixed_size_string.hpp:78
eprosima::fastrtps::fixed_string::fixed_string
fixed_string(const std::string &str) noexcept
Definition: fixed_size_string.hpp:71
eprosima::fastrtps::fixed_string::to_string
std::string to_string() const
Definition: fixed_size_string.hpp:79
eprosima::fastrtps::fixed_string::fixed_string
fixed_string() noexcept
Default constructor.
Definition: fixed_size_string.hpp:50
eprosima
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23
eprosima::fastrtps::fixed_string
Template class for non-alloc strings.
Definition: fixed_size_string.hpp:44