123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #ifndef FASTRTPS_UTILS_FIXED_SIZE_STRING_HPP_
- #define FASTRTPS_UTILS_FIXED_SIZE_STRING_HPP_
- #include <string>
- #include <cstring>
- #ifdef _WIN32
- #define MEMCCPY _memccpy
- #else
- #define MEMCCPY memccpy
- #endif
- #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
- namespace eprosima {
- namespace fastrtps {
- template <size_t MAX_CHARS>
- struct fixed_string
- {
- public:
- static constexpr size_t max_size = MAX_CHARS;
-
- fixed_string() noexcept
- {
- memset(string_data, 0, sizeof(string_data) );
- string_len = 0;
- }
-
-
- fixed_string (const char* c_string) noexcept : fixed_string()
- {
- set(c_string != nullptr ? c_string : "");
- }
-
- fixed_string& operator = (const char* c_string) noexcept
- {
- set(c_string != nullptr ? c_string : "");
- return *this;
- }
-
- fixed_string (const std::string& str) noexcept : fixed_string() { set(str.c_str()); }
- fixed_string& operator = (const std::string& str) noexcept { set(str.c_str()); return *this; }
-
- template<size_t N> fixed_string& operator = (const fixed_string<N> & rhs) noexcept { set(rhs.c_str()); return *this; }
-
- const char* c_str() const noexcept { return string_data; }
- std::string to_string() const { return std::string(string_data); }
-
- bool operator == (const char* rhs) const noexcept { return strncmp(string_data, rhs, MAX_CHARS) == 0; }
- bool operator == (const std::string& rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0; }
- template<size_t N> bool operator == (const fixed_string<N> & rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) == 0; }
-
- bool operator != (const char* rhs) const noexcept { return strncmp(string_data, rhs, MAX_CHARS) != 0; }
- bool operator != (const std::string& rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) != 0; }
- template<size_t N> bool operator != (const fixed_string<N> & rhs) const noexcept { return strncmp(string_data, rhs.c_str(), MAX_CHARS) != 0; }
- operator const char* () const noexcept { return c_str(); }
- size_t size() const noexcept { return string_len; }
- private:
- void set(const char* c_string) noexcept
- {
- char* result = (char*) MEMCCPY(string_data, c_string, '\0', MAX_CHARS);
- string_len = (result == nullptr) ? MAX_CHARS : (size_t)(result - string_data) - 1u;
- }
- char string_data[MAX_CHARS + 1];
- size_t string_len;
- };
- using string_255 = fixed_string<255>;
- }
- }
- #endif
- #endif
|