paho-mqtt-cpp
MQTT C++ Client for POSIX and Windows
Loading...
Searching...
No Matches
buffer_view.h
Go to the documentation of this file.
1
7
8/*******************************************************************************
9 * Copyright (c) 2017-2020 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_view_h
25#define __mqtt_buffer_view_h
26
27#include "mqtt/types.h"
28#include <iostream>
29
30namespace mqtt {
31
33
40template <typename T>
42{
43public:
45 using value_type = T;
47 using size_type = size_t;
48
49private:
51 const value_type* data_;
53 size_type sz_;
54
55public:
62 : data_(data), sz_(n) {}
68 buffer_view(const std::basic_string<value_type>& str)
69 : data_(str.data()), sz_(str.size()) {}
74 const value_type* data() const { return data_; }
79 size_type size() const { return sz_; }
84 size_type length() const { return sz_; }
90 const value_type& operator[](size_t i) const { return data_[i]; }
95 std::basic_string<value_type> str() const {
96 return std::basic_string<value_type>(data_, sz_);
97 }
102 string to_string() const {
103 static_assert(sizeof(char) == sizeof(T), "can only get string for char or byte buffers");
104 return string(reinterpret_cast<const char*>(data_), sz_);
105 }
106};
107
108
116template <typename T>
117std::ostream& operator<<(std::ostream& os, const buffer_view<T>& buf) {
118 if (buf.size() > 0)
119 os.write(buf.data(), sizeof(T)*buf.size());
120 return os;
121}
122
125
128
129
131// end namespace mqtt
132}
133
134#endif // __mqtt_buffer_view_h
135
Definition: buffer_view.h:42
const value_type & operator[](size_t i) const
Definition: buffer_view.h:90
T value_type
Definition: buffer_view.h:45
size_t size_type
Definition: buffer_view.h:47
string to_string() const
Definition: buffer_view.h:102
std::basic_string< value_type > str() const
Definition: buffer_view.h:95
size_type length() const
Definition: buffer_view.h:84
size_type size() const
Definition: buffer_view.h:79
buffer_view(const std::basic_string< value_type > &str)
Definition: buffer_view.h:68
const value_type * data() const
Definition: buffer_view.h:74
buffer_view(const value_type *data, size_type n)
Definition: buffer_view.h:61
Definition: async_client.h:49
std::string string
Definition: types.h:40
std::ostream & operator<<(std::ostream &os, const buffer_ref< T > &buf)
Definition: buffer_ref.h:279