diff --git a/boost/smart_ptr/null_deleter.hpp b/boost/smart_ptr/null_deleter.hpp new file mode 100644 index 0000000..1fd2cfe --- /dev/null +++ b/boost/smart_ptr/null_deleter.hpp @@ -0,0 +1,31 @@ +#ifndef BOOST_SMART_PTR_NULL_DELETER_HPP_INCLUDED +#define BOOST_SMART_PTR_NULL_DELETER_HPP_INCLUDED + +// +// null_deleter.hpp +// +// (C) Copyright 2004 Robert Ramey and Martin Ecker +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/smart_ptr/null_deleter.htm for documentation. +// + +namespace boost +{ + +// A deleter that does nothing, allows usage of shared_ptr with stack +// allocated or static objects. +// Taken from boost/serialization/shared_ptr.hpp + +struct null_deleter +{ + void operator()(void const *) const {} +}; + +} // namespace boost + + +#endif // #ifndef BOOST_SMART_PTR_NULL_DELETER_HPP_INCLUDED diff --git a/libs/smart_ptr/null_deleter.htm b/libs/smart_ptr/null_deleter.htm new file mode 100644 index 0000000..aead4ba --- /dev/null +++ b/libs/smart_ptr/null_deleter.htm @@ -0,0 +1,30 @@ + + + + pointer_cast.hpp + + +

C++ BoostNull Deleter

+

Synopsis

+

A trivial deleter for use with boost::shared_ptr<...> to make it possible to + have boost::shared_ptr<...> "own" objects that are cleaned up by + some other mechanism (for example because they are created on the stack). +

+
+namespace boost {
+
+struct null_deleter
+{
+    void operator()(void const *) const;
+};
+
+} // namespace boost
+            
+
+
+

Revised: $Date$

+

Copyright 2004 Robert Ramey and Martin Ecker. Use, modification, and distribution are subject to + the Boost Software License, Version 1.0. (See accompanying file + LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)

+ + diff --git a/libs/smart_ptr/smart_ptr.htm b/libs/smart_ptr/smart_ptr.htm index 92ad2be..9115740 100644 --- a/libs/smart_ptr/smart_ptr.htm +++ b/libs/smart_ptr/smart_ptr.htm @@ -72,6 +72,17 @@ +

A trivial deleter to allow use of shared_ptr with stack + allocated objects is provided as well:

+
+ + + + + + +
null_deleter<boost/smart_ptr/null_deleter.hpp>No-op deleter for shared_ptr.
+

A test program, smart_ptr_test.cpp, is provided to verify correct operation.

A page on compatibility with older versions of diff --git a/libs/smart_ptr/test/Jamfile.v2 b/libs/smart_ptr/test/Jamfile.v2 index 66d8007..16ce659 100644 --- a/libs/smart_ptr/test/Jamfile.v2 +++ b/libs/smart_ptr/test/Jamfile.v2 @@ -69,5 +69,6 @@ import testing ; [ run get_deleter_array_test.cpp ] [ run ip_hash_test.cpp ] [ run owner_less_test.cpp ] + [ run null_deleter_test.cpp ] ; } diff --git a/libs/smart_ptr/test/null_deleter_test.cpp b/libs/smart_ptr/test/null_deleter_test.cpp new file mode 100644 index 0000000..2fc8060 --- /dev/null +++ b/libs/smart_ptr/test/null_deleter_test.cpp @@ -0,0 +1,87 @@ +#include + +// +// null_deleter_test.cpp +// +// Copyright (c) 2012 Carsten Neumann +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#include + +#include +#include +#include + +int ctor_count = 0; +int dtor_count = 0; + +class A +{ +public: + A(void) + { + ++ctor_count; + } + + ~A(void) + { + ++dtor_count; + } + + int dummy; +}; + +int main(int, char* argv[]) +{ + { + // reset counts + ctor_count = 0; + dtor_count = 0; + + A a0; + + { + boost::shared_ptr sp(&a0, boost::null_deleter()); + BOOST_TEST(sp.use_count() == 1); + BOOST_TEST(ctor_count == 1); + BOOST_TEST(dtor_count == 0); + } + + BOOST_TEST(ctor_count == 1); + BOOST_TEST(dtor_count == 0); + } + + BOOST_TEST(ctor_count == 1); + BOOST_TEST(dtor_count == 1); + + { + // reset counts + ctor_count = 0; + dtor_count = 0; + + A a0; + boost::shared_ptr sp0(&a0, boost::null_deleter()); + boost::shared_ptr sp1(sp0); + + BOOST_TEST(sp0.use_count() == 2); + BOOST_TEST(ctor_count == 1); + BOOST_TEST(dtor_count == 0); + + sp0.reset(); + BOOST_TEST(ctor_count == 1); + BOOST_TEST(dtor_count == 0); + + sp1.reset(); + BOOST_TEST(ctor_count == 1); + BOOST_TEST(dtor_count == 0); + } + + BOOST_TEST(ctor_count == 1); + BOOST_TEST(dtor_count == 1); + + return boost::report_errors(); +}