diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2025-07-25 22:59:30 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2025-07-25 22:59:30 +0200 |
| commit | d734f1e7803a756822524e0580ec8e9c93b821c6 (patch) | |
| tree | 28daa14da0ec330adaa87f1316167cf18274cfe4 /lib/src/turns-turn-order.c | |
| parent | fb2fbeca028d7a854df1c4b23bd0395d11e1f724 (diff) | |
| download | turns-main.tar.xz turns-main.zip | |
lib: make Turns.Participant comparablemain
Diffstat (limited to 'lib/src/turns-turn-order.c')
| -rw-r--r-- | lib/src/turns-turn-order.c | 37 |
1 files changed, 15 insertions, 22 deletions
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); |
