Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
driven_table.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
95# ifndef DRIVEN_TABLE_H
96# define DRIVEN_TABLE_H
97
98# include <aleph.H>
99# include <tpl_dynArray.H>
100# include <ah-errors.H>
101# include <functional>
102# include <limits>
103# include <memory>
104# include <type_traits>
105
114using Event_Fct [[deprecated("Use the templated Event_Table with std::function handlers instead")]]
115 = void *(*)(void *);
116
138template <typename Signature>
140{
141public:
142 using Event_Handler = std::function<Signature>;
143
144protected:
145 [[nodiscard]] virtual const Event_Handler* read_table(size_t i) const = 0;
146
147 virtual void write_table(size_t i, Event_Handler&& event_fct) = 0;
148
149 virtual void clear_slot(size_t i) = 0;
150
151public:
155 [[nodiscard]] virtual size_t size() const = 0;
156
158 virtual ~Event_Table() = default;
159
160private:
165 {
166 write_table(size(), std::move(fct));
167 }
168
169public:
179 template <typename Callable>
180 void register_event(const size_t index, Callable&& fct)
181 {
182 ah_range_error_if(index >= size()) << "Index out of range";
183 ah_range_error_if(read_table(index) != nullptr) << "Index is already used";
184
185 write_table(index, Event_Handler(std::forward<Callable>(fct)));
186 }
187
196 template <typename Callable>
197 size_t register_event(Callable&& fct)
198 {
199 append_event_to_table(Event_Handler(std::forward<Callable>(fct)));
200 return size() - 1;
201 }
202
210 void unregister_event(const size_t index)
211 {
212 ah_range_error_if(index >= size()) << "Index out of range";
213 ah_range_error_if(read_table(index) == nullptr) << "Index is not used";
214
215 clear_slot(index);
216 }
217
223 bool is_registered(const size_t index) const
224 {
225 return index < size() and read_table(index) != nullptr;
226 }
227
235 template <typename F>
236 bool check(const size_t, F) const
237 {
238 return false;
239 }
240
251 template <typename... Args>
252 auto execute_event(const size_t index, Args&&... args) const
253 {
254 ah_range_error_if(index >= size())
255 << "index " << index << " out of range [0, " << size() << ')';
256
257 const auto* event_fct = read_table(index);
258
259 ah_range_error_if(event_fct == nullptr) << "Index is not used";
260
261 return (*event_fct)(std::forward<Args>(args)...);
262 }
263};
264
265
270template <>
271class Event_Table<void*(void*)>
272{
273public:
274 using Event_Handler = std::function<void*(void*)>;
275
276protected:
277 [[nodiscard]] virtual const Event_Handler* read_table(size_t i) const = 0;
278
279 virtual void write_table(size_t i, Event_Handler&& event_fct) = 0;
280
281 virtual void clear_slot(size_t i) = 0;
282
283public:
284 [[nodiscard]] virtual size_t size() const = 0;
285
286 virtual ~Event_Table() = default;
287
288private:
290 {
291 write_table(size(), std::move(fct));
292 }
293
294public:
295 template <typename Callable>
296 void register_event(const size_t index, Callable&& fct)
297 {
298 ah_range_error_if(index >= size()) << "Index out of range";
299 ah_range_error_if(read_table(index) != nullptr) << "Index is already used";
300
301 write_table(index, Event_Handler(std::forward<Callable>(fct)));
302 }
303
304 template <typename Callable>
305 size_t register_event(Callable&& fct)
306 {
307 append_event_to_table(Event_Handler(std::forward<Callable>(fct)));
308 return size() - 1;
309 }
310
311 void unregister_event(const size_t index)
312 {
313 ah_range_error_if(index >= size()) << "Index out of range";
314 ah_range_error_if(read_table(index) == nullptr) << "Index is not used";
315
316 clear_slot(index);
317 }
318
319 bool is_registered(const size_t index) const
320 {
321 return index < size() and read_table(index) != nullptr;
322 }
323
324 // Legacy check for void* function pointers - compares function addresses
325 bool check(const size_t index, void* (*fct)(void*)) const
326 {
327 if (index >= size() || !is_registered(index))
328 return false;
329
330 const auto* handler = read_table(index);
331 if (!handler)
332 return false;
333
334 // Get the target function pointer from std::function
335 auto target = handler->template target<void* (*)(void*)>();
336 return target && *target == fct;
337 }
338
339 // Special override for void*(void*) with default nullptr argument
340 void* execute_event(const size_t index, void* input = nullptr) const
341 {
342 ah_range_error_if(index >= size())
343 << "index " << index << " out of range [0, " << size() << ')';
344
345 const auto* event_fct = read_table(index);
346
347 ah_range_error_if(event_fct == nullptr) << "Index is not used";
348
349 return (*event_fct)(input);
350 }
351};
352
353
380template <typename Signature>
381class Static_Event_Table : public Event_Table<Signature>
382{
383public:
385
386private:
387 std::unique_ptr<Event_Handler[]> table;
388 size_t size_table;
389
394 void verify_index(const size_t i) const
395 {
396 ah_range_error_if(i >= size_table) << "index out of range";
397 }
398
400 [[nodiscard]] const Event_Handler* read_table(const size_t i) const override
401 {
402 verify_index(i);
403 return table[i] ? &table[i] : nullptr;
404 }
405
407 void write_table(const size_t i, Event_Handler&& event_fct) override
408 {
409 verify_index(i);
410 table[i] = std::move(event_fct);
411 }
412
414 void clear_slot(size_t i) override
415 {
416 verify_index(i);
417 table[i] = Event_Handler();
418 }
419
422 static Event_Handler * allocate(const size_t num_events)
423 {
424 if (num_events == 0)
425 return nullptr;
426
427 // Before new[]: for a count whose bytes do not fit in a size_t, what
428 // new[] asks of the allocator depends on the compiler
429 ah_overflow_error_if(num_events > std::numeric_limits<size_t>::max() / sizeof(Event_Handler))
430 << "Static_Event_Table: " << num_events << " events do not fit in a size_t";
431 return new Event_Handler[num_events];
432 }
433
434public:
436 [[nodiscard]] size_t size() const override { return size_table; }
437
447 explicit Static_Event_Table(const size_t num_events = 0)
448 : table(allocate(num_events)),
449 size_table(num_events)
450 {
451 }
452
455
457
465 : table(std::move(other.table)), size_table(other.size_table)
466 {
467 other.size_table = 0;
468 }
469
478 {
479 if (this != &other)
480 {
481 table = std::move(other.table);
482 size_table = other.size_table;
483 other.size_table = 0;
484 }
485 return *this;
486 }
487
489 ~Static_Event_Table() override = default;
490};
491
492
533template <typename Signature>
534class Dynamic_Event_Table : public Event_Table<Signature>
535{
536public:
538
539private:
541
543 [[nodiscard]] const Event_Handler* read_table(const size_t i) const override
544 {
545 const Event_Handler& handler = table[i];
546 return handler ? &handler : nullptr;
547 }
548
550 void write_table(const size_t i, Event_Handler&& event_fct) override
551 {
552 table[i] = std::move(event_fct);
553 }
554
556 void clear_slot(size_t i) override
557 {
558 table[i] = Event_Handler();
559 }
560
561public:
563 [[nodiscard]] size_t size() const override { return table.size(); }
564
572 explicit Dynamic_Event_Table(const size_t num_events = 0) : table(num_events)
573 {
574 // Initialize all slots with empty handlers to avoid DynArray access exceptions
575 for (size_t i = 0; i < num_events; ++i)
576 table[i] = Event_Handler();
577 }
578
581
583
586
588 Dynamic_Event_Table& operator=(Dynamic_Event_Table&&) noexcept = default;
589
591 ~Dynamic_Event_Table() override = default;
592};
593
594
595// =============================================================================
596// Legacy Type Aliases for Backward Compatibility
597// =============================================================================
598
608 [[deprecated("Use Static_Event_Table<Signature> with a typed signature instead")]]
609 = Static_Event_Table<void*(void*)>;
610
620 [[deprecated("Use Dynamic_Event_Table<Signature> with a typed signature instead")]]
621 = Dynamic_Event_Table<void*(void*)>;
622
623
624# endif
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:356
Core header for the Aleph-w library.
size_t size() const noexcept
Return the current dimension of array.
Dynamic (growable) event table implementation.
size_t size() const override
Get the current table size (implements Event_Table virtual method)
typename Event_Table< Signature >::Event_Handler Event_Handler
const Event_Handler * read_table(const size_t i) const override
Read event at index (implements Event_Table virtual method)
Dynamic_Event_Table & operator=(const Dynamic_Event_Table &)=delete
Dynamic_Event_Table(Dynamic_Event_Table &&) noexcept=default
Move constructor.
Dynamic_Event_Table(const Dynamic_Event_Table &)=delete
Copying disabled (events may contain non-copyable captures)
Dynamic_Event_Table(const size_t num_events=0)
Construct a dynamic event table.
DynArray< Event_Handler > table
Dynamic array of event handlers.
void clear_slot(size_t i) override
Clear event at index (implements Event_Table virtual method)
void write_table(const size_t i, Event_Handler &&event_fct) override
Write event at index (implements Event_Table virtual method)
void append_event_to_table(Event_Handler &&fct)
void register_event(const size_t index, Callable &&fct)
virtual ~Event_Table()=default
bool check(const size_t index, void *(*fct)(void *)) const
std::function< void *(void *)> Event_Handler
size_t register_event(Callable &&fct)
virtual size_t size() const =0
void unregister_event(const size_t index)
virtual const Event_Handler * read_table(size_t i) const =0
virtual void write_table(size_t i, Event_Handler &&event_fct)=0
virtual void clear_slot(size_t i)=0
void * execute_event(const size_t index, void *input=nullptr) const
bool is_registered(const size_t index) const
Abstract base class for type-safe event tables.
virtual void write_table(size_t i, Event_Handler &&event_fct)=0
virtual size_t size() const =0
Get the number of event slots in the table.
void append_event_to_table(Event_Handler &&fct)
Append an event to the end of the table (internal use).
bool is_registered(const size_t index) const
Check if an event is registered at the given index.
virtual ~Event_Table()=default
Virtual destructor for proper cleanup in derived classes.
void register_event(const size_t index, Callable &&fct)
Register an event at a specific index.
std::function< Signature > Event_Handler
virtual void clear_slot(size_t i)=0
size_t register_event(Callable &&fct)
Append a new event and return its auto-assigned index.
void unregister_event(const size_t index)
Unregister an event at the given index.
bool check(const size_t, F) const
Legacy check method (for backward compatibility).
virtual const Event_Handler * read_table(size_t i) const =0
auto execute_event(const size_t index, Args &&... args) const
Execute the event at the given index with type-safe arguments.
Fixed-size event table implementation.
void write_table(const size_t i, Event_Handler &&event_fct) override
Write event at index (implements Event_Table virtual method)
void clear_slot(size_t i) override
Clear event at index (implements Event_Table virtual method)
Static_Event_Table(const size_t num_events=0)
Construct a static event table with a fixed size.
~Static_Event_Table() override=default
Destructor - automatically frees the underlying array.
Static_Event_Table(const Static_Event_Table &)=delete
Copying disabled (events may contain non-copyable captures)
Static_Event_Table & operator=(const Static_Event_Table &)=delete
Static_Event_Table & operator=(Static_Event_Table &&other) noexcept
Move assignment - transfers ownership of the array.
static Event_Handler * allocate(const size_t num_events)
The handlers of a table of num_events events, all of them empty; none for a table without events.
size_t size_table
Fixed size of the table.
typename Event_Table< Signature >::Event_Handler Event_Handler
std::unique_ptr< Event_Handler[]> table
Array of event handlers.
const Event_Handler * read_table(const size_t i) const override
Read event at index (implements Event_Table virtual method)
Static_Event_Table(Static_Event_Table &&other) noexcept
Move constructor - transfers ownership of the array.
void verify_index(const size_t i) const
Verify index is within valid range.
size_t size() const override
Get table size (implements Event_Table virtual method)
void *(*)(void *) Event_Fct
Type alias for legacy event handler functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
and
Check uniqueness with explicit hash + equality functors.
Lazy and scalable dynamic array implementation.