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
|
/*
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2024 Vladimir Sadovnikov <sadko4u@gmail.com>
*
* This file is part of lsp-tk-lib
* Created on: 12 июн. 2017 г.
*
* lsp-tk-lib 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
* any later version.
*
* lsp-tk-lib 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 lsp-tk-lib. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef LSP_PLUG_IN_TK_SYS_SLOTS_H_
#define LSP_PLUG_IN_TK_SYS_SLOTS_H_
#ifndef LSP_PLUG_IN_TK_IMPL
#error "use <lsp-plug.in/tk/tk.h>"
#endif
#include <lsp-plug.in/lltl/darray.h>
namespace lsp
{
namespace tk
{
/**
* Slot class for event handling in publisher-subscriber mode
*/
class Slot
{
protected:
enum bind_flags_t
{
BIND_DFL = 0,
BIND_ENABLED = 1 << 0,
BIND_INTERCEPT = 1 << 1
};
typedef struct item_t
{
handler_id_t nID; // Identifier of handler
size_t nFlags; // Additional flags
event_handler_t pHandler; // Handler
void *pPtr; // Additional argument to pass
} item_t;
protected:
lltl::darray<item_t> vItems;
handler_id_t nID; // ID generator
bool bTracking; // Tracking skip messages
protected:
inline item_t *find_item(handler_id_t id);
handler_id_t bind(event_handler_t handler, bool intercept, void *arg, bool enabled);
size_t disable_all(bool handler, bool interceptor);
size_t enable_all(bool handler, bool interceptor);
status_t track_result(status_t result) const;
public:
explicit Slot(bool tracking = true);
Slot(const Slot &) = delete;
Slot(Slot &&) = delete;
~Slot();
Slot & operator = (const Slot &) = delete;
Slot & operator = (Slot &&) = delete;
public:
/** Bind slot
*
* @param handler event handler routine
* @param arg argument
* @param enabled binding is enabled
* @return identifier of handler or negative status code
*/
handler_id_t bind(event_handler_t handler, void *arg = NULL, bool enabled = true);
/** Intercept slot
*
* @param handler interceptor handler routine
* @param arg argument
* @param enabled interceptor is enabled
* @return identifier of interceptor or negative status code
*/
handler_id_t intercept(event_handler_t handler, void *arg = NULL, bool enabled = true);
/** Unbind handler or interceptor by identifier
*
* @param id handler identifier
* @return status of operation
*/
status_t unbind(handler_id_t id);
/** Unbind handler or interceptor by contents. Removes first occured binding
*
* @param handler event handler routine
* @param arg argument
* @return identifier of removed handler on success, negative status code on error
*/
handler_id_t unbind(event_handler_t handler, void *arg = NULL);
/** Unbind all handlers and interceptors for the slot
*
* @return number of handlers removed from bindings
*/
size_t unbind_all();
/** Disable event handler or interceptor for the slot
*
* @param id handler identifier
* @return status of operation
*/
status_t disable(handler_id_t id);
/** Disable all event handlers or interceptors
*
* @return number of non-disabled handlers that were disabled
*/
size_t disable_all();
/** Disable all event handlers only
*
* @return number of non-disabled handlers that were disabled
*/
size_t disable_all_bindings();
/** Disable all event interceptors only
*
* @return number of non-disabled handlers that were disabled
*/
size_t disable_all_interceptors();
/** Enable event handler in the slot
*
* @param id handler identifier
* @return status of operation
*/
status_t enable(handler_id_t id);
/** Enable all event handlers and interceptors
*
* @return number of non-enabled handlers that were disabled
*/
size_t enable_all();
/** Enable all interceptors
*
* @return number of non-enabled interceptors that were enabled
*/
size_t enable_all_interceptors();
/** Enable all bindings
*
* @return number of non-enabled handlers that were enabled
*/
size_t enable_all_bindings();
/** Execute slot handlers
*
* @param sender the object that initiated event
* @param data data to process
* @return status of operation
*/
status_t execute(Widget *sender, void *data);
};
} /* namespace tk */
} /* namespace lsp */
#endif /* LSP_PLUG_IN_TK_SYS_SLOTS_H_ */
|