summaryrefslogtreecommitdiff
path: root/lib/src/turns-turn-order.cpp
blob: 3d1b4eb21cf3227b97614f066b5cb04b8db5f31e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "turns-turn-order.h"

#include "turns-participant.h"

#include <gio/gio.h>
#include <glib-object.h>
#include <glib.h>

#include <array>
#include <bit>
#include <cstddef>
#include <limits>

G_BEGIN_DECLS

struct _TurnsTurnOrder
{
  GObject parent_instance;

  GList participants;
  gsize participant_count;
  gboolean running;
};

static void turns_turn_order_list_model_init(GListModelInterface * iface);

G_DEFINE_FINAL_TYPE_WITH_CODE(TurnsTurnOrder,
                              turns_turn_order,
                              G_TYPE_OBJECT,
                              G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL, turns_turn_order_list_model_init));

G_END_DECLS

namespace
{
  enum struct property
  {
    ParticipantCount = 1,
    Running,
    N_PROPERTIES,
  };

  auto static constinit properties = std::array<GParamSpec *, static_cast<std::size_t>(property::N_PROPERTIES)>{};

  auto get_property(GObject * self, guint id, GValue * value, GParamSpec * specification) -> void
  {
    auto instance = TURNS_TURN_ORDER(self);

    switch (static_cast<property>(id))
    {
    case property::ParticipantCount:
      return g_value_set_uint64(value, turns_turn_order_get_participant_count(instance));
    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<std::size_t>(property::ParticipantCount)] =
      g_param_spec_uint64("participant-count",
                          "Participant Count",
                          "The number of participants in this turn order.",
                          0,
                          std::numeric_limits<gsize>::max(),
                          0,
                          static_cast<GParamFlags>(G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY));

  properties[static_cast<std::size_t>(property::Running)] =
      g_param_spec_boolean("running",
                           "Running",
                           "Whether or not the turn order is running (e.g. has been started)",
                           false,
                           static_cast<GParamFlags>(G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY));

  g_object_class_install_properties(object_class, properties.size(), properties.data());
}

static void turns_turn_order_init(TurnsTurnOrder * self)
{
  self->participants = {
      .data = nullptr,
      .next = nullptr,
      .prev = nullptr,
  };
  self->participant_count = 0;
  self->running = false;
}

static gpointer turns_turn_order_list_model_get_item(TurnsTurnOrder * self, guint position)
{
  g_return_val_if_fail(position < self->participant_count, nullptr);
  return g_list_nth_data(&self->participants, position);
}

static guint turns_turn_order_list_model_get_n_items(TurnsTurnOrder * self)
{
  return turns_turn_order_get_participant_count(self);
}

static GType turns_turn_order_list_model_get_item_type(TurnsTurnOrder * self)
{
  static_cast<void>(self);
  return TURNS_TYPE_PARTICIPANT;
}

static void turns_turn_order_list_model_init(GListModelInterface * iface)
{
  iface->get_item = std::bit_cast<decltype(iface->get_item)>(&turns_turn_order_list_model_get_item);
  iface->get_item_type = std::bit_cast<decltype(iface->get_item_type)>(&turns_turn_order_list_model_get_item_type);
  iface->get_n_items = std::bit_cast<decltype(iface->get_n_items)>(&turns_turn_order_list_model_get_n_items);
}

TurnsTurnOrder * turns_turn_order_new()
{
  return TURNS_TURN_ORDER(g_object_new(TURNS_TYPE_TURN_ORDER, nullptr));
}

gsize turns_turn_order_get_participant_count(TurnsTurnOrder const * self)
{
  g_return_val_if_fail(TURNS_IS_TURN_ORDER(const_cast<TurnsTurnOrder *>(self)), 0);
  return self->participant_count;
}

gboolean turns_turn_order_get_running(TurnsTurnOrder const * self)
{
  g_return_val_if_fail(TURNS_IS_TURN_ORDER(const_cast<TurnsTurnOrder *>(self)), false);
  return self->running;
}

G_END_DECLS