summaryrefslogtreecommitdiff
path: root/lib/src/turnsmm/participant.cpp
blob: 81b277783da7a1155818159c2d2556f754f101c7 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * SPDX-FileCopyrightText: 2025 Felix Morgner <felix.morgner@gmail.com>
 * SPDX-License-Identifier: LGPL-2.1-only
 */

#include "turnsmm/participant.hpp"

#include "turns-participant.h"
#include "turnsmm/enum_helpers.hpp"
#include "turnsmm/private/participant_p.hpp"

#include <glib-object.h>

#include <glibmm/class.h>
#include <glibmm/object.h>
#include <glibmm/objectbase.h>
#include <glibmm/private/object_p.h>
#include <glibmm/propertyproxy.h>
#include <glibmm/refptr.h>
#include <glibmm/ustring.h>
#include <glibmm/utility.h>
#include <glibmm/wrap.h>

#include <bit>
#include <compare>

namespace Turns
{

  namespace
  {
    auto constinit _class = Participant_Class{};
  }  // namespace

  auto Participant_Class::init() -> Glib::Class const &
  {
    if (!gtype_)
    {
      class_init_func_ = &class_init_function;
      gtype_ = turns_participant_get_type();
    }
    return *this;
  }

  auto Participant_Class::class_init_function(void * gclass, void * data) -> void
  {
    auto const klass = static_cast<BaseClassType *>(gclass);
    CppClassParent::class_init_function(klass, data);
  }

  auto Participant_Class::wrap_new(GObject * object) -> Glib::ObjectBase *
  {
    return new Participant(TURNS_PARTICIPANT(object));
  }

  auto Participant::get_base_type() -> GType
  {
    return turns_participant_get_type();
  }

  auto Participant::get_type() -> GType
  {
    return _class.init().get_type();
  }

  auto Participant::create() -> Glib::RefPtr<Participant>
  {
    return Glib::make_refptr_for_instance(new Participant{});
  }

  auto Participant::create(Glib::ustring const & name, float priority, Disposition disposition) -> Glib::RefPtr<Participant>
  {
    return Glib::make_refptr_for_instance(new Participant{name, priority, disposition});
  }

  Participant::Participant()
      : Glib::ObjectBase{nullptr}
      , Glib::Object{Glib::ConstructParams{_class.init()}}
  {
  }

  Participant::Participant(Glib::ustring const & name, float priority, Disposition disposition)
      : Glib::ObjectBase{nullptr}
      , Glib::Object{Glib::ConstructParams{_class.init(), "name", name.c_str(), "priority", priority, "disposition", disposition, nullptr}}
  {
  }

  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_);
  }

  auto Participant::gobj() const -> BaseObjectType const *
  {
    return std::bit_cast<BaseObjectType const *>(gobject_);
  }

  auto Participant::gobj_copy() noexcept -> BaseObjectType *
  {
    reference();
    return gobj();
  }

  auto Participant::get_active() const noexcept -> bool
  {
    return turns_participant_get_active(const_cast<BaseObjectType *>(unwrap(this)));
  }

  auto Participant::get_defeated() const noexcept -> bool
  {
    return turns_participant_get_defeated(const_cast<BaseObjectType *>(unwrap(this)));
  }

  auto Participant::get_disposition() const noexcept -> Disposition
  {
    return static_cast<Disposition>(turns_participant_get_disposition(const_cast<BaseObjectType *>(unwrap(this))));
  }

  auto Participant::get_id() const noexcept -> Glib::ustring
  {
    return Glib::convert_const_gchar_ptr_to_ustring(turns_participant_get_id(unwrap(this)));
  }

  auto Participant::get_name() const -> Glib::ustring
  {
    return turns_participant_get_name(const_cast<BaseObjectType *>(unwrap(this)));
  }

  auto Participant::get_priority() const noexcept -> float
  {
    return turns_participant_get_priority(const_cast<BaseObjectType *>(unwrap(this)));
  }

  auto Participant::set_active(bool value) noexcept -> void
  {
    return turns_participant_set_active(unwrap(this), value);
  }

  auto Participant::set_defeated(bool value) noexcept -> void
  {
    return turns_participant_set_defeated(unwrap(this), value);
  }

  auto Participant::set_disposition(Disposition value) noexcept -> void
  {
    return turns_participant_set_disposition(unwrap(this), static_cast<TurnsParticipantDisposition>(value));
  }

  auto Participant::set_name(Glib::ustring const & value) noexcept -> void
  {
    return turns_participant_set_name(unwrap(this), value.c_str());
  }

  auto Participant::set_priority(float value) noexcept -> void
  {
    return turns_participant_set_priority(unwrap(this), value);
  }

  auto Participant::property_active() const noexcept -> Glib::PropertyProxy_ReadOnly<bool>
  {
    return {this, "active"};
  }

  auto Participant::property_active() noexcept -> Glib::PropertyProxy<bool>
  {
    return {this, "active"};
  }

  auto Participant::property_defeated() const noexcept -> Glib::PropertyProxy_ReadOnly<bool>
  {
    return {this, "defeated"};
  }

  auto Participant::property_defeated() noexcept -> Glib::PropertyProxy<bool>
  {
    return {this, "defeated"};
  }

  auto Participant::property_disposition() const noexcept -> Glib::PropertyProxy_ReadOnly<Disposition>
  {
    return {this, "disposition"};
  }

  auto Participant::property_disposition() noexcept -> Glib::PropertyProxy<Disposition>
  {
    return {this, "disposition"};
  }

  auto Participant::property_name() const noexcept -> Glib::PropertyProxy_ReadOnly<Glib::ustring const &>
  {
    return {this, "name"};
  }

  auto Participant::property_name() noexcept -> Glib::PropertyProxy<Glib::ustring const &>
  {
    return {this, "name"};
  }

  auto Participant::property_priority() const noexcept -> Glib::PropertyProxy_ReadOnly<float>
  {
    return {this, "priority"};
  }

  auto Participant::property_priority() noexcept -> Glib::PropertyProxy<float>
  {
    return {this, "priority"};
  }

  Participant::Participant(BaseObjectType * gobj)
      : Glib::Object((GObject *)gobj)
  {
    static_assert(enum_matches<Disposition::Neutral, TURNS_PARTICIPANT_DISPOSITION_NEUTRAL>);
    static_assert(enum_matches<Disposition::Friendly, TURNS_PARTICIPANT_DISPOSITION_FRIENDLY>);
    static_assert(enum_matches<Disposition::Hostile, TURNS_PARTICIPANT_DISPOSITION_HOSTILE>);
    static_assert(enum_matches<Disposition::Secret, TURNS_PARTICIPANT_DISPOSITION_SECRET>);
  }

}  // namespace Turns

namespace Glib
{
  TURNS_DEFINE_VALUE_SPECIALIZATION(Participant::Disposition, participant_disposition);

  auto wrap(TurnsParticipant * object, bool copy) -> Glib::RefPtr<Turns::Participant>
  {
    return Glib::make_refptr_for_instance<Turns::Participant>(dynamic_cast<Turns::Participant *>(Glib::wrap_auto(G_OBJECT(object), copy)));
  }
}  // namespace Glib