Loading...
Searching...
No Matches
buffer_ref.h
Go to the documentation of this file.
/*******************************************************************************
* Copyright (c) 2017-2024 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
*******************************************************************************/
#ifndef __mqtt_buffer_ref_h
#define __mqtt_buffer_ref_h
#include <cstring>
#include <iostream>
#include "mqtt/types.h"
namespace mqtt {
template <typename T>
class buffer_ref
{
public:
using value_type = T;
using blob = std::basic_string<value_type>;
using pointer_type = std::shared_ptr<const blob>;
private:
pointer_type data_;
public:
buffer_ref() = default;
buffer_ref(const buffer_ref& buf) = default;
buffer_ref(buffer_ref&& buf) = default;
buffer_ref(const blob& b) : data_{std::make_shared<blob>(b)} {}
buffer_ref(blob&& b) : data_{std::make_shared<blob>(std::move(b))} {}
buffer_ref(const pointer_type& p) : data_(p) {}
buffer_ref(pointer_type&& p) : data_(std::move(p)) {}
buffer_ref(const value_type* buf, size_t n) : data_{std::make_shared<blob>(buf, n)} {}
buffer_ref(const char* buf)
: buffer_ref(reinterpret_cast<const value_type*>(buf), std::strlen(buf)) {
static_assert(
sizeof(char) == sizeof(T), "can only use C arr with char or byte buffers"
);
}
buffer_ref& operator=(const buffer_ref& rhs) = default;
buffer_ref& operator=(buffer_ref&& rhs) = default;
buffer_ref& operator=(const blob& b) {
data_.reset(new blob(b));
return *this;
}
buffer_ref& operator=(blob&& b) {
data_.reset(new blob(std::move(b)));
return *this;
}
buffer_ref& operator=(const char* cstr) {
static_assert(
sizeof(char) == sizeof(T), "can only use C arr with char or byte buffers"
);
data_.reset(new blob(reinterpret_cast<const value_type*>(cstr), strlen(cstr)));
return *this;
}
template <typename OT>
buffer_ref& operator=(const buffer_ref<OT>& rhs) {
static_assert(
sizeof(OT) == sizeof(T), "Can only assign buffers if values the same size"
);
data_.reset(new blob(reinterpret_cast<const value_type*>(rhs.data()), rhs.size()));
return *this;
}
void reset() { data_.reset(); }
explicit operator bool() const { return bool(data_); }
bool is_null() const { return !data_; }
bool empty() const { return !data_ || data_->empty(); }
const value_type* data() const { return data_->data(); }
size_t size() const { return data_->size(); }
size_t length() const { return data_->length(); }
const blob& str() const { return *data_; }
const blob& to_string() const { return str(); }
const char* c_str() const { return data_->c_str(); }
const pointer_type& ptr() const { return data_; }
const value_type& operator[](size_t i) const { return (*data_)[i]; }
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const buffer_ref<T>& buf) {
if (!buf.empty())
os.write(buf.data(), buf.size());
return os;
}
using string_ref = buffer_ref<char>;
using binary_ref = buffer_ref<char>;
} // namespace mqtt
#endif // __mqtt_buffer_ref_h
mqtt::buffer_ref
Definition buffer_ref.h:61
mqtt::buffer_ref::buffer_ref
buffer_ref(const char *buf)
Definition buffer_ref.h:136
mqtt::buffer_ref::buffer_ref
buffer_ref(pointer_type &&p)
Definition buffer_ref.h:124
mqtt::buffer_ref::value_type
T value_type
Definition buffer_ref.h:67
mqtt::buffer_ref::operator=
buffer_ref & operator=(blob &&b)
Definition buffer_ref.h:175
mqtt::buffer_ref::operator=
buffer_ref & operator=(const buffer_ref &rhs)=default
mqtt::buffer_ref::operator=
buffer_ref & operator=(buffer_ref &&rhs)=default
mqtt::buffer_ref::buffer_ref
buffer_ref()=default
mqtt::buffer_ref::buffer_ref
buffer_ref(const buffer_ref &buf)=default
mqtt::buffer_ref::c_str
const char * c_str() const
Definition buffer_ref.h:264
mqtt::buffer_ref::size
size_t size() const
Definition buffer_ref.h:243
mqtt::buffer_ref::operator=
buffer_ref & operator=(const buffer_ref< OT > &rhs)
Definition buffer_ref.h:201
mqtt::buffer_ref::is_null
bool is_null() const
Definition buffer_ref.h:227
mqtt::buffer_ref::operator=
buffer_ref & operator=(const blob &b)
Definition buffer_ref.h:163
mqtt::buffer_ref::data
const value_type * data() const
Definition buffer_ref.h:238
mqtt::buffer_ref::to_string
const blob & to_string() const
Definition buffer_ref.h:258
mqtt::buffer_ref::blob
std::basic_string< value_type > blob
Definition buffer_ref.h:72
mqtt::buffer_ref::operator[]
const value_type & operator[](size_t i) const
Definition buffer_ref.h:275
mqtt::buffer_ref::str
const blob & str() const
Definition buffer_ref.h:253
mqtt::buffer_ref::pointer_type
std::shared_ptr< const blob > pointer_type
Definition buffer_ref.h:77
mqtt::buffer_ref::ptr
const pointer_type & ptr() const
Definition buffer_ref.h:269
mqtt::buffer_ref::length
size_t length() const
Definition buffer_ref.h:248
mqtt::buffer_ref::buffer_ref
buffer_ref(const blob &b)
Definition buffer_ref.h:102
mqtt::buffer_ref::operator=
buffer_ref & operator=(const char *cstr)
Definition buffer_ref.h:184
mqtt::buffer_ref::buffer_ref
buffer_ref(const value_type *buf, size_t n)
Definition buffer_ref.h:130
mqtt::buffer_ref::buffer_ref
buffer_ref(buffer_ref &&buf)=default
mqtt::buffer_ref::reset
void reset()
Definition buffer_ref.h:211
mqtt::buffer_ref::buffer_ref
buffer_ref(const pointer_type &p)
Definition buffer_ref.h:116
mqtt::buffer_ref::empty
bool empty() const
Definition buffer_ref.h:233
mqtt::buffer_ref::buffer_ref
buffer_ref(blob &&b)
Definition buffer_ref.h:108
mqtt
Definition async_client.h:60
mqtt::operator<<
std::ostream & operator<<(std::ostream &os, const buffer_ref< T > &buf)
Definition buffer_ref.h:286
types.h