|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/// Small shim implementing the needed Boost serialization classes for liblsl |
| 4 | + |
| 5 | +#include <cstdint> |
| 6 | +#include <stdexcept> |
| 7 | +#include <streambuf> |
| 8 | +#include <type_traits> |
| 9 | + |
| 10 | +/// dummy #defines to prevent portable archive from loading additional boost archive parts |
| 11 | +#define NO_EXPLICIT_TEMPLATE_INSTANTIATION |
| 12 | +#define BOOST_ARCHIVE_SERIALIZER_INCLUDED |
| 13 | +#define BOOST_SERIALIZATION_REGISTER_ARCHIVE(typename) |
| 14 | + |
| 15 | +// forward declaration |
| 16 | +namespace lsl { |
| 17 | +class sample; |
| 18 | +} |
| 19 | + |
| 20 | +/// Maps LSL types to an index without needing full RTTI |
| 21 | +template <typename T> struct lsl_type_index {}; |
| 22 | +template <> struct lsl_type_index<lsl::sample> { static const int idx = 0; }; |
| 23 | +/// two classes used in unit tests |
| 24 | +template <> struct lsl_type_index<struct Testclass> { static const int idx = 1; }; |
| 25 | +template <> struct lsl_type_index<struct Testclass2> { static const int idx = 2; }; |
| 26 | + |
| 27 | +/// keep track of classes already seen once, needed for a field in Boost.Archive |
| 28 | +class serialized_class_tracker { |
| 29 | + bool seen_[3]{false}; |
| 30 | + |
| 31 | +public: |
| 32 | + /// Return true /if an instance of this class has already been (de)serialized before |
| 33 | + template <typename T> inline bool already_seen(const T &) { |
| 34 | + if (seen_[lsl_type_index<T>::idx]) return true; |
| 35 | + seen_[lsl_type_index<T>::idx] = true; |
| 36 | + return false; |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +namespace lslboost { |
| 41 | +#ifndef BOOST_INTEGER_HPP |
| 42 | +/// Maps a type size in bits to the corresponding uintXY_t type |
| 43 | +template <int Size> struct uint_t {}; |
| 44 | +template <> struct uint_t<8> { using least = uint8_t; }; |
| 45 | +template <> struct uint_t<16> { using least = uint16_t; }; |
| 46 | +template <> struct uint_t<32> { using least = uint32_t; }; |
| 47 | +template <> struct uint_t<64> { using least = uint64_t; }; |
| 48 | +#endif |
| 49 | + |
| 50 | +namespace archive { |
| 51 | +using library_version_type = uint8_t; |
| 52 | +inline library_version_type BOOST_ARCHIVE_VERSION() { return 17; } |
| 53 | +enum flags { no_header = 1, no_codecvt = 2 }; |
| 54 | + |
| 55 | +struct archive_exception : std::exception { |
| 56 | + archive_exception(int) {} |
| 57 | + enum errors { invalid_signature, unsupported_version, other_exception }; |
| 58 | +}; |
| 59 | + |
| 60 | +/// Common class to store flags |
| 61 | +class flagstore { |
| 62 | + int flags_; |
| 63 | + |
| 64 | +public: |
| 65 | + flagstore(int flags) : flags_(flags) {} |
| 66 | + int get_flags() const { return flags_; } |
| 67 | +}; |
| 68 | + |
| 69 | +template <typename Archive> struct basic_binary_oarchive : public flagstore { |
| 70 | + basic_binary_oarchive(int flags_) : flagstore(flags_) {} |
| 71 | +}; |
| 72 | + |
| 73 | +template <typename Archive> struct basic_binary_iarchive : public flagstore { |
| 74 | + basic_binary_iarchive(int flags_) : flagstore(flags_) {} |
| 75 | +}; |
| 76 | + |
| 77 | +/// Helper base class for calling methods of an descendant class, i.e. Archive::fn, not this->fn |
| 78 | +template <typename Archive> class archive_upcaster { |
| 79 | +public: |
| 80 | + archive_upcaster() { |
| 81 | + static_assert( |
| 82 | + std::is_base_of<typename std::remove_reference<decltype(*this)>::type, Archive>::value, |
| 83 | + "Archive must inherit from basic_binary_i/oprimitive"); |
| 84 | + } |
| 85 | + |
| 86 | + /** Return a reference to the actual archive implementation instead of the base class |
| 87 | + * |
| 88 | + * This is needed for most operations to call Archive::fn, not basic_binary_primitive::fn */ |
| 89 | + inline Archive &archive_impl() { return *((Archive *)this); } |
| 90 | +}; |
| 91 | + |
| 92 | +/* |
| 93 | +template<typename Archive> class basic_binary_primitive { |
| 94 | + std::streambuf &sb; |
| 95 | +}*/ |
| 96 | + |
| 97 | +template <typename Archive, typename CharT, typename OSTraits> |
| 98 | +class basic_binary_oprimitive : private serialized_class_tracker, |
| 99 | + private archive_upcaster<Archive> { |
| 100 | + std::streambuf &sb; |
| 101 | + |
| 102 | + /// calls Archive::serialize instead of this->serialize() |
| 103 | + template <typename T> Archive &delegate_save(const T &t) { |
| 104 | + this->archive_impl().save(t); |
| 105 | + return this->archive_impl(); |
| 106 | + } |
| 107 | + |
| 108 | +public: |
| 109 | + basic_binary_oprimitive(std::streambuf &sbuf, int) : archive_upcaster<Archive>(), sb(sbuf) {} |
| 110 | + |
| 111 | + template <typename T> void save_binary(const T *data, std::size_t bytes) { |
| 112 | + sb.sputn(reinterpret_cast<const char *>(data), bytes); |
| 113 | + } |
| 114 | + |
| 115 | + template <typename T> void save(const T &t) { save_binary<T>(&t, sizeof(T)); } |
| 116 | + |
| 117 | + void save(const std::string &s) { |
| 118 | + delegate_save(s.size()); |
| 119 | + save_binary(s.data(), s.size()); |
| 120 | + } |
| 121 | + |
| 122 | + template <typename T> |
| 123 | + std::enable_if_t<std::is_arithmetic<T>::value, Archive &> operator<<(const T &t) { |
| 124 | + return delegate_save(t); |
| 125 | + } |
| 126 | + |
| 127 | + Archive &operator<<(const std::string &t) { return delegate_save(t); } |
| 128 | + |
| 129 | + /// calls the `serialize()` member function if it exists |
| 130 | + template <typename T, void (T::*fn)(Archive &, uint32_t) const = &T::serialize> |
| 131 | + Archive &operator<<(const T &t) { |
| 132 | + if (!this->already_seen(t)) save<uint16_t>(0); |
| 133 | + t.serialize(this->archive_impl(), 0); |
| 134 | + return this->archive_impl(); |
| 135 | + } |
| 136 | + |
| 137 | + template <typename T> Archive &operator&(const T &t) { return this->archive_impl() << t; } |
| 138 | +}; |
| 139 | + |
| 140 | + |
| 141 | +template <class Archive, typename CharT, typename OSTraits> |
| 142 | +class basic_binary_iprimitive : private serialized_class_tracker, |
| 143 | + private archive_upcaster<Archive> { |
| 144 | + std::streambuf &sb; |
| 145 | + |
| 146 | + template <typename T> Archive &delegate_load(T &t) { |
| 147 | + this->archive_impl().load(t); |
| 148 | + return this->archive_impl(); |
| 149 | + } |
| 150 | + |
| 151 | +public: |
| 152 | + basic_binary_iprimitive(std::streambuf &sbuf, int) : archive_upcaster<Archive>(), sb(sbuf) {} |
| 153 | + |
| 154 | + /// Load raw binary data from the stream |
| 155 | + template <typename T> void load_binary(T *data, std::size_t bytes) { |
| 156 | + sb.sgetn(reinterpret_cast<char *>(data), bytes); |
| 157 | + } |
| 158 | + |
| 159 | + template <typename T> void load(T &t) { load_binary<T>(&t, sizeof(T)); } |
| 160 | + |
| 161 | + void load(std::string &s) { |
| 162 | + std::size_t size; |
| 163 | + delegate_load(size); |
| 164 | + char *data = new char[size]; |
| 165 | + load_binary(data, size); |
| 166 | + s.assign(data, size); |
| 167 | + delete[] data; |
| 168 | + } |
| 169 | + |
| 170 | + Archive &operator>>(std::string &t) { return delegate_load(t); } |
| 171 | + |
| 172 | + template <typename T> |
| 173 | + std::enable_if_t<std::is_arithmetic<T>::value, Archive &> operator>>(T &t) { |
| 174 | + return delegate_load(t); |
| 175 | + } |
| 176 | + |
| 177 | + /// calls the `serialize()` member function if it exists |
| 178 | + template <typename T, void (T::*fn)(Archive &, uint32_t) = &T::serialize> |
| 179 | + Archive &operator>>(T &t) { |
| 180 | + if (!this->already_seen(t)) { |
| 181 | + uint16_t dummy; |
| 182 | + load(dummy); |
| 183 | + } |
| 184 | + t.serialize(this->archive_impl(), 0); |
| 185 | + return this->archive_impl(); |
| 186 | + } |
| 187 | + |
| 188 | + template <typename T> Archive &operator&(T &t) { return this->archive_impl() >> t; } |
| 189 | + |
| 190 | + void set_library_version(int) {} |
| 191 | +}; |
| 192 | +} // namespace archive |
| 193 | +} // namespace lslboost |
0 commit comments