summaryrefslogtreecommitdiff
path: root/domain/src/turn_order.cpp
blob: 6435db9e063eef9093cc8260dfd67ffe3ae50045 (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
#include "turns/domain/turn_order.hpp"

#include "turns/domain/participant.hpp"

#include <compare>
#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_empty{*this, "empty", true}
  {
    Glib::Binding::bind_property(m_model->property_n_items(), m_empty.get_proxy(), Glib::Binding::Flags::DEFAULT, [](auto n) {
      return n == 0;
    });
  }

  auto turn_order::append(Glib::RefPtr<participant> item) -> void
  {
    if (auto [found, index] = m_model->find(item, equal_comparator); !found)
    {
      m_model->insert_sorted(item, comparator);
      item->property_priority().signal_changed().connect([this] { m_model->sort(comparator); });
    }
  }

  auto turn_order::append(Glib::ustring const & name, float priority, disposition disposition) -> void
  {
    auto participant = participant::create(name, priority, disposition);
    append(participant);
  }

  auto turn_order::get_participant(unsigned int index) -> Glib::RefPtr<participant>
  {
    return m_model->get_item(index);
  }

  auto turn_order::list_model() -> Glib::RefPtr<Gio::ListModel>
  {
    return m_model;
  }

  auto turn_order::n_participants() -> unsigned int
  {
    return m_model->get_n_items();
  }

  auto turn_order::remove(Glib::RefPtr<participant> item) -> void
  {
    if (auto [was_found, index] = m_model->find(item); was_found)
    {
      remove(index);
    }
  }

  auto turn_order::remove(unsigned int index) -> void
  {
    m_model->remove(index);
  }

  auto turn_order::remove_all() -> void
  {
    m_model->remove_all();
  }

  auto turn_order::start() -> void
  {
  }

  auto turn_order::get_empty() const noexcept -> bool
  {
    return m_empty;
  }

  auto turn_order::property_empty() const -> Glib::PropertyProxy_ReadOnly<bool>
  {
    return m_empty.get_proxy();
  }

}  // namespace turns::domain