Skip to content

Commit

Permalink
trapped error with failed void_upcast.
Browse files Browse the repository at this point in the history
helper collection improved - still not done yet
update shared pointer to handle C++11
removed old shared pointer helper from portable binary archive
  • Loading branch information
robertramey committed Jun 19, 2014
1 parent 421c1f2 commit 2183deb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
14 changes: 6 additions & 8 deletions example/portable_binary_iarchive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <boost/archive/archive_exception.hpp>
#include <boost/archive/basic_binary_iprimitive.hpp>
#include <boost/archive/detail/common_iarchive.hpp>
#include <boost/archive/shared_ptr_helper.hpp>
#include <boost/archive/detail/register_archive.hpp>

#include "portable_binary_archive.hpp"
Expand All @@ -39,16 +38,17 @@ class portable_binary_iarchive_exception :
public boost::archive::archive_exception
{
public:
typedef enum {
incompatible_integer_size
} exception_code;
enum exception_code {
incompatible_integer_size
} m_exception_code ;
portable_binary_iarchive_exception(exception_code c = incompatible_integer_size ) :
boost::archive::archive_exception(boost::archive::archive_exception::other_exception)
boost::archive::archive_exception(boost::archive::archive_exception::other_exception),
m_exception_code(c)
{}
virtual const char *what( ) const throw( )
{
const char *msg = "programmer error";
switch(code){
switch(m_exception_code){
case incompatible_integer_size:
msg = "integer cannot be represented";
break;
Expand All @@ -74,8 +74,6 @@ class portable_binary_iarchive :
public boost::archive::detail::common_iarchive<
portable_binary_iarchive
>
,
public boost::archive::detail::shared_ptr_helper
{
typedef boost::archive::basic_binary_iprimitive<
portable_binary_iarchive,
Expand Down
51 changes: 28 additions & 23 deletions include/boost/archive/detail/helper_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@
// See http://www.boost.org for updates, documentation, and revision history.

#include <cstddef> // NULL
#include <typeinfo>
#include <map>
#include <utility>

#include <boost/serialization/type_info_implementation.hpp>
#include <boost/foreach.hpp>
#include <boost/config.hpp>

namespace boost {
#ifndef BOOST_NO_CXX11_SMART_PTR
#include <memory>
#else
#include <boost/serialization/type_info_implementation.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#endif

namespace serialization {
class extended_type_info;
}
namespace boost {

namespace archive {
namespace detail {
Expand All @@ -36,46 +40,47 @@ class helper_collection
helper_collection(const helper_collection&); // non-copyable
helper_collection& operator = (const helper_collection&); // non-copyable

// note: we dont' actually "share" the function object pointer
// we only use shared_ptr to make sure that it get's deleted

// note: consider replacement of map with vector of pairs. In practice
// there will likely be only a couple of helper instances per
// archive instance so map is way overkill in terms of time and space.
typedef std::map<
const boost::serialization::extended_type_info *,
void *
> collection;

#ifndef BOOST_NO_CXX11_SMART_PTR
typedef std::map<
const std::type_info *,
std::shared_ptr<void>
> collection;
#else
#endif

typedef collection::value_type helper_value_type;
typedef collection::iterator helper_iterator;

// dynamically allocated to minimize penalty when not used
collection * m_helpers;
std::unique_ptr<collection> m_helpers;

collection & helpers(){
if(!m_helpers)
m_helpers = new collection;
m_helpers = std::unique_ptr<collection>(new collection);
return * m_helpers;
};
protected:
helper_collection() :
m_helpers(NULL)
helper_collection()
{}
~helper_collection(){
BOOST_FOREACH(void * vp, m_helpers){
delete vp;
}
delete m_helpers;
}
public:
template<typename Helper>
Helper& get_helper(Helper * = NULL) {
const boost::serialization::extended_type_info * eti =
&boost::serialization::type_info_implementation<Helper>::
type::get_const_instance();
const std::type_info * eti = & typeid(Helper);
helper_iterator it = helpers().find(eti);
if(it == helpers().end()){
it = helpers().insert(
helper_value_type(
std::make_pair(
eti,
new Helper
std::make_shared<Helper>()
)
).first;
}
Expand Down
15 changes: 7 additions & 8 deletions include/boost/archive/detail/iserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,14 +506,13 @@ struct load_pointer_type {
) {
// tweak the pointer back to the base class
void * upcast = const_cast<void *>(
boost::serialization::void_upcast(
eti,
boost::serialization::singleton<
typename
boost::serialization::type_info_implementation< T >::type
>::get_const_instance(),
t
)
boost::serialization::void_upcast(
eti,
boost::serialization::singleton<
typename
boost::serialization::type_info_implementation< T >::type
>::get_const_instance(),
t
)
);
if(NULL == upcast)
Expand Down
6 changes: 3 additions & 3 deletions include/boost/serialization/shared_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ inline void serialize(
// for a template when the compiler supports partial template specialization

#ifndef BOOST_NO_CXX11_SMART_PTR
#include <memory>
#include <boost/static_assert.hpp>

// note: we presume that any compiler/library which supports C++11
// std::pointers also supports template partial specialization
// trap here if such presumption were to turn out to wrong!!!
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
inline dummy(int x){ return x / 0 };
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
BOOST_STATIC_ASSERT(false);
#endif

namespace boost {
Expand Down
2 changes: 2 additions & 0 deletions test/test_shared_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ namespace std{
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/export.hpp>

#ifdef BOOST_NO_CXX11_SMART_PTR
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/weak_ptr.hpp>
#endif

#include "test_tools.hpp"

Expand Down

0 comments on commit 2183deb

Please sign in to comment.