diff options
Diffstat (limited to 'lib/rawDataReader.h')
-rw-r--r-- | lib/rawDataReader.h | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/rawDataReader.h b/lib/rawDataReader.h new file mode 100644 index 0000000..0ed2847 --- /dev/null +++ b/lib/rawDataReader.h @@ -0,0 +1,76 @@ +#ifndef MYGRATE_RAW_DATA_READER_H +#define MYGRATE_RAW_DATA_READER_H + +#include "helpers.h" +#include <array> +#include <cstddef> +#include <cstdint> +#include <cstring> +#include <ctime> +#include <mysql.h> // IWYU pragma: keep +#include <span> +#include <stdexcept> +#include <string_view> + +#include <mariadb_rpl.h> + +namespace MyGrate { + struct PackedInteger { + }; + template<typename T> struct type_map { + using target = T; + }; + template<> struct type_map<PackedInteger> { + using target = uint64_t; + }; + + class RawDataReader { + public: + explicit RawDataReader(const void * const d, std::size_t len); + explicit RawDataReader(const MARIADB_STRING & str); + + template<typename T, size_t L = sizeof(T)> + typename type_map<T>::target + readValue() + { + static_assert(L > 0 && L <= sizeof(T), "Read exceeds target size"); + return readValue<T>(L); + } + + template<typename T> + typename type_map<T>::target + readValue(size_t L) + { + verify<std::logic_error>(L > 0 && L <= sizeof(T), "Read exceeds target size"); + offsetSizeCheck(L); + T v {}; + memcpy(&v, rawdata + offset, L); + offset += L; + return v; + } + + template<typename T> + T + viewValue(size_t L) + { + static_assert(sizeof(typename T::value_type) == sizeof(std::byte)); + offsetSizeCheck(L); + T v {reinterpret_cast<const typename T::value_type *>(rawdata + offset), L}; + offset += L; + return v; + } + + void discard(size_t); + + private: + void offsetSizeCheck(size_t) const; + + const std::byte * const rawdata; + const std::size_t len; + std::size_t offset {0}; + }; + + template<> uint64_t RawDataReader::readValue<PackedInteger>(); +} + +#endif |