Skip to content

Commit

Permalink
Rework serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Oct 14, 2024
1 parent 30fab56 commit ed72048
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 71 deletions.
70 changes: 53 additions & 17 deletions include/NZSL/Serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef NZSL_SERIALIZER_HPP
#define NZSL_SERIALIZER_HPP

#include <NazaraUtils/FunctionRef.hpp>
#include <NZSL/Config.hpp>
#include <string>
#include <vector>
Expand All @@ -18,18 +19,34 @@ namespace nzsl
public:
virtual ~AbstractSerializer();

virtual void Serialize(bool value);
virtual void Serialize(double value);
virtual void Serialize(float value);
virtual void Serialize(std::int8_t value);
virtual void Serialize(std::int16_t value);
virtual void Serialize(std::int32_t value);
virtual void Serialize(std::int64_t value);
virtual void Serialize(std::uint8_t value) = 0;
virtual void Serialize(std::uint16_t value) = 0;
virtual void Serialize(std::uint32_t value) = 0;
virtual void Serialize(std::uint64_t value) = 0;
virtual void Serialize(const std::string& value);
virtual void Serialize(std::size_t offset, bool value);
virtual void Serialize(std::size_t offset, double value);
virtual void Serialize(std::size_t offset, float value);
virtual void Serialize(std::size_t offset, std::int8_t value);
virtual void Serialize(std::size_t offset, std::int16_t value);
virtual void Serialize(std::size_t offset, std::int32_t value);
virtual void Serialize(std::size_t offset, std::int64_t value);
virtual void Serialize(std::size_t offset, std::uint8_t value) = 0;
virtual void Serialize(std::size_t offset, std::uint16_t value) = 0;
virtual void Serialize(std::size_t offset, std::uint32_t value) = 0;
virtual void Serialize(std::size_t offset, std::uint64_t value) = 0;
virtual void Serialize(std::size_t offset, const std::string& value);
virtual void Serialize(std::size_t offset, const void* data, std::size_t size) = 0;

virtual std::size_t Serialize(bool value);
virtual std::size_t Serialize(double value);
virtual std::size_t Serialize(float value);
virtual std::size_t Serialize(std::int8_t value);
virtual std::size_t Serialize(std::int16_t value);
virtual std::size_t Serialize(std::int32_t value);
virtual std::size_t Serialize(std::int64_t value);
virtual std::size_t Serialize(std::uint8_t value) = 0;
virtual std::size_t Serialize(std::uint16_t value) = 0;
virtual std::size_t Serialize(std::uint32_t value) = 0;
virtual std::size_t Serialize(std::uint64_t value) = 0;
virtual std::size_t Serialize(const std::string& value);
virtual std::size_t Serialize(const void* data, std::size_t size);
virtual std::size_t Serialize(std::size_t size, const Nz::FunctionRef<std::size_t(void* data)>& callback) = 0;
};

class NZSL_API AbstractDeserializer
Expand All @@ -49,6 +66,10 @@ namespace nzsl
virtual void Deserialize(std::uint32_t& value) = 0;
virtual void Deserialize(std::uint64_t& value) = 0;
virtual void Deserialize(std::string& value);
virtual void Deserialize(void* data, std::size_t size) = 0;
virtual void Deserialize(std::size_t size, const Nz::FunctionRef<std::size_t(const void* data)>& callback) = 0;

virtual void SeekTo(std::size_t offset) = 0;
};

class NZSL_API Serializer : public AbstractSerializer
Expand All @@ -59,13 +80,23 @@ namespace nzsl
Serializer(Serializer&&) noexcept = default;
~Serializer() = default;

inline const std::vector<std::uint8_t>& GetData() const;
inline const std::vector<std::uint8_t>& GetData() const&;
inline std::vector<std::uint8_t> GetData() &&;

using AbstractSerializer::Serialize;
void Serialize(std::uint8_t value) override;
void Serialize(std::uint16_t value) override;
void Serialize(std::uint32_t value) override;
void Serialize(std::uint64_t value) override;

void Serialize(std::size_t offset, std::uint8_t value) override;
void Serialize(std::size_t offset, std::uint16_t value) override;
void Serialize(std::size_t offset, std::uint32_t value) override;
void Serialize(std::size_t offset, std::uint64_t value) override;
void Serialize(std::size_t offset, const void* data, std::size_t size) override;

std::size_t Serialize(std::uint8_t value) override;
std::size_t Serialize(std::uint16_t value) override;
std::size_t Serialize(std::uint32_t value) override;
std::size_t Serialize(std::uint64_t value) override;
std::size_t Serialize(const void* data, std::size_t size) override;
std::size_t Serialize(std::size_t size, const Nz::FunctionRef<std::size_t(void* data)>& callback) override;

Serializer& operator=(const Serializer&) = default;
Serializer& operator=(Serializer&&) noexcept = default;
Expand All @@ -87,12 +118,17 @@ namespace nzsl
void Deserialize(std::uint16_t& value) override;
void Deserialize(std::uint32_t& value) override;
void Deserialize(std::uint64_t& value) override;
void Deserialize(void* data, std::size_t size) override;
void Deserialize(std::size_t size, const Nz::FunctionRef<std::size_t(const void* data)>& callback) override;

void SeekTo(std::size_t offset) override;

Deserializer& operator=(const Deserializer&) = default;
Deserializer& operator=(Deserializer&&) noexcept = default;

private:
const std::uint8_t* m_ptr;
const std::uint8_t* m_ptrBegin;
const std::uint8_t* m_ptrEnd;
};
}
Expand Down
8 changes: 7 additions & 1 deletion include/NZSL/Serializer.inl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@

namespace nzsl
{
inline const std::vector<std::uint8_t>& Serializer::GetData() const
inline const std::vector<std::uint8_t>& Serializer::GetData() const&
{
return m_data;
}

inline std::vector<std::uint8_t> Serializer::GetData() &&
{
return std::move(m_data);
}

inline Deserializer::Deserializer(const void* data, std::size_t dataSize)
{
m_ptr = static_cast<const std::uint8_t*>(data);
m_ptrBegin = m_ptr;
m_ptrEnd = m_ptr + dataSize;
}
}
Loading

0 comments on commit ed72048

Please sign in to comment.