diff options
author | rtk0c <[email protected]> | 2022-06-30 21:38:53 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-06-30 21:38:53 -0700 |
commit | 7fe47a9d5b1727a61dc724523b530762f6d6ba19 (patch) | |
tree | e95be6e66db504ed06d00b72c579565bab873277 /app/source/Cplt/Utils/IO/DataStream.hpp | |
parent | 2cf952088d375ac8b2f45b144462af0953436cff (diff) |
Restructure project
Diffstat (limited to 'app/source/Cplt/Utils/IO/DataStream.hpp')
-rw-r--r-- | app/source/Cplt/Utils/IO/DataStream.hpp | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/app/source/Cplt/Utils/IO/DataStream.hpp b/app/source/Cplt/Utils/IO/DataStream.hpp new file mode 100644 index 0000000..133adc2 --- /dev/null +++ b/app/source/Cplt/Utils/IO/DataStream.hpp @@ -0,0 +1,210 @@ +#pragma once + +#include "FileStream.hpp" +#include <Cplt/fwd.hpp> + +#include <bit> +#include <cstddef> +#include <cstdint> +#include <span> + +class BaseDataStream +{ +private: + std::endian mEndian = std::endian::big; + +public: + std::endian GetEndianness() const; + void SetEndianness(std::endian endianness); +}; + +class InputDataStream : public BaseDataStream +{ +private: + InputFileStream mBackend; + +public: + static constexpr bool IsSerializer() + { + return false; + } + + InputDataStream(InputFileStream stream); + + void ReadBytes(size_t byteCount, std::byte* buffer); + void ReadBytes(size_t byteCount, char* buffer); + void ReadBytes(size_t byteCount, signed char* buffer); + void ReadBytes(size_t byteCount, unsigned char* buffer); + + template <class TInserter> + void ReadBytes(size_t byteCount, TInserter&& inserter) + { + for (size_t i = 0; i < byteCount; ++i) { + uint8_t byte; + Read(byte); + + inserter = byte; + } + } + + void Read(int8_t& n); + void Read(int16_t& n); + void Read(int32_t& n); + void Read(int64_t& n); + + void Read(uint8_t& n); + void Read(uint16_t& n); + void Read(uint32_t& n); + void Read(uint64_t& n); + + void Read(float& n); + void Read(double& n); + + template <class TEnum> + requires std::is_enum_v<TEnum> + void ReadEnum(TEnum& e) + { + std::underlying_type_t<TEnum> n; + Read(n); + e = static_cast<TEnum>(e); + } + + template <class TObject> + void ReadObject(TObject& obj) + { + obj.ReadFromDataStream(*this); + } + + template <class TAdapter, class TObject> + void ReadObjectAdapted(TObject& obj) + { + TAdapter::ReadFromDataStream(*this, obj); + } + +public: + // Proxy functions for writing templated IO functions + + template <class T> + void Bytes(size_t byteCount, T* buffer) + { + ReadBytes(byteCount, buffer); + } + + template <class T> + void Value(T& t) + { + Read(t); + } + + template <class T> + void Enum(T& t) + { + ReadEnum(t); + } + + template <class T> + void Object(T& obj) + { + ReadObject(obj); + } + + template <class TAdapter, class TObject> + void ObjectAdapted(TObject& obj) + { + ReadObjectAdapted<TAdapter>(obj); + } +}; + +class OutputDataStream : public BaseDataStream +{ +private: + OutputFileStream mBackend; + +public: + static constexpr bool IsSerializer() + { + return true; + } + + OutputDataStream(OutputFileStream stream); + + void WriteBytes(size_t byteCount, const std::byte* buffer); + void WriteBytes(size_t byteCount, const char* buffer); + void WriteBytes(size_t byteCount, const signed char* buffer); + void WriteBytes(size_t byteCount, const unsigned char* buffer); + + template <class TIterator> + void WriteBytes(TIterator&& begin, TIterator&& end) + { + for (; begin != end; ++begin) { + uint8_t byte = *begin; + Write(byte); + } + } + + void Write(int8_t n); + void Write(int16_t n); + void Write(int32_t n); + void Write(int64_t n); + + void Write(uint8_t n); + void Write(uint16_t n); + void Write(uint32_t n); + void Write(uint64_t n); + + void Write(float n); + void Write(double n); + + template <class TEnum> + requires std::is_enum_v<TEnum> + void WriteEnum(TEnum e) + { + auto n = static_cast<std::underlying_type_t<TEnum>>(e); + Write(n); + } + + template <class TObject> + void WriteObject(const TObject& obj) + { + obj.WriteToDataStream(*this); + } + + template <class TAdapter, class TObject> + void WriteObjectAdapted(const TObject& obj) + { + TAdapter::WriteToDataStream(*this, obj); + } + +public: + // Proxy functions for writing templated IO functions + + template <class T> + void Bytes(size_t byteCount, T* buffer) + { + WriteBytes(byteCount, buffer); + } + + template <class T> + void Value(T t) + { + Write(t); + } + + template <class T> + void Enum(T t) + { + WriteEnum(t); + } + + template <class T> + void Object(T& obj) + { + WriteObject(obj); + } + + template <class TAdapter, class TObject> + void ObjectAdapted(TObject& obj) + { + WriteObjectAdapted<TAdapter>(obj); + } +}; |