summaryrefslogtreecommitdiffhomepage
path: root/options/connector.hpp
blob: 68e86096c8623c19ab4530f7f1fc3e67c6f100d5 (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
/* 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.
 */

#pragma once

#include <map>
#include <vector>
#include <tuple>
#include <typeinfo>
#include <typeindex>
#include <QVariant>
#include <QString>
#include <QMutex>
#include <QMutexLocker>

#include "export.hpp"

namespace options {

class value_;

namespace detail {

class OTR_OPTIONS_EXPORT connector
{
    friend class ::options::value_;

    using value_type = value_*;
    using value_vec = std::vector<value_type>;
    using comparator = bool(*)(const QVariant&, const QVariant&);
    using tt = std::tuple<value_vec, comparator, std::type_index>;
    std::map<QString, tt> connected_values;

    void on_value_destructed(const QString& name, value_type val);
    void on_value_created(const QString& name, value_type val);
    bool on_value_destructed_impl(const QString& name, value_type val);

protected:
    void notify_values(const QString& name) const;
    void notify_all_values() const;
    virtual QMutex* get_mtx() const = 0;

    template<typename F>
    void forall(F&& fun)
    {
        QMutexLocker l(get_mtx());

        for (auto& pair : connected_values)
            for (auto& val : std::get<0>(pair.second))
                fun(pair.first, val);
    }

public:
    connector();
    virtual ~connector();

    bool is_equal(const QString& name, const QVariant& val1, const QVariant& val2) const;

    connector(const connector&) = default;
    connector& operator=(const connector&) = default;
    connector(connector&&) = default;
    connector& operator=(connector&&) = default;
};

} // ns detail
} // ns options