FastBuffer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #ifndef _FASTCDR_CDRBUFFER_H_
  15. #define _FASTCDR_CDRBUFFER_H_
  16. #include "fastcdr_dll.h"
  17. #include <stdint.h>
  18. #include <cstdio>
  19. #include <string.h>
  20. #include <cstddef>
  21. #include <utility>
  22. inline uint32_t size_to_uint32(size_t val) {
  23. #if defined(_WIN32) || !defined(FASTCDR_ARM32)
  24. // On 64 bit platforms and all Windows architectures (because of C4267), explicitly cast.
  25. return static_cast<uint32_t>(val);
  26. #else
  27. // Skip useless cast on 32-bit builds.
  28. return val;
  29. #endif
  30. }
  31. namespace eprosima
  32. {
  33. namespace fastcdr
  34. {
  35. /*!
  36. * @brief This class implements the iterator used to go through a FastBuffer.
  37. */
  38. class Cdr_DllAPI _FastBuffer_iterator
  39. {
  40. public:
  41. /*!
  42. * @brief Default constructor.
  43. * The iterator points any position.
  44. */
  45. _FastBuffer_iterator() : m_buffer(NULL), m_currentPosition(NULL) {}
  46. /*!
  47. * @brief Constructor.
  48. * The iterator points to the indicated position.
  49. * @param buffer Pointer to the raw buffer.
  50. * @param index Position of the raw buffer where the iterator will point.
  51. */
  52. explicit _FastBuffer_iterator(char *buffer, size_t index) : m_buffer(buffer), m_currentPosition(&m_buffer[index]){}
  53. /*!
  54. * @brief This operator changes the iterator's raw buffer.
  55. * This operator makes the iterator point to the same position but in another raw buffer.
  56. * The new raw buffer is the same than the source iterator's.
  57. * @param iterator The source iterator. The iterator will use the source iterator's raw buffer after this operation.
  58. */
  59. inline
  60. void operator<<(const _FastBuffer_iterator &iterator)
  61. {
  62. ptrdiff_t diff = m_currentPosition - m_buffer;
  63. m_buffer = iterator.m_buffer;
  64. m_currentPosition = m_buffer + diff;
  65. }
  66. /*!
  67. * @brief This operator changes the position where the iterator points.
  68. * This operator takes the index of the source iterator, but the iterator continues using its raw buffer.
  69. * @param The source iterator. The iterator will use the source's iterator index to point to its own raw buffer.
  70. */
  71. inline
  72. void operator>>(const _FastBuffer_iterator &iterator)
  73. {
  74. ptrdiff_t diff = iterator.m_currentPosition - iterator.m_buffer;
  75. m_currentPosition = m_buffer + diff;
  76. }
  77. /*!
  78. * @brief This operator copies a data in the raw buffer.
  79. * The copy uses the size of the data type.
  80. * @param data Data to be copied. Cannot be NULL.
  81. */
  82. template<typename _T>
  83. inline
  84. void operator<<(const _T &data)
  85. {
  86. memcpy(m_currentPosition, &data, sizeof(_T));
  87. }
  88. /*!
  89. * @brief This operator copies data from the raw buffer to a variable.
  90. * The copy uses the size of the data type.
  91. * @param data Data to be filled.
  92. */
  93. template<typename _T>
  94. inline
  95. void operator>>(_T &data)
  96. {
  97. memcpy(&data, m_currentPosition, sizeof(_T));
  98. }
  99. /*!
  100. * @brief This function copies a buffer into the raw buffer.
  101. * @param src The source buffer.
  102. * @param size The number of bytes to be copied.
  103. */
  104. inline
  105. void memcopy(const void* src, const size_t size)
  106. {
  107. if (size > 0) {
  108. memcpy(m_currentPosition, src, size);
  109. }
  110. }
  111. /*!
  112. * @brief This function copies from the raw buffer to a external buffer.
  113. * @param dst The destination buffer.
  114. * @param size The size of bytes to be copied.
  115. */
  116. inline
  117. void rmemcopy(void* dst, const size_t size)
  118. {
  119. if (size > 0) {
  120. memcpy(dst, m_currentPosition, size);
  121. }
  122. }
  123. /*!
  124. * @brief This function increments the position where the iterator points.
  125. * @param numBytes Number of bytes the iterator moves the position.
  126. */
  127. inline
  128. void operator+=(size_t numBytes)
  129. {
  130. m_currentPosition += numBytes;
  131. }
  132. /*!
  133. * @brief This operator returns the subtraction of the current interator's position and the source iterator's position.
  134. * @iterator Source iterator whose position is subtracted to the current iterator's position.
  135. * @return The result of subtract the current iterator's position and the source iterator's position.
  136. */
  137. inline
  138. size_t operator-(const _FastBuffer_iterator &it) const
  139. {
  140. return static_cast<size_t>(m_currentPosition - it.m_currentPosition);
  141. }
  142. /*!
  143. * @brief This function increments the iterator in one the position.
  144. * @return The current iterator.
  145. */
  146. inline
  147. _FastBuffer_iterator operator++()
  148. {
  149. ++m_currentPosition;
  150. return *this;
  151. }
  152. /*!
  153. * @brief This function increments the iterator in one the position.
  154. * @return The current iterator.
  155. */
  156. inline
  157. _FastBuffer_iterator operator++(int)
  158. {
  159. _FastBuffer_iterator tmp = *this;
  160. ++*this;
  161. return tmp;
  162. }
  163. /*!
  164. * @brief This function returns the current position in the raw buffer.
  165. * @return The current position in the raw buffer.
  166. */
  167. inline
  168. char* operator&()
  169. {
  170. return m_currentPosition;
  171. }
  172. private:
  173. //! Pointer to the raw buffer.
  174. char *m_buffer;
  175. //! Current position in the raw buffer.
  176. char *m_currentPosition;
  177. };
  178. /*!
  179. * @brief This class represents a stream of bytes that contains (or will contain)
  180. * serialized data. This class is used by the serializers to serialize
  181. * or deserialize using their representation.
  182. * @ingroup FASTCDRAPIREFERENCE
  183. */
  184. class Cdr_DllAPI FastBuffer
  185. {
  186. public:
  187. typedef _FastBuffer_iterator iterator;
  188. /*!
  189. * @brief This constructor creates an internal stream and assigns it to the eprosima::fastcdr::FastBuffers object.
  190. * The user can obtain this internal stream using the function eprosima::fastcdr::FastBuffers::getBuffer(). Be careful because this internal stream
  191. * is deleted in the destruction of the eprosima::fastcdr::FastBuffers object.
  192. */
  193. FastBuffer();
  194. /*!
  195. * @brief This constructor assigns the user's stream of bytes to the eprosima::fastcdr::FastBuffers object.
  196. * The user's stream will be used to serialize.
  197. *
  198. * @param buffer The user's buffer that will be used. This buffer is not deallocated in the object's destruction. Cannot be NULL.
  199. * @param bufferSize The length of user's buffer.
  200. */
  201. FastBuffer(char* const buffer, const size_t bufferSize);
  202. //! Move constructor
  203. FastBuffer(FastBuffer&& fbuffer) : m_buffer(nullptr), m_bufferSize(0), m_internalBuffer(true)
  204. {
  205. std::swap(m_buffer, fbuffer.m_buffer);
  206. std::swap(m_bufferSize, fbuffer.m_bufferSize);
  207. std::swap(m_internalBuffer, fbuffer.m_internalBuffer);
  208. }
  209. //! Move assignment
  210. FastBuffer& operator=(FastBuffer&& fbuffer)
  211. {
  212. std::swap(m_buffer, fbuffer.m_buffer);
  213. std::swap(m_bufferSize, fbuffer.m_bufferSize);
  214. std::swap(m_internalBuffer, fbuffer.m_internalBuffer);
  215. return *this;
  216. }
  217. /*!
  218. * @brief Default destructor.
  219. */
  220. virtual ~FastBuffer();
  221. /*!
  222. * @brief This function returns the stream that the eprosima::fastcdr::FastBuffers uses to serialize data.
  223. * @return The stream used by eprosima::fastcdr::FastBuffers to serialize data.
  224. */
  225. inline char* getBuffer() const { return m_buffer;}
  226. /*!
  227. * @brief This function returns the size of the allocated memory of the stream that the eprosima::fastcdr::FastBuffers uses to serialize data.
  228. * @return The size of the allocated memory of the stream used by the eprosima::fastcdr::FastBuffers to serialize data.
  229. */
  230. inline size_t getBufferSize() const { return m_bufferSize;}
  231. /*!
  232. * @brief This function returns a iterator that points to the begining of the stream.
  233. * @return The new iterator.
  234. */
  235. inline
  236. iterator begin()
  237. {
  238. return (iterator(m_buffer, 0));
  239. }
  240. /*!
  241. * @brief This function returns a iterator that points to the end of the stream.
  242. * @return The new iterator.
  243. */
  244. inline
  245. iterator end()
  246. {
  247. return (iterator(m_buffer, m_bufferSize));
  248. }
  249. /*!
  250. * @brief This function reserves memory for the internal raw buffer. It will only do so if the buffer is not yet allocated and is not externally set.
  251. * @param size The size of the memory to be allocated.
  252. * @return True if the allocation suceeded. False if the raw buffer was set externally or is already allocated.
  253. */
  254. bool reserve(size_t size);
  255. /*!
  256. * @brief This function resizes the raw buffer. It will call the user's defined function for this purpose.
  257. * @param minSizeInc The minimun growth expected of the current raw buffer.
  258. * @return True if the operation works. False if it does not.
  259. */
  260. bool resize(size_t minSizeInc);
  261. private:
  262. FastBuffer(const FastBuffer&) = delete;
  263. FastBuffer& operator=(const FastBuffer&) = delete;
  264. //! @brief Pointer to the stream of bytes that contains the serialized data.
  265. char *m_buffer;
  266. //! @brief The total size of the user's buffer.
  267. size_t m_bufferSize;
  268. //! @brief This variable indicates if the managed buffer is internal or is from the user.
  269. bool m_internalBuffer;
  270. };
  271. } //namespace fastcdr
  272. } //namespace eprosima
  273. #endif // _FASTCDR_FASTCDRBUFFER_H_