From 9513f7303ffde9fbda869e346523a23197f4ece9 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 13 May 2025 15:59:02 +0200 Subject: lib: begin TurnOrder implementation --- lib/CMakeLists.txt | 7 +++ lib/src/turns-init.cpp | 4 +- lib/src/turns-participant.h | 2 +- lib/src/turns-turn-order.cpp | 80 +++++++++++++++++++++++++++ lib/src/turns-turn-order.h | 18 +++++++ lib/src/turnsmm/init.cpp | 5 +- lib/src/turnsmm/private/turn-order_p.hpp | 31 +++++++++++ lib/src/turnsmm/turn-order.cpp | 92 ++++++++++++++++++++++++++++++++ lib/src/turnsmm/turn-order.hpp | 45 ++++++++++++++++ lib/tests/turns-turn-order.cpp | 16 ++++++ lib/tests/turnsmm/turn-order.cpp | 16 ++++++ 11 files changed, 313 insertions(+), 3 deletions(-) create mode 100644 lib/src/turns-turn-order.cpp create mode 100644 lib/src/turns-turn-order.h create mode 100644 lib/src/turnsmm/private/turn-order_p.hpp create mode 100644 lib/src/turnsmm/turn-order.cpp create mode 100644 lib/src/turnsmm/turn-order.hpp create mode 100644 lib/tests/turns-turn-order.cpp create mode 100644 lib/tests/turnsmm/turn-order.cpp (limited to 'lib') diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index e5a4916..3342c30 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -4,12 +4,14 @@ set(HEADERS "src/turns-disposition.h" "src/turns-init.h" "src/turns-participant.h" + "src/turns-turn-order.h" "src/turns.h" ) set(SOURCES "src/turns-init.cpp" "src/turns-participant.cpp" + "src/turns-turn-order.cpp" ) add_library("lib" @@ -67,15 +69,18 @@ set(CXX_HEADERS "src/turnsmm/enums.hpp" "src/turnsmm/init.hpp" "src/turnsmm/participant.hpp" + "src/turnsmm/turn-order.hpp" "src/turnsmm.hpp" "src/turnsmm/private/participant_p.hpp" + "src/turnsmm/private/turn-order_p.hpp" ) set(CXX_SOURCES "src/turnsmm/enums.cpp" "src/turnsmm/init.cpp" "src/turnsmm/participant.cpp" + "src/turnsmm/turn-order.cpp" ) add_library("libmm" @@ -123,6 +128,7 @@ install(TARGETS "libmm" add_executable("lib-tests" "tests/runtime_init.cpp" "tests/turns-participant.cpp" + "tests/turns-turn-order.cpp" ) target_link_libraries("lib-tests" PRIVATE @@ -142,6 +148,7 @@ catch_discover_tests("lib-tests") add_executable("libmm-tests" "tests/turnsmm/runtime_init.cpp" "tests/turnsmm/participant.cpp" + "tests/turnsmm/turn-order.cpp" ) target_link_libraries("libmm-tests" PRIVATE diff --git a/lib/src/turns-init.cpp b/lib/src/turns-init.cpp index 544dc14..6fbe837 100644 --- a/lib/src/turns-init.cpp +++ b/lib/src/turns-init.cpp @@ -2,6 +2,7 @@ #include "turns-enums.h" #include "turns-participant.h" +#include "turns-turn-order.h" #include #include @@ -10,8 +11,9 @@ G_BEGIN_DECLS auto turns_init() -> void { - g_type_ensure(TURNS_TYPE_PARTICIPANT); g_type_ensure(TURNS_TYPE_DISPOSITION); + g_type_ensure(TURNS_TYPE_PARTICIPANT); + g_type_ensure(TURNS_TYPE_TURN_ORDER); } G_END_DECLS \ No newline at end of file diff --git a/lib/src/turns-participant.h b/lib/src/turns-participant.h index a0e0d98..3ddfaa6 100644 --- a/lib/src/turns-participant.h +++ b/lib/src/turns-participant.h @@ -20,7 +20,7 @@ G_DECLARE_FINAL_TYPE(TurnsParticipant, turns_participant, TURNS, PARTICIPANT, GO * - @p priority is 0.0f * - @p disposition is Disposition.Neutral. */ -TurnsParticipant * turns_participant_new() G_GNUC_WARN_UNUSED_RESULT; +TurnsParticipant * turns_participant_new(void) G_GNUC_WARN_UNUSED_RESULT; /** * @brief Construct a new Participant with the given values. diff --git a/lib/src/turns-turn-order.cpp b/lib/src/turns-turn-order.cpp new file mode 100644 index 0000000..538ff11 --- /dev/null +++ b/lib/src/turns-turn-order.cpp @@ -0,0 +1,80 @@ +#include "turns-turn-order.h" + +#include +#include + +#include +#include + +G_BEGIN_DECLS + +struct _TurnsTurnOrder +{ + GObject parent_instance; + + gboolean running; +}; + +G_DEFINE_FINAL_TYPE(TurnsTurnOrder, turns_turn_order, G_TYPE_OBJECT); + +G_END_DECLS + +namespace +{ + enum struct property + { + Running = 1, + N_PROPERTIES, + }; + + auto static constinit properties = std::array(property::N_PROPERTIES)>{}; + + auto get_property(GObject * self, guint id, GValue * value, GParamSpec * specification) -> void + { + auto instance = TURNS_TURN_ORDER(self); + + switch (static_cast(id)) + { + case property::Running: + return g_value_set_boolean(value, turns_turn_order_get_running(instance)); + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(self, id, specification); + } + } + +} // namespace + +G_BEGIN_DECLS + +static void turns_turn_order_class_init(TurnsTurnOrderClass * klass) +{ + auto object_class = G_OBJECT_CLASS(klass); + + object_class->get_property = get_property; + + properties[static_cast(property::Running)] = + g_param_spec_boolean("running", + "Running", + "Whether or not the turn order is running (e.g. has been started)", + false, + static_cast(G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY)); + + g_object_class_install_properties(object_class, properties.size(), properties.data()); +} + +static void turns_turn_order_init(TurnsTurnOrder *) +{ +} + +TurnsTurnOrder * turns_turn_order_new() +{ + return TURNS_TURN_ORDER(g_object_new(TURNS_TYPE_TURN_ORDER, nullptr)); +} + +gboolean turns_turn_order_get_running(TurnsTurnOrder const * self) +{ + g_return_val_if_fail(TURNS_IS_TURN_ORDER(const_cast(self)), false); + return self->running; +} + +G_END_DECLS \ No newline at end of file diff --git a/lib/src/turns-turn-order.h b/lib/src/turns-turn-order.h new file mode 100644 index 0000000..2370445 --- /dev/null +++ b/lib/src/turns-turn-order.h @@ -0,0 +1,18 @@ +#ifndef TURNS_TURN_ORDER_H +#define TURNS_TURN_ORDER_H + +#include +#include + +G_BEGIN_DECLS + +#define TURNS_TYPE_TURN_ORDER turns_turn_order_get_type() +G_DECLARE_FINAL_TYPE(TurnsTurnOrder, turns_turn_order, TURNS, TURN_ORDER, GObject) + +TurnsTurnOrder * turns_turn_order_new(void) G_GNUC_WARN_UNUSED_RESULT; + +gboolean turns_turn_order_get_running(TurnsTurnOrder const * self) G_GNUC_WARN_UNUSED_RESULT; + +G_END_DECLS + +#endif diff --git a/lib/src/turnsmm/init.cpp b/lib/src/turnsmm/init.cpp index 77c217b..3104b1d 100644 --- a/lib/src/turnsmm/init.cpp +++ b/lib/src/turnsmm/init.cpp @@ -1,9 +1,10 @@ #include "turnsmm/init.hpp" #include "turns-init.h" -#include "turnsmm/enums.hpp" #include "turnsmm/participant.hpp" #include "turnsmm/private/participant_p.hpp" // IWYU pragma: keep +#include "turnsmm/private/turn-order_p.hpp" // IWYU pragma: keep +#include "turnsmm/turn-order.hpp" #include #include @@ -21,8 +22,10 @@ namespace Turns turns_init(); WRAP_CLASS(Participant, participant); + WRAP_CLASS(TurnOrder, turn_order); ENSURE_TYPE(Participant); + ENSURE_TYPE(TurnOrder); } } // namespace Turns \ No newline at end of file diff --git a/lib/src/turnsmm/private/turn-order_p.hpp b/lib/src/turnsmm/private/turn-order_p.hpp new file mode 100644 index 0000000..b128fd4 --- /dev/null +++ b/lib/src/turnsmm/private/turn-order_p.hpp @@ -0,0 +1,31 @@ +#ifndef TURNSMM_TURN_ORDER_P_HPP +#define TURNSMM_TURN_ORDER_P_HPP + +#include "turns-turn-order.h" + +#include +#include +#include + +#include + +namespace Turns +{ + + class TurnOrder_Class : Glib::Class + { + public: + using BaseClassParent = GObjectClass; + using BaseClassType = TurnsTurnOrderClass; + using BaseObjectType = TurnsTurnOrder; + using CppClassParent = Glib::Object_Class; + using CppObjectType = class TURN_ORDER; + + auto init() -> Glib::Class const &; + auto static class_init_function(void * gclass, void * data) -> void; + auto static wrap_new(GObject * object) -> Glib::ObjectBase *; + }; + +} // namespace Turns + +#endif \ No newline at end of file diff --git a/lib/src/turnsmm/turn-order.cpp b/lib/src/turnsmm/turn-order.cpp new file mode 100644 index 0000000..8dbcd9c --- /dev/null +++ b/lib/src/turnsmm/turn-order.cpp @@ -0,0 +1,92 @@ +#include "turnsmm/turn-order.hpp" + +#include "turns-turn-order.h" +#include "turnsmm/private/turn-order_p.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace Turns +{ + + namespace + { + auto constinit _class = TurnOrder_Class{}; + } // namespace + + auto TurnOrder_Class::init() -> Glib::Class const & + { + if (!gtype_) + { + class_init_func_ = &class_init_function; + gtype_ = turns_turn_order_get_type(); + } + return *this; + } + + auto TurnOrder_Class::class_init_function(void * gclass, void * data) -> void + { + auto const klass = static_cast(gclass); + CppClassParent::class_init_function(klass, data); + } + + auto TurnOrder_Class::wrap_new(GObject * object) -> Glib::ObjectBase * + { + return new TurnOrder(TURNS_TURN_ORDER(object)); + } + + TurnOrder::TurnOrder() + : Glib::ObjectBase{nullptr} + , Glib::Object{Glib::ConstructParams{_class.init()}} + { + } + + auto TurnOrder::gobj() noexcept -> BaseObjectType * + { + return std::bit_cast(gobject_); + } + + auto TurnOrder::gobj() const -> BaseObjectType const * + { + return std::bit_cast(gobject_); + } + + auto TurnOrder::gobj_copy() noexcept -> BaseObjectType * + { + reference(); + return gobj(); + } + + auto TurnOrder::get_running() const noexcept -> bool + { + return turns_turn_order_get_running(const_cast(unwrap(this))); + } + + auto TurnOrder::property_running() const noexcept -> Glib::PropertyProxy_ReadOnly + { + return {this, "running"}; + } + + TurnOrder::TurnOrder(BaseObjectType * gobj) + : Glib::Object(G_OBJECT(gobj)) + { + } + +} // namespace Turns + +namespace Glib +{ + auto wrap(TurnsTurnOrder * object, bool copy) -> Glib::RefPtr + { + return Glib::make_refptr_for_instance(dynamic_cast(Glib::wrap_auto(G_OBJECT(object), copy))); + } +} // namespace Glib \ No newline at end of file diff --git a/lib/src/turnsmm/turn-order.hpp b/lib/src/turnsmm/turn-order.hpp new file mode 100644 index 0000000..822e879 --- /dev/null +++ b/lib/src/turnsmm/turn-order.hpp @@ -0,0 +1,45 @@ +#ifndef TURNSMM_TURN_ORDER_HPP +#define TURNSMM_TURN_ORDER_HPP + +#include "turns-turn-order.h" + +#include +#include +#include +#include + +namespace Turns +{ + + class TurnOrder final : public Glib::Object + { + public: + using BaseClassType = TurnsTurnOrderClass; + using BaseObjectType = TurnsTurnOrder; + using CppClassType = class TurnOrder_Class; + using CppObjectType = TurnOrder; + + TurnOrder(); + + [[nodiscard]] auto gobj() noexcept -> BaseObjectType *; + [[nodiscard]] auto gobj() const -> BaseObjectType const *; + [[nodiscard]] auto gobj_copy() noexcept -> BaseObjectType *; + + [[nodiscard]] auto get_running() const noexcept -> bool; + + [[nodiscard]] auto property_running() const noexcept -> Glib::PropertyProxy_ReadOnly; + + protected: + friend TurnOrder_Class; + + explicit TurnOrder(BaseObjectType * gobj); + }; + +} // namespace Turns + +namespace Glib +{ + auto wrap(TurnsTurnOrder * object, bool copy = false) -> Glib::RefPtr; +} // namespace Glib + +#endif \ No newline at end of file diff --git a/lib/tests/turns-turn-order.cpp b/lib/tests/turns-turn-order.cpp new file mode 100644 index 0000000..feb0aa1 --- /dev/null +++ b/lib/tests/turns-turn-order.cpp @@ -0,0 +1,16 @@ +#include "turns-turn-order.h" + +#include + +SCENARIO("Creating a turn order", "[lib][object][lifetime]") +{ + GIVEN("A turn order constructed using turns_turn_order_new()") + { + g_autoptr(TurnsTurnOrder) instance = turns_turn_order_new(); + + THEN("it's running state is false") + { + REQUIRE_FALSE(turns_turn_order_get_running(instance)); + } + } +} \ No newline at end of file diff --git a/lib/tests/turnsmm/turn-order.cpp b/lib/tests/turnsmm/turn-order.cpp new file mode 100644 index 0000000..d6178ba --- /dev/null +++ b/lib/tests/turnsmm/turn-order.cpp @@ -0,0 +1,16 @@ +#include "turnsmm/turn-order.hpp" + +#include + +SCENARIO("Creating a turn order", "[lib][object][lifetime]") +{ + GIVEN("A turn order constructed using the default constructor") + { + auto instance = Turns::TurnOrder{}; + + THEN("it's running state is false") + { + REQUIRE_FALSE(instance.get_running()); + } + } +} \ No newline at end of file -- cgit v1.2.3