template<typename TObject, typename TEventArgs>
class Event<std::function<void(TObject*, const TEventArgs& e)>>
template<typename TObject, typename TEventArgs>
requires std::derived_from<TEventArgs, EventArgs>
class EventHandler: public std::function<void(TObject*, const TEventArgs&)>
{
public:
using Base = std::function<void(TObject*, const TEventArgs&)>;
using Object = TObject;
using EventArgs = TEventArgs;
public:
EventHandler() = default;
template<typename TCallable>
requires !std::is_same_v<std::decay_t<TCallable>, EventHandler>
EventHandler(TCallable&& handler):
Base(std::forward<TCallable>(handler))
{}
EventHandler(const EventHandler& handler):
Base(static_cast<const Base&>(handler))
{}
EventHandler(EventHandler&& handler) noexcept:
Base(static_cast<Base&&>(handler))
{}
template<typename TCallable>
requires !std::is_same_v<std::decay_t<TCallable>, EventHandler>
EventHandler& operator=(TCallable&& handler)
{
Base::operator=(std::forward<TCallable>(handler));
return *this;
}
EventHandler& operator=(const EventHandler& handler)
{
Base::operator=(static_cast<const Base&>(handler));
return *this;
}
EventHandler& operator=(EventHandler&& handler) noexcept
{
Base::operator=(static_cast<Base&&>(handler));
return *this;
}
bool operator==(const EventHandler& eventHandler) const noexcept
{
return this->target_type() == eventHandler.target_type();
}
bool operator!=(const EventHandler& eventHandler) const noexcept
{
return !operator==(eventHandler);
}
};
template<typename TEventHandler,
typename TObject = typename TEventHandler::Object,
typename TEventArgs = typename TEventHandler::EventArgs> requires
std::is_base_of_v<EventHandler<typename TEventHandler::Object,
typename TEventHandler::EventArgs>, TEventHandler>
class Event
{
public:
using EventHanlder = TEventHandler;
protected:
std::forward_list<TEventHandler> _handlers;
public:
void Clear() noexcept { _handlers.clear(); }
template<typename TCallable>
requires !std::is_same_v<std::decay_t<TCallable>, TEventHandler>
void operator+=(TCallable&& callable)
{
_handlers.emplace_front(std::forward<TCallable>(callable));
}
void operator+=(TEventHandler&& handler)
{
_handlers.push_front(std::move(handler));
}
void operator+=(const TEventHandler& handler)
{
_handlers.push_front(handler);
}
template<typename TCallable>
requires !std::is_same_v<std::decay_t<TCallable>, TEventHandler>
void operator-=(TCallable&& callable)
{
_handlers.remove(std::forward<TCallable>(callable));
}
void operator-=(TEventHandler&& handler)
{
_handlers.remove(std::move(handler));
}
void operator-=(const TEventHandler& handler)
{
_handlers.remove(handler);
}
operator bool() const noexcept { return !_handlers.empty(); }
void operator()(TObject* sender, const TEventArgs& e) const
{
for(const TEventHandler& handler : _handlers)
{
handler(sender, e);
}
}
};
template<typename THandler>
class Event
{
protected:
std::forward_list<THandler> _handlers;
public:
void operator+=(auto&& handler)
{
_handlers.emplace_front(std::forward<decltype(handler)>(handler));
}
void operator-=(auto&& handler)
{
_handlers.remove(std::forward<decltype(handler)>(handler));
}
operator bool() const noexcept
{
return !_handlers.empty();
}
template<typename TObject, typename TEventArgs> requires std::derived_from<TEventArgs, EventArgs>
void operator()(TObject* sender, const TEventArgs& e) const
{
for(const THandler& handler : _handlers)
{
handler(sender, e);
}
}
void Clear() noexcept
{
_handlers.clear();
}
};
template<typename TObject, typename TEventArgs> requires std::derived_from<TEventArgs, EventArgs>
class EventHandler: public std::function<void(TObject*, const TEventArgs&)>
{
public:
using ObjectType = TObject;
using ArgsType = TEventArgs;
};
void operator()(THandler::ObjectType* sender, const THandler::ArgsType& e) const
{
for(const THandler& handler : _handlers)
{
handler(sender, e);
}
}
template<typename TObject, typename TEventArgs> requires std::derived_from<TEventArgs, EventArgs>
void operator()(TObject* sender, const TEventArgs& e) const
{
for(const THandler& handler : _handlers)
{
handler(sender, e);
}
}
int main()
{
// some code
HAL_TIM_Base_Start(&htim3);
HAL_ADC_Start_DMA(&hadc1, &adcValue, DMABufferSize);
while(1)
{
// some code
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim == &htim2)
{
Display4Digit7Seg_Show(&display, NextDigitPosition());
}
if(htim == &htim3)
{
HAL_ADC_Start_DMA(&hadc1, &adcValue, DMABufferSize);
}
}
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}