123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- /////////////////////////////////////////////////////////////////////////////
- /// @file buffer_view.h
- /// Buffer reference type for the Paho MQTT C++ library.
- /// @date April 18, 2017
- /// @author Frank Pagliughi
- /////////////////////////////////////////////////////////////////////////////
- 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
- }
|