summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2025-05-12 13:10:48 +0200
committerFelix Morgner <felix.morgner@gmail.com>2025-05-12 13:10:48 +0200
commit45ef4948db670224c7cc727507f84924bd826002 (patch)
tree45ab5e37a3cb062f4276e8b1456cb5397e58eddc /core/src
parentcf6951bbfe99bf494f22c1b1d02fb6a8f45e73a9 (diff)
downloadturns-45ef4948db670224c7cc727507f84924bd826002.tar.xz
turns-45ef4948db670224c7cc727507f84924bd826002.zip
core: begin c-style API
Diffstat (limited to 'core/src')
-rw-r--r--core/src/turns-enums.c.in37
-rw-r--r--core/src/turns-participant.cpp190
2 files changed, 227 insertions, 0 deletions
diff --git a/core/src/turns-enums.c.in b/core/src/turns-enums.c.in
new file mode 100644
index 0000000..adef39b
--- /dev/null
+++ b/core/src/turns-enums.c.in
@@ -0,0 +1,37 @@
+/*** BEGIN file-header ***/
+#include "turns/turns-enums.h"
+/*** END file-header ***/
+
+/*** BEGIN file-production ***/
+/* enumerations from "@basename@" */
+#include "turns/@basename@"
+/*** END file-production ***/
+
+/*** BEGIN value-header ***/
+GType
+@enum_name@_get_type (void)
+{
+ static gsize static_g_@type@_type_id;
+
+ if (g_once_init_enter (&static_g_@type@_type_id))
+ {
+ static const G@Type@Value values[] = {
+/*** END value-header ***/
+
+/*** BEGIN value-production ***/
+ { @VALUENAME@, "@VALUENAME@", "@valuenick@" },
+/*** END value-production ***/
+
+/*** BEGIN value-tail ***/
+ { 0, NULL, NULL }
+ };
+
+ GType g_@type@_type_id =
+ g_@type@_register_static (g_intern_static_string ("@EnumName@"), values);
+
+ g_once_init_leave (&static_g_@type@_type_id, g_@type@_type_id);
+ }
+ return static_g_@type@_type_id;
+}
+
+/*** END value-tail ***/
diff --git a/core/src/turns-participant.cpp b/core/src/turns-participant.cpp
new file mode 100644
index 0000000..91f1696
--- /dev/null
+++ b/core/src/turns-participant.cpp
@@ -0,0 +1,190 @@
+#include "turns/turns-participant.h"
+
+#include "turns/turns-disposition.h"
+#include "turns/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;
+
+ gchar * name;
+ gfloat priority;
+ TurnsDisposition disposition;
+};
+
+G_END_DECLS
+
+namespace
+{
+ enum struct property
+ {
+ Name = 1,
+ Priority,
+ Disposition,
+ 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 participant = TURNS_PARTICIPANT(self);
+
+ switch (static_cast<property>(id))
+ {
+ case property::Name:
+ g_value_set_string(value, participant->name);
+ return;
+ case property::Priority:
+ g_value_set_float(value, participant->priority);
+ return;
+ case property::Disposition:
+ g_value_set_enum(value, static_cast<gint>(participant->disposition));
+ 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:
+ g_set_str(&participant->name, g_value_get_string(value));
+ return;
+ case property::Priority:
+ participant->priority = g_value_get_float(value);
+ return;
+ case property::Disposition:
+ participant->disposition = static_cast<TurnsDisposition>(g_value_get_enum(value));
+ return;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(self, id, specification);
+ }
+ }
+} // namespace
+
+G_BEGIN_DECLS
+
+G_DEFINE_TYPE(TurnsParticipant, turns_participant, G_TYPE_OBJECT)
+
+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;
+
+ 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));
+
+ 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));
+
+ 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_HOSTILE,
+ static_cast<GParamFlags>(G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
+
+ 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(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));
+}
+
+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;
+}
+
+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;
+}
+
+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)]);
+}
+
+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)]);
+}
+
+G_END_DECLS