Program Listing for File FastBuffer.h
↰ Return to documentation for file (include/fastcdr/FastBuffer.h)
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#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)
// On 64 bit platforms and all Windows architectures (because of C4267), explicitly cast.
return static_cast<uint32_t>(val);
#else
// Skip useless cast on 32-bit builds.
return val;
#endif // if defined(_WIN32) || !defined(FASTCDR_ARM32)
}
namespace eprosima {
namespace fastcdr {
class Cdr_DllAPI _FastBuffer_iterator
{
public:
_FastBuffer_iterator() = default;
explicit _FastBuffer_iterator(
char* buffer,
size_t index)
: buffer_(buffer)
, current_position_(&buffer_[index])
{
}
inline
void operator <<(
const _FastBuffer_iterator& iterator)
{
ptrdiff_t diff = current_position_ - buffer_;
buffer_ = iterator.buffer_;
current_position_ = buffer_ + diff;
}
inline
void operator >>(
const _FastBuffer_iterator& iterator)
{
ptrdiff_t diff = iterator.current_position_ - iterator.buffer_;
current_position_ = buffer_ + diff;
}
template<typename _T>
inline
void operator <<(
const _T& data)
{
memcpy(current_position_, &data, sizeof(_T));
}
template<typename _T>
inline
void operator >>(
_T& data)
{
memcpy(&data, current_position_, sizeof(_T));
}
inline
void memcopy(
const void* src,
const size_t size)
{
if (size > 0)
{
memcpy(current_position_, src, size);
}
}
inline
void rmemcopy(
void* dst,
const size_t size)
{
if (size > 0)
{
memcpy(dst, current_position_, size);
}
}
inline
void operator +=(
size_t num_bytes)
{
current_position_ += num_bytes;
}
inline
void operator -=(
size_t num_bytes)
{
current_position_ -= num_bytes;
}
inline
size_t operator -(
const _FastBuffer_iterator& it) const
{
return static_cast<size_t>(current_position_ - it.current_position_);
}
inline
_FastBuffer_iterator operator ++()
{
++current_position_;
return *this;
}
inline
_FastBuffer_iterator operator ++(
int)
{
_FastBuffer_iterator tmp = *this;
++*this;
return tmp;
}
inline
char* operator &()
{
return current_position_;
}
bool operator ==(
const _FastBuffer_iterator& other_iterator) const
{
return other_iterator.current_position_ == current_position_;
}
bool operator !=(
const _FastBuffer_iterator& other_iterator) const
{
return !(other_iterator == *this);
}
private:
char* buffer_ {nullptr};
char* current_position_ {nullptr};
};
class Cdr_DllAPI FastBuffer
{
public:
typedef _FastBuffer_iterator iterator;
FastBuffer() = default;
FastBuffer(
char* const buffer,
const size_t bufferSize);
FastBuffer(
FastBuffer&& fbuffer)
: buffer_(nullptr)
, size_(0)
, m_internalBuffer(true)
{
std::swap(buffer_, fbuffer.buffer_);
std::swap(size_, fbuffer.size_);
std::swap(m_internalBuffer, fbuffer.m_internalBuffer);
}
FastBuffer& operator =(
FastBuffer&& fbuffer)
{
std::swap(buffer_, fbuffer.buffer_);
std::swap(size_, fbuffer.size_);
std::swap(m_internalBuffer, fbuffer.m_internalBuffer);
return *this;
}
virtual ~FastBuffer();
inline char* getBuffer() const
{
return buffer_;
}
inline size_t getBufferSize() const
{
return size_;
}
inline
iterator begin()
{
return (iterator(buffer_, 0));
}
inline
iterator end()
{
return (iterator(buffer_, size_));
}
bool reserve(
size_t size);
bool resize(
size_t min_size_inc);
private:
FastBuffer(
const FastBuffer&) = delete;
FastBuffer& operator =(
const FastBuffer&) = delete;
char* buffer_ { nullptr };
size_t size_ { 0 };
bool m_internalBuffer { true };
};
} //namespace fastcdr
} //namespace eprosima
#endif // _FASTCDR_FASTCDRBUFFER_H_