PahoMqttCpp
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 <iostream>
28
29#include "mqtt/types.h"
30
31namespace mqtt {
32
34
41template <typename T>
43{
44public:
46 using value_type = T;
48 using size_type = size_t;
49
50private:
52 const value_type* data_;
54 size_type sz_;
55
56public:
62 buffer_view(const value_type* data, size_type n) : data_(data), sz_(n) {}
68 buffer_view(const std::basic_string<value_type>& str)
69 : data_(str.data()), sz_(str.size()) {}
70
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 }
98
102 string to_string() const {
103 static_assert(
104 sizeof(char) == sizeof(T), "can only get string for char or byte buffers"
105 );
106 return string(reinterpret_cast<const char*>(data_), sz_);
107 }
108};
109
117template <typename T>
118std::ostream& operator<<(std::ostream& os, const buffer_view<T>& buf) {
119 if (buf.size() > 0)
120 os.write(buf.data(), sizeof(T) * buf.size());
121 return os;
122}
123
126
129
131// end namespace mqtt
132} // namespace mqtt
133
134#endif // __mqtt_buffer_view_h
Definition buffer_view.h:43
const value_type & operator[](size_t i) const
Definition buffer_view.h:90
T value_type
Definition buffer_view.h:46
size_t size_type
Definition buffer_view.h:48
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:62
Definition async_client.h:60
buffer_view< char > binary_view
Definition buffer_view.h:128
std::string string
Definition types.h:43
std::ostream & operator<<(std::ostream &os, const buffer_ref< T > &buf)
Definition buffer_ref.h:286
buffer_view< char > string_view
Definition buffer_view.h:125