Program Listing for File FastCdr.h
↰ Return to documentation for file (include/fastcdr/FastCdr.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_FASTCDR_H_
#define _FASTCDR_FASTCDR_H_
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
#if !__APPLE__ && !__FreeBSD__ && !__VXWORKS__
#include <malloc.h>
#else
#include <stdlib.h>
#endif // if !__APPLE__ && !__FreeBSD__ && !__VXWORKS__
#include "fastcdr_dll.h"
#include "FastBuffer.h"
#include "exceptions/NotEnoughMemoryException.h"
#include "exceptions/BadParamException.h"
namespace eprosima {
namespace fastcdr {
class Cdr_DllAPI FastCdr
{
public:
class Cdr_DllAPI state
{
friend class FastCdr;
public:
state(
const FastCdr& fastcdr);
state(
const state&);
private:
state& operator =(
const state&) = delete;
const FastBuffer::iterator current_position_;
};
FastCdr(
FastBuffer& cdr_buffer);
bool jump(
size_t num_bytes);
void reset();
char* get_current_position();
inline size_t get_serialized_data_length() const
{
return current_position_ - cdr_buffer_.begin();
}
FastCdr::state get_state();
void set_state(
FastCdr::state& state);
inline FastCdr& operator <<(
const uint8_t octet_t)
{
return serialize(octet_t);
}
inline FastCdr& operator <<(
const char char_t)
{
return serialize(char_t);
}
inline FastCdr& operator <<(
const int8_t int8)
{
return serialize(int8);
}
inline FastCdr& operator <<(
const uint16_t ushort_t)
{
return serialize(ushort_t);
}
inline FastCdr& operator <<(
const int16_t short_t)
{
return serialize(short_t);
}
inline FastCdr& operator <<(
const uint32_t ulong_t)
{
return serialize(ulong_t);
}
inline FastCdr& operator <<(
const int32_t long_t)
{
return serialize(long_t);
}
inline FastCdr& operator <<(
const wchar_t wchar)
{
return serialize(wchar);
}
inline FastCdr& operator <<(
const uint64_t ulonglong_t)
{
return serialize(ulonglong_t);
}
inline FastCdr& operator <<(
const int64_t longlong_t)
{
return serialize(longlong_t);
}
inline FastCdr& operator <<(
const float float_t)
{
return serialize(float_t);
}
inline FastCdr& operator <<(
const double double_t)
{
return serialize(double_t);
}
inline FastCdr& operator <<(
const long double ldouble_t)
{
return serialize(ldouble_t);
}
inline FastCdr& operator <<(
const bool bool_t)
{
return serialize(bool_t);
}
inline FastCdr& operator <<(
const char* string_t)
{
return serialize(string_t);
}
inline FastCdr& operator <<(
const wchar_t* string_t)
{
return serialize(string_t);
}
inline FastCdr& operator <<(
const std::string& string_t)
{
return serialize(string_t);
}
inline FastCdr& operator <<(
const std::wstring& string_t)
{
return serialize(string_t);
}
template<class _T, size_t _Size>
inline FastCdr& operator <<(
const std::array<_T, _Size>& array_t)
{
return serialize<_T, _Size>(array_t);
}
template<class _T>
inline FastCdr& operator <<(
const std::vector<_T>& vector_t)
{
return serialize<_T>(vector_t);
}
template<class _T>
inline FastCdr& operator <<(
const _T& type_t)
{
type_t.serialize(*this);
return *this;
}
inline FastCdr& operator >>(
uint8_t& octet_t)
{
return deserialize(octet_t);
}
inline FastCdr& operator >>(
char& char_t)
{
return deserialize(char_t);
}
inline FastCdr& operator >>(
int8_t& int8)
{
return deserialize(int8);
}
inline FastCdr& operator >>(
uint16_t& ushort_t)
{
return deserialize(ushort_t);
}
inline FastCdr& operator >>(
int16_t& short_t)
{
return deserialize(short_t);
}
inline FastCdr& operator >>(
uint32_t& ulong_t)
{
return deserialize(ulong_t);
}
inline FastCdr& operator >>(
int32_t& long_t)
{
return deserialize(long_t);
}
inline FastCdr& operator >>(
wchar_t& wchar)
{
return deserialize(wchar);
}
inline FastCdr& operator >>(
uint64_t& ulonglong_t)
{
return deserialize(ulonglong_t);
}
inline FastCdr& operator >>(
int64_t& longlong_t)
{
return deserialize(longlong_t);
}
inline FastCdr& operator >>(
float& float_t)
{
return deserialize(float_t);
}
inline FastCdr& operator >>(
double& double_t)
{
return deserialize(double_t);
}
inline FastCdr& operator >>(
long double& ldouble_t)
{
return deserialize(ldouble_t);
}
inline FastCdr& operator >>(
bool& bool_t)
{
return deserialize(bool_t);
}
inline FastCdr& operator >>(
char*& string_t)
{
return deserialize(string_t);
}
inline FastCdr& operator >>(
std::string& string_t)
{
return deserialize(string_t);
}
inline FastCdr& operator >>(
std::wstring& string_t)
{
return deserialize(string_t);
}
template<class _T, size_t _Size>
inline FastCdr& operator >>(
std::array<_T, _Size>& array_t)
{
return deserialize<_T, _Size>(array_t);
}
template<class _T>
inline FastCdr& operator >>(
std::vector<_T>& vector_t)
{
return deserialize<_T>(vector_t);
}
template<class _T>
inline FastCdr& operator >>(
_T& type_t)
{
type_t.deserialize(*this);
return *this;
}
inline
FastCdr& serialize(
const uint8_t octet_t)
{
return serialize(static_cast<char>(octet_t));
}
inline
FastCdr& serialize(
const char char_t)
{
if (((last_position_ - current_position_) >= sizeof(char_t)) || resize(sizeof(char_t)))
{
current_position_++ << char_t;
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& serialize(
const int8_t int8)
{
return serialize(static_cast<char>(int8));
}
inline
FastCdr& serialize(
const uint16_t ushort_t)
{
return serialize(static_cast<int16_t>(ushort_t));
}
inline
FastCdr& serialize(
const int16_t short_t)
{
if (((last_position_ - current_position_) >= sizeof(short_t)) || resize(sizeof(short_t)))
{
current_position_ << short_t;
current_position_ += sizeof(short_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& serialize(
const uint32_t ulong_t)
{
return serialize(static_cast<int32_t>(ulong_t));
}
inline
FastCdr& serialize(
const int32_t long_t)
{
if (((last_position_ - current_position_) >= sizeof(long_t)) || resize(sizeof(long_t)))
{
current_position_ << long_t;
current_position_ += sizeof(long_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& serialize(
const wchar_t wchar)
{
return serialize(static_cast<uint32_t>(wchar));
}
inline
FastCdr& serialize(
const uint64_t ulonglong_t)
{
return serialize(static_cast<int64_t>(ulonglong_t));
}
inline
FastCdr& serialize(
const int64_t longlong_t)
{
if (((last_position_ - current_position_) >= sizeof(longlong_t)) || resize(sizeof(longlong_t)))
{
current_position_ << longlong_t;
current_position_ += sizeof(longlong_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& serialize(
const float float_t)
{
if (((last_position_ - current_position_) >= sizeof(float_t)) || resize(sizeof(float_t)))
{
current_position_ << float_t;
current_position_ += sizeof(float_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& serialize(
const double double_t)
{
if (((last_position_ - current_position_) >= sizeof(double_t)) || resize(sizeof(double_t)))
{
current_position_ << double_t;
current_position_ += sizeof(double_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& serialize(
const long double ldouble_t)
{
if (((last_position_ - current_position_) >= sizeof(ldouble_t)) || resize(sizeof(ldouble_t)))
{
current_position_ << ldouble_t;
#if defined(_WIN32)
current_position_ += sizeof(ldouble_t);
current_position_ << static_cast<long double>(0);
#endif // if defined(_WIN32)
current_position_ += sizeof(ldouble_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
FastCdr& serialize(
const bool bool_t);
FastCdr& serialize(
const char* string_t);
FastCdr& serialize(
const wchar_t* string_t);
inline
FastCdr& serialize(
const std::string& string_t)
{
// Check there are no null characters in the string.
const char* c_str = string_t.c_str();
const auto str_len = strlen(c_str);
if (string_t.size() > str_len)
{
throw exception::BadParamException("The string contains null characters");
}
return serialize(c_str);
}
inline
FastCdr& serialize(
const std::wstring& string_t)
{
return serialize(string_t.c_str());
}
template<class _T, size_t _Size>
inline FastCdr& serialize(
const std::array<_T, _Size>& array_t)
{
return serialize_array(array_t.data(), array_t.size());
}
template<class _T = bool>
FastCdr& serialize(
const std::vector<bool>& vector_t)
{
return serialize_bool_sequence(vector_t);
}
template<class _T>
FastCdr& serialize(
const std::vector<_T>& vector_t)
{
state state_before_error(*this);
*this << static_cast<int32_t>(vector_t.size());
try
{
return serialize_array(vector_t.data(), vector_t.size());
}
catch (eprosima::fastcdr::exception::Exception& ex)
{
set_state(state_before_error);
ex.raise();
}
return *this;
}
#ifdef _MSC_VER
template<>
FastCdr& serialize<bool>(
const std::vector<bool>& vector_t)
{
return serialize_bool_sequence(vector_t);
}
#endif // ifdef _MSC_VER
template<class _T>
inline FastCdr& serialize(
const _T& type_t)
{
type_t.serialize(*this);
return *this;
}
inline
FastCdr& serialize_array(
const uint8_t* octet_t,
size_t num_elements)
{
return serialize_array(reinterpret_cast<const char*>(octet_t), num_elements);
}
FastCdr& serialize_array(
const char* char_t,
size_t num_elements);
inline
FastCdr& serialize_array(
const int8_t* int8,
size_t num_elements)
{
return serialize_array(reinterpret_cast<const char*>(int8), num_elements);
}
inline
FastCdr& serialize_array(
const uint16_t* ushort_t,
size_t num_elements)
{
return serialize_array(reinterpret_cast<const int16_t*>(ushort_t), num_elements);
}
FastCdr& serialize_array(
const int16_t* short_t,
size_t num_elements);
inline
FastCdr& serialize_array(
const uint32_t* ulong_t,
size_t num_elements)
{
return serialize_array(reinterpret_cast<const int32_t*>(ulong_t), num_elements);
}
FastCdr& serialize_array(
const int32_t* long_t,
size_t num_elements);
FastCdr& serialize_array(
const wchar_t* wchar,
size_t num_elements);
inline
FastCdr& serialize_array(
const uint64_t* ulonglong_t,
size_t num_elements)
{
return serialize_array(reinterpret_cast<const int64_t*>(ulonglong_t), num_elements);
}
FastCdr& serialize_array(
const int64_t* longlong_t,
size_t num_elements);
FastCdr& serialize_array(
const float* float_t,
size_t num_elements);
FastCdr& serialize_array(
const double* double_t,
size_t num_elements);
FastCdr& serialize_array(
const long double* ldouble_t,
size_t num_elements);
FastCdr& serialize_array(
const bool* bool_t,
size_t num_elements);
inline
FastCdr& serialize_array(
const std::string* string_t,
size_t num_elements)
{
for (size_t count = 0; count < num_elements; ++count)
{
serialize(string_t[count].c_str());
}
return *this;
}
inline
FastCdr& serialize_array(
const std::wstring* string_t,
size_t num_elements)
{
for (size_t count = 0; count < num_elements; ++count)
{
serialize(string_t[count].c_str());
}
return *this;
}
template<class _T>
FastCdr& serialize_array(
const std::vector<_T>* vector_t,
size_t num_elements)
{
for (size_t count = 0; count < num_elements; ++count)
{
serialize(vector_t[count]);
}
return *this;
}
template<class _T>
FastCdr& serialize_array(
const _T* type_t,
size_t num_elements)
{
for (size_t count = 0; count < num_elements; ++count)
{
type_t[count].serialize(*this);
}
return *this;
}
template<class _T>
FastCdr& serialize_sequence(
const _T* sequence_t,
size_t num_elements)
{
state state_before_error(*this);
serialize(static_cast<int32_t>(num_elements));
try
{
return serialize_array(sequence_t, num_elements);
}
catch (eprosima::fastcdr::exception::Exception& ex)
{
set_state(state_before_error);
ex.raise();
}
return *this;
}
inline
FastCdr& deserialize(
uint8_t& octet_t)
{
return deserialize(reinterpret_cast<char&>(octet_t));
}
inline
FastCdr& deserialize(
char& char_t)
{
if ((last_position_ - current_position_) >= sizeof(char_t))
{
current_position_++ >> char_t;
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& deserialize(
int8_t& int8)
{
return deserialize(reinterpret_cast<char&>(int8));
}
inline
FastCdr& deserialize(
uint16_t& ushort_t)
{
return deserialize(reinterpret_cast<int16_t&>(ushort_t));
}
inline
FastCdr& deserialize(
int16_t& short_t)
{
if ((last_position_ - current_position_) >= sizeof(short_t))
{
current_position_ >> short_t;
current_position_ += sizeof(short_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& deserialize(
uint32_t& ulong_t)
{
return deserialize(reinterpret_cast<int32_t&>(ulong_t));
}
inline
FastCdr& deserialize(
int32_t& long_t)
{
if ((last_position_ - current_position_) >= sizeof(long_t))
{
current_position_ >> long_t;
current_position_ += sizeof(long_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& deserialize(
wchar_t& wchar)
{
uint32_t ret;
deserialize(ret);
wchar = static_cast<wchar_t>(ret);
return *this;
}
inline
FastCdr& deserialize(
uint64_t& ulonglong_t)
{
return deserialize(reinterpret_cast<int64_t&>(ulonglong_t));
}
inline
FastCdr& deserialize(
int64_t& longlong_t)
{
if ((last_position_ - current_position_) >= sizeof(longlong_t))
{
current_position_ >> longlong_t;
current_position_ += sizeof(longlong_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& deserialize(
float& float_t)
{
if ((last_position_ - current_position_) >= sizeof(float_t))
{
current_position_ >> float_t;
current_position_ += sizeof(float_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& deserialize(
double& double_t)
{
if ((last_position_ - current_position_) >= sizeof(double_t))
{
current_position_ >> double_t;
current_position_ += sizeof(double_t);
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
inline
FastCdr& deserialize(
long double& ldouble_t)
{
if ((last_position_ - current_position_) >= sizeof(ldouble_t))
{
current_position_ >> ldouble_t;
current_position_ += sizeof(ldouble_t);
#if defined(_WIN32)
current_position_ += sizeof(ldouble_t);
#endif // if defined(_WIN32)
return *this;
}
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}
FastCdr& deserialize(
bool& bool_t);
FastCdr& deserialize(
char*& string_t);
FastCdr& deserialize(
wchar_t*& string_t);
inline
FastCdr& deserialize(
std::string& string_t)
{
uint32_t length = 0;
const char* str = read_string(length);
string_t.assign(str, length);
return *this;
}
inline
FastCdr& deserialize(
std::wstring& string_t)
{
uint32_t length = 0;
string_t = read_wstring(length);
return *this;
}
template<class _T, size_t _Size>
inline FastCdr& deserialize(
std::array<_T, _Size>& array_t)
{
return deserialize_array(array_t.data(), array_t.size());
}
template<class _T = bool>
FastCdr& deserialize(
std::vector<bool>& vector_t)
{
return deserialize_bool_sequence(vector_t);
}
template<class _T>
FastCdr& deserialize(
std::vector<_T>& vector_t)
{
uint32_t sequence_length = 0;
state state_before_error(*this);
*this >> sequence_length;
try
{
vector_t.resize(sequence_length);
return deserialize_array(vector_t.data(), vector_t.size());
}
catch (eprosima::fastcdr::exception::Exception& ex)
{
set_state(state_before_error);
ex.raise();
}
return *this;
}
#ifdef _MSC_VER
template<>
FastCdr& deserialize<bool>(
std::vector<bool>& vector_t)
{
return deserialize_bool_sequence(vector_t);
}
#endif // ifdef _MSC_VER
template<class _T>
inline FastCdr& deserialize(
_T& type_t)
{
type_t.deserialize(*this);
return *this;
}
inline
FastCdr& deserialize_array(
uint8_t* octet_t,
size_t num_elements)
{
return deserialize_array(reinterpret_cast<char*>(octet_t), num_elements);
}
FastCdr& deserialize_array(
char* char_t,
size_t num_elements);
inline
FastCdr& deserialize_array(
int8_t* int8,
size_t num_elements)
{
return deserialize_array(reinterpret_cast<char*>(int8), num_elements);
}
inline
FastCdr& deserialize_array(
uint16_t* ushort_t,
size_t num_elements)
{
return deserialize_array(reinterpret_cast<int16_t*>(ushort_t), num_elements);
}
FastCdr& deserialize_array(
int16_t* short_t,
size_t num_elements);
inline
FastCdr& deserialize_array(
uint32_t* ulong_t,
size_t num_elements)
{
return deserialize_array(reinterpret_cast<int32_t*>(ulong_t), num_elements);
}
FastCdr& deserialize_array(
int32_t* long_t,
size_t num_elements);
FastCdr& deserialize_array(
wchar_t* wchar,
size_t num_elements);
inline
FastCdr& deserialize_array(
uint64_t* ulonglong_t,
size_t num_elements)
{
return deserialize_array(reinterpret_cast<int64_t*>(ulonglong_t), num_elements);
}
FastCdr& deserialize_array(
int64_t* longlong_t,
size_t num_elements);
FastCdr& deserialize_array(
float* float_t,
size_t num_elements);
FastCdr& deserialize_array(
double* double_t,
size_t num_elements);
FastCdr& deserialize_array(
long double* ldouble_t,
size_t num_elements);
FastCdr& deserialize_array(
bool* bool_t,
size_t num_elements);
inline
FastCdr& deserialize_array(
std::string* string_t,
size_t num_elements)
{
for (size_t count = 0; count < num_elements; ++count)
{
deserialize(string_t[count]);
}
return *this;
}
inline
FastCdr& deserialize_array(
std::wstring* string_t,
size_t num_elements)
{
for (size_t count = 0; count < num_elements; ++count)
{
deserialize(string_t[count]);
}
return *this;
}
template<class _T>
FastCdr& deserialize_array(
std::vector<_T>* vector_t,
size_t num_elements)
{
for (size_t count = 0; count < num_elements; ++count)
{
deserialize(vector_t[count]);
}
return *this;
}
template<class _T>
FastCdr& deserialize_array(
_T* type_t,
size_t num_elements)
{
for (size_t count = 0; count < num_elements; ++count)
{
type_t[count].deserialize(*this);
}
return *this;
}
template<class _T = std::string>
FastCdr& deserialize_sequence(
std::string*& sequence_t,
size_t& num_elements)
{
return deserialize_string_sequence(sequence_t, num_elements);
}
template<class _T = std::wstring>
FastCdr& deserialize_sequence(
std::wstring*& sequence_t,
size_t& num_elements)
{
return deserialize_wstring_sequence(sequence_t, num_elements);
}
template<class _T>
FastCdr& deserialize_sequence(
_T*& sequence_t,
size_t& num_elements)
{
uint32_t sequence_length = 0;
state state_before_error(*this);
deserialize(sequence_length);
try
{
sequence_t = reinterpret_cast<_T*>(calloc(sequence_length, sizeof(_T)));
deserialize_array(sequence_t, sequence_length);
}
catch (eprosima::fastcdr::exception::Exception& ex)
{
free(sequence_t);
sequence_t = NULL;
set_state(state_before_error);
ex.raise();
}
num_elements = sequence_length;
return *this;
}
#ifdef _MSC_VER
template<>
FastCdr& deserialize_sequence<std::string>(
std::string*& sequence_t,
size_t& num_elements)
{
return deserialize_string_sequence(sequence_t, num_elements);
}
template<>
FastCdr& deserialize_sequence<std::wstring>(
std::wstring*& sequence_t,
size_t& num_elements)
{
return deserialize_wstring_sequence(sequence_t, num_elements);
}
#endif // ifdef _MSC_VER
private:
FastCdr(
const FastCdr&) = delete;
FastCdr& operator =(
const FastCdr&) = delete;
FastCdr& serialize_bool_sequence(
const std::vector<bool>& vector_t);
FastCdr& deserialize_bool_sequence(
std::vector<bool>& vector_t);
FastCdr& deserialize_string_sequence(
std::string*& sequence_t,
size_t& num_elements);
FastCdr& deserialize_wstring_sequence(
std::wstring*& sequence_t,
size_t& num_elements);
template<class _T, size_t _Size>
FastCdr& serialize_array(
const std::array<_T, _Size>* array_t,
size_t num_elements)
{
return serialize_array(array_t->data(), num_elements * array_t->size());
}
template<class _T, size_t _Size>
FastCdr& deserialize_array(
std::array<_T, _Size>* array_t,
size_t num_elements)
{
return deserialize_array(array_t->data(), num_elements * array_t->size());
}
bool resize(
size_t min_size_inc);
const char* read_string(
uint32_t& length);
std::wstring read_wstring(
uint32_t& length);
FastBuffer& cdr_buffer_;
FastBuffer::iterator current_position_;
FastBuffer::iterator last_position_;
};
} //namespace fastcdr
} //namespace eprosima
#endif //_FASTCDR_FASTCDR_H_