PahoMqttCpp
MQTT C++ Client for POSIX and Windows
Toggle main menu visibility
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
31
namespace
mqtt
{
32
34
41
template
<
typename
T>
42
class
buffer_view
43
{
44
public
:
46
using
value_type
= T;
48
using
size_type
= size_t;
49
50
private
:
52
const
value_type
* data_;
54
size_type
sz_;
55
56
public
:
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
117
template
<
typename
T>
118
std::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
125
using
string_view
=
buffer_view<char>
;
126
128
using
binary_view
=
buffer_view<char>
;
129
131
// end namespace mqtt
132
}
// namespace mqtt
133
134
#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
Generated on
for PahoMqttCpp by
1.17.0