Paho C++  1.0
The Paho MQTT C++ Client Library
 All Classes Files Functions Variables Typedefs Friends
buffer_ref.h
Go to the documentation of this file.
1 
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 v1.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-v10.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 
31 namespace mqtt {
32 
34 
58 template <typename T>
60 {
61 public:
66  using value_type = T;
71  using blob = std::basic_string<value_type>;
76  using pointer_type = std::shared_ptr<const blob>;
77 
78 private:
80  pointer_type data_;
81 
82 public:
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;
151  buffer_ref& operator=(buffer_ref&& rhs) =default;
160  buffer_ref& operator=(const blob& b) {
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 
278 template <typename T>
279 std::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 
290 using string_ref = buffer_ref<char>;
291 
298 using binary_ref = buffer_ref<char>;
299 
301 // end namespace mqtt
302 }
303 
304 #endif // __mqtt_buffer_ref_h
305 
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