History.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 History.h
  16. *
  17. */
  18. #ifndef _FASTDDS_RTPS_HISTORY_H_
  19. #define _FASTDDS_RTPS_HISTORY_H_
  20. #include <fastrtps/fastrtps_dll.h>
  21. #include <fastdds/rtps/history/CacheChangePool.h>
  22. #include <fastdds/rtps/common/SequenceNumber.h>
  23. #include <fastdds/rtps/common/Guid.h>
  24. #include <fastdds/rtps/attributes/HistoryAttributes.h>
  25. #include <fastrtps/utils/TimedMutex.hpp>
  26. #include <cassert>
  27. namespace eprosima {
  28. namespace fastrtps {
  29. namespace rtps {
  30. /**
  31. * Class History, container of the different CacheChanges and the methods to access them.
  32. * @ingroup COMMON_MODULE
  33. */
  34. class History
  35. {
  36. protected:
  37. History(
  38. const HistoryAttributes& att);
  39. History(
  40. History&&) = delete;
  41. History& operator =(
  42. History&&) = delete;
  43. virtual ~History();
  44. public:
  45. using iterator = std::vector<CacheChange_t*>::iterator;
  46. using reverse_iterator = std::vector<CacheChange_t*>::reverse_iterator;
  47. using const_iterator = std::vector<CacheChange_t*>::const_iterator;
  48. //!Attributes of the History
  49. HistoryAttributes m_att;
  50. /**
  51. * Reserve a CacheChange_t from the CacheChange pool.
  52. * @param[out] change Pointer to pointer to the CacheChange_t to reserve
  53. * @param[in] calculateSizeFunc Function to calculate the size of the change.
  54. * @return True is reserved
  55. */
  56. RTPS_DllAPI inline bool reserve_Cache(
  57. CacheChange_t** change,
  58. const std::function<uint32_t()>& calculateSizeFunc)
  59. {
  60. std::lock_guard<RecursiveTimedMutex> guard(*mp_mutex);
  61. return m_changePool.reserve_Cache(change, calculateSizeFunc);
  62. }
  63. RTPS_DllAPI inline bool reserve_Cache(
  64. CacheChange_t** change,
  65. uint32_t dataSize)
  66. {
  67. std::lock_guard<RecursiveTimedMutex> guard(*mp_mutex);
  68. return m_changePool.reserve_Cache(change, dataSize);
  69. }
  70. /**
  71. * release a previously reserved CacheChange_t.
  72. * @param ch Pointer to the CacheChange_t.
  73. */
  74. RTPS_DllAPI inline void release_Cache(
  75. CacheChange_t* ch)
  76. {
  77. std::lock_guard<RecursiveTimedMutex> guard(*mp_mutex);
  78. return m_changePool.release_Cache(ch);
  79. }
  80. /**
  81. * Check if the history is full
  82. * @return true if the History is full.
  83. */
  84. RTPS_DllAPI bool isFull()
  85. {
  86. return m_isHistoryFull;
  87. }
  88. /**
  89. * Get the History size.
  90. * @return Size of the history.
  91. */
  92. RTPS_DllAPI size_t getHistorySize()
  93. {
  94. std::lock_guard<RecursiveTimedMutex> guard(*mp_mutex);
  95. return m_changes.size();
  96. }
  97. /**
  98. * Remove all changes from the History
  99. * @return True if everything was correctly removed.
  100. */
  101. RTPS_DllAPI bool remove_all_changes();
  102. /**
  103. * Remove a specific change from the history.
  104. * @param ch Pointer to the CacheChange_t.
  105. * @return True if removed.
  106. */
  107. virtual bool remove_change(
  108. CacheChange_t* ch) = 0;
  109. /**
  110. * Get the beginning of the changes history iterator.
  111. * @return Iterator to the beginning of the vector.
  112. */
  113. RTPS_DllAPI iterator changesBegin()
  114. {
  115. return m_changes.begin();
  116. }
  117. RTPS_DllAPI reverse_iterator changesRbegin()
  118. {
  119. return m_changes.rbegin();
  120. }
  121. /**
  122. * Get the end of the changes history iterator.
  123. * @return Iterator to the end of the vector.
  124. */
  125. RTPS_DllAPI iterator changesEnd()
  126. {
  127. return m_changes.end();
  128. }
  129. RTPS_DllAPI reverse_iterator changesRend()
  130. {
  131. return m_changes.rend();
  132. }
  133. /**
  134. * Get the minimum CacheChange_t.
  135. * @param min_change Pointer to pointer to the minimum change.
  136. * @return True if correct.
  137. */
  138. RTPS_DllAPI bool get_min_change(
  139. CacheChange_t** min_change);
  140. /**
  141. * Get the maximum CacheChange_t.
  142. * @param max_change Pointer to pointer to the maximum change.
  143. * @return True if correct.
  144. */
  145. RTPS_DllAPI bool get_max_change(
  146. CacheChange_t** max_change);
  147. /**
  148. * Get the maximum serialized payload size
  149. * @return Maximum serialized payload size
  150. */
  151. RTPS_DllAPI inline uint32_t getTypeMaxSerialized()
  152. {
  153. return m_changePool.getInitialPayloadSize();
  154. }
  155. /*!
  156. * Get the mutex
  157. * @return Mutex
  158. */
  159. RTPS_DllAPI inline RecursiveTimedMutex* getMutex()
  160. {
  161. assert(mp_mutex != nullptr); return mp_mutex;
  162. }
  163. RTPS_DllAPI bool get_change(
  164. const SequenceNumber_t& seq,
  165. const GUID_t& guid,
  166. CacheChange_t** change) const;
  167. const_iterator get_change_nts(
  168. const SequenceNumber_t& seq,
  169. const GUID_t& guid,
  170. CacheChange_t** change,
  171. const_iterator hint) const;
  172. /**
  173. * @brief A method to get the change with the earliest timestamp
  174. * @param change Pointer to pointer to earliest change
  175. * @return True on success
  176. */
  177. bool get_earliest_change(
  178. CacheChange_t** change);
  179. protected:
  180. //!Vector of pointers to the CacheChange_t.
  181. std::vector<CacheChange_t*> m_changes;
  182. //!Variable to know if the history is full without needing to block the History mutex.
  183. bool m_isHistoryFull;
  184. //!Pool of cache changes reserved when the History is created.
  185. CacheChangePool m_changePool;
  186. //!Print the seqNum of the changes in the History (for debuggisi, mng purposes).
  187. void print_changes_seqNum2();
  188. //!Mutex for the History.
  189. RecursiveTimedMutex* mp_mutex;
  190. };
  191. }
  192. } /* namespace rtps */
  193. } /* namespace eprosima */
  194. #endif /* _FASTDDS_RTPS_HISTORY_H_ */