|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r55282 - in sandbox/fmhess: boost/generic_ptr libs/generic_ptr/test
From: fmhess_at_[hidden]
Date: 2009-07-30 14:45:17
Author: fmhess
Date: 2009-07-30 10:38:56 EDT (Thu, 30 Jul 2009)
New Revision: 55282
URL: http://svn.boost.org/trac/boost/changeset/55282
Log:
Added support for passing a custom deleter and cloner object
to a cloning constructor.
Text files modified:
sandbox/fmhess/boost/generic_ptr/cloning.hpp | 48 +++++++++++++++++++++++++++++++++++++
sandbox/fmhess/libs/generic_ptr/test/cloning_test.cpp | 51 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 99 insertions(+), 0 deletions(-)
Modified: sandbox/fmhess/boost/generic_ptr/cloning.hpp
==============================================================================
--- sandbox/fmhess/boost/generic_ptr/cloning.hpp (original)
+++ sandbox/fmhess/boost/generic_ptr/cloning.hpp 2009-07-30 10:38:56 EDT (Thu, 30 Jul 2009)
@@ -59,6 +59,27 @@
T * px;
};
+ template<typename GenericPointer, typename Deleter, typename Cloner>
+ class clone_factory_pdc_impl: public clone_factory_impl_base
+ {
+ public:
+ clone_factory_pdc_impl(GenericPointer p, Deleter d, Cloner c): px(p), deleter(d), cloner(c)
+ {}
+ ~clone_factory_pdc_impl()
+ {
+ deleter(px);
+ }
+ virtual void * get_pointer() { return get_plain_old_pointer(px); }
+ virtual clone_factory_pdc_impl* make_clone()
+ {
+ return new clone_factory_pdc_impl(cloner(px), deleter, cloner);
+ }
+ private:
+ GenericPointer px;
+ Deleter deleter;
+ Cloner cloner;
+ };
+
class clone_factory
{
public:
@@ -67,6 +88,9 @@
template<typename T>
explicit clone_factory(T * p): _impl(new clone_factory_impl<T>(p))
{}
+ template<typename T, typename Deleter, typename Cloner>
+ clone_factory(T p, Deleter d, Cloner c): _impl(new clone_factory_pdc_impl<T, Deleter, Cloner>(p, d, c))
+ {}
clone_factory(const clone_factory &other): _impl(other._impl->make_clone())
{}
#ifndef BOOST_NO_RVALUE_REFERENCES
@@ -95,6 +119,27 @@
}
}
+ class default_cloning_deleter
+ {
+ public:
+ template<typename GenericPointer>
+ void operator()(const GenericPointer &p) const
+ {
+ delete_clone(get_plain_old_pointer(p));
+ }
+ };
+
+ class default_cloner
+ {
+ public:
+ template<typename GenericPointer>
+ GenericPointer operator()(const GenericPointer & p) const
+ {
+ if(get_plain_old_pointer(p) == 0) return p;
+ return GenericPointer(new_clone(*p));
+ }
+ };
+
template<typename T>
class cloning
{
@@ -117,6 +162,9 @@
template<typename U>
cloning( U p ): _cloner(get_plain_old_pointer(p)), px( p )
{}
+ template<typename U, typename Deleter, typename Cloner = default_cloner>
+ cloning(U p, Deleter d, Cloner c = default_cloner()): _cloner(p, d, c), px( p )
+ {}
cloning(const cloning & other):
_cloner(other._cloner),
px
Modified: sandbox/fmhess/libs/generic_ptr/test/cloning_test.cpp
==============================================================================
--- sandbox/fmhess/libs/generic_ptr/test/cloning_test.cpp (original)
+++ sandbox/fmhess/libs/generic_ptr/test/cloning_test.cpp 2009-07-30 10:38:56 EDT (Thu, 30 Jul 2009)
@@ -11,6 +11,7 @@
#include <boost/config.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/generic_ptr/cloning.hpp>
+#include <boost/generic_ptr/null_deleter.hpp>
class X_base
{
@@ -103,11 +104,61 @@
BOOST_TEST(cp2 != cp3);
}
+void deleter_test()
+{
+ BOOST_TEST(X::instances == 0);
+ X *x0 = new X();
+ X *x1 = 0;
+ {
+ boost::generic_ptr::cloning<X*> cp0(x0, boost::generic_ptr::null_deleter());
+ BOOST_TEST(X::instances == 1);
+ {
+ boost::generic_ptr::cloning<X*> cp1 = cp0;
+ BOOST_TEST(X::instances == 2);
+ x1 = get_plain_old_pointer(cp1);
+ }
+ BOOST_TEST(X::instances == 2);
+ }
+ BOOST_TEST(X::instances == 2);
+ using boost::delete_clone;
+ delete_clone(x0);
+ delete_clone(x1);
+ BOOST_TEST(X::instances == 0);
+}
+
+class null_cloner
+{
+public:
+ template<typename GenericPointer>
+ GenericPointer operator()(const GenericPointer &p)
+ {
+ return p;
+ }
+};
+
+void custom_cloner_test()
+{
+ X *x0 = new X();
+ BOOST_TEST(X::instances == 1);
+ {
+ boost::generic_ptr::cloning<X*> cp0(x0, boost::generic_ptr::null_deleter(), null_cloner());
+ BOOST_TEST(X::instances == 1);
+ boost::generic_ptr::cloning<X*> cp1 = cp0;
+ BOOST_TEST(X::instances == 1);
+ }
+ BOOST_TEST(X::instances == 1);
+ using boost::delete_clone;
+ delete_clone(x0);
+ BOOST_TEST(X::instances == 0);
+}
+
int main()
{
clone_test();
move_test();
no_slice_test();
cast_test();
+ deleter_test();
+ custom_cloner_test();
return 0;
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk