Skip to content

Commit

Permalink
feat: new typed serialization interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyAndNotDown committed Sep 16, 2024
1 parent 3d78ab7 commit e043fe7
Show file tree
Hide file tree
Showing 18 changed files with 421 additions and 480 deletions.
21 changes: 7 additions & 14 deletions Engine/Source/Common/Include/Common/Math/Box.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,20 @@ namespace Common {
namespace Common { // NOLINT
template <Serializable T>
struct Serializer<Box<T>> {
static constexpr uint32_t typeId
static constexpr size_t typeId
= HashUtils::StrCrc32("Common::Box")
+ Serializer<T>::typeId;

static void Serialize(SerializeStream& stream, const Box<T>& value)
static size_t Serialize(SerializeStream& stream, const Box<T>& value)
{
TypeIdSerializer<Box<T>>::Serialize(stream);

Serializer<Vec<T, 3>>::Serialize(stream, value.min);
Serializer<Vec<T, 3>>::Serialize(stream, value.max);
return Serializer<Vec<T, 3>>::Serialize(stream, value.min)
+ Serializer<Vec<T, 3>>::Serialize(stream, value.max);
}

static bool Deserialize(DeserializeStream& stream, Box<T>& value)
static size_t Deserialize(DeserializeStream& stream, Box<T>& value)
{
if (!TypeIdSerializer<Box<T>>::Deserialize(stream)) {
return false;
}

Serializer<Vec<T, 3>>::Deserialize(stream, value.min);
Serializer<Vec<T, 3>>::Deserialize(stream, value.max);
return true;
return Serializer<Vec<T, 3>>::Deserialize(stream, value.min)
+ Serializer<Vec<T, 3>>::Deserialize(stream, value.max);
}
};

Expand Down
58 changes: 22 additions & 36 deletions Engine/Source/Common/Include/Common/Math/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,57 +71,43 @@ namespace Common {
namespace Common {
template <>
struct Serializer<Color> {
static constexpr uint32_t typeId = HashUtils::StrCrc32("Common::Color");
static constexpr size_t typeId = HashUtils::StrCrc32("Common::Color");

static void Serialize(SerializeStream& stream, const Color& value)
static size_t Serialize(SerializeStream& stream, const Color& value)
{
TypeIdSerializer<Color>::Serialize(stream);

Serializer<uint8_t>::Serialize(stream, value.r);
Serializer<uint8_t>::Serialize(stream, value.g);
Serializer<uint8_t>::Serialize(stream, value.b);
Serializer<uint8_t>::Serialize(stream, value.a);
return Serializer<uint8_t>::Serialize(stream, value.r)
+ Serializer<uint8_t>::Serialize(stream, value.g)
+ Serializer<uint8_t>::Serialize(stream, value.b)
+ Serializer<uint8_t>::Serialize(stream, value.a);
}

static bool Deserialize(DeserializeStream& stream, Color& value)
static size_t Deserialize(DeserializeStream& stream, Color& value)
{
if (!TypeIdSerializer<Color>::Deserialize(stream)) {
return false;
}

Serializer<uint8_t>::Deserialize(stream, value.r);
Serializer<uint8_t>::Deserialize(stream, value.g);
Serializer<uint8_t>::Deserialize(stream, value.b);
Serializer<uint8_t>::Deserialize(stream, value.a);
return true;
return Serializer<uint8_t>::Deserialize(stream, value.r)
+ Serializer<uint8_t>::Deserialize(stream, value.g)
+ Serializer<uint8_t>::Deserialize(stream, value.b)
+ Serializer<uint8_t>::Deserialize(stream, value.a);
}
};

template <>
struct Serializer<LinearColor> {
static constexpr uint32_t typeId = HashUtils::StrCrc32("Common::LinearColor");
static constexpr size_t typeId = HashUtils::StrCrc32("Common::LinearColor");

static void Serialize(SerializeStream& stream, const LinearColor& value)
static size_t Serialize(SerializeStream& stream, const LinearColor& value)
{
TypeIdSerializer<Color>::Serialize(stream);

Serializer<float>::Serialize(stream, value.r);
Serializer<float>::Serialize(stream, value.g);
Serializer<float>::Serialize(stream, value.b);
Serializer<float>::Serialize(stream, value.a);
return Serializer<float>::Serialize(stream, value.r)
+ Serializer<float>::Serialize(stream, value.g)
+ Serializer<float>::Serialize(stream, value.b)
+ Serializer<float>::Serialize(stream, value.a);
}

static bool Deserialize(DeserializeStream& stream, LinearColor& value)
static size_t Deserialize(DeserializeStream& stream, LinearColor& value)
{
if (!TypeIdSerializer<Color>::Deserialize(stream)) {
return false;
}

Serializer<float>::Deserialize(stream, value.r);
Serializer<float>::Deserialize(stream, value.g);
Serializer<float>::Deserialize(stream, value.b);
Serializer<float>::Deserialize(stream, value.a);
return true;
return Serializer<float>::Deserialize(stream, value.r)
+ Serializer<float>::Deserialize(stream, value.g)
+ Serializer<float>::Deserialize(stream, value.b)
+ Serializer<float>::Deserialize(stream, value.a);
}
};

Expand Down
34 changes: 10 additions & 24 deletions Engine/Source/Common/Include/Common/Math/Half.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,45 +84,31 @@ namespace Common {
namespace Common {
template <std::endian E>
struct Serializer<Internal::FullFloat<E>> {
static constexpr uint32_t typeId = HashUtils::StrCrc32("Common::Internal::FullFloat");
static constexpr size_t typeId = HashUtils::StrCrc32("Common::Internal::FullFloat");

static void Serialize(SerializeStream& stream, const Internal::FullFloat<E>& value)
static size_t Serialize(SerializeStream& stream, const Internal::FullFloat<E>& value)
{
TypeIdSerializer<Internal::FullFloat<E>>::Serialize(stream);

Serializer<float>::Serialize(stream, value.value);
return Serializer<float>::Serialize(stream, value.value);
}

static bool Deserialize(DeserializeStream& stream, Internal::FullFloat<E>& value)
static size_t Deserialize(DeserializeStream& stream, Internal::FullFloat<E>& value)
{
if (!TypeIdSerializer<Internal::FullFloat<E>>::Deserialize(stream)) {
return false;
}

Serializer<float>::Deserialize(stream, value.value);
return true;
return Serializer<float>::Deserialize(stream, value.value);
}
};

template <std::endian E>
struct Serializer<HalfFloat<E>> {
static constexpr uint32_t typeId = HashUtils::StrCrc32("Common::Internal::HalfFloat");
static constexpr size_t typeId = HashUtils::StrCrc32("Common::Internal::HalfFloat");

static void Serialize(SerializeStream& stream, const HalfFloat<E>& value)
static size_t Serialize(SerializeStream& stream, const HalfFloat<E>& value)
{
TypeIdSerializer<HalfFloat<E>>::Serialize(stream);

Serializer<uint16_t>::Serialize(stream, value.value);
return Serializer<uint16_t>::Serialize(stream, value.value);
}

static bool Deserialize(DeserializeStream& stream, HalfFloat<E>& value)
static size_t Deserialize(DeserializeStream& stream, HalfFloat<E>& value)
{
if (!TypeIdSerializer<HalfFloat<E>>::Deserialize(stream)) {
return false;
}

Serializer<uint16_t>::Deserialize(stream, value.value);
return true;
return Serializer<uint16_t>::Deserialize(stream, value.value);
}
};

Expand Down
21 changes: 9 additions & 12 deletions Engine/Source/Common/Include/Common/Math/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,29 +304,26 @@ namespace Common {
namespace Common { // NOLINT
template <Serializable T, uint8_t R, uint8_t C>
struct Serializer<Mat<T, R, C>> {
static constexpr uint32_t typeId
static constexpr size_t typeId
= HashUtils::StrCrc32("Common::Matrix")
+ Serializer<T>::typeId + (R << 8) + C;

static void Serialize(SerializeStream& stream, const Mat<T, R, C>& value)
static size_t Serialize(SerializeStream& stream, const Mat<T, R, C>& value)
{
TypeIdSerializer<Mat<T, R, C>>::Serialize(stream);

auto serialized = 0;
for (auto i = 0; i < R * C; i++) {
Serializer<T>::Serialize(stream, value.data[i]);
serialized += Serializer<T>::Serialize(stream, value.data[i]);
}
return serialized;
}

static bool Deserialize(DeserializeStream& stream, Mat<T, R, C>& value)
static size_t Deserialize(DeserializeStream& stream, Mat<T, R, C>& value)
{
if (!TypeIdSerializer<Mat<T, R, C>>::Deserialize(stream)) {
return false;
}

auto deserialized = 0;
for (auto i = 0; i < R * C; i++) {
Serializer<T>::Deserialize(stream, value.data[i]);
deserialized += Serializer<T>::Deserialize(stream, value.data[i]);
}
return true;
return deserialized;
}
};

Expand Down
62 changes: 24 additions & 38 deletions Engine/Source/Common/Include/Common/Math/Projection.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,63 +61,49 @@ namespace Common {
namespace Common {
template <Serializable T>
struct Serializer<ReversedZOrthogonalProjection<T>> {
static constexpr uint32_t typeId
static constexpr size_t typeId
= HashUtils::StrCrc32("Common::ReversedZOrthogonalProjection")
+ Serializer<T>::typeId;

static void Serialize(SerializeStream& stream, const ReversedZOrthogonalProjection<T>& value)
static size_t Serialize(SerializeStream& stream, const ReversedZOrthogonalProjection<T>& value)
{
TypeIdSerializer<ReversedZOrthogonalProjection<T>>::Serialize(stream);

Serializer<T>::Serialize(stream, value.width);
Serializer<T>::Serialize(stream, value.height);
Serializer<T>::Serialize(stream, value.nearPlane);
Serializer<std::optional<T>>::Serialize(stream, value.farPlane);
return Serializer<T>::Serialize(stream, value.width)
+ Serializer<T>::Serialize(stream, value.height)
+ Serializer<T>::Serialize(stream, value.nearPlane)
+ Serializer<std::optional<T>>::Serialize(stream, value.farPlane);
}

static bool Deserialize(DeserializeStream& stream, ReversedZOrthogonalProjection<T>& value)
static size_t Deserialize(DeserializeStream& stream, ReversedZOrthogonalProjection<T>& value)
{
if (!TypeIdSerializer<ReversedZOrthogonalProjection<T>>::Deserialize(stream)) {
return false;
}

Serializer<T>::Deserialize(stream, value.width);
Serializer<T>::Deserialize(stream, value.height);
Serializer<T>::Deserialize(stream, value.nearPlane);
Serializer<std::optional<T>>::Deserialize(stream, value.farPlane);
return true;
return Serializer<T>::Deserialize(stream, value.width)
+ Serializer<T>::Deserialize(stream, value.height)
+ Serializer<T>::Deserialize(stream, value.nearPlane)
+ Serializer<std::optional<T>>::Deserialize(stream, value.farPlane);
}
};

template <Serializable T>
struct Serializer<ReversedZPerspectiveProjection<T>> {
static constexpr uint32_t typeId
static constexpr size_t typeId
= HashUtils::StrCrc32("Common::ReversedZPerspectiveProjection")
+ Serializer<T>::typeId;

static void Serialize(SerializeStream& stream, const ReversedZPerspectiveProjection<T>& value)
static size_t Serialize(SerializeStream& stream, const ReversedZPerspectiveProjection<T>& value)
{
TypeIdSerializer<ReversedZPerspectiveProjection<T>>::Serialize(stream);

Serializer<T>::Serialize(stream, value.fov);
Serializer<T>::Serialize(stream, value.width);
Serializer<T>::Serialize(stream, value.height);
Serializer<T>::Serialize(stream, value.nearPlane);
Serializer<std::optional<T>>::Serialize(stream, value.farPlane);
return Serializer<T>::Serialize(stream, value.fov)
+ Serializer<T>::Serialize(stream, value.width)
+ Serializer<T>::Serialize(stream, value.height)
+ Serializer<T>::Serialize(stream, value.nearPlane)
+ Serializer<std::optional<T>>::Serialize(stream, value.farPlane);
}

static bool Deserialize(DeserializeStream& stream, ReversedZPerspectiveProjection<T>& value)
static size_t Deserialize(DeserializeStream& stream, ReversedZPerspectiveProjection<T>& value)
{
if (!TypeIdSerializer<ReversedZPerspectiveProjection<T>>::Deserialize(stream)) {
return false;
}

Serializer<T>::Deserialize(stream, value.fov);
Serializer<T>::Deserialize(stream, value.width);
Serializer<T>::Deserialize(stream, value.height);
Serializer<T>::Deserialize(stream, value.nearPlane);
Serializer<std::optional<T>>::Deserialize(stream, value.farPlane);
return true;
return Serializer<T>::Deserialize(stream, value.fov)
+ Serializer<T>::Deserialize(stream, value.width)
+ Serializer<T>::Deserialize(stream, value.height)
+ Serializer<T>::Deserialize(stream, value.nearPlane)
+ Serializer<std::optional<T>>::Deserialize(stream, value.farPlane);
}
};

Expand Down
63 changes: 21 additions & 42 deletions Engine/Source/Common/Include/Common/Math/Quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,79 +123,58 @@ namespace Common {
namespace Common {
template <Serializable T>
struct Serializer<Angle<T>> {
static constexpr uint32_t typeId
static constexpr size_t typeId
= HashUtils::StrCrc32("Common::Angle")
+ Serializer<T>::typeId;

static void Serialize(SerializeStream& stream, const Angle<T>& value)
static size_t Serialize(SerializeStream& stream, const Angle<T>& value)
{
TypeIdSerializer<Angle<T>>::Serialize(stream);

Serializer<T>::Serialize(stream, value.value);
return Serializer<T>::Serialize(stream, value.value);
}

static bool Deserialize(DeserializeStream& stream, Angle<T>& value)
static size_t Deserialize(DeserializeStream& stream, Angle<T>& value)
{
if (!TypeIdSerializer<Angle<T>>::Deserialize(stream)) {
return false;
}

Serializer<T>::Deserialize(stream, value.value);
return true;
return Serializer<T>::Deserialize(stream, value.value);
}
};

template <Serializable T>
struct Serializer<Radian<T>> {
static constexpr uint32_t typeId
static constexpr size_t typeId
= HashUtils::StrCrc32("Common::Radian")
+ Serializer<T>::typeId;

static void Serialize(SerializeStream& stream, const Radian<T>& value)
static size_t Serialize(SerializeStream& stream, const Radian<T>& value)
{
TypeIdSerializer<Radian<T>>::Serialize(stream);

Serializer<T>::Serialize(stream, value.value);
return Serializer<T>::Serialize(stream, value.value);
}

static bool Deserialize(DeserializeStream& stream, Radian<T>& value)
static size_t Deserialize(DeserializeStream& stream, Radian<T>& value)
{
if (!TypeIdSerializer<Radian<T>>::Deserialize(stream)) {
return false;
}

Serializer<T>::Deserialize(stream, value.value);
return true;
return Serializer<T>::Deserialize(stream, value.value);
}
};

template <Serializable T>
struct Serializer<Quaternion<T>> {
static constexpr uint32_t typeId
static constexpr size_t typeId
= HashUtils::StrCrc32("Common::Quaternion")
+ Serializer<T>::typeId;

static void Serialize(SerializeStream& stream, const Quaternion<T>& value)
static size_t Serialize(SerializeStream& stream, const Quaternion<T>& value)
{
TypeIdSerializer<Quaternion<T>>::Serialize(stream);

Serializer<T>::Serialize(stream, value.x);
Serializer<T>::Serialize(stream, value.y);
Serializer<T>::Serialize(stream, value.z);
Serializer<T>::Serialize(stream, value.w);
return Serializer<T>::Serialize(stream, value.x)
+ Serializer<T>::Serialize(stream, value.y)
+ Serializer<T>::Serialize(stream, value.z)
+ Serializer<T>::Serialize(stream, value.w);
}

static bool Deserialize(DeserializeStream& stream, Quaternion<T>& value)
static size_t Deserialize(DeserializeStream& stream, Quaternion<T>& value)
{
if (!TypeIdSerializer<Quaternion<T>>::Deserialize(stream)) {
return false;
}

Serializer<T>::Deserialize(stream, value.x);
Serializer<T>::Deserialize(stream, value.y);
Serializer<T>::Deserialize(stream, value.z);
Serializer<T>::Deserialize(stream, value.w);
return true;
return Serializer<T>::Deserialize(stream, value.x)
+ Serializer<T>::Deserialize(stream, value.y)
+ Serializer<T>::Deserialize(stream, value.z)
+ Serializer<T>::Deserialize(stream, value.w);
}
};

Expand Down
Loading

0 comments on commit e043fe7

Please sign in to comment.