LIBWIRE
Next-generation C++17 networking library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
memory_view.hpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2018 Max Mazurov (fox.cpp) <fox.cpp [at] disroot [dot] org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #pragma once
23 
24 #include <cstddef>
25 #include <cstdint>
26 #include <iterator>
27 
32 /*
33  * If you had to open this file to find answer for your question - we are so
34  * sorry. Please open issue with your question so we can update documentation
35  * to answer it.
36  */
37 
38 #ifdef __cpp_exceptions
39 # define LIBWIRE_EXCEPTIONS_ENABLED_BOOL true
40 #else
41 # define LIBWIRE_EXCEPTIONS_ENABLED_BOOL false
42 #endif
43 
44 namespace libwire {
58  class memory_view {
59  public:
60  using value_type = uint8_t;
61  using size_type = size_t;
62  using difference_type = std::ptrdiff_t;
64  using const_reference = const value_type&;
65  using pointer = value_type*;
66  using const_pointer = const pointer;
67  using iterator = pointer;
69  using reverse_iterator = std::reverse_iterator<iterator>;
70  using const_reverse_iterator = std::reverse_iterator<iterator>;
71 
72  memory_view() noexcept;
73  memory_view(void* memory, size_t size_bytes) noexcept;
74 
75 #ifdef __cpp_exceptions
76  const_reference at(size_t) const;
77 #endif
78  const_reference operator[](size_t) const noexcept;
79 
80 #ifdef __cpp_exceptions
81  reference at(size_t);
82 #endif
83  reference operator[](size_t) noexcept;
84 
85  const_reference front() const noexcept;
86  const_reference back() const noexcept;
87 
88  reference front() noexcept;
89  reference back() noexcept;
90 
91  const_pointer data() const noexcept;
92  pointer data() noexcept;
93 
94  const_iterator begin() const noexcept;
95  const_iterator end() const noexcept;
96 
97  iterator begin() noexcept;
98  iterator end() noexcept;
99 
100  const_iterator cbegin() const noexcept;
101  const_iterator cend() const noexcept;
102 
106  size_type size() const noexcept;
107 
111  size_type max_size() const noexcept;
112 
117  size_type capacity() const noexcept;
118 
124  void shrink_back(size_type bytes_count) noexcept;
125 
131  void shrink_front(size_type bytes_count) noexcept;
132 
136  void clear() noexcept;
137 
146  void resize(size_t new_size) noexcept(!LIBWIRE_EXCEPTIONS_ENABLED_BOOL);
147 
148  void swap(memory_view& other) noexcept;
149 
150  private:
151  uint8_t* data_;
152  size_t size_;
153  size_t capacity_;
154  };
155 } // namespace libwire
void shrink_front(size_type bytes_count) noexcept
"Hide" X bytes from begin of memory.
const_iterator begin() const noexcept
value_type & reference
Definition: memory_view.hpp:63
const_iterator cbegin() const noexcept
size_type max_size() const noexcept
Same as capacity.
Non-owning STL-like wrapper for raw memory.
Definition: memory_view.hpp:58
std::ptrdiff_t difference_type
Definition: memory_view.hpp:62
value_type * pointer
Definition: memory_view.hpp:65
const_reference operator[](size_t) const noexcept
size_type size() const noexcept
Currently visible memory size.
#define LIBWIRE_EXCEPTIONS_ENABLED_BOOL
Defines memory_view wrapper.
Definition: memory_view.hpp:41
const value_type & const_reference
Definition: memory_view.hpp:64
const pointer const_pointer
Definition: memory_view.hpp:66
void shrink_back(size_type bytes_count) noexcept
"Hide" X bytes from end of memory.
const_reference back() const noexcept
memory_view() noexcept
const_reference front() const noexcept
const_pointer data() const noexcept
size_type capacity() const noexcept
Return size of underlying memory size FROM begin() TO BIGGEST POSSIBLE end().
const_pointer const_iterator
Definition: memory_view.hpp:68
std::reverse_iterator< iterator > const_reverse_iterator
Definition: memory_view.hpp:70
void resize(size_t new_size) noexcept(!false)
"Hide" X bytes from end of memory so that new_size is left visible.
std::reverse_iterator< iterator > reverse_iterator
Definition: memory_view.hpp:69
const_iterator cend() const noexcept
void clear() noexcept
Same as resize (0).
void swap(memory_view &other) noexcept
const_iterator end() const noexcept