summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-06 17:30:31 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-06 17:30:31 +0200
commit437dd5940bad6133561cb896cd558881fa5c8b44 (patch)
treeb8f8103801d0f232ae05e728e83774673ae8413f /serialize
parentded69f52906990cf975a62c0efbaca4b6cfa5e88 (diff)
a
Diffstat (limited to 'serialize')
-rw-r--r--serialize/anim.cpp8
-rw-r--r--serialize/anim.hpp4
-rw-r--r--serialize/json-helper.hpp59
-rw-r--r--serialize/magnum-vector.hpp20
4 files changed, 45 insertions, 46 deletions
diff --git a/serialize/anim.cpp b/serialize/anim.cpp
index b7d3ce95..dffe2449 100644
--- a/serialize/anim.cpp
+++ b/serialize/anim.cpp
@@ -23,14 +23,14 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim, name, nframes, actionframe, fps, groups
# pragma clang diagnostic pop
#endif
-std::tuple<anim, bool> anim::from_json(const std::filesystem::path& pathname) noexcept
+std::tuple<anim, bool> anim::from_json(const std::filesystem::path& pathname)
{
- return json_helper<anim>::from_json(pathname);
+ return json_helper::from_json<anim>(pathname);
}
-bool anim::to_json(const std::filesystem::path& pathname) const noexcept
+bool anim::to_json(const std::filesystem::path& pathname) const
{
- return json_helper<anim>::to_json(*this, pathname);
+ return json_helper::to_json(*this, pathname);
}
} // namespace Magnum::Examples::Serialize
diff --git a/serialize/anim.hpp b/serialize/anim.hpp
index f03e3c8c..726efa44 100644
--- a/serialize/anim.hpp
+++ b/serialize/anim.hpp
@@ -32,8 +32,8 @@ struct anim_group final
struct anim final
{
- static std::tuple<anim, bool> from_json(const std::filesystem::path& pathname) noexcept;
- [[nodiscard]] bool to_json(const std::filesystem::path& pathname) const noexcept;
+ static std::tuple<anim, bool> from_json(const std::filesystem::path& pathname);
+ [[nodiscard]] bool to_json(const std::filesystem::path& pathname) const;
static constexpr int default_fps = 24;
std::string name;
diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp
index af6fd211..4a9ee0ac 100644
--- a/serialize/json-helper.hpp
+++ b/serialize/json-helper.hpp
@@ -6,57 +6,46 @@
#include <nlohmann/json.hpp>
#include <Corrade/Utility/DebugStl.h>
-template<typename t>
struct json_helper final {
- [[nodiscard]] static std::tuple<t, bool> from_json(const std::filesystem::path& pathname) noexcept;
- [[nodiscard]] static bool to_json(const t& self, const std::filesystem::path& pathname) noexcept;
+ template<typename t>
+ [[nodiscard]]
+ static std::tuple<t, bool> from_json(const std::filesystem::path& pathname);
+
+ template<typename t>
+ [[nodiscard]]
+ static bool to_json(const t& self, const std::filesystem::path& pathname);
};
template<typename t>
-std::tuple<t, bool> json_helper<t>::from_json(const std::filesystem::path& pathname) noexcept {
+std::tuple<t, bool> json_helper::from_json(const std::filesystem::path& pathname) {
using namespace nlohmann;
using Corrade::Utility::Error;
std::ifstream s;
s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit);
- try {
- s.open(pathname, std::ios_base::in);
- } catch (const std::ios::failure& e) {
- Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "': " << e.what();
- return { {}, false };
- }
+ s.open(pathname, std::ios_base::in);
t ret;
- try {
- json j;
- s >> j;
- using nlohmann::from_json;
- from_json(j, ret);
- } catch (const std::exception& e) {
- Error{Error::Flag::NoSpace} << "failed to parse '" << pathname << "': " << e.what();
- return { {}, false };
- }
+ json j;
+ s >> j;
+ using nlohmann::from_json;
+ from_json(j, ret);
return { std::move(ret), true };
}
template<typename t>
-bool json_helper<t>::to_json(const t& self, const std::filesystem::path& pathname) noexcept {
+bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) {
using Corrade::Utility::Error;
- try {
- nlohmann::json j(self);
+ nlohmann::json j = self;
- std::ofstream s;
- s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit);
- try {
- s.open(pathname, std::ios_base::out | std::ios_base::trunc);
- } catch (const std::ios::failure& e) {
- Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what();
- return false;
- }
- s << j.dump(4);
- s.flush();
- } catch (const std::exception& e) {
- Error{Error::Flag::NoSpace} << "failed writing to '" << pathname << "': " << e.what();
+ std::ofstream s;
+ s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit);
+ try {
+ s.open(pathname, std::ios_base::out | std::ios_base::trunc);
+ } catch (const std::ios::failure& e) {
+ Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what();
return false;
}
-
+ s << j.dump(4);
+ s.flush();
return true;
}
+
diff --git a/serialize/magnum-vector.hpp b/serialize/magnum-vector.hpp
index 3e95c8eb..eed37708 100644
--- a/serialize/magnum-vector.hpp
+++ b/serialize/magnum-vector.hpp
@@ -1,25 +1,35 @@
#pragma once
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector.h>
+#include <Magnum/Math/Vector2.h>
+#include <Magnum/Math/Vector3.h>
+#include <Magnum/Math/Vector4.h>
+#include <Magnum/Math/Color.h>
#include <nlohmann/json.hpp>
namespace nlohmann {
template<std::size_t N, typename T>
-struct adl_serializer<Magnum::Math::Vector<N, T>> final {
- static void to_json(json& j, const Magnum::Math::Vector2<T>& val)
+struct adl_serializer<Magnum::Math::Vector<N, T>> {
+ static void to_json(json& j, const Magnum::Math::Vector<N, T>& val)
{
std::array<T, N> array{};
- for (std::size_t i; i < std::size(val); i++)
+ for (std::size_t i = 0; i < N; i++)
array[i] = val[i];
j = array;
}
- static void from_json(const json& j, Magnum::Math::Vector2<T>& val)
+ static void from_json(const json& j, Magnum::Math::Vector<N, T>& val)
{
std::array<T, N> array = j;
- for (std::size_t i; i < std::size(val); i++)
+ for (std::size_t i = 0; i < N; i++)
val[i] = array[i];
}
};
+template<typename T> struct adl_serializer<Magnum::Math::Vector2<T>> : adl_serializer<Magnum::Math::Vector<2, T>> {};
+template<typename T> struct adl_serializer<Magnum::Math::Vector3<T>> : adl_serializer<Magnum::Math::Vector<3, T>> {};
+template<typename T> struct adl_serializer<Magnum::Math::Vector4<T>> : adl_serializer<Magnum::Math::Vector<4, T>> {};
+template<typename T> struct adl_serializer<Magnum::Math::Color3<T>> : adl_serializer<Magnum::Math::Vector<3, T>> {};
+template<typename T> struct adl_serializer<Magnum::Math::Color4<T>> : adl_serializer<Magnum::Math::Vector<4, T>> {};
+
} // namespace nlohmann