diff options
Diffstat (limited to 'lib/src')
| -rw-r--r-- | lib/src/turns-participant.c | 17 | ||||
| -rw-r--r-- | lib/src/turns-turn-order.c | 37 | ||||
| -rw-r--r-- | lib/src/turnsmm/participant.cpp | 12 |
3 files changed, 44 insertions, 22 deletions
diff --git a/lib/src/turns-participant.c b/lib/src/turns-participant.c index c18ae69..38ba0cd 100644 --- a/lib/src/turns-participant.c +++ b/lib/src/turns-participant.c @@ -163,6 +163,23 @@ TurnsParticipant * turns_participant_new_with(gchar const * name, gfloat priorit return TURNS_PARTICIPANT(g_object_new(TURNS_TYPE_PARTICIPANT, "name", name, "priority", priority, "disposition", disposition, nullptr)); } +gint turns_participant_compare(TurnsParticipant const * self, TurnsParticipant const * other) +{ + g_return_val_if_fail(TURNS_IS_PARTICIPANT((GObject *)self), 0); + g_return_val_if_fail(TURNS_IS_PARTICIPANT((GObject *)other), 0); + + if (self->priority < other->priority) + { + return -1; + } + else if (self->priority > other->priority) + { + return 1; + } + + return 0; +} + gboolean turns_participant_get_active(TurnsParticipant const * self) { g_return_val_if_fail(TURNS_IS_PARTICIPANT((TurnsParticipant *)self), false); diff --git a/lib/src/turns-turn-order.c b/lib/src/turns-turn-order.c index 6a68145..3c04247 100644 --- a/lib/src/turns-turn-order.c +++ b/lib/src/turns-turn-order.c @@ -44,35 +44,28 @@ G_DEFINE_FINAL_TYPE_WITH_CODE(TurnsTurnOrder, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL, turns_turn_order_list_model_init)) -static gint compare_participant(void const * lhs, void const * rhs, void * sort_mode) +static gint compare_participant(void const * lhs, void const * rhs, void * data) { - auto left_priority = turns_participant_get_priority(lhs); - auto right_priority = turns_participant_get_priority(rhs); + g_return_val_if_fail(TURNS_IS_TURN_ORDER(data), 0); - auto result = 0; + auto turn_order = TURNS_TURN_ORDER(data); - if (left_priority < right_priority) + switch (turn_order->sort_mode) { - result = -1; - } - else if (left_priority > right_priority) - { - result = 1; - } - - if ((TurnsTurnOrderSortMode)(long long)sort_mode == TURNS_TURN_ORDER_SORT_MODE_DESCENDING) - { - return result * -1; + case TURNS_TURN_ORDER_SORT_MODE_DESCENDING: + return -turns_participant_compare(lhs, rhs); + case TURNS_TURN_ORDER_SORT_MODE_ASCENDING: + return turns_participant_compare(lhs, rhs); + default: + g_warning("Invalid sort mode: %d", turn_order->sort_mode); + return 0; } - - return result; } static void sort_participants(TurnsTurnOrder * self, bool inserting) { - auto const sort_mode = turns_turn_order_get_sort_mode(self); - - g_ptr_array_sort_values_with_data(self->participants, &compare_participant, (gpointer)sort_mode); + g_return_if_fail(TURNS_IS_TURN_ORDER(self)); + g_ptr_array_sort_values_with_data(self->participants, &compare_participant, self); if (!inserting) { @@ -104,7 +97,7 @@ static void set_running(TurnsTurnOrder * self, gboolean value) g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_RUNNING]); } -static void handle_participant_property_changed(TurnsParticipant const *, GParamSpec *, TurnsTurnOrder * self) +static void handle_priority_changed(TurnsParticipant const *, GParamSpec *, TurnsTurnOrder * self) { sort_participants(self, false); } @@ -245,7 +238,7 @@ void turns_turn_order_add(TurnsTurnOrder * self, TurnsParticipant * participant) { g_return_if_fail(participant != nullptr); - g_signal_connect(participant, "notify::priority", (GCallback)&handle_participant_property_changed, self); + g_signal_connect(participant, "notify::priority", (GCallback)&handle_priority_changed, self); auto was_empty = turns_turn_order_get_empty(self); diff --git a/lib/src/turnsmm/participant.cpp b/lib/src/turnsmm/participant.cpp index 758f062..81b2777 100644 --- a/lib/src/turnsmm/participant.cpp +++ b/lib/src/turnsmm/participant.cpp @@ -22,6 +22,7 @@ #include <glibmm/wrap.h> #include <bit> +#include <compare> namespace Turns { @@ -84,6 +85,17 @@ namespace Turns { } + auto Participant::operator<=>(Participant const & other) const noexcept -> std::strong_ordering + { + auto result = turns_participant_compare(unwrap(this), other.gobj()); + return result == 0 ? std::strong_ordering::equivalent : (result < 0 ? std::strong_ordering::less : std::strong_ordering::greater); + } + + auto Participant::operator==(Participant const & other) const noexcept -> bool + { + return (*this <=> other) == std::strong_ordering::equivalent; + } + auto Participant::gobj() noexcept -> BaseObjectType * { return std::bit_cast<BaseObjectType *>(gobject_); |
