Expression Templates Library (ETL)
serializer.hpp
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
8 #pragma once
9 
10 namespace etl {
11 
15 template <typename Stream>
16 struct serializer {
17  using stream_t = Stream;
18  using char_t = typename stream_t::char_type;
19 
21 
27  template <typename... Args>
28  explicit serializer(Args&&... args) : stream(std::forward<Args>(args)...) {}
29 
35  template <typename T>
36  serializer& operator<<(const T& value) {
37  if constexpr (std::is_arithmetic_v<T>) {
38  stream.write(reinterpret_cast<const char_t*>(&value), sizeof(T));
39  } else {
40  serialize(*this, value);
41  }
42 
43  return *this;
44  }
45 };
46 
47 } //end of namespace etl
Stream stream_t
The type of stream to use.
Definition: serializer.hpp:17
void serialize(serializer< Stream > &os, const dyn_matrix_impl< T, SO, D > &matrix)
Serialize the given matrix using the given serializer.
Definition: dyn.hpp:688
stream_t stream
The stream.
Definition: serializer.hpp:20
A serializer for ETL expressions.
Definition: serializer.hpp:16
Root namespace for the ETL library.
Definition: adapter.hpp:15
typename stream_t::char_type char_t
The char type of the stream.
Definition: serializer.hpp:18
serializer(Args &&... args)
Construct the serializer by forwarding the arguments to the stream.
Definition: serializer.hpp:28
serializer & operator<<(const T &value)
Outputs the given value to the stream.
Definition: serializer.hpp:36