fixed_size_string.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /*!
  15. * @file fixed_size_string.hpp
  16. *
  17. */
  18. #ifndef FASTRTPS_UTILS_FIXED_SIZE_STRING_HPP_
  19. #define FASTRTPS_UTILS_FIXED_SIZE_STRING_HPP_
  20. #include <string>
  21. #include <cstring>
  22. #ifdef _WIN32
  23. #define MEMCCPY _memccpy
  24. #else
  25. #define MEMCCPY memccpy
  26. #endif
  27. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  28. namespace eprosima {
  29. namespace fastrtps {
  30. /**
  31. * Template class for non-alloc strings. Will be truncated when assigned from a longer string.
  32. * @tparam MAX_CHARS Maximum number of characters is specified as the template parameter.
  33. * Space for an additional null terminator will be reserved.
  34. * @ingroup UTILITIES_MODULE
  35. */
  36. template <size_t MAX_CHARS>
  37. struct fixed_string
  38. {
  39. public:
  40. static constexpr size_t max_size = MAX_CHARS;
  41. /// Default constructor.
  42. fixed_string() noexcept
  43. {
  44. memset(string_data, 0, sizeof(string_data) );
  45. string_len = 0;
  46. }
  47. // We don't need to define copy/move constructors/assignment operators as the default ones would be enough
  48. // Construct / assign from a C string
  49. fixed_string (const char* c_string) noexcept : fixed_string()
  50. {
  51. set(c_string != nullptr ? c_string : "");
  52. }
  53. fixed_string& operator = (const char* c_string) noexcept
  54. {
  55. set(c_string != nullptr ? c_string : "");
  56. return *this;
  57. }
  58. // Construct / assign from a std::string
  59. fixed_string (const std::string& str) noexcept : fixed_string() { set(str.c_str()); }
  60. fixed_string& operator = (const std::string& str) noexcept { set(str.c_str()); return *this; }
  61. // Assign from fixed_string of any size
  62. template<size_t N> fixed_string& operator = (const fixed_string<N> & rhs) noexcept { set(rhs.c_str()); return *this; }
  63. // Converters to standard types
  64. const char* c_str() const noexcept { return string_data; }
  65. std::string to_string() const { return std::string(string_data); }
  66. // Equality comparisons
  67. bool operator == (const char* rhs) const noexcept { return strncmp(string_data, rhs, MAX_CHARS) == 0; }
  68. bool operator == (const std::string& rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0; }
  69. template<size_t N> bool operator == (const fixed_string<N> & rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0; }
  70. // Inequality comparisons
  71. bool operator != (const char* rhs) const noexcept { return strncmp(string_data, rhs, MAX_CHARS) != 0; }
  72. bool operator != (const std::string& rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) != 0; }
  73. template<size_t N> bool operator != (const fixed_string<N> & rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) != 0; }
  74. operator const char* () const noexcept { return c_str(); }
  75. size_t size() const noexcept { return string_len; }
  76. private:
  77. void set(const char* c_string) noexcept
  78. {
  79. char* result = (char*) MEMCCPY(string_data, c_string, '\0', MAX_CHARS);
  80. string_len = (result == nullptr) ? MAX_CHARS : (size_t)(result - string_data) - 1u;
  81. }
  82. char string_data[MAX_CHARS + 1]; ///< Holds string data, including ending null character.
  83. size_t string_len; ///< Holds current string length.
  84. };
  85. using string_255 = fixed_string<255>;
  86. } /* namespace fastrtps */
  87. } /* namespace eprosima */
  88. #endif
  89. #endif /* FASTRTPS_UTILS_FIXED_SIZE_STRING_HPP_ */