|
Boost Users : |
Subject: Re: [Boost-users] boost::signals2::signal - is a "decimated" version available?
From: Igor R (boost.lists_at_[hidden])
Date: 2012-05-22 02:12:29
> The signal is sometimes is generated at 100Hz. Â Other times it is
> generated at 1Hz. Â The dialog provides a "slot" for this signal that
> updates the controls on the screen. Â I'd like to limit the "slot rate"
> to 10Hz. Â By "slot rate" I mean the rate at which the slot receives
> notification from the signal. Â This will prevent the screen updates
> from happening faster than can be seen.
>
> Has this been done already? Â If not, I'm going to implement something
> like the pseudocode below. Â Currently I'm not sure how I to specify
> "arguments" nor how I am going to specify the function "SlotCallback".
<...>
> Pseudocode:
>
> float MaxPeriod = 0.001; // 10Hz
>
> class SignalDecimator
> {
>
> public:
>
> Â void Register(signal Signal, slot Slot)
> Â {
> Â Â Signal.connect(&SignalDecimator::SlotCallback, this, _1, _2, _3, _4...);
>
> Â Â mSlot = Slot;
> Â }
>
> private:
>
> Â void SlotCallback(arguments Arguments)
> Â {
> Â Â if (TimeSinceLastCall() > MaxPeriod)
> Â Â {
> Â Â Â mSlot(Arguments);
> Â Â }
> Â Â else
> Â Â {
> Â Â Â mArguments = Arguments;
>
> Â Â Â if (!TimerIsRunning())
> Â Â Â {
> Â Â Â Â StartTimer(MaxPeriod - TimeSinceLastCall());
> Â Â Â }
> Â Â }
> Â }
Do you really need to store arguments and re-signal with timer? If all
you need is just to ignore too frequent signals, you can do this in a
much simpler way:
void SlotCallback(arguments Arguments)
{
auto now = boost::posix_time::microsec_clock::universal_time();
 if (now - lastNotified > boost::posix_time::millisec(100))
   mSlot(Arguments);
else
lastNotified = now;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net