OGLplus  (0.59.0) a C++ wrapper for rendering APIs

size_and_data.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SERIALIZE_SIZE_AND_DATA_HPP
10 #define EAGINE_SERIALIZE_SIZE_AND_DATA_HPP
11 
12 #include "../branch_predict.hpp"
13 #include "../is_within_limits.hpp"
14 #include "../memory/span_algo.hpp"
15 #include "../multi_byte_seq.hpp"
16 
17 namespace eagine {
18 //------------------------------------------------------------------------------
22 static inline auto
24  -> memory::block {
25 
26  const auto opt_size_cp = limit_cast<mbs::code_point>(src.size());
27  if(EAGINE_LIKELY(opt_size_cp)) {
28  const auto size_cp = extract(opt_size_cp);
29  const auto opt_size_len = mbs::required_sequence_length(size_cp);
30  if(EAGINE_LIKELY(opt_size_len)) {
31  if(extract(opt_size_len) + src.size() <= dst.size()) {
32  const auto opt_skip_len = mbs::encode_code_point(size_cp, dst);
33  if(EAGINE_LIKELY(opt_skip_len)) {
34  const auto skip_len =
35  limit_cast<span_size_t>(extract(opt_skip_len));
36  copy(src, skip(dst, skip_len));
37  return head(dst, skip_len + src.size());
38  }
39  }
40  }
41  }
42 
43  return {};
44 }
45 //------------------------------------------------------------------------------
50 static inline auto skip_data_with_size(memory::const_block src) noexcept
51  -> span_size_t {
52  const auto opt_skip_len = mbs::decode_sequence_length(src);
53  if(const auto opt_data_len = mbs::do_decode_code_point(src, opt_skip_len)) {
54  return extract(opt_skip_len) + extract(opt_data_len);
55  }
56  return 0;
57 }
58 //------------------------------------------------------------------------------
62 static inline auto get_data_with_size(memory::block src) noexcept
63  -> memory::block {
64  const memory::const_block tmp{src};
65  const auto opt_skip_len = mbs::decode_sequence_length(tmp);
66  if(const auto opt_data_len = mbs::do_decode_code_point(tmp, opt_skip_len)) {
67  return head(
68  skip(src, extract(opt_skip_len)),
69  limit_cast<span_size_t>(extract(opt_data_len)));
70  }
71  return {};
72 }
73 //------------------------------------------------------------------------------
77 static inline auto get_data_with_size(memory::const_block src) noexcept
79  const auto opt_skip_len = mbs::decode_sequence_length(src);
80  if(const auto opt_data_len = mbs::do_decode_code_point(src, opt_skip_len)) {
81  return head(
82  skip(src, extract(opt_skip_len)),
83  limit_cast<span_size_t>(extract(opt_data_len)));
84  }
85  return {};
86 }
87 //------------------------------------------------------------------------------
92 template <typename Function>
93 static inline void
94 for_each_data_with_size(memory::const_block src, Function function) noexcept {
95  while(src) {
96  const auto opt_skip_len = mbs::decode_sequence_length(src);
97  const auto opt_data_len = mbs::do_decode_code_point(src, opt_skip_len);
98  if(opt_data_len) {
99  const auto skip_len = extract(opt_skip_len);
100  const auto data_len = extract(opt_data_len);
101  if(data_len > 0) {
102  function(
103  head(skip(src, skip_len), limit_cast<span_size_t>(data_len)));
104  src = skip(src, skip_len + data_len);
105  } else {
106  break;
107  }
108  } else {
109  break;
110  }
111  }
112 }
113 //------------------------------------------------------------------------------
114 } // namespace eagine
115 
116 #endif // EAGINE_SERIALIZE_SIZE_AND_DATA_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
Common code is placed in this namespace.
Definition: eagine.hpp:21
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
static constexpr auto head(basic_span< T, P, S > s, L l) noexcept -> basic_span< T, P, S >
Returns the first l elements from the front of a span.
Definition: span_algo.hpp:99
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
static constexpr auto skip(basic_span< T, P, S > s, L l) noexcept -> basic_span< T, P, S >
Skips a specified count of elements from the front of a span.
Definition: span_algo.hpp:60
static void for_each_data_with_size(memory::const_block src, Function function) noexcept
In a larger block with sub-blocks with size, calls function on each sub-block.
Definition: size_and_data.hpp:94
static auto get_data_with_size(memory::block src) noexcept -> memory::block
Extracts a sub-block from a larger mutable block with encoded sub-block size.
Definition: size_and_data.hpp:62
static auto skip_data_with_size(memory::const_block src) noexcept -> span_size_t
In a block starting with sub-block with size returns the size of the sub-block.
Definition: size_and_data.hpp:50
static auto store_data_with_size(memory::const_block src, memory::block dst) noexcept -> memory::block
Encodes the size of the source block into destination, copies data afterwards.
Definition: size_and_data.hpp:23

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).