Skip to content

Commit

Permalink
Use std::unique_ptr instead of std::auto_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseefeld committed Sep 23, 2016
1 parent 63e3079 commit 5029273
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 13 deletions.
34 changes: 28 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,44 @@ addons:
- python3-dev
- libboost-all-dev
- xsltproc
- docbook-xsl
- python-docutils


cache:
directories:
- $HOME/Boost

before_install:
# The Trusty image has several Python versions pre-installed compiled with
# conflicting UCS2 and UCS4 unicode. Modify the PATH to skip the TravisCI python.
# See https://github.com/travis-ci/travis-ci/issues/4948 for details.
- export PATH=$(echo $PATH | tr ':' "\n" | sed '/\/opt\/python/d' | tr "\n" ":" | sed "s|::|:|g")
- sudo pip install future
# The Trusty image has several Python versions pre-installed compiled with
# conflicting UCS2 and UCS4 unicode. Modify the PATH to skip the TravisCI python.
# See https://github.com/travis-ci/travis-ci/issues/4948 for details.
- export PATH=$(echo $PATH | tr ':' "\n" | sed '/\/opt\/python/d' | tr "\n" ":" | sed "s|::|:|g")
- sudo pip install future

install:
# Install our own version of Boost (the subset we need) as the system version is
# too old (for C++11 support).
- rm -rf $HOME/Boost
- |
set -e
if [ ! -d $HOME/Boost ]; then
echo "rebuilding Boost prerequisites"
wget https://sourceforge.net/projects/boost/files/boost/1.61.0/boost_1_61_0.tar.gz/download
tar xf download
pushd boost_1_61_0
./bootstrap.sh
./b2 tools/bcp
mkdir -p $HOME/Boost
dist/bin/bcp python tools/boostbook tools/quickbook $HOME/Boost &> /dev/null
popd
fi
before_script:
- scons --version

script:
- scons config --python=$PYTHON
- scons config --python=$PYTHON --boost-include=$HOME/Boost
- if [ "$DOC" ]; then scons doc; else scons && scons test; fi

after_success:
Expand Down
13 changes: 11 additions & 2 deletions include/boost/python/make_constructor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ namespace detail
template <class U>
void dispatch(U* x, mpl::true_) const
{
std::auto_ptr<U> owner(x);
dispatch(owner, mpl::false_());
#if __cplusplus < 201103L
std::auto_ptr<U> owner(x);
dispatch(owner, mpl::false_());
#else
std::unique_ptr<U> owner(x);
dispatch(std::move(owner), mpl::false_());
#endif
}

template <class Ptr>
Expand All @@ -58,7 +63,11 @@ namespace detail

void* memory = holder::allocate(this->m_self, offsetof(instance_t, storage), sizeof(holder));
try {
#if __cplusplus < 201103L
(new (memory) holder(x))->install(this->m_self);
#else
(new (memory) holder(std::move(x)))->install(this->m_self);
#endif
}
catch(...) {
holder::deallocate(this->m_self, memory);
Expand Down
6 changes: 5 additions & 1 deletion include/boost/python/object/make_ptr_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ struct make_ptr_instance
template <class Arg>
static inline Holder* construct(void* storage, PyObject*, Arg& x)
{
return new (storage) Holder(x);
#if __cplusplus < 201103L
return new (storage) Holder(x);
#else
return new (storage) Holder(std::move(x));
#endif
}

template <class Ptr>
Expand Down
8 changes: 8 additions & 0 deletions include/boost/python/object/pointer_holder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,21 @@ struct pointer_holder_back_reference : instance_holder

template <class Pointer, class Value>
inline pointer_holder<Pointer,Value>::pointer_holder(Pointer p)
#if __cplusplus < 201103L
: m_p(p)
#else
: m_p(std::move(p))
#endif
{
}

template <class Pointer, class Value>
inline pointer_holder_back_reference<Pointer,Value>::pointer_holder_back_reference(Pointer p)
#if __cplusplus < 201103L
: m_p(p)
#else
: m_p(std::move(p))
#endif
{
}

Expand Down
10 changes: 9 additions & 1 deletion include/boost/python/object/py_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ struct py_function
{}

py_function(py_function const& rhs)
: m_impl(rhs.m_impl)
#if __cplusplus < 201103L
: m_impl(rhs.m_impl)
#else
: m_impl(std::move(rhs.m_impl))
#endif
{}

PyObject* operator()(PyObject* args, PyObject* kw) const
Expand Down Expand Up @@ -164,7 +168,11 @@ struct py_function
}

private:
#if __cplusplus < 201103L
mutable std::auto_ptr<py_function_impl_base> m_impl;
#else
mutable std::unique_ptr<py_function_impl_base> m_impl;
#endif
};

}}} // namespace boost::python::objects
Expand Down
4 changes: 3 additions & 1 deletion include/boost/python/to_python_indirect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ namespace detail
// copy constructor.
# if defined(__ICL) && __ICL < 600
typedef boost::shared_ptr<T> smart_pointer;
# else
# elif __cplusplus < 201103L
typedef std::auto_ptr<T> smart_pointer;
# else
typedef std::unique_ptr<T> smart_pointer;
# endif
typedef objects::pointer_holder<smart_pointer, T> holder_t;

Expand Down
14 changes: 12 additions & 2 deletions test/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ for test in [('injected',),
('polymorphism',),
('polymorphism2',),
('wrapper_held_type',),
('polymorphism2_auto_ptr',),
('auto_ptr',),
('minimal',),
('args',),
('raw_ctor',),
Expand Down Expand Up @@ -94,6 +92,18 @@ for test in [('injected',),
('pointer_vector',)]:
tests+=env.BPLTest(*test)

if env['CXX11']:
for test in [
]:
tests+=env.BPLTest(*test)
else:
for test in [
('polymorphism2_auto_ptr',),
('auto_ptr',),
]:
tests+=env.BPLTest(*test)


test = env.BoostRunPythonScript('test_builtin_converters.py')
Depends(
test,
Expand Down

4 comments on commit 5029273

@KindDragon
Copy link

@KindDragon KindDragon commented on 5029273 Dec 29, 2016

Choose a reason for hiding this comment

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

This doesn't work well with VS2015 with /std:c++latest (https://blogs.msdn.microsoft.com/vcblog/2016/08/12/stl-fixes-in-vs-2015-update-3/). Maybe you can use define BOOST_NO_AUTO_PTR here instead of #if __cplusplus < 201103L?

@KindDragon
Copy link

Choose a reason for hiding this comment

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

Or better use define BOOST_NO_CXX11_SMART_PTR

@stefanseefeld
Copy link
Member Author

@stefanseefeld stefanseefeld commented on 5029273 Dec 29, 2016 via email

Choose a reason for hiding this comment

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

@KindDragon
Copy link

Choose a reason for hiding this comment

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

Using std::unique_ptr doesn't imply no-std::auto_ptr. (Modern compilers
do support std::auto_ptr, but deprecate its use.)

Then you can use define BOOST_NO_CXX11_SMART_PTR (The standard library header has no shared_ptr and unique_ptr.)

Please sign in to comment.