31#ifndef ETL_SIGNAL_INCLUDED
32#define ETL_SIGNAL_INCLUDED
66 signal_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
67 : exception{reason_, file_name_, line_number_}
76 class signal_full :
public signal_exception
80 signal_full(string_type file_name_, numeric_type line_number_)
81 : signal_exception{ETL_ERROR_TEXT(
"signal:full", ETL_SIGNAL_FILE_ID
"A"), file_name_, line_number_}
98 template <
typename TFunction,
size_t Size,
typename TSlot = etl::delegate<TFunction>>
103 using slot_type = TSlot;
104 using size_type = size_t;
105 using span_type = etl::span<const slot_type>;
112 template <
typename... TSlots>
113 ETL_CONSTEXPR14
explicit signal(TSlots&&... slots) ETL_NOEXCEPT
114 : slot_list{etl::forward<TSlots>(slots)...}
115 , slot_list_end{slot_list +
sizeof...(slots)}
117 static_assert((etl::are_all_same<slot_type, etl::decay_t<TSlots>...>::value),
"All slots must be slot_type");
118 static_assert(
sizeof...(slots) <= Size,
"Number of slots exceeds capacity");
126 signal(
const signal& other) ETL_NOEXCEPT
128 , slot_list_end{slot_list + other.size()}
130 etl::copy(other.begin(), other.end(), slot_list);
139 signal& operator=(
const signal& other) ETL_NOEXCEPT
143 etl::copy(other.begin(), other.end(), slot_list);
144 slot_list_end = slot_list + other.size();
155 signal(signal&& other) ETL_NOEXCEPT
157 , slot_list_end{slot_list + other.size()}
159 etl::move(other.begin(), other.end(), slot_list);
160 other.slot_list_end = other.slot_list;
170 signal& operator=(signal&& other) ETL_NOEXCEPT
174 etl::move(other.begin(), other.end(), slot_list);
175 slot_list_end = slot_list + other.size();
176 other.slot_list_end = other.slot_list;
188 bool connect(
const slot_type& slot) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
190 if (!connected(slot))
192 ETL_ASSERT_OR_RETURN_VALUE(!full(), ETL_ERROR(signal_full),
false);
199 #if ETL_HAS_INITIALIZER_LIST && ETL_USING_CPP17
207 bool connect(std::initializer_list<const slot_type> slots) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
209 for (
const slot_type& slot : slots)
211 if (!connected(slot))
213 ETL_ASSERT_OR_RETURN_VALUE(!full(), ETL_ERROR(signal_full),
false);
229 bool connect(
const span_type slots) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
231 for (
const slot_type& slot : slots)
233 if (!connected(slot))
235 ETL_ASSERT_OR_RETURN_VALUE(!full(), ETL_ERROR(signal_full),
false);
248 void disconnect(
const slot_type& slot) ETL_NOEXCEPT
250 const auto end_itr =
end();
251 const auto itr = etl::find(
begin(), end_itr, slot);
256 etl::copy(etl::next(itr), end_itr, itr);
257 slot_list_end = etl::prev(slot_list_end);
261 #if ETL_HAS_INITIALIZER_LIST && ETL_USING_CPP17
267 void disconnect(std::initializer_list<const slot_type> slots) ETL_NOEXCEPT
269 for (
const slot_type& slot : slots)
281 void disconnect(
const span_type slots) ETL_NOEXCEPT
283 for (
const slot_type& slot : slots)
292 void disconnect_all() ETL_NOEXCEPT
294 slot_list_end =
begin();
303 ETL_CONSTEXPR14
bool connected(
const slot_type& slot)
const ETL_NOEXCEPT
311 ETL_CONSTEXPR14
bool empty() const ETL_NOEXCEPT
320 ETL_CONSTEXPR14
bool full() const ETL_NOEXCEPT
322 return size() == max_size();
328 ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
336 ETL_CONSTEXPR14 size_type
size() const ETL_NOEXCEPT
338 return static_cast<size_type
>(etl::distance(
begin(),
end()));
344 ETL_CONSTEXPR14 size_type available() const ETL_NOEXCEPT
346 return max_size() -
size();
355 template <
typename... TArgs>
356 void operator()(TArgs&&... args)
const ETL_NOEXCEPT
358 for (
const slot_type& slot : *
this)
360 if (slot_is_valid(slot))
362 slot(etl::forward<TArgs>(args)...);
369 using iterator = slot_type*;
370 using const_iterator =
const slot_type*;
372 slot_type slot_list[Size];
373 iterator slot_list_end;
378 void append_slot(
const slot_type& slot) ETL_NOEXCEPT
380 (*slot_list_end) = slot;
381 slot_list_end = etl::next(slot_list_end);
387 template <
typename TSlotType,
typename... TArgs>
388 static typename etl::enable_if_t<etl::is_delegate<TSlotType>::value,
bool> slot_is_valid(
const TSlotType& s) ETL_NOEXCEPT
396 template <
typename TSlotType,
typename... TArgs>
397 static typename etl::enable_if_t<!etl::is_delegate<TSlotType>::value,
bool> slot_is_valid(
const TSlotType&) ETL_NOEXCEPT
405 ETL_CONSTEXPR14 iterator
begin() ETL_NOEXCEPT
413 ETL_CONSTEXPR14 const_iterator
begin() const ETL_NOEXCEPT
421 ETL_CONSTEXPR14 iterator
end() ETL_NOEXCEPT
423 return slot_list_end;
429 ETL_CONSTEXPR14 const_iterator
end() const ETL_NOEXCEPT
431 return slot_list_end;
ETL_NODISCARD ETL_CONSTEXPR14 bool any_of(TIterator begin, TIterator end, TUnaryPredicate predicate)
Definition algorithm.h:2210
Definition exception.h:59
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1434
TContainer::iterator end(TContainer &container)
Definition iterator.h:1166
TContainer::iterator begin(TContainer &container)
Definition iterator.h:1136