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
|
#include "turns/domain/turn_order.hpp"
#include "turns/domain/participant.hpp"
#include <compare>
#include <limits>
#include <typeinfo>
#include <glibmm/binding.h>
#include <glibmm/refptr.h>
namespace turns::domain
{
namespace
{
auto constexpr comparator = [](auto lhs, auto rhs) {
auto result = *lhs <=> *rhs;
if (result == std::partial_ordering::equivalent || result == std::partial_ordering::unordered)
{
return 0;
}
else if (result == std::partial_ordering::less)
{
return 1;
}
return -1;
};
auto constexpr equal_comparator = [](auto lhs, auto rhs) {
return (lhs->get_name() == rhs->get_name()) && (lhs->get_priority() && rhs->get_priority());
};
} // namespace
auto turn_order::create() -> Glib::RefPtr<turn_order>
{
return Glib::make_refptr_for_instance(new turn_order{});
}
turn_order::turn_order()
: Glib::ObjectBase{typeid(turn_order)}
, m_model{Gio::ListStore<participant>::create()}
, m_active_participant(*this, "active_participant", std::numeric_limits<active_participant_type>::max())
, m_empty{*this, "empty", true}
, m_has_next{*this, "has-next", false}
, m_has_previous{*this, "has-previous", false}
, m_running{*this, "running", false}
, m_round{*this, "round", 0}
{
Glib::Binding::bind_property(m_model->property_n_items(), m_empty.get_proxy(), Glib::Binding::Flags::DEFAULT, [](auto n) {
return n == 0;
});
}
/** Modifiers */
auto turn_order::add(Glib::ustring const & name, float priority, disposition disposition) -> void
{
auto participant = participant::create(name, priority, disposition);
if (auto [found, index] = m_model->find(participant, equal_comparator); !found)
{
auto position = m_model->insert_sorted(participant, comparator);
participant->property_priority().signal_changed().connect([this] { m_model->sort(comparator); });
if (m_active_participant != std::numeric_limits<active_participant_type>::max() && position <= m_active_participant)
{
m_active_participant = m_active_participant + 1;
}
}
}
auto turn_order::clear() -> void
{
m_model->remove_all();
m_active_participant = std::numeric_limits<active_participant_type>::max();
m_has_next = false;
m_has_previous = false;
m_running = false;
}
auto turn_order::next() -> void
{
m_active_participant = (m_active_participant + 1) % size();
if (!m_active_participant)
{
m_round = round() + 1;
}
m_has_previous = m_active_participant || m_round;
}
auto turn_order::previous() -> void
{
if (!m_has_previous)
{
return;
}
if (m_active_participant)
{
m_active_participant = m_active_participant - 1;
}
else if (m_round)
{
m_round = round() - 1;
m_active_participant = size() - 1;
}
m_has_previous = m_active_participant || m_round;
}
auto turn_order::remove(unsigned int index) -> void
{
m_model->remove(index);
if (empty())
{
m_active_participant = std::numeric_limits<active_participant_type>::max();
m_has_next = false;
m_has_previous = false;
m_running = false;
}
else if (m_active_participant >= size() - 1)
{
m_active_participant = size() - 1;
}
else if (index <= m_active_participant)
{
m_active_participant = m_active_participant - 1;
}
}
auto turn_order::reset() -> void
{
m_running = false;
m_active_participant = 0;
}
auto turn_order::start() -> void
{
if (m_active_participant == std::numeric_limits<active_participant_type>::max())
{
m_active_participant = 0;
}
m_running = true;
m_has_next = true;
}
auto turn_order::stop() -> void
{
m_running = false;
m_has_next = false;
}
/** Querries */
auto turn_order::active_participant() const noexcept -> active_participant_type
{
return m_active_participant;
}
auto turn_order::empty() const noexcept -> bool
{
return m_empty;
}
auto turn_order::get(unsigned int index) const noexcept -> Glib::RefPtr<participant>
{
return m_model->get_item(index);
}
auto turn_order::list_model() -> Glib::RefPtr<Gio::ListModel>
{
return m_model;
}
auto turn_order::round() const noexcept -> round_type
{
return m_round;
}
auto turn_order::running() const noexcept -> running_type
{
return m_running;
}
auto turn_order::size() const noexcept -> unsigned int
{
return m_model->get_n_items();
}
/** Properties */
auto turn_order::property_active_participant() const -> Glib::PropertyProxy_ReadOnly<active_participant_type>
{
return m_active_participant.get_proxy();
}
auto turn_order::property_empty() const -> Glib::PropertyProxy_ReadOnly<bool>
{
return m_empty.get_proxy();
}
auto turn_order::property_has_next() const -> Glib::PropertyProxy_ReadOnly<has_next_type>
{
return m_has_next.get_proxy();
}
auto turn_order::property_has_previous() const -> Glib::PropertyProxy_ReadOnly<has_previous_type>
{
return m_has_previous.get_proxy();
}
auto turn_order::property_running() const -> Glib::PropertyProxy_ReadOnly<running_type>
{
return m_running.get_proxy();
}
auto turn_order::property_round() const -> Glib::PropertyProxy_ReadOnly<round_type>
{
return m_round.get_proxy();
}
} // namespace turns::domain
|