summaryrefslogtreecommitdiff
path: root/lib/src
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2025-05-16 18:39:44 +0200
committerFelix Morgner <felix.morgner@gmail.com>2025-05-16 18:39:44 +0200
commit842097d321e2ad9eedd7a171cdcca001d112de55 (patch)
treedc058fe2fe0a43198564882c12bea0acd3749af8 /lib/src
parent39d704461bff127a54b0b64e68135ed48b3d989b (diff)
downloadturns-842097d321e2ad9eedd7a171cdcca001d112de55.tar.xz
turns-842097d321e2ad9eedd7a171cdcca001d112de55.zip
lib: implement addition of participants
Diffstat (limited to 'lib/src')
-rw-r--r--lib/src/turns-turn-order.cpp27
-rw-r--r--lib/src/turns-turn-order.h4
-rw-r--r--lib/src/turnsmm/turn-order.cpp10
-rw-r--r--lib/src/turnsmm/turn-order.hpp4
4 files changed, 42 insertions, 3 deletions
diff --git a/lib/src/turns-turn-order.cpp b/lib/src/turns-turn-order.cpp
index 407ac3b..46ea0e6 100644
--- a/lib/src/turns-turn-order.cpp
+++ b/lib/src/turns-turn-order.cpp
@@ -39,6 +39,25 @@ namespace
auto static constinit properties = std::array<GParamSpec *, static_cast<std::size_t>(property::N_PROPERTIES)>{};
+ auto compare_participant(TurnsParticipant const * lhs, TurnsParticipant const * rhs)
+ {
+ auto left_priority = turns_participant_get_priority(lhs);
+ auto right_priority = turns_participant_get_priority(rhs);
+
+ if (left_priority < right_priority)
+ {
+ return -1;
+ }
+ else if (left_priority > right_priority)
+ {
+ return 1;
+ }
+ else
+ {
+ return 0;
+ }
+ }
+
auto finalize(GObject * self)
{
auto instance = TURNS_TURN_ORDER(self);
@@ -91,7 +110,7 @@ static void turns_turn_order_init(TurnsTurnOrder * self)
static gpointer turns_turn_order_list_model_get_item(TurnsTurnOrder * self, guint position)
{
g_return_val_if_fail(position < turns_turn_order_get_participant_count(self), nullptr);
- return g_slist_nth_data(self->participants, position);
+ return g_object_ref(g_slist_nth_data(self->participants, position));
}
static guint turns_turn_order_list_model_get_n_items(TurnsTurnOrder * self)
@@ -117,6 +136,12 @@ TurnsTurnOrder * turns_turn_order_new()
return TURNS_TURN_ORDER(g_object_new(TURNS_TYPE_TURN_ORDER, nullptr));
}
+void turns_turn_order_add(TurnsTurnOrder * self, TurnsParticipant * participant)
+{
+ g_return_if_fail(participant != nullptr);
+ self->participants = g_slist_insert_sorted(self->participants, g_object_ref(participant), std::bit_cast<GCompareFunc>(&compare_participant));
+}
+
gsize turns_turn_order_get_participant_count(TurnsTurnOrder const * self)
{
g_return_val_if_fail(TURNS_IS_TURN_ORDER(const_cast<TurnsTurnOrder *>(self)), 0);
diff --git a/lib/src/turns-turn-order.h b/lib/src/turns-turn-order.h
index 3af566b..4b1b61a 100644
--- a/lib/src/turns-turn-order.h
+++ b/lib/src/turns-turn-order.h
@@ -1,6 +1,8 @@
#ifndef TURNS_TURN_ORDER_H
#define TURNS_TURN_ORDER_H
+#include "turns-participant.h"
+
#include <glib-object.h>
#include <glib.h>
@@ -11,6 +13,8 @@ G_DECLARE_FINAL_TYPE(TurnsTurnOrder, turns_turn_order, TURNS, TURN_ORDER, GObjec
TurnsTurnOrder * turns_turn_order_new(void) G_GNUC_WARN_UNUSED_RESULT;
+void turns_turn_order_add(TurnsTurnOrder * self, TurnsParticipant * participant);
+
gsize turns_turn_order_get_participant_count(TurnsTurnOrder const * self) G_GNUC_WARN_UNUSED_RESULT;
gboolean turns_turn_order_get_running(TurnsTurnOrder const * self) G_GNUC_WARN_UNUSED_RESULT;
diff --git a/lib/src/turnsmm/turn-order.cpp b/lib/src/turnsmm/turn-order.cpp
index b29589e..c89bd92 100644
--- a/lib/src/turnsmm/turn-order.cpp
+++ b/lib/src/turnsmm/turn-order.cpp
@@ -1,6 +1,7 @@
#include "turnsmm/turn-order.hpp"
#include "turns-turn-order.h"
+#include "turnsmm/participant.hpp"
#include "turnsmm/private/turn-order_p.hpp"
#include <glibmm/class.h>
@@ -79,14 +80,19 @@ namespace Turns
return gobj();
}
+ auto TurnOrder::add(Glib::RefPtr<Participant> const & participant) noexcept -> void
+ {
+ return turns_turn_order_add(unwrap(this), unwrap(participant));
+ }
+
auto TurnOrder::get_participant_count() const noexcept -> std::size_t
{
- return turns_turn_order_get_participant_count(const_cast<BaseObjectType *>(unwrap(this)));
+ return turns_turn_order_get_participant_count(unwrap(this));
}
auto TurnOrder::get_running() const noexcept -> bool
{
- return turns_turn_order_get_running(const_cast<BaseObjectType *>(unwrap(this)));
+ return turns_turn_order_get_running(unwrap(this));
}
auto TurnOrder::property_running() const noexcept -> Glib::PropertyProxy_ReadOnly<bool>
diff --git a/lib/src/turnsmm/turn-order.hpp b/lib/src/turnsmm/turn-order.hpp
index 8e6cdb3..9485275 100644
--- a/lib/src/turnsmm/turn-order.hpp
+++ b/lib/src/turnsmm/turn-order.hpp
@@ -2,6 +2,7 @@
#define TURNSMM_TURN_ORDER_HPP
#include "turns-turn-order.h"
+#include "turnsmm/participant.hpp"
#include <glibmm/object.h>
#include <glibmm/propertyproxy.h>
@@ -9,6 +10,7 @@
#include <glibmm/ustring.h>
#include <giomm/listmodel.h>
+
#include <glib-object.h>
#include <cstddef>
@@ -34,6 +36,8 @@ namespace Turns
[[nodiscard]] auto gobj() const -> BaseObjectType const *;
[[nodiscard]] auto gobj_copy() noexcept -> BaseObjectType *;
+ auto add(Glib::RefPtr<Participant> const & participant) noexcept -> void;
+
[[nodiscard]] auto get_participant_count() const noexcept -> std::size_t;
[[nodiscard]] auto get_running() const noexcept -> bool;