File: slot_connection.hpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (130 lines) | stat: -rw-r--r-- 3,953 bytes parent folder | download | duplicates (2)
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
/************************************************************************
 *
 * Copyright (C) 2009-2024 IRCAD France
 * Copyright (C) 2012-2019 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#pragma once

#define FWCOM_SLOTCONNECTION_HPP

#include "core/com/slot_base.hpp"
#include "core/com/slot_connection_base.hpp"

namespace sight::core::com
{

template<typename F>
struct signal;

template<typename F>
struct slot_connection;

/**
 * @brief Slot connection implementation.
 * This class is for internal use purpose.
 */
template<typename ... A>
struct SIGHT_CORE_CLASS_API slot_connection<void(A ...)>: slot_connection_base
{
    /**
     * @name Typedefs
     * @{ */
    using signature_type = void (A ...);

    using self_t = slot_connection<signature_type>;

    using sptr = std::shared_ptr<self_t>;

    using signal_type      = signal<signature_type>;
    using signal_sptr_type = std::shared_ptr<signal_type>;
    using signal_wptr_type = std::weak_ptr<signal_type>;

    using slot_wrapper_type      = slot_run<signature_type>;
    using slot_wrapper_sptr_type = std::shared_ptr<slot_wrapper_type>;

    using slot_run_type      = slot_run<signature_type>;
    using slot_run_sptr_type = std::shared_ptr<slot_run_type>;

    using pair_type = std::pair<bool, std::weak_ptr<slot_run_type> >;
    /**  @} */

    /// Disconnect the related slot.
    void disconnect() override;

    ~slot_connection() override;

    /// Build a new connection with the given signal and slot.
    slot_connection(const signal_sptr_type& _signal, const slot_run_sptr_type& _slot);

    /// Build a new connection with the given signal, slot and wrapper.
    slot_connection(
        const signal_sptr_type& _signal,
        const SPTR(slot_base)& _slot,
        const slot_wrapper_sptr_type& _slot_wrapper
    );

    protected:

        template<typename F>
        friend struct signal;

        /// Connect the related Signal and Slot together.
        void connect_no_lock() override;

        /// *NOT THREAD SAFE* Disconnect the related signal.
        void disconnect_signal_no_lock(const signal_sptr_type& _sig);

        /// *NOT THREAD SAFE* Disconnect the related slot.
        void disconnect_slot_no_lock(const SPTR(slot_base)& _slot);

        /// *NOT THREAD SAFE* Disconnect the related slot and signal.
        void disconnect_weak_lock() override;

        /**
         * @brief Returns a blocker on the connection.
         * The connection is blocked until the returned shared pointer dies.
         */
        slot_connection_base::blocker_sptr_type get_blocker() override;

        /// Unblock this connection.
        void unblock();

        /// Related Signal.
        signal_wptr_type m_signal;

        /// Related Slot.
        WPTR(slot_base) m_connected_slot;

        /// Slot wrapper.
        SPTR(slot_base) m_slot_wrapper;

        /**
         * @brief Associates state of this connection to related Slot.
         * If m_pair.first is false, the connection is blocked.
         */
        pair_type m_pair;

        /// Connection blocker.
        slot_connection_base::blocker_wptr_type m_weak_blocker;

        mutable core::mt::read_write_mutex m_mutex;
};

} // namespace sight::core::com