diff options
| -rw-r--r-- | core/include/turns/core/json_ext.hpp | 43 | ||||
| -rw-r--r-- | core/include/turns/core/turn_order.hpp | 8 | ||||
| -rw-r--r-- | core/src/participant.cpp | 11 | ||||
| -rw-r--r-- | core/src/turn_order.cpp | 40 |
4 files changed, 97 insertions, 5 deletions
diff --git a/core/include/turns/core/json_ext.hpp b/core/include/turns/core/json_ext.hpp new file mode 100644 index 0000000..3eedb64 --- /dev/null +++ b/core/include/turns/core/json_ext.hpp @@ -0,0 +1,43 @@ +#ifndef TURNS_CORE_JSON_EXT_HPP +#define TURNS_CORE_JSON_EXT_HPP + +#include <glibmm/property.h> +#include <glibmm/ustring.h> + +#include <nlohmann/json.hpp> + +#include <string> + +NLOHMANN_JSON_NAMESPACE_BEGIN + +template<typename T> +struct adl_serializer<Glib::Property<T>> +{ + static void to_json(json & to, Glib::Property<T> const & from) + { + to = from.get_value(); + } + + static void from_json(json const & from, Glib::Property<T> & to) + { + to = from.template get<T>(); + } +}; + +template<> +struct adl_serializer<Glib::ustring> +{ + static void to_json(json & to, Glib::ustring const & from) + { + to = static_cast<std::string>(from); + } + + static void from_json(json const & from, Glib::ustring & to) + { + to = from.template get<std::string>(); + } +}; + +NLOHMANN_JSON_NAMESPACE_END + +#endif
\ No newline at end of file diff --git a/core/include/turns/core/turn_order.hpp b/core/include/turns/core/turn_order.hpp index 59556bb..5dbc6c6 100644 --- a/core/include/turns/core/turn_order.hpp +++ b/core/include/turns/core/turn_order.hpp @@ -14,6 +14,8 @@ #include <optional> #include <vector> +#include <nlohmann/json_fwd.hpp> + namespace turns::core { @@ -40,6 +42,7 @@ namespace turns::core turn_order(); auto static create() -> Glib::RefPtr<turn_order>; + auto static create(nlohmann::json const & from) -> Glib::RefPtr<turn_order>; /** Properties */ auto is_empty() const -> Glib::PropertyProxy_ReadOnly<is_empty_type>; @@ -60,7 +63,12 @@ namespace turns::core auto start() -> void; auto stop() -> void; + /** Serialization */ + auto serialize() -> nlohmann::json; + private: + explicit turn_order(nlohmann::json const & from); + auto get_item_type_vfunc() -> GType override; auto get_n_items_vfunc() -> unsigned override; auto get_item_vfunc(unsigned position) -> void * override; diff --git a/core/src/participant.cpp b/core/src/participant.cpp index 0c32172..6b69754 100644 --- a/core/src/participant.cpp +++ b/core/src/participant.cpp @@ -1,5 +1,7 @@ #include "turns/core/participant.hpp" +#include "turns/core/json_ext.hpp" + #include <glibmm/refptr.h> #include <nlohmann/json.hpp> @@ -50,11 +52,10 @@ namespace turns::core auto participant::serialize() -> nlohmann::json { return nlohmann::json{ - {"active", m_is_active.get_value() }, - {"disposition", m_disposition.get_value() }, - {"name", static_cast<std::string>(m_name.get_value())}, - {"priority", m_priority.get_value() }, + {"active", m_is_active }, + {"disposition", m_disposition}, + {"name", m_name }, + {"priority", m_priority }, }; } - } // namespace turns::core
\ No newline at end of file diff --git a/core/src/turn_order.cpp b/core/src/turn_order.cpp index 835baab..6442d37 100644 --- a/core/src/turn_order.cpp +++ b/core/src/turn_order.cpp @@ -1,10 +1,14 @@ #include "turns/core/turn_order.hpp" +#include "turns/core/json_ext.hpp" #include "turns/core/participant.hpp" #include <glibmm/refptr.h> +#include <nlohmann/json.hpp> + #include <algorithm> +#include <ranges> #include <typeinfo> namespace turns::core @@ -29,11 +33,34 @@ namespace turns::core { } + turn_order::turn_order(nlohmann::json const & from) + : turn_order{} + { + m_round_number = from.value("round", invalid_round_number); + m_data = from.value("participants", std::vector<nlohmann::json>{}) | + std::views::transform([](auto const & j) { return participant::create(j); }) | std::ranges::to<std::vector>(); + + auto active = std::ranges::find_if(m_data, [](auto participant) { return participant->is_active(); }); + if (active != std::ranges::end(m_data)) + { + m_active = std::ranges::distance(std::ranges::begin(m_data), active); + } + + m_is_empty = m_data.empty(); + m_has_next = !m_is_empty; + m_has_previous = m_round_number > 0 || m_active > 0; + } + auto turn_order::create() -> Glib::RefPtr<turn_order> { return Glib::make_refptr_for_instance(new turn_order{}); } + auto turn_order::create(nlohmann::json const & from) -> Glib::RefPtr<turn_order> + { + return Glib::make_refptr_for_instance(new turn_order{from}); + } + /** Queries */ auto turn_order::is_empty() const -> Glib::PropertyProxy_ReadOnly<is_empty_type> @@ -176,6 +203,19 @@ namespace turns::core m_progress = 0; } + /** Serialization */ + auto turn_order::serialize() -> nlohmann::json + { + auto serialized = nlohmann::json{}; + if (m_round_number != invalid_round_number) + { + serialized["round"] = m_round_number; + } + serialized["participants"] = m_data | std::views::transform([](auto const & p) { return p->serialize(); }) | std::ranges::to<std::vector>(); + ; + return serialized; + } + /** ListModel implementation */ auto turn_order::get_item_type_vfunc() -> GType |
