TimedEvent.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @file TimedEvent.h
  16. *
  17. */
  18. #ifndef _FASTDDS_RTPS_RESOURCES_TIMEDEVENT_H_
  19. #define _FASTDDS_RTPS_RESOURCES_TIMEDEVENT_H_
  20. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  21. #include <fastdds/rtps/common/Time_t.h>
  22. #include <thread>
  23. #include <functional>
  24. #include <cstdint>
  25. namespace eprosima {
  26. namespace fastrtps {
  27. namespace rtps {
  28. class TimedEventImpl;
  29. class ResourceEvent;
  30. /*!
  31. * Implementation of events.
  32. * This class can be used to launch an event through ResourceEvent's internal thread.
  33. *
  34. * In the construct you can set the callback to be called when the expiration time expires.
  35. * @code
  36. TimedEvent* event = new TimedEvent(resource_event_,
  37. [&]() -> bool
  38. {
  39. std::cout << "hello" << std::endl;
  40. return true;
  41. },
  42. 100);
  43. * @endcode
  44. *
  45. * The signature of the callback is:
  46. * - The returned value tells if the event has to be scheduled again or not. true value tells to reschedule the event.
  47. * false value doesn't.
  48. *
  49. * Usually the callback will call a method of the owner of the event. So the callback surely accesses internal data that
  50. * object. Then you have to be aware of deleting the event before deleting any other attribute of the object. Here are
  51. * explained two cases:
  52. *
  53. * - The event is an attribute of the class. Then it has to be declared as the last member of the class. Then we assure
  54. * it will be the last one on being constructed and the first one on being deleted.
  55. * @code
  56. class Owner
  57. {
  58. int attr1;
  59. long attr2;
  60. TimedEvent event; // Declared as the last member of the class.
  61. };
  62. * @endcode
  63. *
  64. * - The class has a pointer to the event (TimedEvent is created in heap). Then the pointer has to be the first one on
  65. * being freed in the destructor of the class.
  66. * @code
  67. class Owner
  68. {
  69. int* attr1;
  70. TimedEvent* event;
  71. long attr2,
  72. };
  73. Owner::~Owner()
  74. {
  75. delete(event); // First pointer to be deleted;
  76. delete(attr1);
  77. }
  78. * @endcode
  79. *
  80. * @ingroup MANAGEMENT_MODULE
  81. * @warning Read carefully the detailed description. This class cannot be used in any way.
  82. */
  83. class TimedEvent
  84. {
  85. public:
  86. /*!
  87. * @brief Default constructor.
  88. *
  89. * The event is not created scheduled.
  90. * @param service ResourceEvent object that will operate with the event.
  91. * @param callback Callback called when the event expires.
  92. * @param milliseconds Expiration time in milliseconds.
  93. */
  94. TimedEvent(
  95. ResourceEvent& service,
  96. std::function<bool()> callback,
  97. double milliseconds);
  98. //! Default destructor.
  99. virtual ~TimedEvent();
  100. /*!
  101. * @brief Cancels any previous scheduling of the event.
  102. */
  103. void cancel_timer();
  104. /*!
  105. * @brief Schedules the event if there is not a previous scheduling.
  106. */
  107. void restart_timer();
  108. /*!
  109. * @brief Schedules the event if there is not a previous scheduling.
  110. * @note Non-blocking call version.
  111. * @param timeout Time point in the future until the method can be blocked.
  112. */
  113. void restart_timer(
  114. const std::chrono::steady_clock::time_point& timeout);
  115. /**
  116. * Update event interval.
  117. * When updating the interval, the timer is not restarted and the new interval will only be used the next time you call restart_timer().
  118. *
  119. * @param inter New interval for the timedEvent
  120. * @return true on success
  121. */
  122. bool update_interval(
  123. const Duration_t& inter);
  124. /**
  125. * Update event interval.
  126. * When updating the interval, the timer is not restarted and the new interval will only be used the next time you call restart_timer().
  127. *
  128. * @param time_millisec New interval for the timedEvent
  129. * @return true on success
  130. */
  131. bool update_interval_millisec(
  132. double time_millisec);
  133. /**
  134. * Get the milliseconds interval
  135. * @return Milliseconds interval
  136. */
  137. double getIntervalMilliSec();
  138. /**
  139. * Get the remaining milliseconds for the timer to expire
  140. * @return Remaining milliseconds for the timer to expire
  141. */
  142. double getRemainingTimeMilliSec();
  143. private:
  144. ResourceEvent& service_;
  145. TimedEventImpl* impl_;
  146. };
  147. } /* namespace rtps */
  148. } /* namespace fastrtps */
  149. } /* namespace eprosima */
  150. #endif //DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  151. #endif //_FASTDDS_RTPS_RESOURCES_TIMEDEVENT_H_