join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
view.hpp
Go to the documentation of this file.
25#ifndef JOIN_CORE_VIEW_HPP
26#define JOIN_CORE_VIEW_HPP
27
28// C++.
29#include <type_traits>
30#include <string_view>
31#include <algorithm>
32#include <concepts>
33#include <compare>
34#include <limits>
35
36// C.
37#include <cassert>
38#include <cstdint>
39#include <cstddef>
40
41namespace join
42{
46 template <typename Type>
47 class __attribute__ ((packed)) View
48 {
49 public:
53 constexpr View () noexcept = default;
54
60 constexpr View (Type* data, size_t size) noexcept
61 : _data (data)
62 , _size (static_cast<uint32_t> (size))
63 {
64 assert (size <= std::numeric_limits<uint32_t>::max ());
65 }
66
71 constexpr Type* data () const noexcept
72 {
73 return _data;
74 }
75
80 constexpr size_t size () const noexcept
81 {
82 return _size;
83 }
84
89 constexpr bool empty () const noexcept
90 {
91 return _size == 0;
92 }
93
98 constexpr Type* begin () const noexcept
99 {
100 return _data;
101 }
102
107 constexpr Type* end () const noexcept
108 {
109 return _data + _size;
110 }
111
117 constexpr Type& operator[] (size_t pos) const noexcept
118 {
119 return _data[pos];
120 }
121
122 private:
124 Type* _data = nullptr;
125
127 uint32_t _size = 0;
128 };
129
136 template <typename Type>
137 constexpr bool operator== (const View<Type>& lhs, const View<Type>& rhs)
138 {
139 return std::equal (lhs.begin (), lhs.end (), rhs.begin (), rhs.end ());
140 }
141
148 template <typename Type>
149 constexpr std::partial_ordering operator<=> (const View<Type>& lhs, const View<Type>& rhs)
150 {
151 return std::lexicographical_compare_three_way (lhs.begin (), lhs.end (), rhs.begin (), rhs.end ());
152 }
153
160 template <typename Type>
161 requires std::same_as<std::remove_const_t<Type>, char>
162 constexpr bool operator== (const View<Type>& lhs, const View<Type>& rhs)
163 {
164 return std::string_view (lhs.data (), lhs.size ()) == std::string_view (rhs.data (), rhs.size ());
165 }
166
173 template <typename Type>
174 requires std::same_as<std::remove_const_t<Type>, char>
175 constexpr std::partial_ordering operator<=> (const View<Type>& lhs, const View<Type>& rhs)
176 {
177 return std::string_view (lhs.data (), lhs.size ()) <=> std::string_view (rhs.data (), rhs.size ());
178 }
179}
180
181#endif
Definition acceptor.hpp:32
bool operator==(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:196
__attribute__((cold, noinline)) void throwException(Args &&... args)
throw an exception out of the hot path.
Definition utils.hpp:75
std::strong_ordering operator<=>(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
three-way compare endpoints.
Definition endpoint.hpp:208