summaryrefslogtreecommitdiffhomepage
path: root/options/connector.cpp
blob: c1bbce39d80c2a09306b5077930eda72bf19b2dc (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
#include "compat/util.hpp"
#include "connector.hpp"
#include "value.hpp"

namespace options {
namespace detail {

connector::~connector() {}

bool connector::on_value_destructed_impl(const QString& name, const base_value* val)
{
    QMutexLocker l(get_mtx());

    const bool ok = progn(
        auto it = connected_values.find(name);

        if (it != connected_values.end())
        {
            std::vector<const base_value*>& values = (*it).second;
            for (auto it = values.begin(); it != values.end(); it++)
            {
                if (*it == val)
                {
                    values.erase(it);
                    return true;
                }
            }
        }
        return false;
    );

    return ok;
}



void connector::on_value_destructed(const QString& name, const base_value* val)
{
    const bool ok = on_value_destructed_impl(name, val);

    if (!ok)
        qWarning() << "options/connector: value destructed without creating;"
                   << "bundle"
                   << (val && val->b ? val->b->name() : "<NULL>")
                   << "value-name" << name
                   << "value-ptr" << quintptr(val);
}

void connector::on_value_created(const QString& name, const base_value* val)
{
    QMutexLocker l(get_mtx());

    if (on_value_destructed_impl(name, val))
    {
        qWarning() << "options/connector: value created twice;"
                   << "bundle"
                   << (val && val->b ? val->b->name() : "<NULL>")
                   << "value-name" << name
                   << "value-ptr" << quintptr(val);
    }

    auto it = connected_values.find(name);

    if (it != connected_values.end())
    {
        std::vector<const base_value*>& values = (*it).second;
        values.push_back(val);
    }
    else
    {
        std::vector<const base_value*> vec;
        vec.push_back(val);
        connected_values[name] = vec;
    }
}

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

void connector::notify_all_values() const
{
    for (auto& pair : connected_values)
        for (const base_value* val : pair.second)
            val->bundle_value_changed();
}

connector::connector()
{
}

}

}