etl/docs/callbacks/signal.md
John Wellbelove 4a88884b39
Issue/add hugo support for documentation (#1449)
* Add ranges

* Initial Hugo setup

* Work in progress

* Added selection for local or remote site

* Updated to 'light' theme

* Changed to using Hextra Hugo theme

* Changed to using Hextra Hugo theme

* Changed to Hextra Hugo theme

* Change to Hextra Hugo theme

* Updated Hugo setup.

* Updated Hugo setup.

# Conflicts:
#	docs/releases/_index.md

* Work in progress

* Added new fonts

Added new documentation

* Latest documentation updates

* Latest documentation updates

# Conflicts:
#	docs/containers/array.md
#	docs/containers/array_view.md
#	docs/containers/array_wrapper.md
#	docs/containers/bip_buffer_spsc_atomic.md
#	docs/containers/bitset.md
#	docs/containers/indirect_vector.md
#	docs/containers/vector.md
#	docs/getting-started/compilers.md

* Added bloom_filter markdown doc

* Added more documentation

Updated CSS for light and dark modes

* Fixed some menus

Added mode documentation files

* Updated CSS rules

Added badges to home page
Added uniqur_ptr + pool tutorial

* Fixed formatting on the home page markdown

Modified light amd dark code formatting

* Updated unique_ptr-with-pool

* Added container and shared message tutorials

* Updates to documentation

* Added const_multimap

* Updated source-formatting.md

* Added initial raw text files form Web site editor

* Innore coverage build directory

* Exported raw text documentation files from the web site editor

* Hugo updates

* Added Hugo intalation and markdown descriptions

* More addition to the documentation

* Added closure.md and updates to delegate.md

* Added format.md

* Added documentation for etl::delegate_observable, etl::function, Base64 codec

* Added io_port documentation

* Added basic_format_spec

* Added documentation for string_stream and string utilities.

* Added more documentation

Updated the documentation CSS

* Added documentation for clocks, day, duration

* Added more documentation for chrono classes

Updated callouts

* More chrono documentation

* Completed chrono documentation

* Maths functions documentation

* Completed maths documentation

* Completed maths documentation

* Completed maths documentation

* Completed maths documentation

* Added multiple documentation files

* Added iterator.md

* Added debug_count.md and versions.md

* Added debug_count.md and versions.md

* Added more documentation

* More documentation

* Added some design pattern documentation

Modified some of the layout files
Modified the About documentation

* Converted more documentation pages

Modified the site CSS

* Added more documentation

Moced some documentation files to new directories

* Added more documentation

Tweaks to CSS

* Added callback_timer_deferred_locked documentation

* Added callback_timer_locked documentation

* More documentation updates

* More documentation updates

* More documentation updates

* New documentation files.

Harmonised file name format

* New documentation files.

* Multiple document updates

* Multiple document updates

* Final conversion of web pages

* Updates before PR

* Updates before PR

* Updates before PR

# Conflicts:
#	docs/blog/_index.md

* Final pre PR updates

* Updates to message framework documentation

* Renamed directory

* Fix spelling

* Added author and date to blog files

Moved documentation files merged from development

* Fixed 'Description' typo

* Fix typos

# Conflicts:
#	docs/IO/io_port.md
#	docs/containers/sets/const-multiset.md
#	docs/containers/sets/const-set.md
#	docs/maths/correlation.md
#	docs/maths/gamma.md

* Renamed two files to lower case

* Minor renaming

* Added author and date

* Updated callout on bresenham_line.md

Added support for showing the ETL version on the documentation first page, by copying the version.txt file as a hugo asset.
Updated the Python 'update_release.py' to copy 'version.txt'

* Replace space in filename with hyphen.

Added more information to hugo-commands.md

* Replace space in filename with hyphen.

Added more information to hugo-commands.md

# Conflicts:
#	docs/getting-started/view-the-docs-locally/hugo-commands.md

* Added a link to pseudo_moving_average.md

* Updated title pages for groups

* Fixed missing 404 for non-existent pages

* Fixed coordinate variable names in the 'Calculating the intersection' example

---------

Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.co.uk>
2026-06-06 13:12:44 +01:00

328 lines
7.9 KiB
Markdown

---
title: "signal"
---
{{< callout type="info">}}
Header: `signal.h`
From: `20.44.0`
{{< /callout >}}
A class that implements simple signal/slot framework.
Uses `etl::delegate` as the default slot type, though other types may be used.
```cpp
template <typename TFunction, size_t Size, typename TSlot = etl::delegate<TFunction>>
class signal
```
`TFunction` The callback function signature.
`Size` The maximum numbr of slots for the signal.
`TSlot` The callback slot type. Default = `etl::delegate<TFunction>`
## Types
`slot_type` Defined as the slot type.
`size_type` The size type used internally.
`span_type` The span type used in the connect API.
## Constructors
```cpp
template <typename... TSlots>
ETL_CONSTEXPR14 explicit signal(TSlots&&... slots) ETL_NOEXCEPT
```
Construct the signal from a variadic list of slots.
Can be used as a `constexpr` constructor.
Static asserts if any of the slots are not `slot_type`.
Static asserts if the number of slots exceeds capacity.
## Connect
```cpp
bool connect(const slot_type& slot)
```
Connects the slot, if not already connected and returns `true`.
If the signal is full, asserts `etl::signal_full` and returns `false`.
---
```cpp
bool connect(std::initializer_list<const slot_type> slots)
```
Connects all of the slots and returns `true`.
If the number of slots exceeds the signal's max size, asserts an `etl::signal_full` and returns `false`.
Enabled if `ETL_HAS_INITIALIZER_LIST` and `ETL_USING_CPP17` are defined as `1`.
---
```cpp
bool connect(const span_type slots)
```
Connects all of the slots and returns `true`.
If the number of slots exceeds the signal's max size, asserts an `etl::signal_full` and returns `false`.
## Disconnect
```cpp
void disconnect(const slot_type& slot) ETL_NOEXCEPT
```
Disconnects slot from the signal.
If the signal does not contain the slot there is no error.
---
```cpp
void disconnect(std::initializer_list<const slot_type> slots) ETL_NOEXCEPT
```
Disconnects all of the slots from the signal.
If the signal does not contain a particular slot, there is no error.
Enabled if `ETL_HAS_INITIALIZER_LIST` and `ETL_USING_CPP17` are defined as `1`.
---
```cpp
void disconnect(const span_type slots) ETL_NOEXCEPT
```
Disconnects all of the slots from the signal.
If the signal does not contain a particular slot, there is no error.
```cpp
void disconnect_all() ETL_NOEXCEPT
```
Disconnects all slots from the signal.
## Status
```cpp
ETL_CONSTEXPR14 bool connected(const slot_type& slot) const ETL_NOEXCEPT
```
Checks if a slot is connected to the signal.
---
```cpp
ETL_CONSTEXPR14 bool empty() const ETL_NOEXCEPT
```
Return `true` if the signal has no slots connected.
---
```cpp
ETL_CONSTEXPR14 bool full() const ETL_NOEXCEPT
```
Return `true` if the signal has the maximum number of slots connected.
---
```cpp
ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
```
Returns the total number of slots that can be connected.
---
```cpp
ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
```
Returns the total slots currently connected.
---
```cpp
ETL_CONSTEXPR14 size_type available() const ETL_NOEXCEPT
```
Returns the total empty slots available.
## Call
```cpp
template <typename... TArgs>
void operator()(TArgs&&... args) const ETL_NOEXCEPT
```
Function operator that calls each slot with the supplied parameters.
## Errors
```cpp
etl::signal_full
```
Indicates that an attempt to add a slot to a full signal occurred.
Inherits from `etl::signal_exception`.
## Examples
```cpp
constexpr size_t MaxSlots = 3;
using callback_type = void(int a, int b);
using signal_type = etl::signal<callback_type, MaxSlots>;
using slot_type = signal_type::slot_type;
using span_type = signal_type::span_type;
using not_slot_type = etl::delegate<void(int)>;
```
**Defining the slot functions**
```cpp
void Function1(int a, int b)
{
std::cout << "Function1: " << a << "," << b << "\n";
}
void Function2(int a, int b)
{
std::cout << "Function2: " << a << "," << b << "\n";
}
void Function3(int a, int b)
{
std::cout << "Function3: " << a << "," << b << "\n";
}
0
void Function4(int a, int b)
{
std::cout << "Function4: " << a << "," << b << "\n";
}
void Function5(int a)
{
std::cout << "Function5: " << a << "\n";
}
```
**Creating the slots**
```cpp
constexpr slot_type MakeSlot1() noexcept
{
return slot_type::create<Function1>();
}
constexpr slot_type MakeSlot2() noexcept
{
return slot_type::create<Function2>();
}
constexpr slot_type MakeSlot3() noexcept
{
return slot_type::create<Function3>();
}
constexpr slot_type MakeSlot4() noexcept
{
return slot_type::create<Function4>();
}
constexpr not_slot_type MakeSlot5() noexcept
{
return not_slot_type::create<Function5>();
}
```
**Define the signals as `constexpr`**
```cpp
// Define the signal and connect as constexpr
constexpr signal_type({ MakeSlot1(), MakeSlot2(), MakeSlot3() });
// Define the signal and connect as constexpr.
// Static assert "Number of slots exceeds capacity"
constexpr signal_type({ MakeSlot1(), MakeSlot2(), MakeSlot3(), MakeSlot4() });
// Define the signal and connect as constexpr.
// Static assert "All slots must be slot_type"
constexpr signal_type({ MakeSlot1(), MakeSlot2(), MakeSlot5() });
```
**Defining the signal at runtime**
```cpp
// Define the signal.
signal_type signal;
// Connect one at a time.
signal.connect(MakeSlot1());
signal.connect(MakeSlot2());
signal.connect(MakeSlot3());
// Connect using initializer_list.
signal.connect({ MakeSlot1(), MakeSlot2(), MakeSlot3() });
// Connect using span.
const slot_type slot_list[] = { MakeSlot1(), MakeSlot2(), MakeSlot3() };
signal.connect(slot_list);
```
**Checking the status**
```cpp
// Define the signal.
signal_type signal;
signal.max_size() // Returns 3
signal.size() // Returns 0
signal.available() // Returns 3
signal.empty(); // Returns true
signal.full(); // Returns false
signal.connected(MakeSlot1()) // Returns false
signal.connected(MakeSlot2()) // Returns false
signal.connected(MakeSlot3()) // Returns false
signal.connect(MakeSlot1()); // Returns true
signal.max_size() // Returns 3
signal.size() // Returns 1
signal.available() // Returns 2
signal.empty(); // Returns false
signal.full(); // Returns false
signal.connected(MakeSlot1()) // Returns true
signal.connected(MakeSlot2()) // Returns false
signal.connected(MakeSlot3()) // Returns false
signal.connect(MakeSlot1()); // Already connected. Returns true
signal.max_size() // Returns 3
signal.size() // Returns 1
signal.available() // Returns 2
signal.empty(); // Returns false
signal.full(); // Returns false
signal.connected(MakeSlot1()) // Returns true
signal.connected(MakeSlot2()) // Returns false
signal.connected(MakeSlot3()) // Returns false
signal.connect(MakeSlot2()); // Returns true
signal.max_size() // Returns 3
signal.size() // Returns 2
signal.available() // Returns 1
signal.empty(); // Returns false
signal.full(); // Returns false
signal.connected(MakeSlot1()) // Returns true
signal.connected(MakeSlot2()) // Returns true
signal.connected(MakeSlot3()) // Returns false
signal.connect(MakeSlot3()); // Returns true
signal.max_size() // Returns 3
signal.size() // Returns 3
signal.available() // Returns 0
signal.empty(); // Returns false
signal.full(); // Returns true
signal.connected(MakeSlot1()) // Returns true
signal.connected(MakeSlot2()) // Returns true
signal.connected(MakeSlot3()) // Returns true
signal.connect(MakeSlot4()); // ETL_ASSERT etl::signal_full. Returns false
```
**Calling the signal**
```cpp
signal_type signal({ MakeSlot1(), MakeSlot2(), MakeSlot3() });
signal(1, 2); // Call all of the slots with the parameters 1 & 2
```
**Output**
```
Function1: 1,2
Function2: 1,2
Function3: 1,2
```