123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- #ifndef _FASTCDR_CDRBUFFER_H_
- #define _FASTCDR_CDRBUFFER_H_
- #include "fastcdr_dll.h"
- #include <stdint.h>
- #include <cstdio>
- #include <string.h>
- #include <cstddef>
- #include <utility>
- inline uint32_t size_to_uint32(size_t val) {
- #if defined(_WIN32) || !defined(FASTCDR_ARM32)
-
- return static_cast<uint32_t>(val);
- #else
-
- return val;
- #endif
- }
- namespace eprosima
- {
- namespace fastcdr
- {
-
- class Cdr_DllAPI _FastBuffer_iterator
- {
- public:
-
- _FastBuffer_iterator() : m_buffer(NULL), m_currentPosition(NULL) {}
-
- explicit _FastBuffer_iterator(char *buffer, size_t index) : m_buffer(buffer), m_currentPosition(&m_buffer[index]){}
-
- inline
- void operator<<(const _FastBuffer_iterator &iterator)
- {
- ptrdiff_t diff = m_currentPosition - m_buffer;
- m_buffer = iterator.m_buffer;
- m_currentPosition = m_buffer + diff;
- }
-
- inline
- void operator>>(const _FastBuffer_iterator &iterator)
- {
- ptrdiff_t diff = iterator.m_currentPosition - iterator.m_buffer;
- m_currentPosition = m_buffer + diff;
- }
-
- template<typename _T>
- inline
- void operator<<(const _T &data)
- {
- memcpy(m_currentPosition, &data, sizeof(_T));
- }
-
- template<typename _T>
- inline
- void operator>>(_T &data)
- {
- memcpy(&data, m_currentPosition, sizeof(_T));
- }
-
- inline
- void memcopy(const void* src, const size_t size)
- {
- if (size > 0) {
- memcpy(m_currentPosition, src, size);
- }
- }
-
- inline
- void rmemcopy(void* dst, const size_t size)
- {
- if (size > 0) {
- memcpy(dst, m_currentPosition, size);
- }
- }
-
- inline
- void operator+=(size_t numBytes)
- {
- m_currentPosition += numBytes;
- }
-
- inline
- size_t operator-(const _FastBuffer_iterator &it) const
- {
- return static_cast<size_t>(m_currentPosition - it.m_currentPosition);
- }
-
- inline
- _FastBuffer_iterator operator++()
- {
- ++m_currentPosition;
- return *this;
- }
-
- inline
- _FastBuffer_iterator operator++(int)
- {
- _FastBuffer_iterator tmp = *this;
- ++*this;
- return tmp;
- }
-
- inline
- char* operator&()
- {
- return m_currentPosition;
- }
- private:
-
- char *m_buffer;
-
- char *m_currentPosition;
- };
-
- class Cdr_DllAPI FastBuffer
- {
- public:
- typedef _FastBuffer_iterator iterator;
-
- FastBuffer();
-
- FastBuffer(char* const buffer, const size_t bufferSize);
-
- FastBuffer(FastBuffer&& fbuffer) : m_buffer(nullptr), m_bufferSize(0), m_internalBuffer(true)
- {
- std::swap(m_buffer, fbuffer.m_buffer);
- std::swap(m_bufferSize, fbuffer.m_bufferSize);
- std::swap(m_internalBuffer, fbuffer.m_internalBuffer);
- }
-
- FastBuffer& operator=(FastBuffer&& fbuffer)
- {
- std::swap(m_buffer, fbuffer.m_buffer);
- std::swap(m_bufferSize, fbuffer.m_bufferSize);
- std::swap(m_internalBuffer, fbuffer.m_internalBuffer);
- return *this;
- }
-
- virtual ~FastBuffer();
-
- inline char* getBuffer() const { return m_buffer;}
-
- inline size_t getBufferSize() const { return m_bufferSize;}
-
- inline
- iterator begin()
- {
- return (iterator(m_buffer, 0));
- }
-
- inline
- iterator end()
- {
- return (iterator(m_buffer, m_bufferSize));
- }
-
- bool reserve(size_t size);
-
- bool resize(size_t minSizeInc);
- private:
- FastBuffer(const FastBuffer&) = delete;
- FastBuffer& operator=(const FastBuffer&) = delete;
-
- char *m_buffer;
-
- size_t m_bufferSize;
-
- bool m_internalBuffer;
- };
- }
- }
- #endif
|