Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r58070 - in sandbox/variadic_templates: boost libs/composite_tagged/sandbox
From: cppljevans_at_[hidden]
Date: 2009-11-30 16:47:12


Author: cppljevans
Date: 2009-11-30 16:47:11 EST (Mon, 30 Nov 2009)
New Revision: 58070
URL: http://svn.boost.org/trac/boost/changeset/58070

Log:
composite_tagged now calls component DTOR's
except for all_of_packed whicn no longer calls
either CTOR's or DTOR's. It only calls memcpy.

Text files modified:
   sandbox/variadic_templates/boost/composite_tagged.hpp | 90 ++++++++++++++++++++++++++------------
   sandbox/variadic_templates/libs/composite_tagged/sandbox/composite_tagged.leaf.test.cpp | 91 +++++++++++++++++++++++++++++++++++++--
   2 files changed, 146 insertions(+), 35 deletions(-)

Modified: sandbox/variadic_templates/boost/composite_tagged.hpp
==============================================================================
--- sandbox/variadic_templates/boost/composite_tagged.hpp (original)
+++ sandbox/variadic_templates/boost/composite_tagged.hpp 2009-11-30 16:47:11 EST (Mon, 30 Nov 2009)
@@ -9,6 +9,8 @@
 //
 
 #include <cstddef>
+#include <cstring>
+
 #include <boost/math/common_factor_ct.hpp>
 #include <boost/aligned_storage.hpp>
 #include <boost/mpl/for_each.hpp>
@@ -17,8 +19,6 @@
 #include <boost/mpl/package_range_c.hpp>
 #include <boost/mpl/mk_indexed_pairs.hpp>
 
-#include <iostream>
-
 namespace boost
 {
 namespace composite_tags
@@ -154,7 +154,7 @@
  * These traits include at least the size
  * and possibly the offsets and alignments
  * of the composite's components.
- * Also a layout contains injection and projection
+ * Also a layout contains inject and project
  * functions for creating components of the composite
  * in a memory buffer and retrieving the components
  * from that memory buffer.
@@ -231,6 +231,11 @@
         {
             return 0;
         }
+ static
+ void
+ destroy(index_type index_arg, char*buffer_composite)
+ {
+ }
     };
       template
       < typename HeadLayout
@@ -301,6 +306,20 @@
                 TailComponent*p=static_cast<TailComponent*>(buffer_tail);
                 return p;
             }
+ static
+ void
+ destroy(index_type index_arg, char*buffer_composite)
+ {
+ if(index_arg == index_part)
+ {
+ TailComponent*p=project( index_wrap<index_part>(), buffer_composite);
+ p->~TailComponent();
+ }
+ else
+ {
+ HeadLayout::destroy( index_arg, buffer_composite);
+ }
+ }
         };
     };
     
@@ -355,6 +374,10 @@
     composite_tagged(void)
     : my_index(detail_composite_tagged::index_undefined)
     {}
+ ~composite_tagged(void)
+ {
+ layout_type::destroy( my_index, buffer.address());
+ }
       template
       < index_type Index
>
@@ -379,6 +402,7 @@
       ( typename result_type<indexValu>::type const& tail_component
       )
     {
+ layout_type::destroy( my_index, buffer.address());
         detail_composite_tagged::index_wrap<indexValu> index;
         layout_type::inject(index,buffer.address(),tail_component);
         my_index=indexValu;
@@ -458,14 +482,10 @@
         }
             static
           void
- project(index_wrap<index_part> index_arg, char const*buffer_composite)
- {
- }
- static
- void
- project(index_wrap<index_part> index_arg, char*buffer_composite)
+ project(index_wrap<index_part> index_arg, char const*buffer_composite,...)
         {
         }
+
     };
       template
       < typename HeadLayout
@@ -517,26 +537,21 @@
             
             {
                 void*buffer_tail=buffer_composite+HeadLayout::size_part;
- new(buffer_tail) TailComponent(tail_component);
+ std::size_t const size_tail=sizeof(TailComponent);
+ void const* tail_ptr=&tail_component;
+ std::memcpy(buffer_tail, tail_ptr, size_tail);
             }
               using HeadLayout::
             project
             ;
                 static
- TailComponent const&
- project(index_wrap<index_part> index_arg, char const*buffer_composite)
- {
- void const*buffer_tail=buffer_composite+HeadLayout::size_part;
- TailComponent const*p=static_cast<TailComponent const*>(buffer_tail);
- return *p;
- }
- static
- TailComponent&
- project(index_wrap<index_part> index_arg, char*buffer_composite)
+ void
+ project(index_wrap<index_part> index_arg, char const*buffer_composite, TailComponent& tail_component)
             {
- void*buffer_tail=buffer_composite+HeadLayout::size_part;
- TailComponent*p=static_cast<TailComponent*>(buffer_tail);
- return *p;
+ void const* buffer_tail=buffer_composite+HeadLayout::size_part;
+ std::size_t const size_tail=sizeof(TailComponent);
+ void*tail_ptr=&tail_component;
+ std::memcpy(tail_ptr, buffer_tail, size_tail);
             }
             
         };//end type struct
@@ -656,20 +671,20 @@
       template
       < IndexType IndexValu
>
- typename result_type<IndexValu>::type const&
- project(void)const
+ void
+ inject(typename result_type<IndexValu>::type const& tail_component)
     {
         detail_composite_tagged::index_wrap<IndexValu> index;
- return layout_type::project(index,buffer);
+ layout_type::inject(index,buffer,tail_component);
     }
       template
       < IndexType IndexValu
>
- typename result_type<IndexValu>::type &
- project(void)
+ void
+ project(typename result_type<IndexValu>::type & tail_component)
     {
         detail_composite_tagged::index_wrap<IndexValu> index;
- return layout_type::project(index,buffer);
+ layout_type::project(index,buffer,tail_component);
     }
 };
 #endif //endif#composite_tags::all_of_packed
@@ -752,6 +767,11 @@
         project(index_wrap<index_part> index_arg, char*buffer_composite)
         {
         }
+ static
+ void
+ destroy(char*buffer_composite)
+ {
+ }
     };
     
       template
@@ -842,6 +862,14 @@
                 TailComponent*p=static_cast<TailComponent*>(buffer_tail);
                 return *p;
             }
+ static
+ void
+ destroy( char*buffer_composite)
+ {
+ TailComponent&p=project( index_wrap<index_part>(), buffer_composite);
+ p.~TailComponent();
+ HeadLayout::destroy(buffer_composite);
+ }
             
         };//end type struct
     };//end push_back struct
@@ -890,6 +918,10 @@
         char*memory=buffer.address();
         detail_composite_tagged::construct_all<layout_type,Components...>::initialize_all(memory);
     }
+ ~composite_tagged(void)
+ {
+ layout_type::destroy( buffer.address());
+ }
  
       template
       < index_type Index

Modified: sandbox/variadic_templates/libs/composite_tagged/sandbox/composite_tagged.leaf.test.cpp
==============================================================================
--- sandbox/variadic_templates/libs/composite_tagged/sandbox/composite_tagged.leaf.test.cpp (original)
+++ sandbox/variadic_templates/libs/composite_tagged/sandbox/composite_tagged.leaf.test.cpp 2009-11-30 16:47:11 EST (Mon, 30 Nov 2009)
@@ -6,11 +6,45 @@
 {
 namespace composite_tagged_leaf
 {
+ static
+ unsigned
+object_id
+=0
+;
+ static
+ int
+object_number
+=0
+;
+struct obj
+{
+ unsigned const
+ my_id
+ ;
+ obj(void)
+ : my_id(++object_id)
+ {
+ ++object_number;
+ }
+ obj(obj const&)
+ : my_id(++object_id)
+ {
+ ++object_number;
+ }
+ ~obj(void)
+ {
+ --object_number;
+ }
+ void
+ operator=(obj const&)
+ {}
+};
   template
   < unsigned I
>
   struct
 charvec_u
+: obj
 {
     char v[2*(I+1)];
     unsigned tag(void)const
@@ -36,6 +70,7 @@
 void test(void)
 {
 #if 0
+ std::cout<<"object_number="<<object_number<<"\n";
     {
             typedef
           detail::layout_make
@@ -54,6 +89,7 @@
     }
 #endif
 #if 1
+ std::cout<<"object_number="<<object_number<<"\n";
     {
             typedef
           composite_tagged
@@ -113,6 +149,7 @@
     }
 #endif
 #if 0
+ std::cout<<"object_number="<<object_number<<"\n";
     {
         std::cout
           <<"sizeof(charvec_u<0>)="<<sizeof(charvec_u<0>)<<"\n"
@@ -176,6 +213,7 @@
     }
 #endif
 #if 1
+ std::cout<<"object_number="<<object_number<<"\n";
     {
             typedef
           composite_tagged
@@ -201,18 +239,58 @@
           <<":offset<1>="<<layout_type::offset(detail_composite_tagged::index_wrap<1>())<<"\n"
           <<":offset<2>="<<layout_type::offset(detail_composite_tagged::index_wrap<2>())<<"\n"
           <<":offset<3>="<<layout_type::offset(detail_composite_tagged::index_wrap<3>())<<"\n"
- <<":project<index_0>="<<tagged_valu.project<index_0>().v<<"\n"
- <<":project<index_1>="<<tagged_valu.project<index_1>().v<<"\n"
         ;
- tagged_valu.project<index_1>().v[0]='b';
+
+ charvec_u<index_1> c_1;
+ c_1.v[0]='b';
+ std::cout
+ <<" (before project<index_1>='b')\n"
+ <<":c_1.v="<<c_1.v<<"\n";
+ tagged_valu.project<index_1>(c_1);
         std::cout
- <<"***composite_tagged<all_of_packed>:\n"
           <<" (after project<index_1>='b')\n"
- <<":project<index_1>="<<tagged_valu.project<index_1>().v<<"\n"
- ;
+ <<":c_1.v="<<c_1.v<<"\n";
+ c_1.v[0]='c';
+ std::cout
+ <<" (before inject<index_1>='c')\n"
+ <<":c_1.v="<<c_1.v<<"\n";
+ tagged_valu.inject<index_1>(c_1);
+ std::cout
+ <<" (after inject<index_1>='c')\n"
+ <<":c_1.v="<<c_1.v<<"\n";
+ c_1.v[0]='d';
+ tagged_valu.project<index_1>(c_1);
+ std::cout
+ <<" (after project<index_1>='d')\n"
+ <<":c_1.v="<<c_1.v<<"\n";
+
+ charvec_u<index_2> c_2;
+ c_2.v[0]='b';
+ std::cout
+ <<" (before project<index_2>='b')\n"
+ <<":c_2.v="<<c_2.v<<"\n";
+ tagged_valu.project<index_2>(c_2);
+ std::cout
+ <<" (after project<index_2>='b')\n"
+ <<":c_2.v="<<c_2.v<<"\n";
+ c_2.v[0]='c';
+ std::cout
+ <<" (before inject<index_2>='c')\n"
+ <<":c_2.v="<<c_2.v<<"\n";
+ tagged_valu.inject<index_2>(c_2);
+ std::cout
+ <<" (after inject<index_2>='c')\n"
+ <<":c_2.v="<<c_2.v<<"\n";
+ c_2.v[0]='d';
+ tagged_valu.project<index_2>(c_2);
+ std::cout
+ <<" (after project<index_2>='d')\n"
+ <<":c_2.v="<<c_2.v<<"\n";
+
     }
 #endif
 #if 1
+ std::cout<<"object_number="<<object_number<<"\n";
     {
             typedef
           composite_tagged
@@ -249,6 +327,7 @@
         ;
     }
 #endif
+ std::cout<<"final:object_number="<<object_number<<"\n";
 }
 
 }//exit composite_tagged_leaf namespace


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