Skip to content

Commit b9baed5

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

File tree

1 file changed

+124
-12
lines changed

1 file changed

+124
-12
lines changed

include/fastcdr/Cdr.h

Lines changed: 124 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,12 @@ class Cdr
768768
is_complex_array_or_string<std::array<_T, _Size>>::value>::type* = nullptr>
769769
Cdr& serialize(
770770
const std::array<_T, _Size>& array_t)
771-
{
772-
Cdr::state dheader_state {allocate_xcdrv2_dheader()};
771+
{
772+
Cdr::state dheader_state {allocate_xcdrv2_dheader()};
773773

774-
serialize_array(array_t.data(), array_t.size());
774+
serialize_array(array_t.data(), array_t.size());
775775

776-
set_xcdrv2_dheader(dheader_state);
776+
set_xcdrv2_dheader(dheader_state);
777777

778778
return *this;
779779
}
@@ -800,6 +800,35 @@ class Cdr
800800
return *this;
801801
}
802802

803+
/*!
804+
* @brief This function template serializes a sequence of non-primitive.
805+
* @param vector_t The sequence that will be serialized in the buffer.
806+
* @return Reference to the eprosima::fastcdr::Cdr object.
807+
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
808+
*/
809+
template<class _T, typename std::enable_if<
810+
!std::is_enum<_T>::value &&
811+
!std::is_arithmetic<_T>::value &&
812+
!is_complex_array_or_string<std::vector<_T>>::value>::type* = nullptr>
813+
Cdr& serialize(
814+
const std::vector<_T>& vector_t)
815+
{
816+
Cdr::state dheader_state {allocate_xcdrv2_dheader()};
817+
818+
serialize(static_cast<int32_t>(vector_t.size()));
819+
820+
try
821+
{
822+
eprosima::fastcdr::serialize_array(*this, vector_t.data(), vector_t.size());
823+
}
824+
catch (exception::Exception& ex)
825+
{
826+
set_state(dheader_state);
827+
ex.raise();
828+
}
829+
830+
set_xcdrv2_dheader(dheader_state);
831+
803832
return *this;
804833
}
805834

@@ -809,8 +838,10 @@ class Cdr
809838
* @return Reference to the eprosima::fastcdr::Cdr object.
810839
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
811840
*/
812-
template<class _T, typename std::enable_if<!std::is_enum<_T>::value &&
813-
!std::is_arithmetic<_T>::value>::type* = nullptr>
841+
template<class _T, typename std::enable_if<
842+
!std::is_enum<_T>::value &&
843+
!std::is_arithmetic<_T>::value &&
844+
is_complex_array_or_string<std::vector<_T>>::value>::type* = nullptr>
814845
Cdr& serialize(
815846
const std::vector<_T>& vector_t)
816847
{
@@ -839,8 +870,10 @@ class Cdr
839870
* @return Reference to the eprosima::fastcdr::Cdr object.
840871
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
841872
*/
842-
template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
843-
std::is_arithmetic<_T>::value>::type* = nullptr>
873+
template<class _T, typename std::enable_if<
874+
(std::is_enum<_T>::value ||
875+
std::is_arithmetic<_T>::value) &&
876+
!is_complex_array_or_string<std::vector<_T>>::value>::type* = nullptr>
844877
Cdr& serialize(
845878
const std::vector<_T>& vector_t)
846879
{
@@ -1942,8 +1975,85 @@ class Cdr
19421975
* @return Reference to the eprosima::fastcdr::Cdr object.
19431976
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
19441977
*/
1945-
template<class _T, typename std::enable_if<!std::is_enum<_T>::value &&
1946-
!std::is_arithmetic<_T>::value>::type* = nullptr>
1978+
template<class _T, typename std::enable_if<
1979+
!std::is_enum<_T>::value &&
1980+
!std::is_arithmetic<_T>::value &&
1981+
!is_complex_array_or_string<std::vector<_T>>::value>::type* = nullptr>
1982+
Cdr& deserialize(
1983+
std::vector<_T>& vector_t)
1984+
{
1985+
uint32_t sequence_length {0};
1986+
1987+
if (CdrVersion::XCDRv2 == cdr_version_)
1988+
{
1989+
uint32_t dheader {0};
1990+
deserialize(dheader);
1991+
1992+
auto offset = offset_;
1993+
1994+
deserialize(sequence_length);
1995+
1996+
if (0 == sequence_length)
1997+
{
1998+
vector_t.clear();
1999+
return *this;
2000+
}
2001+
else
2002+
{
2003+
vector_t.resize(sequence_length);
2004+
}
2005+
2006+
eprosima::fastcdr::deserialize_array(*this, vector_t.data(), vector_t.size());
2007+
2008+
if (offset_ - offset != dheader)
2009+
{
2010+
throw exception::BadParamException("Member size differs from the size specified by DHEADER");
2011+
}
2012+
}
2013+
else
2014+
{
2015+
state state_before_error(*this);
2016+
2017+
deserialize(sequence_length);
2018+
2019+
if (sequence_length == 0)
2020+
{
2021+
vector_t.clear();
2022+
return *this;
2023+
}
2024+
2025+
if ((end_ - offset_) < sequence_length)
2026+
{
2027+
set_state(state_before_error);
2028+
throw exception::NotEnoughMemoryException(
2029+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
2030+
}
2031+
2032+
try
2033+
{
2034+
vector_t.resize(sequence_length);
2035+
eprosima::fastcdr::deserialize_array(*this, vector_t.data(), vector_t.size());
2036+
}
2037+
catch (exception::Exception& ex)
2038+
{
2039+
set_state(state_before_error);
2040+
ex.raise();
2041+
}
2042+
}
2043+
2044+
return *this;
2045+
}
2046+
2047+
/*!
2048+
* @brief This function template deserializes a sequence of non-primitive.
2049+
* @param vector_t The variable that will store the sequence read from the buffer.
2050+
* @return Reference to the eprosima::fastcdr::Cdr object.
2051+
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2052+
*/
2053+
template<class _T, typename std::enable_if<
2054+
!std::is_enum<_T>::value &&
2055+
!std::is_arithmetic<_T>::value &&
2056+
is_complex_array_or_string<std::vector<_T>>::value>::type* = nullptr>
19472057
Cdr& deserialize(
19482058
std::vector<_T>& vector_t)
19492059
{
@@ -2020,8 +2130,10 @@ class Cdr
20202130
* @return Reference to the eprosima::fastcdr::Cdr object.
20212131
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
20222132
*/
2023-
template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
2024-
std::is_arithmetic<_T>::value>::type* = nullptr>
2133+
template<class _T, typename std::enable_if<
2134+
(std::is_enum<_T>::value ||
2135+
std::is_arithmetic<_T>::value) &&
2136+
!is_complex_array_or_string<std::vector<_T>>::value>::type* = nullptr>
20252137
Cdr& deserialize(
20262138
std::vector<_T>& vector_t)
20272139
{

0 commit comments

Comments
 (0)