Paho C++  1.0
The Paho MQTT C++ Client Library
 All Classes Files Functions Variables Typedefs Friends
buffer_view.h
Go to the documentation of this file.
1 
8 /*******************************************************************************
9  * Copyright (c) 2017 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 v1.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-v10.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 
29 namespace mqtt {
30 
32 
39 template <typename T>
41 {
42 public:
44  using value_type = T;
46  using size_type = size_t;
47 
48 private:
50  const value_type* data_;
52  size_type sz_;
53 
54 public:
61  : data_(data), sz_(n) {}
67  buffer_view(const std::basic_string<value_type>& str)
68  : data_(str.data()), sz_(str.size()) {}
73  const value_type* data() const { return data_; }
78  size_type size() const { return sz_; }
83  size_type length() const { return sz_; }
89  const value_type& operator[](size_t i) const { return data_[i]; }
94  std::basic_string<value_type> str() const {
95  return std::basic_string<value_type>(data_, sz_);
96  }
101  string to_string() const {
102  static_assert(sizeof(char) == sizeof(T), "can only get string for char or byte buffers");
103  return string(reinterpret_cast<const char*>(data_), sz_);
104  }
105 };
106 
108 using string_view = buffer_view<char>;
109 
111 using binary_view = buffer_view<char>;
112 
113 
115 // end namespace mqtt
116 }
117 
118 #endif // __mqtt_buffer_view_h
119 
const value_type & operator[](size_t i) const
Access an item in the view.
Definition: buffer_view.h:89
Basic types and type conversions for the Paho MQTT C++ library.
std::basic_string< value_type > str() const
Gets a copy of the view as a string.
Definition: buffer_view.h:94
size_type size() const
Gets the number of items in the view.
Definition: buffer_view.h:78
const value_type * data() const
Gets a pointer the first item in the view.
Definition: buffer_view.h:73
buffer_view(const std::basic_string< value_type > &str)
Constructs a buffer view to a whole string.
Definition: buffer_view.h:67
size_t size_type
The type used to specify number of items in the container.
Definition: buffer_view.h:46
buffer_view(const value_type *data, size_type n)
Constructs a buffer view.
Definition: buffer_view.h:60
string to_string() const
Gets a copy of the view as a string.
Definition: buffer_view.h:101
A reference to a contiguous sequence of items, with no ownership.
Definition: buffer_view.h:40
size_type length() const
Gets the number of items in the view.
Definition: buffer_view.h:83
T value_type
The type of items to be held in the queue.
Definition: buffer_view.h:44