paho-mqtt-cpp
MQTT C++ Client for POSIX and Windows
Loading...
Searching...
No Matches
buffer_ref.h
Go to the documentation of this file.
1
7
8/*******************************************************************************
9 * Copyright (c) 2017 Frank Pagliughi <fpagliughi@mindspring.com>
10 *
11 * All rights reserved. This program and the accompanying materials
12 * are made available under the terms of the Eclipse Public License v2.0
13 * and Eclipse Distribution License v1.0 which accompany this distribution.
14 *
15 * The Eclipse Public License is available at
16 * http://www.eclipse.org/legal/epl-v20.html
17 * and the Eclipse Distribution License is available at
18 * http://www.eclipse.org/org/documents/edl-v10.php.
19 *
20 * Contributors:
21 * Frank Pagliughi - initial implementation and documentation
22 *******************************************************************************/
23
24#ifndef __mqtt_buffer_ref_h
25#define __mqtt_buffer_ref_h
26
27#include "mqtt/types.h"
28#include <iostream>
29#include <cstring>
30
31namespace mqtt {
32
34
58template <typename T>
60{
61public:
66 using value_type = T;
71 using blob = std::basic_string<value_type>;
76 using pointer_type = std::shared_ptr<const blob>;
77
78private:
80 pointer_type data_;
81
82public:
86 buffer_ref() =default;
91 buffer_ref(const buffer_ref& buf) =default;
96 buffer_ref(buffer_ref&& buf) =default;
101 buffer_ref(const blob& b) : data_{std::make_shared<blob>(b)} {}
107 buffer_ref(blob&& b) : data_{std::make_shared<blob>(std::move(b))} {}
115 buffer_ref(const pointer_type& p) : data_(p) {}
123 buffer_ref(pointer_type&& p) : data_(std::move(p)) {}
129 buffer_ref(const value_type* buf, size_t n) : data_{std::make_shared<blob>(buf,n)} {}
135 buffer_ref(const char* buf) : buffer_ref(reinterpret_cast<const value_type*>(buf),
136 std::strlen(buf)) {
137 static_assert(sizeof(char) == sizeof(T), "can only use C arr with char or byte buffers");
138 }
139
145 buffer_ref& operator=(const buffer_ref& rhs) =default;
161 data_.reset(new blob(b));
162 return *this;
163 }
173 data_.reset(new blob(std::move(b)));
174 return *this;
175 }
181 buffer_ref& operator=(const char* cstr) {
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)));
184 return *this;
185 }
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()));
199 return *this;
200 }
204 void reset() { data_.reset(); }
212 explicit operator bool() const { return bool(data_); }
220 bool is_null() const { return !data_; }
226 bool empty() const { return !data_ || data_->empty(); }
231 const value_type* data() const { return data_->data(); }
236 size_t size() const { return data_->size(); }
241 size_t length() const { return data_->length(); }
246 const blob& str() const { return *data_; }
251 const blob& to_string() const { return str(); }
257 const char* c_str() const { return data_->c_str(); }
262 const pointer_type& ptr() const { return data_; }
268 const value_type& operator[](size_t i) const { return (*data_)[i]; }
269};
270
278template <typename T>
279std::ostream& operator<<(std::ostream& os, const buffer_ref<T>& buf) {
280 if (!buf.empty())
281 os.write(buf.data(), buf.size());
282 return os;
283}
284
286
291
299
301// end namespace mqtt
302}
303
304#endif // __mqtt_buffer_ref_h
305
Definition: buffer_ref.h:60
buffer_ref(const char *buf)
Definition: buffer_ref.h:135
buffer_ref(pointer_type &&p)
Definition: buffer_ref.h:123
T value_type
Definition: buffer_ref.h:66
buffer_ref & operator=(blob &&b)
Definition: buffer_ref.h:172
buffer_ref & operator=(const buffer_ref &rhs)=default
buffer_ref & operator=(buffer_ref &&rhs)=default
buffer_ref()=default
buffer_ref(const buffer_ref &buf)=default
const char * c_str() const
Definition: buffer_ref.h:257
size_t size() const
Definition: buffer_ref.h:236
buffer_ref & operator=(const buffer_ref< OT > &rhs)
Definition: buffer_ref.h:196
bool is_null() const
Definition: buffer_ref.h:220
buffer_ref & operator=(const blob &b)
Definition: buffer_ref.h:160
const value_type * data() const
Definition: buffer_ref.h:231
const blob & to_string() const
Definition: buffer_ref.h:251
std::basic_string< value_type > blob
Definition: buffer_ref.h:71
const value_type & operator[](size_t i) const
Definition: buffer_ref.h:268
const blob & str() const
Definition: buffer_ref.h:246
std::shared_ptr< const blob > pointer_type
Definition: buffer_ref.h:76
const pointer_type & ptr() const
Definition: buffer_ref.h:262
size_t length() const
Definition: buffer_ref.h:241
buffer_ref(const blob &b)
Definition: buffer_ref.h:101
buffer_ref & operator=(const char *cstr)
Definition: buffer_ref.h:181
buffer_ref(const value_type *buf, size_t n)
Definition: buffer_ref.h:129
buffer_ref(buffer_ref &&buf)=default
void reset()
Definition: buffer_ref.h:204
buffer_ref(const pointer_type &p)
Definition: buffer_ref.h:115
bool empty() const
Definition: buffer_ref.h:226
buffer_ref(blob &&b)
Definition: buffer_ref.h:107
Definition: async_client.h:49
std::ostream & operator<<(std::ostream &os, const buffer_ref< T > &buf)
Definition: buffer_ref.h:279