Class TimedEvent

Class Documentation

class TimedEvent

Implementation of events. This class can be used to launch an event through ResourceEvent’s internal thread.

In the construct you can set the callback to be called when the expiration time expires.

TimedEvent* event = new TimedEvent(resource_event_,
     [&]() -> bool
     {
         std::cout << "hello" << std::endl;
         return true;
     },
     100);

The signature of the callback is:

  • The returned value tells if the event has to be scheduled again or not. true value tells to reschedule the event. false value doesn’t.

Usually the callback will call a method of the owner of the event. So the callback surely accesses internal data that object. Then you have to be aware of deleting the event before deleting any other attribute of the object. Here are explained two cases:

  • The event is an attribute of the class. Then it has to be declared as the last member of the class. Then we assure it will be the last one on being constructed and the first one on being deleted.

    class Owner
    {
     int attr1;
    
     long attr2;
    
     TimedEvent event;  // Declared as the last member of the class.
    };
    

  • The class has a pointer to the event (TimedEvent is created in heap). Then the pointer has to be the first one on being freed in the destructor of the class.

    class Owner
    {
      int* attr1;
    
      TimedEvent* event;
    
      long attr2,
    };
    
    Owner::~Owner()
    {
     delete(event); // First pointer to be deleted;
    
     delete(attr1);
    }
    

Warning

Read carefully the detailed description. This class cannot be used in any way.

Public Functions

TimedEvent(ResourceEvent &service, std::function<bool()> callback, double milliseconds)

Default constructor.

The event is not created scheduled.

Parameters
  • serviceResourceEvent object that will operate with the event.

  • callback – Callback called when the event expires.

  • milliseconds – Expiration time in milliseconds.

virtual ~TimedEvent()

Default destructor.

void cancel_timer()

Cancels any previous scheduling of the event.

void restart_timer()

Schedules the event if there is not a previous scheduling.

void restart_timer(const std::chrono::steady_clock::time_point &timeout)

Schedules the event if there is not a previous scheduling.

Note

Non-blocking call version.

Parameters

timeout – Time point in the future until the method can be blocked.

void recreate_timer()

Unregisters the event, sets its state to INACTIVE, and re-registers it. It may be seen as a blocking version of cancel_timer.

bool update_interval(const Duration_t &inter)

Update event interval. When updating the interval, the timer is not restarted and the new interval will only be used the next time you call restart_timer().

Parameters

inter – New interval for the timedEvent

Returns

true on success

bool update_interval_millisec(double time_millisec)

Update event interval. When updating the interval, the timer is not restarted and the new interval will only be used the next time you call restart_timer().

Parameters

time_millisec – New interval for the timedEvent

Returns

true on success

double getIntervalMilliSec()

Get the milliseconds interval

Returns

Milliseconds interval

double getRemainingTimeMilliSec()

Get the remaining milliseconds for the timer to expire

Returns

Remaining milliseconds for the timer to expire