-
-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Signal #1012
Merged
jwellbelove
merged 22 commits into
ETLCPP:pull-request/#1012-Implement-Signal
from
drewr95:feature/666-signal
Jan 25, 2025
Merged
Implement Signal #1012
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4bdc774
feat: initial implementation of signal
drewr95 1e72aa3
feat: correctly calculate the end iterator
drewr95 75c6ce1
test: construct of signal
drewr95 095abd2
feat: add connected method
drewr95 fd02f1c
test: connecting a slot to a signal
drewr95 4e860ee
feat: use ETL_CONSTEXPR14 for begin() and end()
drewr95 f1a9b69
test: disconnect slots from signal
drewr95 483d874
test: disconnecting all slots from the signal
drewr95 74199ac
test: calling a signal
drewr95 885e203
test: expected string
drewr95 0194c99
feat: include algorithm
drewr95 ace677a
test: fix broken construct test for constexpr object
drewr95 fc9b15a
test: call empty
drewr95 72dec92
feat: add support for cxx03
drewr95 955907e
test: create test object for each test
drewr95 1eec0cd
feat: handle for cpp11 supported
drewr95 fc92d50
style: formatted #if
drewr95 d1624ec
test: only make a constexpr one for c++14 and on
drewr95 8bf8c96
feat: fix windows 32bit compile issues with any_of usage
drewr95 4d3561d
feat: support constructing lambda delegate
0948751
refactor: remove unecessary check if signal is
bf03165
refactor: make _end be constexpr
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
///\file | ||
|
||
/****************************************************************************** | ||
The MIT License(MIT) | ||
Embedded Template Library. | ||
https://github.com/ETLCPP/etl | ||
https://www.etlcpp.com | ||
Copyright(c) 2014 John Wellbelove, Mark Kitson | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files(the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions : | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
******************************************************************************/ | ||
|
||
#ifndef ETL_SIGNAL_INCLUDED | ||
#define ETL_SIGNAL_INCLUDED | ||
|
||
#include <cstddef> | ||
|
||
#include "exception.h" | ||
#include "error_handler.h" | ||
#include "delegate.h" | ||
#include "platform.h" | ||
|
||
#if defined(ETL_USING_STL) | ||
#include <algorithm> | ||
#include <iterator> | ||
|
||
#if defined(ETL_CPP11_NOT_SUPPORTED) | ||
#include "algorithm.h" | ||
#endif // ETL_CPP11_NOT_SUPPORTED | ||
#else | ||
#include "algorithm.h" | ||
#include "iterator.h" | ||
#endif // ETL_USING_STL | ||
|
||
//***************************************************************************** | ||
///\defgroup signal signal | ||
/// Container that handles storing and calling callbacks. | ||
///\ingroup containers | ||
//***************************************************************************** | ||
|
||
namespace etl | ||
{ | ||
//*************************************************************************** | ||
///\ingroup signal | ||
/// The base class for signal exceptions. | ||
//*************************************************************************** | ||
class signal_exception : public exception | ||
{ | ||
public: | ||
signal_exception(string_type reason_, string_type file_name_, numeric_type line_number_) | ||
: exception{reason_, file_name_, line_number_} | ||
{ | ||
} | ||
}; | ||
|
||
class signal_full final : public signal_exception | ||
{ | ||
public: | ||
signal_full(string_type file_name_, numeric_type line_number_) | ||
: signal_exception{ETL_ERROR_TEXT("signal:full", "A"), file_name_, line_number_} | ||
{ | ||
} | ||
}; | ||
|
||
|
||
//*************************************************************************** | ||
///\ingroup signal | ||
/// | ||
///\brief A lightweight signal class designed for efficient memory usage and | ||
/// ability to store in ROM. | ||
/// | ||
///\tparam T: Callback signature. | ||
///\tparam LENGTH: Maximum number of slots that can be connected to the signal. | ||
///\tparam TSlot: Function-object type or container type that can be invoked. | ||
/// | ||
///\todo Support for return types other than void (aggregate etc.) | ||
//*************************************************************************** | ||
template <typename T, size_t LENGTH, typename TSlot = etl::delegate<T>> | ||
class signal final | ||
{ | ||
public: | ||
using slot_type = TSlot; | ||
using size_type = size_t; | ||
|
||
//************************************************************************* | ||
/// Constructs the signal. | ||
//************************************************************************* | ||
template <typename... TArgs> | ||
ETL_CONSTEXPR14 explicit signal(TArgs&&... args) ETL_NOEXCEPT | ||
: _slots{ETL_OR_STD::forward<TArgs>(args)...} | ||
, _end{_slots + sizeof... (args)} | ||
{ | ||
}; | ||
|
||
//************************************************************************* | ||
///\brief Invokes all the slots connected to the signal. | ||
/// | ||
///\param args: Arguments to pass to the slots. | ||
//************************************************************************* | ||
template <typename... TArgs> | ||
void operator()(TArgs&&... args) const ETL_NOEXCEPT | ||
{ | ||
ETL_OR_STD::for_each(begin(), end(), [&args...](const slot_type& s) { | ||
s(ETL_OR_STD::forward<TArgs>(args)...); | ||
}); | ||
} | ||
|
||
//************************************************************************* | ||
///\brief Connects a slot to the signal. | ||
/// | ||
///\param slot: To connect. | ||
//************************************************************************* | ||
void connect(const slot_type& slot) | ||
{ | ||
ETL_ASSERT_OR_RETURN(!full(), ETL_ERROR(signal_full)); | ||
(*_end) = slot; | ||
_end = ETL_OR_STD::next(_end); | ||
} | ||
|
||
//************************************************************************* | ||
///\brief Disconnects a slot from the signal. | ||
/// | ||
///\param slot: To disconnect. | ||
//************************************************************************* | ||
void disconnect(const slot_type& slot) ETL_NOEXCEPT | ||
{ | ||
const auto end_it = end(); | ||
const auto it = ETL_OR_STD::find(begin(), end_it, slot); | ||
if(it == end_it) | ||
{ | ||
return; | ||
} | ||
|
||
// Copy + replace idiom (expensive). Shifts all elements after 'it' one | ||
// position to the left. | ||
ETL_OR_STD::copy(ETL_OR_STD::next(it), end_it, it); | ||
_end = ETL_OR_STD::prev(_end); | ||
} | ||
|
||
//************************************************************************* | ||
///\brief Disconnects all slots from the signal. | ||
//************************************************************************* | ||
void disconnect_all() ETL_NOEXCEPT | ||
{ | ||
_end = begin(); | ||
} | ||
|
||
//************************************************************************* | ||
///\brief Checks if a slot is connected to the signal. | ||
/// | ||
///\param slot: To check. | ||
///\return true if the slot is connected. | ||
//************************************************************************* | ||
ETL_CONSTEXPR bool connected(const slot_type& slot) const ETL_NOEXCEPT | ||
{ | ||
return etl::any_of(begin(), end(), [&slot](const slot_type& s){return s == slot;}); | ||
} | ||
|
||
//************************************************************************* | ||
///\return true if the signal has no slots connected. | ||
//************************************************************************* | ||
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT | ||
{ | ||
return begin() == end(); | ||
} | ||
|
||
//************************************************************************* | ||
///\return true if the signal has the maximum number of slots connected. | ||
//************************************************************************* | ||
ETL_CONSTEXPR bool full() const ETL_NOEXCEPT | ||
{ | ||
return size() == max_size(); | ||
} | ||
|
||
//************************************************************************* | ||
///\return Total number of slots that can be connected. | ||
//************************************************************************* | ||
ETL_CONSTEXPR size_type max_size() const ETL_NOEXCEPT | ||
{ | ||
return LENGTH; | ||
} | ||
|
||
//************************************************************************* | ||
///\return Total slots currently connected. | ||
//************************************************************************* | ||
ETL_CONSTEXPR size_type size() const ETL_NOEXCEPT | ||
{ | ||
return ETL_OR_STD::distance(begin(), end()); | ||
} | ||
|
||
private: | ||
using iterator = slot_type*; | ||
using const_iterator = const slot_type*; | ||
|
||
slot_type _slots[LENGTH]; | ||
iterator _end; | ||
|
||
//************************************************************************* | ||
///\return Iterator to the beginning of the connected slots. | ||
//************************************************************************* | ||
ETL_CONSTEXPR14 iterator begin() ETL_NOEXCEPT | ||
{ | ||
return _slots; | ||
} | ||
|
||
//************************************************************************* | ||
///\return Const Iterator to the beginning of the connected slots. | ||
//************************************************************************* | ||
ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT | ||
{ | ||
return _slots; | ||
} | ||
|
||
//************************************************************************* | ||
///\return Iterator to the end of the connected slots. | ||
//************************************************************************* | ||
ETL_CONSTEXPR14 iterator end() ETL_NOEXCEPT | ||
{ | ||
return _end; | ||
} | ||
|
||
//************************************************************************* | ||
///\return Const Iterator to the end of the connected slots. | ||
//************************************************************************* | ||
ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT | ||
{ | ||
return _end; | ||
} | ||
}; | ||
} | ||
|
||
#endif // ETL_SIGNAL_INCLUDED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this class is meant to support C++03 then you should use
ETL_FINAL
.You can check basic compatibility from C++03 to C++20 by running the script
etl/test/run-syntax-checks.sh
on Linux. You need both GCC and clang installed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My primary goal was to be c++14 compliant since that is what I use, but I tried to be as lower compliant as possible. I didn't know that script existed, thanks, I'll run it and see what I find :)