Sen API
Sen Libraries
Loading...
Searching...
No Matches
input_stream.h
Go to the documentation of this file.
1// === input_stream.h ==================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_IO_INPUT_STREAM_H
9#define SEN_CORE_IO_INPUT_STREAM_H
10
11// sen
14#include "sen/core/base/span.h"
17
18// std
19#include <cstring>
20
21namespace sen
22{
23
26
29{
30 SEN_COPY_MOVE(InputStreamBase)
31
32public:
33 explicit InputStreamBase(Span<const uint8_t> buffer) noexcept: buffer_(std::move(buffer)) {}
34 ~InputStreamBase() = default;
35
36public:
38 [[nodiscard]] const uint8_t* advance(std::size_t bytes);
39 [[nodiscard]] std::pair<const uint8_t*, std::size_t> tryAdvance(std::size_t bytes);
40 [[nodiscard]] bool atEnd() const noexcept { return cursor_ == buffer_.size(); }
41 [[nodiscard]] std::size_t getPosition() const noexcept { return cursor_; }
42 void setPosition(std::size_t pos) noexcept { cursor_ = pos; }
43
44protected:
45 void reverse(std::size_t bytes) noexcept { cursor_ -= bytes; }
46
47private:
48 Span<const uint8_t> buffer_;
49 std::size_t cursor_ = 0U;
50};
51
55template <typename BufferEndian>
57{
58 SEN_COPY_MOVE(InputStreamTemplate)
59
60public:
62 ~InputStreamTemplate() noexcept = default;
63
64public:
65 void readBool(bool& val);
66 void readInt8(int8_t& val) { readBasic(val); }
67 void readUInt8(uint8_t& val) { readBasic(val); }
68 void readInt16(int16_t& val) { readBasic(val); }
69 void readUInt16(uint16_t& val) { readBasic(val); }
70 void readInt32(int32_t& val) { readBasic(val); }
71 void readUInt32(uint32_t& val) { readBasic(val); }
72 void readInt64(int64_t& val) { readBasic(val); }
73 void readUInt64(uint64_t& val) { readBasic(val); }
76 void readString(std::string& val);
78
79private:
80 template <typename T>
81 impl::IfBasic<T, void> readBasic(T& val);
82};
83
85
87
88//----------------------------------------------------------------------------------------------------------------------
89// Inline implementation
90//----------------------------------------------------------------------------------------------------------------------
91
92template <typename BufferEndian>
94{
95 impl::BoolTransportType tmp;
96 readBasic(tmp);
97 val = (tmp != 0U);
98}
99
100template <typename BufferEndian>
102{
103 constexpr std::size_t size = sizeof(val);
104 std::memcpy(&val, advance(size), size);
105}
106
107template <typename BufferEndian>
109{
110 constexpr std::size_t size = sizeof(val);
111 std::memcpy(&val, advance(size), size);
112}
113
114template <typename BufferEndian>
116{
117 int64_t nanoseconds;
118 readInt64(nanoseconds);
119 val = TimeStamp(nanoseconds);
120}
121
122//--------------------------------------------------------------------------------------------------------------
123// InputStreamTemplate
124//--------------------------------------------------------------------------------------------------------------
125
126template <typename BufferEndian>
128{
129 // read the size of the sequence
130 uint32_t size = 0U;
131 readUInt32(size);
132
133 if (size == 0U)
134 {
135 val.clear();
136 return;
137 }
138
139 // ensure the sequence is empty
140 val.clear();
141 val.resize(size);
142
143 // read the data
144 std::memcpy(val.data(), advance(size), size);
145}
146
147template <typename BufferEndian>
148template <typename T>
149inline impl::IfBasic<T, void> InputStreamTemplate<BufferEndian>::readBasic(T& val)
150{
151 static_assert(std::is_trivially_copyable_v<T>);
152 constexpr std::size_t size = sizeof(T);
153
154 // copy the bytes into our memory
155 std::memcpy(&val, advance(size), size);
156
157 // swap if needed
158 ::sen::impl::swapBytesIfNeeded(val, BufferEndian {});
159}
160
161} // namespace sen
162
163#endif // SEN_CORE_IO_INPUT_STREAM_H
bool atEnd() const noexcept
Definition input_stream.h:40
void setPosition(std::size_t pos) noexcept
Definition input_stream.h:42
std::pair< const uint8_t *, std::size_t > tryAdvance(std::size_t bytes)
std::size_t getPosition() const noexcept
Definition input_stream.h:41
~InputStreamBase()=default
void reverse(std::size_t bytes) noexcept
Definition input_stream.h:45
const uint8_t * advance(std::size_t bytes)
Skips a number of bytes.
InputStreamBase(Span< const uint8_t > buffer) noexcept
Definition input_stream.h:33
Binary input stream. Deserializes values. In general, I/O operations throw on failure.
Definition input_stream.h:57
void readInt32(int32_t &val)
Definition input_stream.h:70
void readInt8(int8_t &val)
Definition input_stream.h:66
void readInt16(int16_t &val)
Definition input_stream.h:68
void readUInt32(uint32_t &val)
Definition input_stream.h:71
void readUInt64(uint64_t &val)
Definition input_stream.h:73
void readUInt16(uint16_t &val)
Definition input_stream.h:69
void readFloat32(float32_t &val)
Definition input_stream.h:101
~InputStreamTemplate() noexcept=default
void readString(std::string &val)
Definition input_stream.h:127
void readUInt8(uint8_t &val)
Definition input_stream.h:67
void readTimeStamp(TimeStamp &val)
Definition input_stream.h:115
void readInt64(int64_t &val)
Definition input_stream.h:72
void readFloat64(float64_t &val)
Definition input_stream.h:108
InputStreamBase(Span< const uint8_t > buffer) noexcept
Definition input_stream.h:33
Contiguous view of elements of type T. Inspired by http://www.open-std.org/jtc1/sc22/wg21/docs/papers...
Definition span.h:34
A point in time.
Definition timestamp.h:26
InputStreamTemplate< LittleEndian > InputStream
Definition input_stream.h:84
float float32_t
Definition numbers.h:16
double float64_t
Definition numbers.h:17
Definition assert.h:17