Boost logo

Boost-Commit :

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


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

Log:
added traits<T>, is_cloneable<T>, is_default_constructable<T>
Added:
   sandbox/cloneable/boost/cloneable/traits.hpp (contents, props changed)
Text files modified:
   sandbox/cloneable/boost/cloneable/base.hpp | 18 +++++++++++-------
   sandbox/cloneable/boost/cloneable/forward_declarations.hpp | 1 +
   sandbox/cloneable/libs/cloneable/test/cloneable.vcproj | 4 ++++
   sandbox/cloneable/libs/cloneable/test/tests.cpp | 23 +++++++++++++++++++++++
   4 files changed, 39 insertions(+), 7 deletions(-)

Modified: sandbox/cloneable/boost/cloneable/base.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/base.hpp (original)
+++ sandbox/cloneable/boost/cloneable/base.hpp 2009-06-30 20:02:20 EDT (Tue, 30 Jun 2009)
@@ -40,14 +40,18 @@
                         };
                 }
 
+ struct is_cloneable_tag { };
+
                 /// base for the given derived type, using the given base class
- template <class Derived, class Base, class HasDefaultCtor>
- struct base : abstract_base<Base,HasDefaultCtor>
+ template <class Derived, class Base, class DefaultCtor>
+ struct base : abstract_base<Base, DefaultCtor>, is_cloneable_tag
                 {
                         typedef Derived derived_type;
                         typedef Base base_type;
- typedef abstract_base<Base,HasDefaultCtor> abstract_base_type;
- typedef base<Derived, Base,HasDefaultCtor> this_type;
+ typedef DefaultCtor default_constructable_type;
+
+ typedef abstract_base<base_type, default_constructable_type> abstract_base_type;
+ typedef base<derived_type, base_type, default_constructable_type> this_type;
 
                         static const size_t alignment; ///< required alignment for allocation
                         mutable derived_type *self_ptr; ///< pointer to derived object in this
@@ -74,7 +78,7 @@
 
                         virtual this_type *create_new(abstract_allocator &alloc) const
                         {
- return detail::create_new<Derived,HasDefaultCtor>::given(this, alloc, alignment);
+ return detail::create_new<Derived, DefaultCtor>::given(this, alloc, alignment);
                         }
 
                         virtual this_type *copy_construct(abstract_allocator &alloc) const
@@ -88,8 +92,8 @@
                 };
 
                 /// ensure correct alignment when allocating derived instances
- template <class Derived, class Base, class HasDefaultCtor>
- const size_t base<Derived, Base, HasDefaultCtor>::alignment = aligned_storage<sizeof(Derived)>::alignment;
+ template <class Derived, class Base, class DefaultCtor>
+ const size_t base<Derived, Base, DefaultCtor>::alignment = aligned_storage<sizeof(Derived)>::alignment;
 
         } // namespace cloneable
 

Modified: sandbox/cloneable/boost/cloneable/forward_declarations.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/forward_declarations.hpp (original)
+++ sandbox/cloneable/boost/cloneable/forward_declarations.hpp 2009-06-30 20:02:20 EDT (Tue, 30 Jun 2009)
@@ -26,6 +26,7 @@
 
                 struct default_construction {};
                 struct no_default_construction {};
+ struct unknown_construction {};
 
                 /// provides a set of pure-virtual methods for allocation, de-allocation, and cloning
                 template <class Base = default_base_type, class DefaultCtor = default_construction>

Added: sandbox/cloneable/boost/cloneable/traits.hpp
==============================================================================
--- (empty file)
+++ sandbox/cloneable/boost/cloneable/traits.hpp 2009-06-30 20:02:20 EDT (Tue, 30 Jun 2009)
@@ -0,0 +1,65 @@
+// 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_TRAITS_HPP
+#define BOOST_CLONEABLE_TRAITS_HPP
+
+#include <boost/mpl/bool.hpp>
+#include <boost/type_traits/is_same.hpp>
+#include <boost/cloneable/detail/prefix.hpp>
+#include <boost/cloneable/base.hpp>
+
+namespace boost
+{
+ namespace cloneable
+ {
+ namespace impl
+ {
+ template <class T, bool>
+ struct traits
+ {
+ BOOST_STATIC_CONSTANT(bool, is_cloneable = false);
+ BOOST_STATIC_CONSTANT(bool, has_default_ctor = false); // this really should be ternary: yes, no or unknown
+ typedef T derived_type;
+ typedef T base_type;
+ typedef unknown_construction default_constructable_type;
+ typedef T abstract_base_type;
+ };
+
+ template <class T>
+ struct traits<T, true>
+ {
+ typedef typename T::derived_type derived_type;
+ typedef typename T::base_type base_type;
+ typedef typename T::default_constructable_type default_constructable_type;
+ typedef typename T::abstract_base_type abstract_base_type;
+
+ BOOST_STATIC_CONSTANT(bool, is_cloneable = true);
+ typedef is_same<default_constructable_type, default_construction> same_type;
+ BOOST_STATIC_CONSTANT(bool, has_default_ctor = same_type::value);
+ };
+
+ template <class T>
+ struct get_traits : traits<T, is_convertible<T *, is_cloneable_tag *>::value> { };
+ }
+
+ template <class T>
+ struct traits : impl::get_traits<T> { };
+
+ template <class T>
+ struct is_cloneable : mpl::bool_<traits<T>::is_cloneable> { };
+
+ template <class T>
+ struct is_default_constructable : mpl::bool_<traits<T>::has_default_ctor> { };
+
+ } // namespace cloneable
+
+} // namespace boost
+
+#include <boost/cloneable/detail/suffix.hpp>
+
+#endif // BOOST_CLONEABLE_TRAITS_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:02:20 EDT (Tue, 30 Jun 2009)
@@ -198,6 +198,10 @@
                                         RelativePath="..\..\..\boost\cloneable\forward_declarations.hpp"
>
                                 </File>
+ <File
+ RelativePath="..\..\..\boost\cloneable\traits.hpp"
+ >
+ </File>
                                 <Filter
                                         Name="detail"
>

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:02:20 EDT (Tue, 30 Jun 2009)
@@ -16,6 +16,7 @@
 #include <boost/monotonic/allocator.hpp>
 #include <boost/monotonic/local.hpp>
 
+#include <boost/cloneable/traits.hpp>
 #include <boost/cloneable/vector.hpp>
 #include <boost/cloneable/map.hpp>
 #include <boost/cloneable/adaptor.hpp>
@@ -220,6 +221,28 @@
         BOOST_ASSERT(typeid(*clone) == typeid(cloneable_external_type));
 }
 
+namespace traits_test
+{
+ struct T0 : base<T0> { };
+ struct T1 : base<T1, default_base_type, no_default_construction> { };
+ struct T2 { };
+}
+
+BOOST_AUTO_TEST_CASE(test_traits)
+{
+ using namespace traits_test;
+ typedef traits<T0> t0_traits;
+
+ BOOST_STATIC_ASSERT(is_cloneable<T0>::value);
+ BOOST_STATIC_ASSERT(is_default_constructable<T0>::value);
+
+ BOOST_STATIC_ASSERT(is_cloneable<T1>::value);
+ BOOST_STATIC_ASSERT(!is_default_constructable<T1>::value);
+
+ BOOST_STATIC_ASSERT(!is_cloneable<T2>::value);
+ BOOST_STATIC_ASSERT(!is_default_constructable<T2>::value);
+}
+
 //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