-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3124b3
commit 19ce5de
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
modules/core/inc/tactile/core/io/save/vector_serialization.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
modules/core/src/tactile/core/io/save/vector_serialization.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |