|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r54665 - in sandbox/cloneable: boost/cloneable boost/cloneable/detail boost/ptr_container libs/cloneable/test
From: christian.schladetsch_at_[hidden]
Date: 2009-07-05 02:32:29
Author: cschladetsch
Date: 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
New Revision: 54665
URL: http://svn.boost.org/trac/boost/changeset/54665
Log:
added cloneable/exception.hpp
added cloneable/heterogenous_map.hpp (TODO: rename to iso_map)
Added:
sandbox/cloneable/boost/cloneable/exception.hpp (contents, props changed)
sandbox/cloneable/boost/cloneable/heterogenous_map.hpp (contents, props changed)
Text files modified:
sandbox/cloneable/boost/cloneable/allocator.hpp | 58 ++++++-
sandbox/cloneable/boost/cloneable/base.hpp | 2
sandbox/cloneable/boost/cloneable/clone.hpp | 3
sandbox/cloneable/boost/cloneable/detail/associative_container_base.hpp | 25 +++
sandbox/cloneable/boost/cloneable/detail/create_new.hpp | 2
sandbox/cloneable/boost/cloneable/forward_declarations.hpp | 23 ++
sandbox/cloneable/boost/cloneable/instance.hpp | 27 +++
sandbox/cloneable/boost/cloneable/map.hpp | 287 ++++++++++++---------------------------
sandbox/cloneable/boost/cloneable/set.hpp | 31 ++-
sandbox/cloneable/boost/cloneable/traits.hpp | 16 +-
sandbox/cloneable/boost/ptr_container/ptr_map.hpp | 4
sandbox/cloneable/boost/ptr_container/ptr_map_adapter.hpp | 1
sandbox/cloneable/libs/cloneable/test/cloneable.vcproj | 8 +
sandbox/cloneable/libs/cloneable/test/tests.cpp | 77 +++++----
14 files changed, 284 insertions(+), 280 deletions(-)
Modified: sandbox/cloneable/boost/cloneable/allocator.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/allocator.hpp (original)
+++ sandbox/cloneable/boost/cloneable/allocator.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -13,37 +13,73 @@
{
namespace cloneable
{
+ namespace impl
+ {
+ template <bool is_class>
+ struct allocator
+ {
+ template <class Object, class Alloc>
+ static Object *allocate_clone(const Object &orig, Alloc &alloc)
+ {
+ typename Alloc::template rebind<Object>::other al(alloc);
+ ptr *clone = al.allocate(1);
+ new (clone) (orig);
+ return clone;
+ }
+ template <class Object, class Alloc>
+ static void deallocate_clone(const Object *ptr, Alloc &alloc)
+ {
+ if (!ptr)
+ return;
+ typename Alloc::template rebind<Object>::other al(alloc);
+ al.deallocate(const_cast<Object *>(ptr), 1);
+ }
+ };
+ template <>
+ struct allocator<true>
+ {
+ template <class Object, class Alloc>
+ static Object *allocate_clone(const Object &orig, Alloc &alloc)
+ {
+ return orig.clone(alloc);
+ }
+
+ template <class Object, class Alloc>
+ static void deallocate_clone(const Object *ptr, Alloc &alloc)
+ {
+ const_cast<Object *>(ptr)->deallocate(alloc);
+ }
+ };
+ }
+
/// a cloning allocator
struct allocator
{
template <class Base>
- static Base* allocate_clone( const Base& object )
+ static Base* allocate_clone(const Base& orig)
{
- // use default allocator
- return object.clone();
+ make_clone_allocator<default_allocator> alloc;
+ return impl::allocator<boost::is_class<Base>::value>::allocate_clone(orig, alloc);
}
template <class Base>
- static void deallocate_clone( const Base* clone )
+ static void deallocate_clone(const Base* clone)
{
- if (!object)
+ if (!clone)
return;
- // use default allocator
- return const_cast<Base *>(object->deallocate());
+ impl::allocator<boost::is_class<Base>::value>::deallocate_clone(clone, alloc);
}
template <class Base, class Alloc>
static Base* allocate_clone(const Base& object, Alloc &alloc )
{
- return object.clone(alloc);
+ return impl::allocator<boost::is_class<Base>::value>::allocate_clone(object, alloc);
}
template <class Base, class Alloc>
static void deallocate_clone(const Base *object, Alloc &alloc )
{
- if (!object)
- return;
- const_cast<Base &>(*object).deallocate(alloc);
+ impl::allocator<boost::is_class<Base>::value>::deallocate_clone(object, alloc);
}
};
Modified: sandbox/cloneable/boost/cloneable/base.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/base.hpp (original)
+++ sandbox/cloneable/boost/cloneable/base.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -49,7 +49,7 @@
struct get_base_type : if_<is_nil<T>, base_type, T> { };
template <class T>
- struct get_default_construction_tag : if_<is_nil<T>, default_construction, T> { };
+ struct get_default_construction_tag : if_<is_nil<T>, default_construction_tag, T> { };
typedef is_convertible<A *, detail::ctag *> a_is_tag;
typedef is_convertible<B *, detail::ctag *> b_is_tag;
Modified: sandbox/cloneable/boost/cloneable/clone.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/clone.hpp (original)
+++ sandbox/cloneable/boost/cloneable/clone.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -140,9 +140,6 @@
return impl::clone<true>::from_base<Derived>(original, alloc);
}
- /// TODO: add to cloneable/exceptions.hpp
- struct not_cloneable { };
-
template <class Base>
Base *create_new(const Base &base)
{
Modified: sandbox/cloneable/boost/cloneable/detail/associative_container_base.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/detail/associative_container_base.hpp (original)
+++ sandbox/cloneable/boost/cloneable/detail/associative_container_base.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -68,6 +68,22 @@
: parent_type(a), predicate(p), container(p,a)
{
}
+ template <class II>
+ associative_container_base(II F, II L, predicate_type pred = predicate_type())
+ : predicate(pred), container(F,L)
+ {
+ }
+
+ template <class II>
+ associative_container_base(II F, II L, allocator_type &a)
+ : parent_type(a), container(F,L)
+ {
+ }
+ template <class II>
+ associative_container_base(II F, II L, predicate_type pred, allocator_type &a)
+ : parent_type(a), predicate(pred), container(F,L)
+ {
+ }
size_t size() const
{
@@ -95,6 +111,15 @@
return impl().end();
}
+ template <class Fun>
+ Fun for_each(Fun fun)
+ {
+ BOOST_FOREACH(value_type &value, *this)
+ {
+ fun(value);
+ }
+ }
+
};
template <class Cont, class Pred, class Base, class Alloc>
Modified: sandbox/cloneable/boost/cloneable/detail/create_new.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/detail/create_new.hpp (original)
+++ sandbox/cloneable/boost/cloneable/detail/create_new.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -29,7 +29,7 @@
}
};
template <class Derived>
- struct create_new<Derived, no_default_construction>
+ struct create_new<Derived, no_default_construction_tag>
{
template <class Self, class Alloc>
static Derived *given(Self *self, Alloc &alloc, size_t alignment)
Added: sandbox/cloneable/boost/cloneable/exception.hpp
==============================================================================
--- (empty file)
+++ sandbox/cloneable/boost/cloneable/exception.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -0,0 +1,36 @@
+// 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_EXCEPTION_HPP
+#define BOOST_CLONEABLE_EXCEPTION_HPP
+
+#include <exception>
+#include <boost/cloneable/detail/prefix.hpp>
+
+#define BOOST_CLONEABLE_EXCEPTION_0(name) \
+ class name : public std::exception \
+ { \
+ public: \
+ name( const char* what = BOOST_PP_STRINGIZE(name) ) : std::exception( what ) \
+ { } \
+ }
+
+namespace boost
+{
+ namespace cloneable
+ {
+ BOOST_CLONEABLE_EXCEPTION_0(empty_object);
+ BOOST_CLONEABLE_EXCEPTION_0(empty_allocator);
+ BOOST_CLONEABLE_EXCEPTION_0(no_default_construction);
+ BOOST_CLONEABLE_EXCEPTION_0(not_cloneable);
+
+ }
+}
+
+#include <boost/cloneable/detail/suffix.hpp>
+
+#endif // BOOST_CLONEABLE_EXCEPTION_HPP
+
+//EOF
Modified: sandbox/cloneable/boost/cloneable/forward_declarations.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/forward_declarations.hpp (original)
+++ sandbox/cloneable/boost/cloneable/forward_declarations.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -12,6 +12,7 @@
#include <boost/utility/result_of.hpp>
#include <boost/pointee.hpp>
#include <boost/cloneable/detail/prefix.hpp>
+#include <boost/cloneable/exception.hpp>
namespace boost
{
@@ -31,9 +32,9 @@
namespace detail { struct ctag { }; }
/// {@ tags to inform about default-constructability
- struct default_construction : detail::ctag, mpl::true_ {};
- struct no_default_construction : detail::ctag, mpl::false_ {};
- struct unknown_construction : detail::ctag {};
+ struct default_construction_tag : detail::ctag, mpl::true_ {};
+ struct no_default_construction_tag : detail::ctag, mpl::false_ {};
+ struct unknown_construction_tag : detail::ctag {};
/// @}
/// provides a set of pure-virtual methods for allocation, de-allocation, and cloning
@@ -50,7 +51,7 @@
template <
class Derived
, class Base = base_type
- , class DefaultConstructionTag = default_construction
+ , class DefaultConstructionTag = default_construction_tag
//, class Base = nil//base_type
//, class DefaultConstructionTag = nil//default_construction
>
@@ -100,14 +101,24 @@
}
};
- /// a mapping of heterogenous objects to heterogenous objects
+ /// a mapping of Key to heterogenous object
+ template
+ <
+ class Key
+ , class Base = base_type
+ , class Pred = std::less<Key>
+ , class Alloc = monotonic::allocator<int>
+ >
+ struct map;
+
+ /// a mapping of heterogenous object to heterogenous object
template
<
class Base = base_type
, class Pred = indirect<std::less<Base> >
, class Alloc = monotonic::allocator<int>
>
- struct map;
+ struct heterogenous_map;
/// a set of heterogenous objects
template
Added: sandbox/cloneable/boost/cloneable/heterogenous_map.hpp
==============================================================================
--- (empty file)
+++ sandbox/cloneable/boost/cloneable/heterogenous_map.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -0,0 +1,246 @@
+// 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_HETEROGENOUS_MAP_HPP
+#define BOOST_CLONEABLE_HETEROGENOUS_MAP_HPP
+
+#include <boost/ptr_container/ptr_map.hpp>
+#include <boost/foreach.hpp>
+
+#include <boost/cloneable/detail/make_clone_allocator.hpp>
+#include <boost/cloneable/detail/associative_container_base.hpp>
+
+namespace boost
+{
+ namespace cloneable
+ {
+ //namespace heterogenous
+ //{
+ /// a map of heterogenous object to heterogenous object
+ // TODO: move to boost/heterogenous/map
+ template <class Base, class Pred, class Alloc>
+ struct heterogenous_map
+ : detail::associative_container_base<
+ std::map<
+ abstract_base<Base> *
+ , abstract_base<Base> *
+ , Pred
+ //, allocator
+ , typename detail::make_clone_allocator<Alloc>::type>
+ , Pred
+ , Base
+ , Alloc>
+ {
+ typedef detail::associative_container_base<
+ std::map<
+ abstract_base<Base> *
+ , abstract_base<Base> *
+ , Pred
+ //, allocator
+ , typename detail::make_clone_allocator<Alloc>::type>
+ , Pred
+ , Base
+ , Alloc>
+ parent_type;
+
+ typedef typename parent_type::container_type container_type;
+ typedef typename parent_type::base_type base_type;
+ typedef typename parent_type::abstract_base_type abstract_base_type;
+ typedef typename parent_type::allocator_type allocator_type;
+ typedef typename parent_type::value_type value_type;
+ typedef typename parent_type::reference reference;
+ typedef typename parent_type::const_reference const_reference;
+ typedef typename parent_type::iterator iterator;
+ typedef typename parent_type::const_iterator const_iterator;
+
+ using parent_type::new_instance;
+ using parent_type::validate;
+
+ typedef typename container_type::key_type key_type;
+ typedef typename container_type::mapped_type mapped_type;
+
+ typedef map<Base, Pred, Alloc> this_type;
+
+ public:
+ heterogenous_map()
+ {
+ }
+ heterogenous_map(allocator_type &a)
+ : parent_type(a)
+ {
+ }
+
+ //template <class II>
+ //map(II F, II L)
+ // : impl(F,L, get_allocator())
+ //{
+ //}
+ //template <class II>
+ //map(II F, II L, allocator_type &a)
+ // : parent_type(a), impl(F,L, get_allocator())
+ //{
+ //}
+
+ private:
+ typedef std::pair<iterator, bool> map_insert_result;
+
+ template <class K, class V>
+ struct insert_result
+ {
+ //TODO: typedef const_instance<K> key_type;
+ typedef instance<K> key_type;
+ typedef instance<V> value_type;
+
+ key_type key;
+ value_type value;
+ iterator where;
+ bool inserted;
+
+ insert_result() : inserted(false) { }
+ insert_result(const key_type &K, const value_type &V, const map_insert_result &R)
+ : key(K), value(V), where(R.first), inserted(R.second) { }
+ };
+
+ template <class K>
+ struct value_adder
+ {
+ private:
+ typedef instance<K> key_type;
+ this_type *parent;
+ key_type key;
+
+ public:
+ value_adder(this_type &P, const key_type &K)
+ : parent(&P), key(K) { }
+
+ template <class V>
+ insert_result<K,V> value()
+ {
+ return insert(parent->new_instance<V>());
+ }
+
+ template <class V, class A0>
+ insert_result<K,V> value(A0 a0)
+ {
+ return insert(parent->new_instance<V>(a0));
+ }
+ template <class V, class A0, class A1>
+ insert_result<K,V> value(A0 a0, A1 a1)
+ {
+ return insert(parent->new_instance<V>(a0,a1));
+ }
+
+ private:
+ template <class V>
+ insert_result<K,V> insert(instance<V> &value)
+ {
+ map_insert_result result = parent->insert(*key.to_abstract(), *value.to_abstract());
+ if (!result.second)
+ {
+ key.release();
+ value.release();
+ }
+ return insert_result<K,V>(key, value, result);
+ }
+ };
+
+ // TODO: make this private
+ map_insert_result insert(abstract_base_type &a, abstract_base_type &b)
+ {
+ impl().insert(std::make_pair(&a, &b));
+ return map_insert_result();
+ }
+ void insert(value_type x)
+ {
+ impl().insert(x);
+ }
+
+ public:
+ template <class K>
+ value_adder<K> key()
+ {
+ return value_adder<K>(*this, new_instance<K>());
+ }
+
+ // TODO: use variadic arguments or BOOST_PP to pass ctor args
+ template <class K, class A0>
+ value_adder<K> key(A0 a0)
+ {
+ return value_adder<K>(*this, new_instance<K>(a0));
+ }
+ template <class K, class A0, class A1>
+ value_adder<K> key(A0 a0, A1 a1)
+ {
+ return value_adder<K>(*this, new_instance<K>(a0,a1));
+ }
+ template <class K, class A0, class A1, class A2>
+ value_adder<K> key(A0 a0, A1 a1, A2 a2)
+ {
+ return value_adder<K>(*this, new_instance<K>(a0,a1,a2));
+ }
+
+ template <class Ty, class Fun>
+ Fun for_each_key(Fun fun)
+ {
+ //BOOST_FOREACH(base_type &b, *this)
+ //{
+ // if (Ty *ptr = dynamic_cast<Ty *>(&b))
+ // {
+ // fun(*ptr);
+ // }
+ //}
+ return fun;
+ }
+
+ 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;
+ }
+
+ template <class K>
+ iterator find()
+ {
+ instance<K> k(get_allocator());
+ BOOST_SCOPE_EXIT((k))
+ {
+ k.release();
+ }
+ BOOST_SCOPE_EXIT_END
+ return impl().find(k.to_abstract());
+ }
+
+ template <class K, class A0>
+ iterator find(A0 a0)
+ {
+ instance<K> k(get_allocator(), a0);
+ BOOST_SCOPE_EXIT((k))
+ {
+ k.release();
+ }
+ BOOST_SCOPE_EXIT_END
+ return impl().find(k.to_abstract());
+ }
+
+ };
+
+ //} // namespace heterogenous
+
+ } // namespace cloneable
+
+} // namespace boost
+
+#include <boost/cloneable/detail/suffix.hpp>
+
+#endif // BOOST_CLONEABLE_HETEROGENOUS_MAP_HPP
+
+//EOF
Modified: sandbox/cloneable/boost/cloneable/instance.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/instance.hpp (original)
+++ sandbox/cloneable/boost/cloneable/instance.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -14,11 +14,13 @@
{
namespace cloneable
{
+ /// common for all instances of the given base and allocator type
template <class Base, class Alloc>
struct instance_base
{
typedef Base base_type;
typedef Alloc allocator_type;
+ typedef instance_base<Base,Alloc> this_type;
private:
allocator_type *alloc;
@@ -33,7 +35,7 @@
allocator_type &get_allocator() const
{
if (!alloc)
- throw std::exception("empty allocator");
+ throw empty_allocator(typeid(this_type).name());
return *alloc;
}
void set_allocator(allocator_type &al)
@@ -48,6 +50,7 @@
virtual void release() = 0;
};
+ /// common for all instances
template <class Abstract, class Derived, class Base, class Alloc>
struct instance_common : instance_base<Base,Alloc>
{
@@ -108,10 +111,26 @@
return 0;
return ptr;//->is_derived_type::self_ptr;
}
+
+ derived_type &derived_ref()
+ {
+ ptr = to_derived();
+ if (ptr == 0)
+ throw empty_object();
+ return *ptr;
+ }
+
+ derived_type *operator->()
+ {
+ return &derived_ref();
+ }
+ derived_type &operator*()
+ {
+ return derived_ref();
+ }
};
- /// a pointer store that can retrieve pointers from up and down
- /// the inheritance tree. stores the allocator that made it.
+ /// a pointer to a general instance
template <class Derived, class Base, class Alloc, class Ctor>
struct instance : instance_common<base<Derived,Base,Ctor>, Derived, Base, Alloc>
{
@@ -130,7 +149,7 @@
}
instance(allocator_type &al)
: parent_type(al
- , detail::create_new<Derived, Ctor>::given(to_abstract(), get_allocator(), abstract_type::alignment))
+ , detail::create_new<Derived, Ctor>::given(to_abstract(), al, abstract_type::alignment))
{
}
template <class A0>
Modified: sandbox/cloneable/boost/cloneable/map.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/map.hpp (original)
+++ sandbox/cloneable/boost/cloneable/map.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -16,24 +16,15 @@
{
namespace cloneable
{
- /// a vector of heterogenous objects
- // TODO: move to boost/heterogenous/map
- template <class Base, class Pred, class Alloc>
- struct map
- : detail::associative_container_base<
- ptr_map<
- abstract_base<Base>
- , abstract_base<Base>
- , Pred
- , allocator
- , typename detail::make_clone_allocator<Alloc>::type>
- , Pred
- , Base
- , Alloc>
- {
- typedef detail::associative_container_base<
+ //namespace heterogenous
+ //{
+ /// a vector of heterogenous objects
+ // TODO: move to boost/heterogenous/map
+ template <class Key, class Base, class Pred, class Alloc>
+ struct map
+ : detail::associative_container_base<
ptr_map<
- abstract_base<Base>
+ Key
, abstract_base<Base>
, Pred
, allocator
@@ -41,220 +32,124 @@
, Pred
, Base
, Alloc>
- parent_type;
-
- typedef typename parent_type::container_type container_type;
- typedef typename parent_type::base_type base_type;
- typedef typename parent_type::abstract_base_type abstract_base_type;
- typedef typename parent_type::allocator_type allocator_type;
- typedef typename parent_type::value_type value_type;
- typedef typename parent_type::reference reference;
- typedef typename parent_type::const_reference const_reference;
- typedef typename parent_type::iterator iterator;
- typedef typename parent_type::const_iterator const_iterator;
-
- using parent_type::new_instance;
- using parent_type::validate;
-
- typedef typename container_type::key_type key_type;
- typedef typename container_type::mapped_type mapped_type;
+ {
+ typedef detail::associative_container_base<
+ ptr_map<
+ Key
+ , abstract_base<Base>
+ , Pred
+ , allocator
+ , typename detail::make_clone_allocator<Alloc>::type>
+ , Pred
+ , Base
+ , Alloc>
+ parent_type;
+
+ typedef typename parent_type::container_type container_type;
+ typedef typename parent_type::base_type base_type;
+ typedef typename parent_type::abstract_base_type abstract_base_type;
+ typedef typename parent_type::allocator_type allocator_type;
+ typedef typename parent_type::value_type value_type;
+ typedef typename parent_type::reference reference;
+ typedef typename parent_type::const_reference const_reference;
+ typedef typename parent_type::iterator iterator;
+ typedef typename parent_type::const_iterator const_iterator;
- typedef map<Base, Pred, Alloc> this_type;
+ using parent_type::new_instance;
+ using parent_type::validate;
- public:
- map()
- {
- }
- map(allocator_type &a)
- : parent_type(a)
- {
- }
+ typedef typename container_type::key_type key_type;
+ typedef typename container_type::mapped_type mapped_type;
- //template <class II>
- //map(II F, II L)
- // : impl(F,L, get_allocator())
- //{
- //}
- //template <class II>
- //map(II F, II L, allocator_type &a)
- // : parent_type(a), impl(F,L, get_allocator())
- //{
- //}
+ typedef map<Base, Pred, Alloc> this_type;
- private:
- typedef std::pair<iterator, bool> map_insert_result;
+ public:
+ map()
+ {
+ }
+ map(allocator_type &a)
+ : parent_type(a)
+ {
+ }
- template <class K, class V>
- struct insert_result
- {
- //TODO: typedef const_instance<K> key_type;
- typedef instance<K> key_type;
- typedef instance<V> value_type;
-
- key_type key;
- value_type value;
- iterator where;
- bool inserted;
-
- insert_result() : inserted(false) { }
- insert_result(const key_type &K, const value_type &V, const map_insert_result &R)
- : key(K), value(V), where(R.first), inserted(R.second) { }
- };
+ //template <class II>
+ //map(II F, II L)
+ // : impl(F,L, get_allocator())
+ //{
+ //}
+ //template <class II>
+ //map(II F, II L, allocator_type &a)
+ // : parent_type(a), impl(F,L, get_allocator())
+ //{
+ //}
- template <class K>
- struct value_adder
- {
- private:
- typedef instance<K> key_type;
- this_type *parent;
- key_type key;
public:
- value_adder(this_type &P, const key_type &K)
- : parent(&P), key(K) { }
+ typedef std::pair<iterator, bool> map_insert_result;
template <class V>
- insert_result<K,V> value()
+ map_insert_result insert(key_type key)
{
- return insert(parent->new_instance<V>());
+ return impl().insert(key, new_instance<V>().to_abstract());
}
-
template <class V, class A0>
- insert_result<K,V> value(A0 a0)
+ map_insert_result insert(key_type key, A0 a0)
+ {
+ return impl().insert(key, new_instance<V>(a0).to_abstract());
+ }
+ template <class K, class A0, class A1>
+ map_insert_result insert(key_type key, A0 a0, A1 a1)
{
- return insert(parent->new_instance<V>(a0));
+ return impl().insert(key, new_instance<V>(a0,a1).to_abstract());
}
- template <class V, class A0, class A1>
- insert_result<K,V> value(A0 a0, A1 a1)
+ template <class K, class A0, class A1, class A2>
+ map_insert_result insert(key_type key, A0 a0, A1 a1, A2 a2)
{
- return insert(parent->new_instance<V>(a0,a1));
+ return impl().insert(key, new_instance<V>(a0,a1,a2).to_abstract());
}
- private:
- template <class V>
- insert_result<K,V> insert(instance<V> &value)
+ template <class Fun>
+ Fun for_each_key(Fun fun)
{
- map_insert_result result = parent->insert(key.to_abstract(), value.to_abstract());
- if (!result.second)
+ BOOST_FOREACH(value_type &val, *this)
{
- key.release();
- value.release();
+ fun(val.first);
}
- return insert_result<K,V>(key, value, result);
+ return fun;
}
- };
- // TODO: make this private
- template <class A, class B>
- map_insert_result insert(A a, B b)
- {
- return map_insert_result();// impl().insert(std::make_pair(a, b));
- }
- void insert(value_type x)
- {
- impl().insert(x);
- }
-
- public:
- template <class K>
- value_adder<K> key()
- {
- return value_adder<K>(*this, new_instance<K>());
- }
-
- // TODO: use variadic arguments or BOOST_PP to pass ctor args
- template <class K, class A0>
- value_adder<K> key(A0 a0)
- {
- return value_adder<K>(*this, new_instance<K>(a0));
- }
- template <class K, class A0, class A1>
- value_adder<K> key(A0 a0, A1 a1)
- {
- return value_adder<K>(*this, new_instance<K>(a0,a1));
- }
- template <class K, class A0, class A1, class A2>
- value_adder<K> key(A0 a0, A1 a1, A2 a2)
- {
- return value_adder<K>(*this, new_instance<K>(a0,a1,a2));
- }
-
- template <class Fun>
- Fun for_each(Fun fun)
- {
- BOOST_FOREACH(value_type &value, *this)
+ template <class Ty, class Fun>
+ Fun for_each_mapped(Fun fun) const
{
- fun(value);
+ BOOST_FOREACH(value_type const &value, *this)
+ {
+ if (Ty *ptr = dynamic_cast<Ty *>(&val.second))
+ {
+ fun(*ptr);
+ }
+ }
+ return fun;
}
- }
- template <class Ty, class Fun>
- Fun for_each_key(Fun fun)
- {
- //BOOST_FOREACH(base_type &b, *this)
- //{
- // if (Ty *ptr = dynamic_cast<Ty *>(&b))
- // {
- // fun(*ptr);
- // }
- //}
- return fun;
- }
-
- 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;
- }
-
- template <class K>
- iterator find()
- {
- instance<K> k(get_allocator());
- BOOST_SCOPE_EXIT((k))
+ iterator find(key_type const &key)
{
- k.release();
+ return impl().find(key);
}
- BOOST_SCOPE_EXIT_END
- return impl().find(k.to_abstract());
- }
- template <class K, class A0>
- iterator find(A0 a0)
- {
- instance<K> k(get_allocator(), a0);
- BOOST_SCOPE_EXIT((k))
+ const_iterator find(key_type const &key) const
{
- k.release();
+ return impl().find(key);
}
- BOOST_SCOPE_EXIT_END
- return impl().find(k.to_abstract());
- }
-
- //reference operator[](key_type const &key)
- //{
- // return impl[n];
- //}
- //const_reference operator[](key_type const &key) const
- //{
- // return impl[n];
- //}
-
- };
-
- } // namespace heterogenous
+ };
+
+ //} // namespace heterogenous
+
+ } // namespace cloneable
} // namespace boost
#include <boost/cloneable/detail/suffix.hpp>
-#endif // BOOST_HETEROGENOUS_MAP_HPP
+#endif // BOOST_CLONEABLE_MAP_HPP
//EOF
Modified: sandbox/cloneable/boost/cloneable/set.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/set.hpp (original)
+++ sandbox/cloneable/boost/cloneable/set.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -66,19 +66,31 @@
{
}
- /*
+ set(predicate_type pred)
+ : parnet_type(pred)
+ {
+ }
+ set(predicate_type pred, allocator_type &a)
+ : parent_type(pred, a)
+ {
+ }
+
template <class II>
- set(II F, II L)
- : impl(F, L, get_allocator())
+ set(II F, II L, predicate_type pred = predicate_type())
+ : parent_type(F, L, pred)
{
}
template <class II>
set(II F, II L, allocator_type &a)
- : parent_type(a), impl(F, L, get_allocator())
+ : parent_type(F, L, a)
+ {
+ }
+ template <class II>
+ set(II F, II L, predicate_type pred, allocator_type &a)
+ : parent_type(F, L, pred, a)
{
}
- */
public:
typedef std::pair<iterator, bool> insert_result;
@@ -126,15 +138,6 @@
return emplace(new_instance<U>(a0, a1, a2));
}
- template <class Fun>
- Fun for_each(Fun fun)
- {
- BOOST_FOREACH(value_type &value, *this)
- {
- fun(value);
- }
- }
-
struct detail
{
template <class U>
Modified: sandbox/cloneable/boost/cloneable/traits.hpp
==============================================================================
--- sandbox/cloneable/boost/cloneable/traits.hpp (original)
+++ sandbox/cloneable/boost/cloneable/traits.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -28,16 +28,16 @@
struct traits<T, true>
{
BOOST_STATIC_CONSTANT(bool, is_cloneable = true);
- typedef is_convertible<T *, default_construction *> has_default_ctor_type;
- typedef is_convertible<T *, no_default_construction *> has_no_default_ctor_type;
- // T is default constructable only if it is convertibel to def_constr and
- // it is also not convertible to no_def_constr. This ensures that a type
- // that inherits from a type that is not default constructible, is also not
- // default constructible no matter what.
+ typedef is_convertible<T *, default_construction_tag *> has_default_ctor_type;
+ typedef is_convertible<T *, no_default_construction_tag *> has_no_default_ctor_type;
+ /// T is default constructable only if it is convertibel to def_constr and
+ /// it is also not convertible to no_def_constr. This ensures that a type
+ /// that inherits from a type that is not default constructible, is also not
+ /// default constructible no matter what.
BOOST_STATIC_CONSTANT(bool, has_default_ctor = has_default_ctor_type::value || !has_no_default_ctor_type::value);
typedef typename mpl::if_<mpl::bool_<has_default_ctor>
- , default_construction
- , no_default_construction>
+ , default_construction_tag
+ , no_default_construction_tag>
::type construction_tag;
};
Modified: sandbox/cloneable/boost/ptr_container/ptr_map.hpp
==============================================================================
--- sandbox/cloneable/boost/ptr_container/ptr_map.hpp (original)
+++ sandbox/cloneable/boost/ptr_container/ptr_map.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 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/cloneable/boost/ptr_container/ptr_map_adapter.hpp
==============================================================================
--- sandbox/cloneable/boost/ptr_container/ptr_map_adapter.hpp (original)
+++ sandbox/cloneable/boost/ptr_container/ptr_map_adapter.hpp 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -210,6 +210,7 @@
else
{
eraser e(&this->base(),key); // nothrow
+ // CJS: why oh use `new T()` !!
mapped_type res = new T(); // strong
ref = res; // nothrow
e.release(); // nothrow
Modified: sandbox/cloneable/libs/cloneable/test/cloneable.vcproj
==============================================================================
--- sandbox/cloneable/libs/cloneable/test/cloneable.vcproj (original)
+++ sandbox/cloneable/libs/cloneable/test/cloneable.vcproj 2009-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -285,6 +285,10 @@
>
</File>
<File
+ RelativePath="..\..\..\boost\cloneable\exception.hpp"
+ >
+ </File>
+ <File
RelativePath="..\..\..\boost\cloneable\forward_declarations.hpp"
>
</File>
@@ -328,6 +332,10 @@
Name="containers"
>
<File
+ RelativePath="..\..\..\boost\cloneable\heterogenous_map.hpp"
+ >
+ </File>
+ <File
RelativePath="..\..\..\boost\cloneable\list.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-07-05 02:32:27 EDT (Sun, 05 Jul 2009)
@@ -26,6 +26,7 @@
#include <boost/cloneable/list.hpp>
#include <boost/cloneable/map.hpp>
#include <boost/cloneable/set.hpp>
+#include <boost/cloneable/heterogenous_map.hpp>
#include <boost/cloneable/adaptor.hpp>
@@ -281,7 +282,7 @@
namespace no_default_ctor_test
{
- struct T0 : base<T0, base_type, no_default_construction>
+ struct T0 : base<T0, base_type, no_default_construction_tag>
{
T0(int) { }
};
@@ -369,7 +370,7 @@
namespace traits_test
{
struct T0 : base<T0> { };
- struct T1 : base<T1, base_type, no_default_construction> { };
+ struct T1 : base<T1, base_type, no_default_construction_tag> { };
struct T2 { };
}
@@ -547,15 +548,15 @@
}
};
- struct L0 : base<L0, list_test_base, no_default_construction>
+ struct L0 : base<L0, list_test_base, no_default_construction_tag>
{
L0(int n) : list_test_base(n) { }
};
- struct L1 : base<L1, list_test_base, no_default_construction>
+ struct L1 : base<L1, list_test_base, no_default_construction_tag>
{
L1(string s, int n) : list_test_base(n) { }
};
- struct L2 : base<L2, list_test_base, no_default_construction>
+ struct L2 : base<L2, list_test_base, no_default_construction_tag>
{
L2(float f, int n, string s) : list_test_base(n) { }
};
@@ -615,54 +616,62 @@
struct M1 : base<M1, my_base>
{
string str;
- M1() { }
- M1(const char *s) : str(s) { }
+ M1(int n = 1) : my_base(n) { }
+ M1(const char *s, int n = 1) : my_base(n), str(s) { }
};
struct M2 : base<M2, my_base>
{
+ M2(int n = 2) : my_base(n) { }
};
struct M3 : base<M3, my_base>
{
+ M3(int n = 3) : my_base(n) { }
};
-
}
-struct wtf_less
-{
- template <class A, class B>
- bool operator()(A const &a, B const &b) const
- {
- return a->number < b->number;
- }
-};
-
BOOST_AUTO_TEST_CASE(test_map)
{
- //return;
using namespace map_test;
- M2 m2;
- const abstract_base<map_test::my_base> &ab2 = m2;
- const map_test::my_base &b2 = ab2;
- std::less<map_test::my_base> less;
- less(ab2,ab2);
+ typedef cloneable::map<int, map_test::my_base> Map;
+ Map map;
+ map.insert<M0>(1);
+ map.insert<M1>(2, "foo");
+ map.insert<M3>(3, 42);
+ // the preacher - prior to bill hicks?
- typedef cloneable::map<map_test::my_base> Map;
- Map map;
- map.key<M2>().value<M3>();
- BOOST_ASSERT(map.size() == 1);
- Map::iterator a = map.find<M2>();
- BOOST_ASSERT(a != map.end());
+ // deep copy
+ Map copy = map;
+ BOOST_ASSERT(copy == map);
+
+ Map::iterator i3 = copy.find(3);
+ BOOST_ASSERT(i3 != copy.end());
+ BOOST_ASSERT(typeid(*i3->second) == typeid(M3));
+ M3 *m3_clone = i3->second->clone_as<M3>();
+ BOOST_ASSERT(m3_clone != 0);
+
+ delete m3_clone;
+}
+
+BOOST_AUTO_TEST_CASE(test_heterogenous_map)
+{
+ //using namespace map_test;
+ //typedef cloneable::heterogenous_map<map_test::my_base> Map;
+ //Map map;
+ //map.key<M2>().value<M3>();
+ //BOOST_ASSERT(map.size() == 1);
+ //Map::iterator a = map.find<M2>();
+ //BOOST_ASSERT(a != map.end());
- map.key<M0>(42).value<M1>("foo");
- BOOST_ASSERT(map.size() == 2);
+ //map.key<M0>(42).value<M1>("foo");
+ //BOOST_ASSERT(map.size() == 2);
- Map::iterator iter = map.find<M0>(42);
- BOOST_ASSERT(iter!= map.end());
+ //Map::iterator iter = map.find<M0>(42);
+ //BOOST_ASSERT(iter!= map.end());
- map.key<M2>().value<M3>();
+ //map.key<M2>().value<M3>();
// Map copy = map;
}
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