Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix behavior for deserializing sharedptr to const obj #418

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/cereal/archives/xml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ namespace cereal
itsNodes.top().node->append_attribute( itsXML.allocate_attribute( "type", "cereal binary data" ) );

finishNode();
};
}

//! @}
/*! @name Internal Functionality
Expand Down Expand Up @@ -436,7 +436,7 @@ namespace cereal
std::memcpy( data, decoded.data(), decoded.size() );

finishNode();
};
}

//! @}
/*! @name Internal Functionality
Expand Down
18 changes: 9 additions & 9 deletions include/cereal/external/base64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ namespace cereal
for(j = i; j < 3; j++)
char_array_3[j] = '\0';

char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[0] = static_cast<unsigned char>((char_array_3[0] & 0xfc) >> 2);
char_array_4[1] = static_cast<unsigned char>(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
char_array_4[2] = static_cast<unsigned char>(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
char_array_4[3] = char_array_3[2] & 0x3f;

for (j = 0; (j < i + 1); j++)
Expand All @@ -95,9 +95,9 @@ namespace cereal
for (i = 0; i <4; i++)
char_array_4[i] = static_cast<unsigned char>(chars.find( char_array_4[i] ));

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
char_array_3[0] = static_cast<unsigned char>((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
char_array_3[1] = static_cast<unsigned char>(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
char_array_3[2] = static_cast<unsigned char>(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);

for (i = 0; (i < 3); i++)
ret += char_array_3[i];
Expand All @@ -112,9 +112,9 @@ namespace cereal
for (j = 0; j <4; j++)
char_array_4[j] = static_cast<unsigned char>(chars.find( char_array_4[j] ));

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
char_array_3[0] = static_cast<unsigned char>((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
char_array_3[1] = static_cast<unsigned char>(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
char_array_3[2] = static_cast<unsigned char>(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);

for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
Expand Down
2 changes: 1 addition & 1 deletion include/cereal/external/rapidxml/rapidxml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace rapidxml

//! Gets human readable description of error.
//! \return Pointer to null terminated description of the error.
virtual const char *what() const throw()
virtual const char *what() const throw() override
{
return m_what;
}
Expand Down
16 changes: 8 additions & 8 deletions include/cereal/types/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,13 @@ namespace cereal
typename std::enable_if<traits::has_load_and_construct<T, Archive>::value, void>::type
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, memory_detail::PtrWrapper<std::shared_ptr<T> &> & wrapper )
{
auto & ptr = wrapper.ptr;

uint32_t id;

ar( CEREAL_NVP_("id", id) );

if( id & detail::msb_32bit )
{
auto ptr = std::const_pointer_cast<std::decay_t<T>>(wrapper.ptr);
// Storage type for the pointer - since we can't default construct this type,
// we'll allocate it using std::aligned_storage and use a custom deleter
using ST = typename std::aligned_storage<sizeof(T), CEREAL_ALIGNOF(T)>::type;
Expand Down Expand Up @@ -314,9 +313,10 @@ namespace cereal

// Mark pointer as valid (initialized)
*valid = true;
wrapper.ptr = ptr;
}
else
ptr = std::static_pointer_cast<T>(ar.getSharedPointer(id));
wrapper.ptr = std::static_pointer_cast<std::decay_t<T>>(ar.getSharedPointer(id));
}

//! Loading std::shared_ptr, case when no user load and construct (wrapper implementation)
Expand All @@ -325,20 +325,20 @@ namespace cereal
typename std::enable_if<!traits::has_load_and_construct<T, Archive>::value, void>::type
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, memory_detail::PtrWrapper<std::shared_ptr<T> &> & wrapper )
{
auto & ptr = wrapper.ptr;

uint32_t id;

ar( CEREAL_NVP_("id", id) );

if( id & detail::msb_32bit )
{
ptr.reset( detail::Construct<T, Archive>::load_andor_construct() );
auto ptr = std::const_pointer_cast<std::decay_t<T>>(wrapper.ptr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line appears superfluous to me, @pkrenz ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that ptr is reset in the line right after, so there's no point in assigning it a value, is there?

ptr.reset( detail::Construct<std::decay_t<T>, Archive>::load_andor_construct() );
ar.registerSharedPointer( id, ptr );
ar( CEREAL_NVP_("data", *ptr) );
wrapper.ptr = ptr;
}
else
ptr = std::static_pointer_cast<T>(ar.getSharedPointer(id));
wrapper.ptr = std::static_pointer_cast<T>(ar.getSharedPointer(id));
}

//! Saving std::unique_ptr (wrapper implementation)
Expand Down Expand Up @@ -422,4 +422,4 @@ namespace cereal
#include "cereal/types/polymorphic.hpp"

#undef CEREAL_ALIGNOF
#endif // CEREAL_TYPES_SHARED_PTR_HPP_
#endif // CEREAL_TYPES_SHARED_PTR_HPP_