Skip to content

Commit

Permalink
Add vector serialization utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Oct 14, 2023
1 parent f3124b3 commit 19ce5de
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
46 changes: 46 additions & 0 deletions modules/core/inc/tactile/core/io/save/vector_serialization.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2023 Albin Johansson (GNU General Public License v3.0)

#pragma once

#include "tactile/core/api.hpp"
#include "tactile/core/container/string.hpp"
#include "tactile/core/math/vector.hpp"
#include "tactile/core/prelude.hpp"

namespace tactile {

/**
* \brief Serializes a mathematical vector for use in save files.
*
* \details The returned string is created by placing each component in a string, where
* each value is separated with a single `;` character. For example, the vector
* `{1, 2, 3, 4}` becomes `"1;2;3;4"`.
*
* \param vec the vector to serialize.
*
* \return a string that encodes the vector components.
*/
[[nodiscard]]
TACTILE_CORE_API auto serialize(const Int2& vec) -> String;

/// \copydoc serialize(const Int2&)
[[nodiscard]]
TACTILE_CORE_API auto serialize(const Int3& vec) -> String;

/// \copydoc serialize(const Int2&)
[[nodiscard]]
TACTILE_CORE_API auto serialize(const Int4& vec) -> String;

/// \copydoc serialize(const Int2&)
[[nodiscard]]
TACTILE_CORE_API auto serialize(const Float2& vec) -> String;

/// \copydoc serialize(const Int2&)
[[nodiscard]]
TACTILE_CORE_API auto serialize(const Float3& vec) -> String;

/// \copydoc serialize(const Int2&)
[[nodiscard]]
TACTILE_CORE_API auto serialize(const Float4& vec) -> String;

} // namespace tactile
39 changes: 39 additions & 0 deletions modules/core/src/tactile/core/io/save/vector_serialization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2023 Albin Johansson (GNU General Public License v3.0)

#include "tactile/core/io/save/vector_serialization.hpp"

#include <fmt/format.h>

namespace tactile {

auto serialize(const Int2& vec) -> String
{
return fmt::format("{};{}", vec.x, vec.y);
}

auto serialize(const Int3& vec) -> String
{
return fmt::format("{};{};{}", vec.x, vec.y, vec.z);
}

auto serialize(const Int4& vec) -> String
{
return fmt::format("{};{};{};{}", vec.x, vec.y, vec.z, vec.w);
}

auto serialize(const Float2& vec) -> String
{
return fmt::format("{};{}", vec.x, vec.y);
}

auto serialize(const Float3& vec) -> String
{
return fmt::format("{};{};{}", vec.x, vec.y, vec.z);
}

auto serialize(const Float4& vec) -> String
{
return fmt::format("{};{};{};{}", vec.x, vec.y, vec.z, vec.w);
}

} // namespace tactile
37 changes: 37 additions & 0 deletions modules/core/test/io/save/vector_serialization_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2023 Albin Johansson (GNU General Public License v3.0)

#include "tactile/core/io/save/vector_serialization.hpp"

#include <gtest/gtest.h>

using namespace tactile;

TEST(VectorSerialization, SerializeInt2)
{
EXPECT_EQ(serialize(Int2 {-42, 942}), "-42;942");
}

TEST(VectorSerialization, SerializeInt3)
{
EXPECT_EQ(serialize(Int3 {539, 84'239, -12}), "539;84239;-12");
}

TEST(VectorSerialization, SerializeInt4)
{
EXPECT_EQ(serialize(Int4 {512, 994, 5, 75}), "512;994;5;75");
}

TEST(VectorSerialization, SerializeFloat2)
{
EXPECT_EQ(serialize(Float2 {1.5f, -83.8f}), "1.5;-83.8");
}

TEST(VectorSerialization, SerializeFloat3)
{
EXPECT_EQ(serialize(Float3 {94.4f, 100.0f, -5.0f}), "94.4;100;-5");
}

TEST(VectorSerialization, SerializeFloat4)
{
EXPECT_EQ(serialize(Float4 {1.0f, 2.0f, 3.0f, 4.0f}), "1;2;3;4");
}

0 comments on commit 19ce5de

Please sign in to comment.