pupene  0.2.0
binary-puppers.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include "pupper.h"
5 #include "pup-core.h"
6 
7 
8 /** \file
9  * Puppers for binary serialization.
10  */
11 namespace pupene {
13  template <typename T>
14  static void pup(std::ostream& stream, T& value) {
15  stream.write(reinterpret_cast<char*>(&value), sizeof(T)); //NOLINT
16  }
17  };
18 
20  template <typename T>
21  static void pup(std::istream& stream, T& value) {
22  stream.read(reinterpret_cast<char*>(&value), sizeof(T)); // NOLINT
23  }
24  };
25 
26  /**
27  * @tparam Stream e.g. `std::istream`, `std::ostream`
28  * @tparam PupStream must provide a static function `pup(Stream&, T&)`
29  *
30  * @see BinaryWriter
31  * @see BinaryReader
32  */
33  template <typename Stream, typename PupStream>
34  class BinaryPupper : public Pupper<BinaryPupper<Stream, PupStream>> {
35  public:
36  explicit BinaryPupper(Stream& stream)
37  : stream(stream) {}
38  ~BinaryPupper() override = default;
39 
40  template <typename T>
41  PupPolicy begin(T& /*value*/, const Meta& /*meta*/) {
42  return PupPolicy::pup_object;
43  }
44 
45  template <typename T>
46  void pup(T& value, const Meta& /*meta*/) {
47  if constexpr (std::is_same<std::string, T>{}) {
48  pupene::fns::pup_iterable(*this, value, {});
49  } else {
50  PupStream::pup(stream, value);
51  }
52  }
53 
54  void end(const Meta& /*meta*/) {}
55 
56  private:
57  Stream& stream;
58  };
59 
60  using BinaryWriter = BinaryPupper<std::ostream, binary_writer_pup>;
61  using BinaryReader = BinaryPupper<std::istream, binary_reader_pup>;
62 }
void end(const Meta &)
pup object using its component pup functions
Definition: debug.cpp:4
void pup(T &value, const Meta &)
static void pup(std::istream &stream, T &value)
Holds name and type of objects.
Definition: traits.h:7
~BinaryPupper() override=default
PupPolicy begin(T &, const Meta &)
BinaryPupper(Stream &stream)
PupPolicy
Controls an object&#39;s pup() behavior.
Definition: pupper.h:17
static void pup(std::ostream &stream, T &value)
Base class for working with puppable types.
Definition: pupper.h:83