Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54553 - in sandbox/cloneable: boost/cloneable libs/cloneable/test
From: christian.schladetsch_at_[hidden]
Date: 2009-06-30 20:18:57


Author: cschladetsch
Date: 2009-06-30 20:18:56 EDT (Tue, 30 Jun 2009)
New Revision: 54553
URL: http://svn.boost.org/trac/boost/changeset/54553

Log:
added cloneable/clone.hpp

Added:
   sandbox/cloneable/boost/cloneable/clone.hpp (contents, props changed)
Text files modified:
   sandbox/cloneable/libs/cloneable/test/cloneable.vcproj | 4 ++++
   sandbox/cloneable/libs/cloneable/test/tests.cpp | 36 +++++++++++++++++++++++++++++++++++-
   2 files changed, 39 insertions(+), 1 deletions(-)

Added: sandbox/cloneable/boost/cloneable/clone.hpp
==============================================================================
--- (empty file)
+++ sandbox/cloneable/boost/cloneable/clone.hpp 2009-06-30 20:18:56 EDT (Tue, 30 Jun 2009)
@@ -0,0 +1,68 @@
+// Copyright (C) 2009 Christian Schladetsch
+//
+// 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)
+
+#ifndef BOOST_CLONEABLE_CLONE_HPP
+#define BOOST_CLONEABLE_CLONE_HPP
+
+#include <boost/cloneable/detail/prefix.hpp>
+#include <boost/cloneable/traits.hpp>
+
+namespace boost
+{
+ namespace cloneable
+ {
+ namespace impl
+ {
+ template <bool>
+ struct clone
+ {
+ template <class T>
+ static T *given(const T &original)
+ {
+ return new T(original);
+ }
+ template <class T, class Alloc>
+ static T *given(const T &original, Alloc &alloc)
+ {
+ return create<T>(alloc, original);
+ }
+ };
+ template <>
+ struct clone<true>
+ {
+ template <class T>
+ static T *given(const T &original)
+ {
+ return original.clone_as<T>();
+ }
+ template <class T, class Alloc>
+ static T *given(const T &original, Alloc &alloc)
+ {
+ return original.clone_as<T>(alloc);
+ }
+ };
+ }
+
+ template <class T>
+ T *clone(const T &original)
+ {
+ return impl::clone<is_cloneable<T>::value>::given(original);
+ }
+
+ template <class T, class Alloc>
+ T *clone(const T &original, Alloc &alloc)
+ {
+ return impl::clone<is_cloneable<T>::value>::given(original, alloc);
+ }
+
+ } // namespace cloneable
+
+} // namespace boost
+
+#include <boost/cloneable/detail/suffix.hpp>
+
+#endif // BOOST_CLONEABLE_CLONE_HPP
+
+//EOF

Modified: sandbox/cloneable/libs/cloneable/test/cloneable.vcproj
==============================================================================
--- sandbox/cloneable/libs/cloneable/test/cloneable.vcproj (original)
+++ sandbox/cloneable/libs/cloneable/test/cloneable.vcproj 2009-06-30 20:18:56 EDT (Tue, 30 Jun 2009)
@@ -195,6 +195,10 @@
>
                                 </File>
                                 <File
+ RelativePath="..\..\..\boost\cloneable\clone.hpp"
+ >
+ </File>
+ <File
                                         RelativePath="..\..\..\boost\cloneable\forward_declarations.hpp"
>
                                 </File>

Modified: sandbox/cloneable/libs/cloneable/test/tests.cpp
==============================================================================
--- sandbox/cloneable/libs/cloneable/test/tests.cpp (original)
+++ sandbox/cloneable/libs/cloneable/test/tests.cpp 2009-06-30 20:18:56 EDT (Tue, 30 Jun 2009)
@@ -16,7 +16,7 @@
 #include <boost/monotonic/allocator.hpp>
 #include <boost/monotonic/local.hpp>
 
-#include <boost/cloneable/traits.hpp>
+#include <boost/cloneable/clone.hpp>
 #include <boost/cloneable/vector.hpp>
 #include <boost/cloneable/map.hpp>
 #include <boost/cloneable/adaptor.hpp>
@@ -243,6 +243,40 @@
         BOOST_STATIC_ASSERT(!is_default_constructable<T2>::value);
 }
 
+namespace free_clone_test
+{
+ struct T0 { };
+ struct T1 : base<T1>
+ {
+ bool copy_constructed;
+ T1() : copy_constructed(false) { }
+ T1(T1 const &) : copy_constructed(true) { }
+ };
+}
+
+BOOST_AUTO_TEST_CASE(test_free_clone)
+{
+ using namespace free_clone_test;
+ T0 *t0 = new T0();
+ T1 *t1 = new T1();
+
+ // test cloning on a type that does not derive from cloneable::base<>
+ // this will use new T0(orig);
+ T0 *t0_clone = clone(*t0);
+
+ // clone using cloneable system
+ T1 *t1_clone = clone(*t1);
+
+ BOOST_ASSERT(t0_clone);
+ BOOST_ASSERT(t1_clone);
+ BOOST_ASSERT(t1_clone->copy_constructed);
+
+ delete t0;
+ delete t1;
+ delete t0_clone;
+ delete t1_clone;
+}
+
 //struct custom_external_cloneable : adaptor<external_type, my_base>
 //{
 // custom_external_cloneable *clone(abstract_allocator &alloc) const


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