24 #ifndef __mqtt_buffer_ref_h
25 #define __mqtt_buffer_ref_h
71 using blob = std::basic_string<value_type>;
137 static_assert(
sizeof(
char) ==
sizeof(T),
"can only use C arr with char or byte buffers");
161 data_.reset(
new blob(b));
173 data_.reset(
new blob(std::move(b)));
182 static_assert(
sizeof(
char) ==
sizeof(T),
"can only use C arr with char or byte buffers");
183 data_.reset(
new blob(reinterpret_cast<const value_type*>(cstr), strlen(cstr)));
195 template <
typename OT>
197 static_assert(
sizeof(OT) ==
sizeof(T),
"Can only assign buffers if values the same size");
198 data_.reset(
new blob(reinterpret_cast<const value_type*>(rhs.
data()), rhs.
size()));
212 explicit operator bool()
const {
return bool(data_); }
226 bool empty()
const {
return !data_ || data_->empty(); }
236 size_t size()
const {
return data_->size(); }
241 size_t length()
const {
return data_->length(); }
257 const char*
c_str()
const {
return data_->c_str(); }
278 template <
typename T>
279 std::ostream& operator<<(std::ostream& os, const buffer_ref<T>& buf) {
281 os.write(buf.data(), buf.size());
290 using string_ref = buffer_ref<char>;
298 using binary_ref = buffer_ref<char>;
304 #endif // __mqtt_buffer_ref_h
size_t size() const
Gets the size of the data buffer.
Definition: buffer_ref.h:236
buffer_ref(pointer_type &&p)
Creates a reference to an existing buffer by moving the shared pointer.
Definition: buffer_ref.h:123
Basic types and type conversions for the Paho MQTT C++ library.
buffer_ref & operator=(const buffer_ref &rhs)=default
Copy the reference to the buffer.
const char * c_str() const
Gets the data buffer as NUL-terminated C string.
Definition: buffer_ref.h:257
const blob & str() const
Gets the data buffer as a string.
Definition: buffer_ref.h:246
buffer_ref(blob &&b)
Creates a reference to a new buffer by moving a string into the buffer.
Definition: buffer_ref.h:107
A reference object for holding immutable data buffers, with cheap copy semantics and lifetime managem...
Definition: buffer_ref.h:59
char value_type
The underlying type for the buffer.
Definition: buffer_ref.h:66
std::basic_string< value_type > blob
The type for the buffer.
Definition: buffer_ref.h:71
buffer_ref()=default
Default constructor creates a null reference.
size_t length() const
Gets the size of the data buffer.
Definition: buffer_ref.h:241
buffer_ref & operator=(blob &&b)
Move a string into this object, creating a new buffer.
Definition: buffer_ref.h:172
buffer_ref & operator=(const blob &b)
Copy a string into this object, creating a new buffer.
Definition: buffer_ref.h:160
const value_type & operator[](size_t i) const
Gets elemental access to the data buffer (read only)
Definition: buffer_ref.h:268
buffer_ref(const blob &b)
Creates a reference to a new buffer by copying data.
Definition: buffer_ref.h:101
bool is_null() const
Determines if the reference is invalid.
Definition: buffer_ref.h:220
buffer_ref(const value_type *buf, size_t n)
Creates a reference to a new buffer containing a copy of the data.
Definition: buffer_ref.h:129
std::shared_ptr< const blob > pointer_type
The pointer we use.
Definition: buffer_ref.h:76
bool empty() const
Determines if the buffer is empty.
Definition: buffer_ref.h:226
buffer_ref(const char *buf)
Creates a reference to a new buffer containing a copy of the NUL-terminated char array.
Definition: buffer_ref.h:135
const pointer_type & ptr() const
Gets a shared pointer to the (const) data buffer.
Definition: buffer_ref.h:262
const value_type * data() const
Gets a const pointer to the data buffer.
Definition: buffer_ref.h:231
buffer_ref & operator=(const char *cstr)
Copy a NUL-terminated C char array into a new buffer.
Definition: buffer_ref.h:181
void reset()
Clears the reference to nil.
Definition: buffer_ref.h:204
buffer_ref(const pointer_type &p)
Creates a reference to an existing buffer by copying the shared pointer.
Definition: buffer_ref.h:115
buffer_ref & operator=(const buffer_ref< OT > &rhs)
Copy another type of buffer reference to this one.
Definition: buffer_ref.h:196
const blob & to_string() const
Gets the data buffer as a string.
Definition: buffer_ref.h:251