Horizon Official Technical Documentation
MessageBuffer.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef MessageBuffer_hpp
31#define MessageBuffer_hpp
32
33#include <cstring>
34#include <cstdint>
35#include <vector>
36
38{
39 typedef std::vector<uint8_t>::size_type size_type;
40
41public:
42 uint32_t MAX_BUFFER_LENGTH{1536};
43
45 {
47 }
48
49 explicit MessageBuffer(std::size_t initial_size)
50 : _wpos(0), _rpos(0), _storage()
51 {
52 _storage.resize(initial_size);
53 }
54
56 : _wpos(right._wpos), _rpos(right._rpos), _storage(right._storage)
57 {
58 }
59
61 : _wpos(right._wpos), _rpos(right._rpos), _storage(right.move())
62 {
63 }
64
65 void reset()
66 {
67 _wpos = 0;
68 _rpos = 0;
69 }
70
71 void resize(size_type bytes)
72 {
73 _storage.resize(bytes);
74 }
75
76 std::string to_string()
77 {
78 return std::string(get_read_pointer(), get_write_pointer());
79 }
80
81 uint8_t *get_base_pointer() { return _storage.data(); }
82 uint8_t *get_read_pointer() { return get_base_pointer() + _rpos; }
83 uint8_t *get_write_pointer() { return get_base_pointer() + _wpos; }
84
85 void read_completed(size_type bytes) { _rpos += bytes; }
86 void write_completed(size_type bytes) { _wpos += bytes; }
87
88 size_type get_active_size() const { return _wpos - _rpos; }
89 size_type get_remaining_space() const { return _storage.size() - _wpos; }
90 size_type get_buffer_size() const { return _storage.size(); }
91
92 // Discards inactive data
93 void normalize()
94 {
95 if (_rpos) {
96 if (_rpos != _wpos)
98 _wpos -= _rpos;
99 _rpos = 0;
100 }
101 }
102
103 // Ensures there's "some" free space, make sure to call normalize() before this
105 {
106 // resize buffer if it's already full
107 if (get_remaining_space() == 0)
108 _storage.resize(_storage.size() * 3 / 2);
109 }
110
111 void write(void const* data, std::size_t size)
112 {
113 if (size) {
114 memcpy(get_write_pointer(), data, size);
115 write_completed(size);
116 }
117 }
118
119 std::vector<uint8_t> &&move()
120 {
121 _wpos = 0;
122 _rpos = 0;
123
124 return std::move(_storage);
125 }
126
127 const std::vector<uint8_t> &copy() { return _storage; }
128
130 {
131 if (this != &right) {
132 _wpos = right._wpos;
133 _rpos = right._rpos;
134 _storage = right._storage;
135 }
136
137 return *this;
138 }
139
141 {
142 if (this != &right) {
143 _wpos = right._wpos;
144 _rpos = right._rpos;
145 _storage = right.move();
146 }
147
148 return *this;
149 }
150
151private:
154 std::vector<uint8_t> _storage;
155};
156
157#endif /* MessageBuffer_hpp */
Definition: MessageBuffer.hpp:38
size_type _rpos
Definition: MessageBuffer.hpp:153
size_type get_remaining_space() const
Definition: MessageBuffer.hpp:89
MessageBuffer & operator=(MessageBuffer &&right)
Definition: MessageBuffer.hpp:140
void reset()
Definition: MessageBuffer.hpp:65
void read_completed(size_type bytes)
Definition: MessageBuffer.hpp:85
MessageBuffer & operator=(MessageBuffer const &right)
Definition: MessageBuffer.hpp:129
size_type get_active_size() const
Definition: MessageBuffer.hpp:88
size_type _wpos
Definition: MessageBuffer.hpp:152
const std::vector< uint8_t > & copy()
Definition: MessageBuffer.hpp:127
std::vector< uint8_t >::size_type size_type
Definition: MessageBuffer.hpp:39
std::vector< uint8_t > _storage
Definition: MessageBuffer.hpp:154
void write_completed(size_type bytes)
Definition: MessageBuffer.hpp:86
MessageBuffer()
Definition: MessageBuffer.hpp:44
MessageBuffer(MessageBuffer const &right)
Definition: MessageBuffer.hpp:55
uint8_t * get_read_pointer()
Definition: MessageBuffer.hpp:82
uint32_t MAX_BUFFER_LENGTH
Definition: MessageBuffer.hpp:42
MessageBuffer(std::size_t initial_size)
Definition: MessageBuffer.hpp:49
std::vector< uint8_t > && move()
Definition: MessageBuffer.hpp:119
uint8_t * get_base_pointer()
Definition: MessageBuffer.hpp:81
uint8_t * get_write_pointer()
Definition: MessageBuffer.hpp:83
MessageBuffer(MessageBuffer &&right)
Definition: MessageBuffer.hpp:60
void normalize()
Definition: MessageBuffer.hpp:93
void ensure_free_space()
Definition: MessageBuffer.hpp:104
void resize(size_type bytes)
Definition: MessageBuffer.hpp:71
void write(void const *data, std::size_t size)
Definition: MessageBuffer.hpp:111
size_type get_buffer_size() const
Definition: MessageBuffer.hpp:90
std::string to_string()
Definition: MessageBuffer.hpp:76