nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
fixed_string.hpp
Go to the documentation of this file.
1// nros-cpp: FixedString — fixed-capacity null-terminated string
2// Freestanding C++14 — no exceptions, no STL required
3//
4// Memory layout is repr(C) compatible: identical to char[N].
5// Used by codegen-generated message structs for string fields.
6
13#ifndef NROS_RCLCPP_FIXED_STRING_HPP
14#define NROS_RCLCPP_FIXED_STRING_HPP
15
16#include <cstddef>
17#include <string.h>
18
19// phase-417 W1.c — `std::string` interop. `<string>` is absent from a minimal
20// freestanding libcpp and present-but-poisoned under `-ffreestanding`, so the
21// interop below exists only where `NROS_CPP_HAS_STD_STRING` does. That macro
22// has one definition site, which carries the measurement for both cases.
23#include "rclcpp/detail/std_detect.hpp"
24
25namespace rclcpp {
26
39template <size_t N> struct FixedString {
40 char data[N];
41
43 FixedString() { data[0] = '\0'; }
44
46 FixedString& operator=(const char* s) {
47 if (s == nullptr) {
48 data[0] = '\0';
49 return *this;
50 }
51 size_t i = 0;
52 for (; i < N - 1 && s[i] != '\0'; ++i) {
53 data[i] = s[i];
54 }
55 data[i] = '\0';
56 return *this;
57 }
58
60 const char* c_str() const { return data; }
61
63 size_t length() const {
64 size_t len = 0;
65 while (len < N && data[len] != '\0')
66 ++len;
67 return len;
68 }
69
71 static constexpr size_t capacity() { return N - 1; }
72
75 size_t size() const { return length(); }
76
78 bool empty() const { return data[0] == '\0'; }
79
81 bool operator==(const char* s) const {
82 if (s == nullptr) return data[0] == '\0';
83 return strncmp(data, s, N) == 0;
84 }
85
86 bool operator!=(const char* s) const { return !(*this == s); }
87
88#ifdef NROS_CPP_HAS_STD_STRING // hosted-family: string-interop
89 // phase-417 W1.c — `std::string` interop.
90 //
91 // Codegen emits a message's string field as `FixedString<N>` where
92 // upstream rosidl emits `std::string`, so the very first line of a ported
93 // node —
94 //
95 // message.data = "Hello, world! " + std::to_string(count_++);
96 //
97 // did not compile: the only assignment was from `const char*`. These
98 // members COPY and call through to the existing `const char*` paths; they
99 // add no data member and no second code path (RFC-0089 §"Who implements an
100 // adopted name" — container/string conversions that copy and delegate are
101 // wrapper-side ergonomics), so the `char[N]` layout the FFI relies on is
102 // unchanged.
103 //
104 // Truncation is the same bounded-capacity contract `operator=(const
105 // char*)` has always had: a longer source is cut at `N - 1` characters.
106
108 FixedString& operator=(const std::string& s) { return *this = s.c_str(); }
109
112 // NOLINTNEXTLINE(google-explicit-constructor)
113 operator std::string() const { return std::string(data, length()); }
114
117 std::string to_string() const { return std::string(data, length()); }
118
119 bool operator==(const std::string& s) const { return *this == s.c_str(); }
120 bool operator!=(const std::string& s) const { return !(*this == s.c_str()); }
121#endif
122};
123
124} // namespace rclcpp
125
126#endif // NROS_RCLCPP_FIXED_STRING_HPP
Definition client.hpp:129
Definition rclcpp.hpp:75
Definition fixed_string.hpp:39
char data[N]
Definition fixed_string.hpp:40
const char * c_str() const
Get a pointer to the null-terminated string.
Definition fixed_string.hpp:60
static constexpr size_t capacity()
Maximum number of characters (excluding null terminator).
Definition fixed_string.hpp:71
FixedString()
Default constructor — empty string.
Definition fixed_string.hpp:43
bool operator==(const char *s) const
Compare with a C string.
Definition fixed_string.hpp:81
bool empty() const
True when the string holds no characters. phase-417 W1.c; ungated.
Definition fixed_string.hpp:78
bool operator!=(const char *s) const
Definition fixed_string.hpp:86
FixedString & operator=(const char *s)
Assign from a C string. Truncates if longer than capacity.
Definition fixed_string.hpp:46
size_t size() const
Definition fixed_string.hpp:75
size_t length() const
Get the length of the string (up to N-1).
Definition fixed_string.hpp:63