Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Serialization Update #358

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Engine/Source/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AddLibrary(
TYPE STATIC
SRC ${SOURCES}
PUBLIC_INC Include
LIB debugbreak cityhash taskflow fmt-lib
LIB debugbreak cityhash taskflow fmt-lib rapidjson
)

file(GLOB TEST_SOURCES Test/*.cpp)
Expand Down
3 changes: 3 additions & 0 deletions Engine/Source/Common/Include/Common/Concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
#include <type_traits>

namespace Common {
template <typename T> concept CppBool = std::is_same_v<T, bool>;
template <typename T> concept CppIntegral = std::is_integral_v<T>;
template <typename T> concept CppIntegralNonBool = CppIntegral<T> && !CppBool<T>;
template <typename T> concept CppFloatingPoint = std::is_floating_point_v<T>;
template <typename T> concept CppSigned = std::is_signed_v<T>;
template <typename T> concept CppUnsigned = std::is_unsigned_v<T>;
template <typename T> concept CppArithmetic = std::is_arithmetic_v<T>;
template <typename T> concept CppArithmeticNonBool = CppArithmetic<T> && !CppBool<T>;
template <typename T> concept CppClass = std::is_class_v<T>;
template <typename T> concept CppVoid = std::is_void_v<T>;
template <typename T> concept CppUnion = std::is_union_v<T>;
Expand Down
17 changes: 15 additions & 2 deletions Engine/Source/Common/Include/Common/Math/Box.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <Common/Math/Vector.h>
#include <Common/Serialization.h>
#include <Common/String.h>

namespace Common {
template <typename T>
Expand Down Expand Up @@ -52,9 +53,8 @@ namespace Common {
}

namespace Common { // NOLINT
template <typename T>
template <Serializable T>
struct Serializer<Box<T>> {
static constexpr bool serializable = true;
static constexpr uint32_t typeId
= HashUtils::StrCrc32("Common::Box")
+ Serializer<T>::typeId;
Expand All @@ -78,6 +78,19 @@ namespace Common { // NOLINT
return true;
}
};

template <StringConvertible T>
struct StringConverter<Box<T>> {
static std::string ToString(const Box<T>& inValue)
{
return fmt::format(
"{min={}, max={}}",
StringConverter<Vec<T, 3>>::ToString(inValue.min),
StringConverter<Vec<T, 3>>::ToString(inValue.max));
}
};

// TODO json converter impl
}

namespace Common {
Expand Down
35 changes: 33 additions & 2 deletions Engine/Source/Common/Include/Common/Math/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdint>

#include <Common/Serialization.h>
#include <Common/String.h>

namespace Common {
struct Color;
Expand Down Expand Up @@ -68,7 +69,6 @@ namespace Common {
namespace Common {
template <>
struct Serializer<Color> {
static constexpr bool serializable = true;
static constexpr uint32_t typeId = HashUtils::StrCrc32("Common::Color");

static void Serialize(SerializeStream& stream, const Color& value)
Expand Down Expand Up @@ -97,7 +97,6 @@ namespace Common {

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

static void Serialize(SerializeStream& stream, const LinearColor& value)
Expand All @@ -123,4 +122,36 @@ namespace Common {
return true;
}
};

template <>
struct StringConverter<Color> {
static std::string ToString(const Color& inValue)
{
return fmt::format(
"{}r={}, g={}, b={}, a={}{}",
"{",
StringConverter<uint8_t>::ToString(inValue.r),
StringConverter<uint8_t>::ToString(inValue.g),
StringConverter<uint8_t>::ToString(inValue.b),
StringConverter<uint8_t>::ToString(inValue.a),
"}");
}
};

template <>
struct StringConverter<LinearColor> {
static std::string ToString(const LinearColor& inValue)
{
return fmt::format(
"{}r={}, g={}, b={}, a={}{}",
"{",
StringConverter<float>::ToString(inValue.r),
StringConverter<float>::ToString(inValue.g),
StringConverter<float>::ToString(inValue.b),
StringConverter<float>::ToString(inValue.a),
"}");
}
};

// TODO json converter impl
}
21 changes: 19 additions & 2 deletions Engine/Source/Common/Include/Common/Math/Half.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <Common/Math/Common.h>
#include <Common/Serialization.h>
#include <Common/String.h>

namespace Common {
template <std::endian E> concept ValidEndian = E == std::endian::little || E == std::endian::big;
Expand Down Expand Up @@ -83,7 +84,6 @@ namespace Common {
namespace Common {
template <std::endian E>
struct Serializer<Internal::FullFloat<E>> {
static constexpr bool serializable = true;
static constexpr uint32_t typeId = HashUtils::StrCrc32("Common::Internal::FullFloat");

static void Serialize(SerializeStream& stream, const Internal::FullFloat<E>& value)
Expand All @@ -106,7 +106,6 @@ namespace Common {

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

static void Serialize(SerializeStream& stream, const HalfFloat<E>& value)
Expand All @@ -126,6 +125,24 @@ namespace Common {
return true;
}
};

template <std::endian E>
struct StringConverter<Internal::FullFloat<E>> {
static std::string ToString(const Internal::FullFloat<E>& inValue)
{
return StringConverter<float>::ToString(inValue.value);
}
};

template <std::endian E>
struct StringConverter<HalfFloat<E>> {
static std::string ToString(const HalfFloat<E>& inValue)
{
return StringConverter<float>::ToString(inValue.AsFloat());
}
};

// TODO json converter impl
}

namespace Common::Internal {
Expand Down
18 changes: 16 additions & 2 deletions Engine/Source/Common/Include/Common/Math/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <Common/Math/Vector.h>
#include <Common/Serialization.h>
#include <Common/String.h>
#include <Common/Debug.h>
#include <Common/Utility.h>

Expand Down Expand Up @@ -301,9 +302,8 @@ namespace Common {
}

namespace Common { // NOLINT
template <typename T, uint8_t R, uint8_t C>
template <Serializable T, uint8_t R, uint8_t C>
struct Serializer<Mat<T, R, C>> {
static constexpr bool serializable = true;
static constexpr uint32_t typeId
= HashUtils::StrCrc32("Common::Matrix")
+ Serializer<T>::typeId + (R << 8) + C;
Expand All @@ -329,6 +329,20 @@ namespace Common { // NOLINT
return true;
}
};

template <StringConvertible T, uint8_t R, uint8_t C>
struct StringConverter<Mat<T, R, C>> {
static std::string ToString(const Mat<T, R, C>& inValue)
{
return fmt::format("{row0={}, row1={}, row2={}, row3={}}",
StringConverter<Vec<T, C>>::ToString(inValue.Row(0)),
StringConverter<Vec<T, C>>::ToString(inValue.Row(1)),
StringConverter<Vec<T, C>>::ToString(inValue.Row(2)),
StringConverter<Vec<T, C>>::ToString(inValue.Row(3)));
}
};

// TODO json converter impl
}

namespace Common::Internal {
Expand Down
36 changes: 32 additions & 4 deletions Engine/Source/Common/Include/Common/Math/Projection.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <Common/Math/Matrix.h>
#include <Common/Math/Quaternion.h>
#include <Common/Serialization.h>
#include <Common/String.h>

namespace Common {
template <FloatingPoint T>
Expand Down Expand Up @@ -52,9 +53,8 @@ namespace Common {
}

namespace Common {
template <typename T>
template <Serializable T>
struct Serializer<ReversedZOrthogonalProjection<T>> {
static constexpr bool serializable = true;
static constexpr uint32_t typeId
= HashUtils::StrCrc32("Common::ReversedZOrthogonalProjection")
+ Serializer<T>::typeId;
Expand Down Expand Up @@ -83,9 +83,8 @@ namespace Common {
}
};

template <typename T>
template <Serializable T>
struct Serializer<ReversedZPerspectiveProjection<T>> {
static constexpr bool serializable = true;
static constexpr uint32_t typeId
= HashUtils::StrCrc32("Common::ReversedZPerspectiveProjection")
+ Serializer<T>::typeId;
Expand Down Expand Up @@ -115,6 +114,35 @@ namespace Common {
return true;
}
};

template <StringConvertible T>
struct StringConverter<ReversedZOrthogonalProjection<T>> {
static std::string ToString(const ReversedZOrthogonalProjection<T>& inValue)
{
return fmt::format(
"{width={}, height={}, near={}, far={}}",
StringConverter<T>::ToString(inValue.width),
StringConverter<T>::ToString(inValue.height),
StringConverter<T>::ToString(inValue.nearPlane),
StringConverter<std::optional<T>>::ToString(inValue.farPlane));
}
};

template <StringConvertible T>
struct StringConverter<ReversedZPerspectiveProjection<T>> {
static std::string ToString(const ReversedZPerspectiveProjection<T>& inValue)
{
return fmt::format(
"{fov={}, width={}, height={}, near={}, far={}}",
StringConverter<T>::ToString(inValue.fov),
StringConverter<T>::ToString(inValue.width),
StringConverter<T>::ToString(inValue.height),
StringConverter<T>::ToString(inValue.nearPlane),
StringConverter<std::optional<T>>::ToString(inValue.farPlane));
}
};

// TODO json converter impl
}

namespace Common {
Expand Down
41 changes: 35 additions & 6 deletions Engine/Source/Common/Include/Common/Math/Quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Common/Math/Half.h>
#include <Common/Math/Matrix.h>
#include <Common/Serialization.h>
#include <Common/String.h>

namespace Common {
template <typename T> struct Angle;
Expand Down Expand Up @@ -120,9 +121,8 @@ namespace Common {
}

namespace Common {
template <typename T>
template <Serializable T>
struct Serializer<Angle<T>> {
static constexpr bool serializable = true;
static constexpr uint32_t typeId
= HashUtils::StrCrc32("Common::Angle")
+ Serializer<T>::typeId;
Expand All @@ -145,9 +145,8 @@ namespace Common {
}
};

template <typename T>
template <Serializable T>
struct Serializer<Radian<T>> {
static constexpr bool serializable = true;
static constexpr uint32_t typeId
= HashUtils::StrCrc32("Common::Radian")
+ Serializer<T>::typeId;
Expand All @@ -170,9 +169,8 @@ namespace Common {
}
};

template <typename T>
template <Serializable T>
struct Serializer<Quaternion<T>> {
static constexpr bool serializable = true;
static constexpr uint32_t typeId
= HashUtils::StrCrc32("Common::Quaternion")
+ Serializer<T>::typeId;
Expand Down Expand Up @@ -200,6 +198,37 @@ namespace Common {
return true;
}
};

template <StringConvertible T>
struct StringConverter<Angle<T>> {
static std::string ToString(const Angle<T>& inValue)
{
return fmt::format("a{}", StringConverter<T>::ToString(inValue.value));
}
};

template <StringConvertible T>
struct StringConverter<Radian<T>> {
static std::string ToString(const Radian<T>& inValue)
{
return StringConverter<T>::ToString(inValue.value);
}
};

template <StringConvertible T>
struct StringConverter<Quaternion<T>> {
static std::string ToString(const Quaternion<T>& inValue)
{
return fmt::format(
"({}, {}, {}, {})",
StringConverter<T>::ToString(inValue.x),
StringConverter<T>::ToString(inValue.y),
StringConverter<T>::ToString(inValue.z),
StringConverter<T>::ToString(inValue.w));
}
};

// TODO json converter impl
}

namespace Common {
Expand Down
17 changes: 15 additions & 2 deletions Engine/Source/Common/Include/Common/Math/Rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <Common/Math/Vector.h>
#include <Common/Serialization.h>
#include <Common/String.h>

namespace Common {
template <typename T>
Expand Down Expand Up @@ -50,9 +51,8 @@ namespace Common {
}

namespace Common {
template <typename T>
template <Serializable T>
struct Serializer<Rect<T>> {
static constexpr bool serializable = true;
static constexpr uint32_t typeId
= HashUtils::StrCrc32("Common::Rect")
+ Serializer<T>::typeId;
Expand All @@ -76,6 +76,19 @@ namespace Common {
return true;
}
};

template <StringConvertible T>
struct StringConverter<Rect<T>> {
static std::string ToString(const Rect<T>& inValue)
{
return fmt::format(
"{min={}, max={}}",
StringConverter<Vec<T, 2>>::ToString(inValue.min),
StringConverter<Vec<T, 2>>::ToString(inValue.max));
}
};

// TODO json converter impl
}

namespace Common {
Expand Down
Loading
Loading