summaryrefslogtreecommitdiffhomepage
path: root/options/connector.cpp
blob: 907701673241c776a0ffc397cca63cb62ff894a7 (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
/* Copyright (c) 2016, Stanislaw Halik <sthalik@misaki.pl>

 * Permission to use, copy, modify, and/or distribute this
 * software for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice and this permission
 * notice appear in all copies.
 */

#include "connector.hpp"
#include "value.hpp"

namespace options::detail {

connector::connector() = default;
connector::~connector() = default;

void connector::on_value_destructed(value_type val)
{
    const QString& name = val->name();

    QMutexLocker l(get_mtx());

    const auto it = connected_values.find(name);

    if (it != connected_values.end())
    {
        value_vec& values = it->second;
        for (auto it = values.begin(); it != values.end(); )
        {
            if (*it == val)
                it = values.erase(it);
            else
                it++;
        }
    }

    if (it != connected_values.end() && it->second.empty())
        connected_values.erase(it);
}

void connector::on_value_created(value_type val)
{
    const QString& name = val->name();

    if (name.isEmpty())
        return;

    QMutexLocker l(get_mtx());

    auto it = connected_values.find(name);

    if (it != connected_values.end())
    {
        value_vec& values = it->second;
        values.push_back(val);
    }
    else
    {
        value_vec vec;
        vec.reserve(4);
        vec.push_back(val);
        connected_values.emplace(name, vec);
    }
}

void connector::notify_values(const QString& name) const
{
    auto it = connected_values.find(name);
    if (it != connected_values.cend())
        for (value_type val : it->second)
            val->notify();
}

void connector::notify_all_values() const
{
    for (const auto& [k, v] : connected_values)
        for (value_type val : v)
            val->notify();
}

} // ns options::detail