summaryrefslogtreecommitdiffhomepage
path: root/options/connector.cpp
blob: 0efac8f1d1697f2d3e2bfc84f48d5018d980de33 (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
/* 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"

#include <utility>

namespace options::detail {

static bool generic_is_equal(const QVariant& val1, const QVariant& val2)
{
    return val1 == val2;
}

connector::~connector() {}

bool connector::is_equal(const QString& name, const QVariant& val1, const QVariant& val2) const
{
    QMutexLocker l(get_mtx());

    auto it = connected_values.find(name);

    if (it != connected_values.cend() && !std::get<0>((*it).second).empty())
        return std::get<1>((*it).second)(val1, val2);
    else
        return generic_is_equal(val1, val2);
}

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

    auto it = connected_values.find(name);

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

void connector::on_value_destructed(const QString& name, value_type val)
{
    if (!name.size())
        return;

    const bool ok = on_value_destructed_impl(name, val);

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

void connector::on_value_created(const QString& name, value_type val)
{
    if (!name.size())
        return;

    QMutexLocker l(get_mtx());

    int i = 1;
    while (on_value_destructed_impl(name, val))
    {
        qWarning() << "options/connector: value created twice;"
                   << "cnt" << i++
                   << "bundle" << val->b->name()
                   << "value-name" << name
                   << "value-ptr" << quintptr(val);
    }

    auto it = connected_values.find(name);

    if (it != connected_values.end())
    {
        tt& tmp = (*it).second;
        std::type_index& typeidx = std::get<2>(tmp);
        std::vector<value_type>& values = std::get<0>(tmp);

        if (typeidx != val->type_index)
            std::get<1>((*it).second) = generic_is_equal;
        values.push_back(val);
    }
    else
    {
        std::vector<value_type> vec;
        vec.push_back(val);
        connected_values.emplace(name, tt(vec, val->cmp, val->type_index));
    }
}

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

void connector::notify_all_values() const
{
    for (auto& pair : connected_values)
        for (value_type val : std::get<0>(pair.second))
            val->bundle_value_changed();
}

connector::connector()
{
}

} // ns options::detail