summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/src/turns-turn-order.c39
-rw-r--r--lib/src/turns-turn-order.h8
-rw-r--r--lib/src/turnsmm/turn-order.cpp5
-rw-r--r--lib/src/turnsmm/turn-order.hpp1
-rw-r--r--lib/tests/turns-turn-order.cpp28
-rw-r--r--lib/tests/turnsmm/turn-order.cpp35
6 files changed, 113 insertions, 3 deletions
diff --git a/lib/src/turns-turn-order.c b/lib/src/turns-turn-order.c
index 071e4c5..5d6f99b 100644
--- a/lib/src/turns-turn-order.c
+++ b/lib/src/turns-turn-order.c
@@ -73,6 +73,27 @@ static void sort_participants(TurnsTurnOrder * self)
g_list_model_items_changed(G_LIST_MODEL(self), 0, participant_count, participant_count);
}
+static void clear_participants(TurnsTurnOrder * self)
+{
+ g_return_if_fail(TURNS_IS_TURN_ORDER(self));
+
+ g_slist_free_full(self->participants, g_object_unref);
+ self->participants = nullptr;
+}
+
+static void set_running(TurnsTurnOrder * self, gboolean value)
+{
+ g_return_if_fail(TURNS_IS_TURN_ORDER(self));
+
+ if (self->running == value)
+ {
+ return;
+ }
+
+ self->running = value;
+ g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_RUNNING]);
+}
+
static void handle_participant_property_changed(TurnsParticipant const *, GParamSpec *, TurnsTurnOrder * self)
{
sort_participants(self);
@@ -120,7 +141,7 @@ static void turns_turn_order_finalize(GObject * self)
{
auto instance = TURNS_TURN_ORDER(self);
- g_slist_free_full(instance->participants, g_object_unref);
+ clear_participants(instance);
G_OBJECT_CLASS(turns_turn_order_parent_class)->finalize(self);
}
@@ -193,6 +214,22 @@ void turns_turn_order_add(TurnsTurnOrder * self, TurnsParticipant * participant)
g_list_model_items_changed(G_LIST_MODEL(self), position, 0, 1);
}
+void turns_turn_order_clear(TurnsTurnOrder * self)
+{
+ g_return_if_fail(TURNS_IS_TURN_ORDER(self));
+
+ auto old_size = turns_turn_order_get_participant_count(self);
+ clear_participants(self);
+
+ g_object_freeze_notify(G_OBJECT(self));
+
+ set_running(self, false);
+
+ g_object_thaw_notify(G_OBJECT(self));
+
+ g_list_model_items_changed(G_LIST_MODEL(self), 0, old_size, 0);
+}
+
gsize turns_turn_order_get_participant_count(TurnsTurnOrder const * self)
{
g_return_val_if_fail(TURNS_IS_TURN_ORDER((TurnsTurnOrder *)self), 0);
diff --git a/lib/src/turns-turn-order.h b/lib/src/turns-turn-order.h
index ce4d992..a69700f 100644
--- a/lib/src/turns-turn-order.h
+++ b/lib/src/turns-turn-order.h
@@ -59,6 +59,14 @@ TurnsTurnOrder * turns_turn_order_new(void) G_GNUC_WARN_UNUSED_RESULT;
void turns_turn_order_add(TurnsTurnOrder * self, TurnsParticipant * participant);
/**
+ * turns_turn_order_clear:
+ * @self: a turn order
+ *
+ * Removes all participants from a turn order.
+ */
+void turns_turn_order_clear(TurnsTurnOrder * self);
+
+/**
* turns_turn_order_get_participant_count:
* @self: a turn order.
*
diff --git a/lib/src/turnsmm/turn-order.cpp b/lib/src/turnsmm/turn-order.cpp
index 7fefa01..35694b2 100644
--- a/lib/src/turnsmm/turn-order.cpp
+++ b/lib/src/turnsmm/turn-order.cpp
@@ -93,6 +93,11 @@ namespace Turns
return turns_turn_order_add(unwrap(this), unwrap(participant));
}
+ auto TurnOrder::clear() noexcept -> void
+ {
+ return turns_turn_order_clear(Glib::unwrap(this));
+ }
+
auto TurnOrder::get_participant_count() const noexcept -> std::size_t
{
return turns_turn_order_get_participant_count(unwrap(this));
diff --git a/lib/src/turnsmm/turn-order.hpp b/lib/src/turnsmm/turn-order.hpp
index caaac24..64f72aa 100644
--- a/lib/src/turnsmm/turn-order.hpp
+++ b/lib/src/turnsmm/turn-order.hpp
@@ -50,6 +50,7 @@ namespace Turns
[[nodiscard]] auto gobj_copy() noexcept -> BaseObjectType *;
auto add(Glib::RefPtr<Participant> const & participant) noexcept -> void;
+ auto clear() noexcept -> void;
[[nodiscard]] auto get_participant_count() const noexcept -> std::size_t;
[[nodiscard]] auto get_running() const noexcept -> bool;
diff --git a/lib/tests/turns-turn-order.cpp b/lib/tests/turns-turn-order.cpp
index 8e6cb99..ab119e1 100644
--- a/lib/tests/turns-turn-order.cpp
+++ b/lib/tests/turns-turn-order.cpp
@@ -3,7 +3,7 @@
* SPDX-License-Identifier: LGPL-2.1-only
*/
- #include "turns-turn-order.h"
+#include "turns-turn-order.h"
#include "turns-participant.h"
@@ -86,6 +86,32 @@ SCENARIO("Modifying a turn order", "[lib][object][data]")
g_autoptr(TurnsParticipant) object = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 0));
REQUIRE(TURNS_PARTICIPANT(object) == participant);
}
+
+ AND_WHEN("calling clear")
+ {
+ turns_turn_order_clear(instance);
+
+ THEN("its participant count is 0")
+ {
+ REQUIRE(turns_turn_order_get_participant_count(instance) == 0);
+ }
+
+ THEN("its running state is false")
+ {
+ REQUIRE_FALSE(turns_turn_order_get_running(instance));
+ }
+
+ THEN("its item count is 0")
+ {
+ REQUIRE(g_list_model_get_n_items(G_LIST_MODEL(instance)) == 0);
+ }
+
+ THEN("its first item is NULL")
+ {
+ REQUIRE(g_list_model_get_item(G_LIST_MODEL(instance), 0) == nullptr);
+ REQUIRE(g_list_model_get_object(G_LIST_MODEL(instance), 0) == nullptr);
+ }
+ }
}
}
}
diff --git a/lib/tests/turnsmm/turn-order.cpp b/lib/tests/turnsmm/turn-order.cpp
index 5013be8..ff8738f 100644
--- a/lib/tests/turnsmm/turn-order.cpp
+++ b/lib/tests/turnsmm/turn-order.cpp
@@ -3,7 +3,7 @@
* SPDX-License-Identifier: LGPL-2.1-only
*/
- #include "turnsmm/turn-order.hpp"
+#include "turnsmm/turn-order.hpp"
#include "turnsmm/participant.hpp"
@@ -92,6 +92,39 @@ SCENARIO("Modifying a turn order", "[lib][object][data]")
{
REQUIRE(instance.get_sort_mode() == Turns::TurnOrder::SortMode::Descending);
}
+
+ AND_WHEN("calling clear")
+ {
+ instance.clear();
+
+ THEN("its participant count is 0")
+ {
+ REQUIRE(instance.get_participant_count() == 0uz);
+ REQUIRE(instance.get_property<std::size_t>("participant-count") == 0);
+ }
+
+ THEN("its running state is false")
+ {
+ REQUIRE_FALSE(instance.get_running());
+ REQUIRE_FALSE(instance.get_property<std::size_t>("running"));
+ }
+
+ THEN("its item count is 0")
+ {
+ REQUIRE(instance.get_n_items() == 0);
+ }
+
+ THEN("its first item is NULL")
+ {
+ REQUIRE(instance.get_object(0) == nullptr);
+ REQUIRE(instance.get_typed_object<Turns::Participant>(0) == nullptr);
+ }
+
+ THEN("its sort mode is descending")
+ {
+ REQUIRE(instance.get_sort_mode() == Turns::TurnOrder::SortMode::Descending);
+ }
+ }
}
}
}