diff options
Diffstat (limited to 'libs/container/test/scoped_allocator_usage_test.cpp')
-rw-r--r-- | libs/container/test/scoped_allocator_usage_test.cpp | 201 |
1 files changed, 117 insertions, 84 deletions
diff --git a/libs/container/test/scoped_allocator_usage_test.cpp b/libs/container/test/scoped_allocator_usage_test.cpp index 111d12a71..61607e7e6 100644 --- a/libs/container/test/scoped_allocator_usage_test.cpp +++ b/libs/container/test/scoped_allocator_usage_test.cpp @@ -1,3 +1,12 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2011-2013. 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) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// #include <boost/container/detail/config_begin.hpp> #include <memory> @@ -7,6 +16,7 @@ #include <boost/container/list.hpp> #include <boost/container/slist.hpp> #include <boost/container/stable_vector.hpp> +#include <boost/container/small_vector.hpp> #include <boost/container/flat_map.hpp> #include <boost/container/flat_set.hpp> #include <boost/container/map.hpp> @@ -48,6 +58,12 @@ public: std::allocator<Ty> m_allocator; template <typename T> friend class SimpleAllocator; + + friend bool operator == (const SimpleAllocator &, const SimpleAllocator &) + { return true; } + + friend bool operator != (const SimpleAllocator &, const SimpleAllocator &) + { return false; } }; class alloc_int @@ -125,6 +141,7 @@ typedef deque<alloc_int, AllocIntAllocator> Deque; typedef list<alloc_int, AllocIntAllocator> List; typedef slist<alloc_int, AllocIntAllocator> Slist; typedef stable_vector<alloc_int, AllocIntAllocator> StableVector; +typedef small_vector<alloc_int, 9, AllocIntAllocator> SmallVector; ///////// //is_unique_assoc @@ -136,26 +153,26 @@ struct is_unique_assoc static const bool value = false; }; -template<class K, class V, class C, class A> -struct is_unique_assoc< map<K, V, C, A> > +template<class Key, class T, class Compare, class Allocator> +struct is_unique_assoc< map<Key, T, Compare, Allocator> > { static const bool value = true; }; -template<class K, class V, class C, class A> -struct is_unique_assoc< flat_map<K, V, C, A> > +template<class Key, class T, class Compare, class Allocator> +struct is_unique_assoc< flat_map<Key, T, Compare, Allocator> > { static const bool value = true; }; -template<class V, class C, class A> -struct is_unique_assoc< set<V, C, A> > +template<class Key, class Compare, class Allocator> +struct is_unique_assoc< set<Key, Compare, Allocator> > { static const bool value = true; }; -template<class V, class C, class A> -struct is_unique_assoc< flat_set<V, C, A> > +template<class Key, class Compare, class Allocator> +struct is_unique_assoc< flat_set<Key, Compare, Allocator> > { static const bool value = true; }; @@ -171,26 +188,26 @@ struct is_map static const bool value = false; }; -template<class K, class V, class C, class A> -struct is_map< map<K, V, C, A> > +template<class Key, class T, class Compare, class Allocator> +struct is_map< map<Key, T, Compare, Allocator> > { static const bool value = true; }; -template<class K, class V, class C, class A> -struct is_map< flat_map<K, V, C, A> > +template<class Key, class T, class Compare, class Allocator> +struct is_map< flat_map<Key, T, Compare, Allocator> > { static const bool value = true; }; -template<class K, class V, class C, class A> -struct is_map< multimap<K, V, C, A> > +template<class Key, class T, class Compare, class Allocator> +struct is_map< multimap<Key, T, Compare, Allocator> > { static const bool value = true; }; -template<class K, class V, class C, class A> -struct is_map< flat_multimap<K, V, C, A> > +template<class Key, class T, class Compare, class Allocator> +struct is_map< flat_multimap<Key, T, Compare, Allocator> > { static const bool value = true; }; @@ -201,26 +218,26 @@ struct is_set static const bool value = false; }; -template<class V, class C, class A> -struct is_set< set<V, C, A> > +template<class Key, class Compare, class Allocator> +struct is_set< set<Key, Compare, Allocator> > { static const bool value = true; }; -template<class V, class C, class A> -struct is_set< flat_set<V, C, A> > +template<class Key, class Compare, class Allocator> +struct is_set< flat_set<Key, Compare, Allocator> > { static const bool value = true; }; -template<class V, class C, class A> -struct is_set< multiset<V, C, A> > +template<class Key, class Compare, class Allocator> +struct is_set< multiset<Key, Compare, Allocator> > { static const bool value = true; }; -template<class V, class C, class A> -struct is_set< flat_multiset<V, C, A> > +template<class Key, class Compare, class Allocator> +struct is_set< flat_multiset<Key, Compare, Allocator> > { static const bool value = true; }; @@ -229,106 +246,97 @@ struct is_set< flat_multiset<V, C, A> > //container_wrapper ///////// +//Try to define-allocator_aware requirements template< class Container , bool Assoc = is_set<Container>::value || is_map<Container>::value , bool UniqueAssoc = is_unique_assoc<Container>::value , bool Map = is_map<Container>::value > -struct container_wrapper - : public Container +struct container_wrapper_inserter { - typedef typename Container::allocator_type allocator_type; + typedef typename Container::const_iterator const_iterator; + typedef typename Container::iterator iterator; - container_wrapper(const allocator_type &a) - : Container(a) - {} + template<class Arg> + static iterator emplace(Container &c, const_iterator p, const Arg &arg) + { return c.emplace(p, arg); } }; template<class Container> //map -struct container_wrapper<Container, true, true, true> - : public Container +struct container_wrapper_inserter<Container, true, true, true> { - typedef typename Container::allocator_type allocator_type; - typedef typename Container::key_compare key_compare; - typedef typename Container::value_type value_type; typedef typename Container::const_iterator const_iterator; typedef typename Container::iterator iterator; - container_wrapper(const allocator_type &a) - : Container(key_compare(), a) - {} - template<class Arg> - iterator emplace(const_iterator, const Arg &arg) - { - return this->Container::emplace(arg, arg).first; - } + static iterator emplace(Container &c, const_iterator, const Arg &arg) + { return c.emplace(arg, arg).first; } }; template<class Container> //set -struct container_wrapper<Container, true, true, false> - : public Container +struct container_wrapper_inserter<Container, true, true, false> { - typedef typename Container::allocator_type allocator_type; - typedef typename Container::key_compare key_compare; - typedef typename Container::value_type value_type; typedef typename Container::const_iterator const_iterator; typedef typename Container::iterator iterator; - container_wrapper(const allocator_type &a) - : Container(key_compare(), a) - {} - template<class Arg> - iterator emplace(const_iterator, const Arg &arg) - { - return this->Container::emplace(arg).first; - } + static iterator emplace(Container &c, const_iterator, const Arg &arg) + { return c.emplace(arg).first; } }; template<class Container> //multimap -struct container_wrapper<Container, true, false, true> - : public Container +struct container_wrapper_inserter<Container, true, false, true> { - typedef typename Container::value_type value_type; - typedef typename Container::key_compare key_compare; - typedef typename Container::allocator_type allocator_type; typedef typename Container::const_iterator const_iterator; typedef typename Container::iterator iterator; - container_wrapper(const allocator_type &a) - : Container(key_compare(), a) - {} - template<class Arg> - iterator emplace(const_iterator, const Arg &arg) - { - return this->Container::emplace(arg, arg); - } + static iterator emplace(Container &c, const_iterator, const Arg &arg) + { return c.emplace(arg, arg); } }; //multiset template<class Container> //multimap -struct container_wrapper<Container, true, false, false> +struct container_wrapper_inserter<Container, true, false, false> +{ + typedef typename Container::const_iterator const_iterator; + typedef typename Container::iterator iterator; + + template<class Arg> + static iterator emplace(Container &c, const_iterator, const Arg &arg) + { return c.emplace(arg); } +}; + +template< class Container> +struct container_wrapper : public Container { - typedef typename Container::value_type value_type; - typedef typename Container::key_compare key_compare; + private: + BOOST_COPYABLE_AND_MOVABLE(container_wrapper) + + public: typedef typename Container::allocator_type allocator_type; typedef typename Container::const_iterator const_iterator; typedef typename Container::iterator iterator; container_wrapper(const allocator_type &a) - : Container(key_compare(), a) + : Container(a) + {} + + container_wrapper(BOOST_RV_REF(container_wrapper) o, const allocator_type &a) + : Container(BOOST_MOVE_BASE(Container, o), a) + {} + + container_wrapper(const container_wrapper &o, const allocator_type &a) + : Container(o, a) {} template<class Arg> - iterator emplace(const_iterator, const Arg &arg) - { - return this->Container::emplace(arg); - } + iterator emplace(const_iterator p, const Arg &arg) + { return container_wrapper_inserter<Container>::emplace(*this, p, arg); } }; + bool test_value_and_state_equals(const alloc_int &r, int value, int state) { return r.get_value() == value && r.get_allocator_state() == state; } @@ -345,19 +353,42 @@ bool one_level_allocator_propagation_test() { typedef container_wrapper<Container> ContainerWrapper; typedef typename ContainerWrapper::iterator iterator; - ContainerWrapper c(SimpleAllocator<MapNode>(5)); + typedef typename ContainerWrapper::allocator_type allocator_type; + typedef typename ContainerWrapper::value_type value_type; + { + ContainerWrapper c(allocator_type(SimpleAllocator<value_type>(5))); - c.clear(); - iterator it = c.emplace(c.cbegin(), 42); + c.clear(); + iterator it = c.emplace(c.cbegin(), 42); - if(!test_value_and_state_equals(*it, 42, 5)) - return false; + if(!test_value_and_state_equals(*it, 42, 5)) + return false; + } + { + ContainerWrapper c2(allocator_type(SimpleAllocator<value_type>(4))); + ContainerWrapper c(::boost::move(c2), allocator_type(SimpleAllocator<value_type>(5))); + c.clear(); + iterator it = c.emplace(c.cbegin(), 42); + + if(!test_value_and_state_equals(*it, 42, 5)) + return false; + }/* + { + ContainerWrapper c2(allocator_type(SimpleAllocator<value_type>(3))); + ContainerWrapper c(c2, allocator_type(SimpleAllocator<value_type>(5))); + + c.clear(); + iterator it = c.emplace(c.cbegin(), 42); + + if(!test_value_and_state_equals(*it, 42, 5)) + return false; + }*/ return true; } int main() -{ +{/* //unique assoc if(!one_level_allocator_propagation_test<FlatMap>()) return 1; @@ -375,7 +406,7 @@ int main() if(!one_level_allocator_propagation_test<FlatMultiSet>()) return 1; if(!one_level_allocator_propagation_test<MultiSet>()) - return 1; + return 1;*/ //sequence containers if(!one_level_allocator_propagation_test<Vector>()) return 1; @@ -387,6 +418,8 @@ int main() return 1; if(!one_level_allocator_propagation_test<StableVector>()) return 1; + if(!one_level_allocator_propagation_test<SmallVector>()) + return 1; return 0; } |