summaryrefslogtreecommitdiff
path: root/lib/src/turns-participant.cpp
blob: a94462e728d17c8d68c5069a21538a09c8fa5da9 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "turns-participant.h"

#include "turns-disposition.h"
#include "turns-enums.h"

#include <glib-object.h>
#include <glib.h>
#include <glibconfig.h>

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

G_BEGIN_DECLS

struct _TurnsParticipant
{
  GObject parent_instance;

  TurnsDisposition disposition;
  gchar * name;
  gfloat priority;
};

G_DEFINE_TYPE(TurnsParticipant, turns_participant, G_TYPE_OBJECT)

G_END_DECLS

namespace
{
  enum struct property
  {
    Disposition = 1,
    Name,
    Priority,
    N_PROPERTIES,
  };

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

  auto finalize(GObject * self)
  {
    auto participant = TURNS_PARTICIPANT(self);

    g_free(participant->name);

    G_OBJECT_CLASS(turns_participant_parent_class)->finalize(self);
  }

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

    switch (static_cast<property>(id))
    {
    case property::Name:
      g_value_set_string(value, turns_participant_get_name(participant));
      return;
    case property::Priority:
      g_value_set_float(value, turns_participant_get_priority(participant));
      return;
    case property::Disposition:
      g_value_set_enum(value, turns_participant_get_disposition(participant));
      return;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID(self, id, specification);
    };
  }

  auto set_property(GObject * self, guint id, GValue const * value, GParamSpec * specification) -> void
  {
    auto participant = TURNS_PARTICIPANT(self);

    switch (static_cast<property>(id))
    {
    case property::Name:
      return turns_participant_set_name(participant, g_value_get_string(value));
    case property::Priority:
      return turns_participant_set_priority(participant, g_value_get_float(value));
    case property::Disposition:
      return turns_participant_set_disposition(participant, static_cast<TurnsDisposition>(g_value_get_enum(value)));
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID(self, id, specification);
    }
  }
}  // namespace

G_BEGIN_DECLS

static void turns_participant_class_init(TurnsParticipantClass * klass)
{
  GObjectClass * object_class = G_OBJECT_CLASS(klass);

  (void)object_class;

  object_class->get_property = get_property;
  object_class->set_property = set_property;
  object_class->finalize = finalize;

  properties[static_cast<std::size_t>(property::Name)] =
      g_param_spec_string("name",
                          "Name",
                          "The Name of the participant",
                          "",
                          static_cast<GParamFlags>(G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY));

  properties[static_cast<std::size_t>(property::Priority)] =
      g_param_spec_float("priority",
                         "Priority",
                         "The turn priority of the participant",
                         -std::numeric_limits<float>::infinity(),
                         +std::numeric_limits<float>::infinity(),
                         0.0f,
                         static_cast<GParamFlags>(G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY));

  properties[static_cast<std::size_t>(property::Disposition)] =
      g_param_spec_enum("disposition",
                        "Disposition",
                        "Disposition of the participant toward the players",
                        turns_disposition_get_type(),
                        TURNS_DISPOSITION_NEUTRAL,
                        static_cast<GParamFlags>(G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY));

  g_object_class_install_properties(object_class, static_cast<guint>(property::N_PROPERTIES), properties.data());
}

static void turns_participant_init(TurnsParticipant * self)
{
  (void)self;
}

TurnsParticipant * turns_participant_new()
{
  return static_cast<TurnsParticipant *>(g_object_new(TURNS_TYPE_PARTICIPANT, nullptr));
}

TurnsParticipant * turns_participant_new_with(gchar const * name, gfloat priority, TurnsDisposition disposition)
{
  g_return_val_if_fail(name != nullptr, nullptr);

  return static_cast<TurnsParticipant *>(
      g_object_new(TURNS_TYPE_PARTICIPANT, "name", name, "priority", priority, "disposition", static_cast<gint>(disposition), nullptr));
}

TurnsDisposition turns_participant_get_disposition(TurnsParticipant const * self)
{
  g_return_val_if_fail(TURNS_IS_PARTICIPANT(const_cast<TurnsParticipant *>(self)), TurnsDisposition::TURNS_DISPOSITION_NEUTRAL);
  return self->disposition;
}

gchar const * turns_participant_get_name(TurnsParticipant const * self)
{
  g_return_val_if_fail(TURNS_IS_PARTICIPANT(const_cast<TurnsParticipant *>(self)), nullptr);
  return self->name;
}

gfloat turns_participant_get_priority(TurnsParticipant const * self)
{
  g_return_val_if_fail(TURNS_IS_PARTICIPANT(const_cast<TurnsParticipant *>(self)), 0.0f);
  return self->priority;
}

void turns_participant_set_disposition(TurnsParticipant * self, TurnsDisposition value)
{
  g_return_if_fail(TURNS_IS_PARTICIPANT(self));

  if (value == self->disposition)
  {
    return;
  }

  self->disposition = value;
  g_object_notify_by_pspec(G_OBJECT(self), properties[static_cast<std::size_t>(property::Disposition)]);
}

void turns_participant_set_name(TurnsParticipant * self, gchar const * value)
{
  g_return_if_fail(TURNS_IS_PARTICIPANT(self));
  g_return_if_fail(value != nullptr);

  if (!g_set_str(&self->name, value))
  {
    return;
  }

  g_object_notify_by_pspec(G_OBJECT(self), properties[static_cast<std::size_t>(property::Name)]);
}

void turns_participant_set_priority(TurnsParticipant * self, gfloat value)
{
  g_return_if_fail(TURNS_IS_PARTICIPANT(self));

  if (value == self->priority)
  {
    return;
  }

  self->priority = value;
  g_object_notify_by_pspec(G_OBJECT(self), properties[static_cast<std::size_t>(property::Priority)]);
}

G_END_DECLS