Loading...
Searching...
No Matches
buffer_view.h
Go to the documentation of this file.
/*******************************************************************************
* Copyright (c) 2017-2020 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_view_h
#define __mqtt_buffer_view_h
#include <iostream>
#include "mqtt/types.h"
namespace mqtt {
template <typename T>
class buffer_view
{
public:
using value_type = T;
using size_type = size_t;
private:
const value_type* data_;
size_type sz_;
public:
buffer_view(const value_type* data, size_type n) : data_(data), sz_(n) {}
buffer_view(const std::basic_string<value_type>& str)
: data_(str.data()), sz_(str.size()) {}
const value_type* data() const { return data_; }
size_type size() const { return sz_; }
size_type length() const { return sz_; }
const value_type& operator[](size_t i) const { return data_[i]; }
std::basic_string<value_type> str() const {
return std::basic_string<value_type>(data_, sz_);
}
string to_string() const {
static_assert(
sizeof(char) == sizeof(T), "can only get string for char or byte buffers"
);
return string(reinterpret_cast<const char*>(data_), sz_);
}
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const buffer_view<T>& buf) {
if (buf.size() > 0)
os.write(buf.data(), sizeof(T) * buf.size());
return os;
}
using string_view = buffer_view<char>;
using binary_view = buffer_view<char>;
// end namespace mqtt
} // namespace mqtt
#endif // __mqtt_buffer_view_h
mqtt::buffer_view
Definition buffer_view.h:43
mqtt::buffer_view::operator[]
const value_type & operator[](size_t i) const
Definition buffer_view.h:90
mqtt::buffer_view::value_type
T value_type
Definition buffer_view.h:46
mqtt::buffer_view::size_type
size_t size_type
Definition buffer_view.h:48
mqtt::buffer_view::to_string
string to_string() const
Definition buffer_view.h:102
mqtt::buffer_view< char >::str
std::basic_string< value_type > str() const
Definition buffer_view.h:95
mqtt::buffer_view::length
size_type length() const
Definition buffer_view.h:84
mqtt::buffer_view< char >::size
size_type size() const
Definition buffer_view.h:79
mqtt::buffer_view::buffer_view
buffer_view(const std::basic_string< value_type > &str)
Definition buffer_view.h:68
mqtt::buffer_view< char >::data
const value_type * data() const
Definition buffer_view.h:74
mqtt::buffer_view::buffer_view
buffer_view(const value_type *data, size_type n)
Definition buffer_view.h:62
mqtt
Definition async_client.h:60
mqtt::binary_view
buffer_view< char > binary_view
Definition buffer_view.h:128
mqtt::string
std::string string
Definition types.h:43
mqtt::operator<<
std::ostream & operator<<(std::ostream &os, const buffer_ref< T > &buf)
Definition buffer_ref.h:286
mqtt::string_view
buffer_view< char > string_view
Definition buffer_view.h:125
types.h