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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/************************************************************************
*
* Copyright (C) 2009-2025 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_SLOTBASE_HPP
#include <sight/core/config.hpp>
#include "core/com/util/convert_function_type.hpp"
#include "core/thread/worker.hpp"
#include <core/base_object.hpp>
#include <core/mt/types.hpp>
#include <core/spy_log.hpp>
#include <format>
#include <future>
#include <queue>
#include <set>
namespace sight::core::thread
{
class worker;
} // namespace sight::core::thread
namespace sight::core::com
{
namespace util
{
template<typename T, typename R>
struct weak_call;
} // namespace util
template<typename F>
struct slot_run;
template<typename F>
class slot;
struct slot_connection_base;
/**
* @brief Base class for Slot implementations.
*/
struct SIGHT_CORE_CLASS_API slot_base : virtual core::base_object
{
/**
* @name typedefs
* @{
* Slot pointer types. */
using sptr = std::shared_ptr<slot_base>;
using wptr = std::weak_ptr<slot_base>;
using csptr = std::shared_ptr<const slot_base>;
using cwptr = std::weak_ptr<const slot_base>;
using idtype = std::string;
/** @} */
/// SlotBase::async_run return type.
using void_shared_future_type = std::shared_future<void>;
/// Connections container type
using connection_set_type = std::set<std::shared_ptr<const slot_connection_base> >;
~slot_base() override
= default;
/**
* @brief Returns Slot's arity.
* The arity defines the number of parameters in Slot signature.
*/
unsigned int arity() const
{
return m_arity;
}
/// Sets Slot's Worker.
void set_worker(const SPTR(core::thread::worker)& _worker)
{
core::mt::write_lock lock(m_worker_mutex);
m_worker = _worker;
}
/// Returns Slot's Worker.
SPTR(core::thread::worker) get_worker() const
{
core::mt::read_lock lock(m_worker_mutex);
return m_worker;
}
/**
* @brief Run the Slot.
* @throw BadRun if given arguments do not match the slot implementation
*/
/**
* @name Run helpers
* @{ */
template<typename A1, typename A2, typename A3>
void run(A1 _a1, A2 _a2, A3 _a3) const;
template<typename A1, typename A2>
void run(A1 _a1, A2 _a2) const;
template<typename A1>
void run(A1 _a1) const;
SIGHT_CORE_API void run() const;
/** @} */
/**
* @brief Call the Slot (with return value).
* @throw BadCall if given arguments do not match the slot implementation
*/
/**
* @name Call helpers
* @{ */
template<typename R, typename A1, typename A2, typename A3>
R call(A1 _a1, A2 _a2, A3 _a3) const;
template<typename R, typename A1, typename A2>
R call(A1 _a1, A2 _a2) const;
template<typename R, typename A1>
R call(A1 _a1) const;
template<typename R>
R call() const;
/** @} */
/**
* @brief Run the Slot asynchronously.
* @throw BadRun if given arguments do not match the slot implementation
* @throws NoWorker if given worker is not valid.
*/
/**
* @name Asynchronous run helpers
* @{ */
template<typename A1, typename A2, typename A3>
void_shared_future_type async_run(A1 _a1, A2 _a2, A3 _a3) const;
template<typename A1, typename A2>
void_shared_future_type async_run(A1 _a1, A2 _a2) const;
template<typename A1>
void_shared_future_type async_run(A1 _a1) const;
SIGHT_CORE_API void_shared_future_type async_run() const;
/** @} */
/**
* @brief Call the Slot asynchronously (with return value).
* @throw BadCall if given arguments do not match the slot implementation
* @throws NoWorker if given worker is not valid.
*/
/**
* @name Asynchronous call helpers
* @{ */
template<typename R, typename A1, typename A2, typename A3>
std::shared_future<R> async_call(A1 _a1, A2 _a2, A3 _a3) const;
template<typename R, typename A1, typename A2>
std::shared_future<R> async_call(A1 _a1, A2 _a2) const;
template<typename R, typename A1>
std::shared_future<R> async_call(A1 _a1) const;
template<typename R>
std::shared_future<R> async_call() const;
/** @} */
/// Returns number of connections.
std::size_t num_connections() const
{
core::mt::read_lock lock(m_connections_mutex);
return m_connections.size();
}
protected:
/// Copy constructor forbidden
slot_base(const slot_base&);
/// Copy operator forbidden
slot_base& operator=(const slot_base&);
/**
* @name SlotBase's friends
* @{ */
template<typename F>
friend struct slot_connection;
template<typename F>
friend struct signal;
template<typename T, typename R>
friend struct util::weak_call;
/** @} */
/// Returns F typeid name.
template<typename F>
std::string get_type_name() const
{
return std::format("function_type({})", typeid(F).name());
}
slot_base(unsigned int _arity) :
m_arity(_arity)
{
}
/// Slot's signature based on typeid.
std::string m_signature;
/// Slot's arity.
const unsigned int m_arity;
/// Slot's Worker.
SPTR(core::thread::worker) m_worker;
/// When the slot is wrapped to reduce the number of arguments in a connection, this stores a pointer
/// to the original slot. This is important in the mechanism used to keep the slot alive during an async call.
WPTR(slot_base) m_source_slot;
/// Container of current connections.
connection_set_type m_connections;
mutable core::mt::read_write_mutex m_connections_mutex;
mutable core::mt::read_write_mutex m_worker_mutex;
};
} // namespace sight::core::com
|