Skip to content

Commit 343f19c

Browse files
committed
Refs #23504: external (de)serialize_array() for std::array
Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
1 parent f8f8dab commit 343f19c

1 file changed

Lines changed: 112 additions & 9 deletions

File tree

include/fastcdr/Cdr.h

Lines changed: 112 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "CdrEncoding.hpp"
3333
#include "cdr/fixed_size_string.hpp"
34+
#include "detail/container_introspection_helpers.hpp"
3435
#include "detail/container_recursive_inspector.hpp"
3536
#include "exceptions/BadParamException.h"
3637
#include "exceptions/Exception.h"
@@ -56,6 +57,18 @@ extern void serialize(
5657
Cdr&,
5758
const _T&);
5859

60+
template<class _T>
61+
extern void serialize_array(
62+
Cdr&,
63+
const _T*,
64+
const size_t);
65+
66+
template<class _T>
67+
extern void deserialize_array(
68+
Cdr&,
69+
_T*,
70+
const size_t);
71+
5972
template<class _T>
6073
extern void deserialize(
6174
Cdr&,
@@ -733,22 +746,59 @@ class Cdr
733746
* @return Reference to the eprosima::fastcdr::Cdr object.
734747
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
735748
*/
736-
template<class _T, size_t _Size>
749+
template<class _T, size_t _Size,
750+
typename std::enable_if<
751+
static_is_multi_array_primitive<std::array<_T, _Size> const*>::value>::type* = nullptr>
737752
Cdr& serialize(
738753
const std::array<_T, _Size>& array_t)
739754
{
740-
if (!is_multi_array_primitive(&array_t))
755+
serialize_array(array_t.data(), array_t.size());
756+
return *this;
757+
}
758+
759+
/*!
760+
* @brief This function template serializes an array.
761+
* @param array_t The array that will be serialized in the buffer.
762+
* @return Reference to the eprosima::fastcdr::Cdr object.
763+
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
764+
*/
765+
template<class _T, size_t _Size,
766+
typename std::enable_if<
767+
!static_is_multi_array_primitive<std::array<_T, _Size> const*>::value &&
768+
is_complex_array_or_string<std::array<_T, _Size>>::value>::type* = nullptr>
769+
Cdr& serialize(
770+
const std::array<_T, _Size>& array_t)
741771
{
742772
Cdr::state dheader_state {allocate_xcdrv2_dheader()};
743773

744774
serialize_array(array_t.data(), array_t.size());
745775

746776
set_xcdrv2_dheader(dheader_state);
747-
}
748-
else
749-
{
750-
serialize_array(array_t.data(), array_t.size());
751-
}
777+
778+
return *this;
779+
}
780+
781+
/*!
782+
* @brief This function template serializes an array.
783+
* @param array_t The array that will be serialized in the buffer.
784+
* @return Reference to the eprosima::fastcdr::Cdr object.
785+
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
786+
*/
787+
template<class _T, size_t _Size,
788+
typename std::enable_if<
789+
!static_is_multi_array_primitive<std::array<_T, _Size> const*>::value &&
790+
!is_complex_array_or_string<std::array<_T, _Size>>::value>::type* = nullptr>
791+
Cdr& serialize(
792+
const std::array<_T, _Size>& array_t)
793+
{
794+
Cdr::state dheader_state {allocate_xcdrv2_dheader()};
795+
796+
eprosima::fastcdr::serialize_array(*this, array_t.data(), array_t.size());
797+
798+
set_xcdrv2_dheader(dheader_state);
799+
800+
return *this;
801+
}
752802

753803
return *this;
754804
}
@@ -1803,11 +1853,29 @@ class Cdr
18031853
* @return Reference to the eprosima::fastcdr::Cdr object.
18041854
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
18051855
*/
1806-
template<class _T, size_t _Size>
1856+
template<class _T, size_t _Size,
1857+
typename std::enable_if<
1858+
static_is_multi_array_primitive<std::array<_T, _Size> const*>::value>::type* = nullptr>
18071859
Cdr& deserialize(
18081860
std::array<_T, _Size>& array_t)
18091861
{
1810-
if (CdrVersion::XCDRv2 == cdr_version_ && !is_multi_array_primitive(&array_t))
1862+
return deserialize_array(array_t.data(), array_t.size());
1863+
}
1864+
1865+
/*!
1866+
* @brief This function template deserializes an array.
1867+
* @param array_t The variable that will store the array read from the buffer.
1868+
* @return Reference to the eprosima::fastcdr::Cdr object.
1869+
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1870+
*/
1871+
template<class _T, size_t _Size,
1872+
typename std::enable_if<
1873+
!static_is_multi_array_primitive<std::array<_T, _Size> const*>::value &&
1874+
is_complex_array_or_string<std::array<_T, _Size>>::value>::type* = nullptr>
1875+
Cdr& deserialize(
1876+
std::array<_T, _Size>& array_t)
1877+
{
1878+
if (CdrVersion::XCDRv2 == cdr_version_)
18111879
{
18121880
uint32_t dheader {0};
18131881
deserialize(dheader);
@@ -1833,6 +1901,41 @@ class Cdr
18331901
return *this;
18341902
}
18351903

1904+
/*!
1905+
* @brief This function template deserializes an array.
1906+
* @param array_t The variable that will store the array read from the buffer.
1907+
* @return Reference to the eprosima::fastcdr::Cdr object.
1908+
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1909+
*/
1910+
template<class _T, size_t _Size,
1911+
typename std::enable_if<
1912+
!static_is_multi_array_primitive<std::array<_T, _Size> const*>::value &&
1913+
!is_complex_array_or_string<std::array<_T, _Size>>::value>::type* = nullptr>
1914+
Cdr& deserialize(
1915+
std::array<_T, _Size>& array_t)
1916+
{
1917+
if (CdrVersion::XCDRv2 == cdr_version_)
1918+
{
1919+
uint32_t dheader {0};
1920+
deserialize(dheader);
1921+
1922+
auto offset = offset_;
1923+
1924+
eprosima::fastcdr::deserialize_array(*this, array_t.data(), _Size);
1925+
1926+
if (offset_ - offset != dheader)
1927+
{
1928+
throw exception::BadParamException("Member size greater than size specified by DHEADER");
1929+
}
1930+
}
1931+
else
1932+
{
1933+
eprosima::fastcdr::deserialize_array(*this, array_t.data(), _Size);
1934+
}
1935+
1936+
return *this;
1937+
}
1938+
18361939
/*!
18371940
* @brief This function template deserializes a sequence of non-primitive.
18381941
* @param vector_t The variable that will store the sequence read from the buffer.

0 commit comments

Comments
 (0)