Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54506 - in sandbox/monotonic: boost/heterogenous boost/ptr_container libs/monotonic/test/clones
From: christian.schladetsch_at_[hidden]
Date: 2009-06-29 17:26:46


Author: cschladetsch
Date: 2009-06-29 17:26:45 EDT (Mon, 29 Jun 2009)
New Revision: 54506
URL: http://svn.boost.org/trac/boost/changeset/54506

Log:
added prototype for heterogenous::map

Added:
   sandbox/monotonic/boost/heterogenous/map.hpp (contents, props changed)
Text files modified:
   sandbox/monotonic/boost/heterogenous/base.hpp | 20 ++++++++++----------
   sandbox/monotonic/boost/heterogenous/common_base.hpp | 8 ++++----
   sandbox/monotonic/boost/ptr_container/ptr_map.hpp | 4 ++--
   sandbox/monotonic/libs/monotonic/test/clones/clones.vcproj | 4 ++++
   sandbox/monotonic/libs/monotonic/test/clones/tests.cpp | 36 ++++++++++++++++++++++++++++++++++++
   5 files changed, 56 insertions(+), 16 deletions(-)

Modified: sandbox/monotonic/boost/heterogenous/base.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/base.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/base.hpp 2009-06-29 17:26:45 EDT (Mon, 29 Jun 2009)
@@ -15,11 +15,11 @@
         namespace heterogenous
         {
                 /// base for the given derived type
- template <class Derived>
- struct base : common_base
+ template <class Derived, class Base = common_base>
+ struct base : Base
                 {
                         typedef Derived derived_type;
- typedef base<derived_type> this_type;
+ typedef base<Derived, Base> this_type;
 
                 private:
                         static size_t alignment; ///< required alignment for allocation
@@ -28,7 +28,7 @@
                 public:
                         base() : self_ptr(0) { }
 
- virtual base<Derived> *allocate(abstract_allocator &alloc) const
+ virtual this_type *allocate(abstract_allocator &alloc) const
                         {
                                 abstract_allocator::pointer bytes = alloc.allocate_bytes(sizeof(derived_type), alignment);
                                 Derived *ptr = reinterpret_cast<Derived *>(bytes);
@@ -42,24 +42,24 @@
                                 alloc.deallocate_bytes(reinterpret_cast<abstract_allocator::pointer>(ptr), alignment);
                         }
 
- virtual base<Derived> *create(abstract_allocator &alloc) const
+ virtual this_type *create(abstract_allocator &alloc) const
                         {
- base<Derived> *ptr = allocate(alloc);
+ this_type *ptr = allocate(alloc);
                                 new (ptr->self_ptr) Derived();
                                 return ptr;
                         }
 
- virtual base<Derived> *copy_construct(const common_base &original, abstract_allocator &alloc) const
+ virtual this_type*copy_construct(const common_base &original, abstract_allocator &alloc) const
                         {
- base<Derived> *ptr = allocate(alloc);
+ this_type *ptr = allocate(alloc);
                                 new (ptr->self_ptr) Derived(static_cast<const Derived &>(original));
                                 return ptr;
                         }
                 };
 
                 /// ensure correct alignment when allocating derived instances
- template <class Derived>
- size_t base<Derived>::alignment = aligned_storage<sizeof(Derived)>::alignment;
+ template <class Derived, class Base>
+ size_t base<Derived, Base>::alignment = aligned_storage<sizeof(Derived)>::alignment;
 
         } // namespace heterogenous
 

Modified: sandbox/monotonic/boost/heterogenous/common_base.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/common_base.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/common_base.hpp 2009-06-29 17:26:45 EDT (Mon, 29 Jun 2009)
@@ -18,10 +18,10 @@
                 {
                         virtual ~common_base() { }
 
- virtual common_base *allocate(abstract_allocator &alloc) const = 0;
- virtual void deallocate(common_base *, abstract_allocator &alloc) const = 0;
- virtual common_base *create(abstract_allocator &alloc) const = 0;
- virtual common_base *copy_construct(const common_base &original, abstract_allocator &alloc) const = 0;
+ virtual common_base *allocate(abstract_allocator &alloc) const /*= 0;*/ { return 0; }
+ virtual void deallocate(common_base *, abstract_allocator &alloc) const /*= 0;*/ { }
+ virtual common_base *create(abstract_allocator &alloc) const /*= 0;*/ { return 0; }
+ virtual common_base *copy_construct(const common_base &original, abstract_allocator &alloc) const /*= 0;*/ { return 0; }
                 };
 
         } // namespace heterogenous

Added: sandbox/monotonic/boost/heterogenous/map.hpp
==============================================================================
--- (empty file)
+++ sandbox/monotonic/boost/heterogenous/map.hpp 2009-06-29 17:26:45 EDT (Mon, 29 Jun 2009)
@@ -0,0 +1,257 @@
+// 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_HETEROGENOUS_MAP_HPP
+#define BOOST_HETEROGENOUS_MAP_HPP
+
+#include <boost/ptr_container/ptr_map.hpp>
+#include <boost/monotonic/allocator.hpp>
+#include <boost/foreach.hpp>
+
+#include <boost/heterogenous/detail/prefix.hpp>
+#include <boost/heterogenous/base.hpp>
+#include <boost/heterogenous/make_clone_allocator.hpp>
+
+namespace boost
+{
+ namespace heterogenous
+ {
+ /// a vector of heterogenous objects
+ template <class Pred, class Base = common_base, class Alloc = monotonic::allocator<int> >
+ struct map
+ {
+ typedef typename make_clone_allocator<Alloc>::type allocator_type;
+ typedef Base base_type;
+ //typedef ptr_map<Base, Base, Pred, allocator, allocator_type> implementation;
+
+ typedef std::map<Base *, Base *, Pred, allocator_type> implementation;
+
+ typedef typename implementation::value_type value_type;
+ typedef typename implementation::reference reference;
+ typedef typename implementation::const_reference const_reference;
+ typedef typename implementation::iterator iterator;
+ typedef typename implementation::const_iterator const_iterator;
+ typedef typename implementation::key_type key_type;
+ typedef typename implementation::mapped_type mapped_type;
+ typedef map<Pred,Base,Alloc> this_type;
+
+ private:
+ implementation impl;
+
+ public:
+ map()
+ {
+ //insert.key.parent = this;
+ }
+ map(allocator_type a)
+ : impl(a)
+ {
+ //insert.key.parent = this;
+ }
+
+ /* purposefully elided
+ template <class II>
+ vector(II F, II L, allocator_type a = allocator_type());
+ vector(size_t reserved);
+ */
+
+ /*struct inserter
+ {
+ struct key_adder
+ {
+ this_type *parent;
+
+ */ struct value_adder
+ {
+ this_type *parent;
+ base_type *key_instance;
+
+ value_adder(this_type *P, base_type *K)
+ {
+ parent = P;
+ key_instance = K;
+ }
+
+ template <class U>
+ /*std::pair<iterator,bool> */void value()
+ {
+ U *val = parent->construct_type<U>();
+ parent->insert(std::make_pair(key_instance, val));
+ }
+ };
+
+ template <class U>
+ //value_adder /*operator()*/set() const
+ value_adder key()
+ {
+ U *key_instance = this->construct_type<U>();
+ return value_adder(this, key_instance);
+ }
+ // } key;
+ //} insert;
+
+ template <class Ty, class Fun>
+ Fun for_each_key(Fun fun)
+ {
+ //BOOST_FOREACH(common_base &b, *this)
+ //{
+ // if (Ty *ptr = dynamic_cast<Ty *>(&b))
+ // {
+ // fun(*ptr);
+ // }
+ //}
+ return fun;
+ }
+
+ void insert(value_type const &x)// key_type& key, mapped_type x )
+ {
+ impl.insert(x);
+ }
+
+
+ template <class Ty, class Fun>
+ Fun for_each_mapped(Fun fun) const
+ {
+ //BOOST_FOREACH(const common_base &base, *this)
+ //{
+ // if (Ty *ptr = dynamic_cast<Ty *>(&base))
+ // {
+ // fun(*ptr);
+ // }
+ //}
+ return fun;
+ }
+
+ size_t size() const
+ {
+ return impl.size();
+ }
+ bool empty() const
+ {
+ return impl.empty();
+ }
+
+ iterator begin()
+ {
+ return impl.begin();
+ }
+ iterator end()
+ {
+ return impl.end();
+ }
+ const_iterator begin() const
+ {
+ return impl.begin();
+ }
+ const_iterator end() const
+ {
+ return impl.end();
+ }
+
+ //reference operator[](size_t n)
+ //{
+ // return impl[n];
+ //}
+ //const_reference operator[](size_t n) const
+ //{
+ // return impl[n];
+ //}
+
+ //template <class Other>
+ //bool is_type_at(size_t n) const
+ //{
+ // return ptr_at<Other>(n) != 0;
+ //}
+ //
+ //template <class Other>
+ //Other &ref_at(size_t n)
+ //{
+ // Other *ptr = ptr_at<Other>(n);
+ // if (ptr == 0)
+ // throw std::bad_cast();
+ // return *ptr;
+ //}
+ //template <class Other>
+ //const Other &ref_at(size_t n) const
+ //{
+ // const Other *ptr = ptr_at<const Other>(n);
+ // if (ptr == 0)
+ // throw std::bad_cast();
+ // return *ptr;
+ //}
+
+ //template <class Other>
+ //Other *ptr_at(size_t n)
+ //{
+ // return dynamic_cast<Other *>(&at(n));
+ //}
+ //template <class Other>
+ //const Other *ptr_at(size_t n) const
+ //{
+ // return dynamic_cast<const Other *>(&at(n));
+ //}
+
+ //// TODO: use variadic arguments or BOOST_PP to pass ctor args
+ //template <class U>
+ //void push_back()
+ //{
+ // U *ptr = construct_type<U>();
+ // impl.push_back(ptr);
+ //}
+ //template <class U, class A0>
+ //void push_back(A0 a0)
+ //{
+ // U *ptr = allocate_type<U>();
+ // new (ptr) U(a0);
+ // impl.push_back(ptr);
+ //}
+ //template <class U, class A0, class A1>
+ //void push_back(A0 a0, A1 a1)
+ //{
+ // U *ptr = allocate_type<U>();
+ // new (ptr) U(a0, a1);
+ // impl.push_back(ptr);
+ //}
+ //template <class U, class A0, class A1, class A2>
+ //void push_back(A0 a0, A1 a1, A2 a2)
+ //{
+ // U *ptr = allocate_type<U>();
+ // new (ptr) U(a0, a1, a2);
+ // impl.push_back(ptr);
+ //}
+
+ typename implementation::allocator_type get_allocator()
+ {
+ return impl.get_allocator();
+ }
+
+ private:
+ template <class U>
+ U *allocate_type()
+ {
+ typename allocator_type::template rebind<U>::other alloc(get_allocator());
+ return alloc.allocate(1);
+ }
+
+ template <class U>
+ U *construct_type()
+ {
+ typename allocator_type::template rebind<U>::other alloc(get_allocator());
+ U *ptr = alloc.allocate(1);
+ alloc.construct(ptr);
+ return ptr;
+ }
+
+ };
+
+ } // namespace heterogenous
+
+} // namespace boost
+
+#include <boost/heterogenous/detail/suffix.hpp>
+
+#endif // BOOST_HETEROGENOUS_MAP_HPP
+
+//EOF

Modified: sandbox/monotonic/boost/ptr_container/ptr_map.hpp
==============================================================================
--- sandbox/monotonic/boost/ptr_container/ptr_map.hpp (original)
+++ sandbox/monotonic/boost/ptr_container/ptr_map.hpp 2009-06-29 17:26:45 EDT (Mon, 29 Jun 2009)
@@ -31,10 +31,10 @@
         class Allocator = std::allocator< std::pair<const Key,void*> >
>
     class ptr_map :
- public ptr_map_adapter<T,std::map<Key,void*,
+ public ptr_map_adapter<T,std::map<Key *,void*,
                                Compare,Allocator>,CloneAllocator>
     {
- typedef ptr_map_adapter<T,std::map<Key,void*,
+ typedef ptr_map_adapter<T,std::map<Key *,void*,
                                 Compare,Allocator>,CloneAllocator>
             base_type;
 

Modified: sandbox/monotonic/libs/monotonic/test/clones/clones.vcproj
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/clones/clones.vcproj (original)
+++ sandbox/monotonic/libs/monotonic/test/clones/clones.vcproj 2009-06-29 17:26:45 EDT (Mon, 29 Jun 2009)
@@ -375,6 +375,10 @@
>
                                 </File>
                                 <File
+ RelativePath="..\..\..\..\boost\heterogenous\map.hpp"
+ >
+ </File>
+ <File
                                         RelativePath="..\..\..\..\boost\heterogenous\vector.hpp"
>
                                 </File>

Modified: sandbox/monotonic/libs/monotonic/test/clones/tests.cpp
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/clones/tests.cpp (original)
+++ sandbox/monotonic/libs/monotonic/test/clones/tests.cpp 2009-06-29 17:26:45 EDT (Mon, 29 Jun 2009)
@@ -9,6 +9,7 @@
 #include <string>
 #include <iostream>
 #include <boost/heterogenous/vector.hpp>
+#include <boost/heterogenous/map.hpp>
 #include <boost/bind.hpp>
 #include <boost/any.hpp>
 #include <boost/variant.hpp>
@@ -63,10 +64,13 @@
 void test_any();
 void test_variant();
 
+void test_map();
+
 int main()
 {
         test_any();
         test_variant();
+ test_map();
 
         // a 'heterogenous' container of objects of any type that derives from common_base
         typedef heterogenous::vector<> vec;
@@ -132,6 +136,7 @@
         vec v;
 
         // an issue here is that instances are copy-constructed into the container.
+ // another issue is that this is effectively typeless.
         // but, types added do not have to derive from anything in order for duplication to work.
         v.push_back(derived(42));
         v.push_back(derived2("foo"));
@@ -153,4 +158,35 @@
         BOOST_ASSERT(boost::get<derived2>(v1[1]).str == "foo");
 
 }
+
+struct my_base : heterogenous::common_base
+{
+ int number;
+ my_base(int n = 0) : number(n) { }
+};
+
+struct T0 : heterogenous::base<T0, my_base>
+{
+};
+
+struct T1 : heterogenous::base<T1, my_base>
+{
+};
+
+struct my_less
+{
+ bool operator()(my_base const *left, my_base const *right) const
+ {
+ return left->number < right->number;
+ }
+};
+
+void test_map()
+{
+ heterogenous::map<my_less, my_base> map;
+
+ map.key<T0>().value<T1>();
+
+}
+
 //EOF


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