Chris@16: /*============================================================================= Chris@16: Copyright (c) 2004 Angus Leeming Chris@16: Copyright (c) 2004 Joel de Guzman Chris@16: Chris@16: Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: ==============================================================================*/ Chris@16: #ifndef PHOENIX_STL_CONTAINER_CONTAINER_HPP Chris@16: #define PHOENIX_STL_CONTAINER_CONTAINER_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace phoenix Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // STL container member functions Chris@16: // Chris@16: // Lazy functions for STL container member functions Chris@16: // Chris@16: // These functions provide a mechanism for the lazy evaluation of the Chris@16: // public member functions of the STL containers. For an overview of Chris@16: // what is meant by 'lazy evaluation', see the comments in operators.hpp Chris@16: // and functions.hpp. Chris@16: // Chris@16: // Lazy functions are provided for all of the member functions of the Chris@16: // following containers: Chris@16: // Chris@16: // deque - list - map - multimap - vector. Chris@16: // Chris@16: // Indeed, should *your* class have member functions with the same names Chris@16: // and signatures as those listed below, then it will automatically be Chris@16: // supported. To summarize, lazy functions are provided for member Chris@16: // functions: Chris@16: // Chris@16: // assign - at - back - begin - capacity - clear - empty - end - Chris@16: // erase - front - get_allocator - insert - key_comp - max_size - Chris@16: // pop_back - pop_front - push_back - push_front - rbegin - rend - Chris@16: // reserve - resize . size - splice - value_comp. Chris@16: // Chris@16: // The lazy functions' names are the same as the corresponding member Chris@16: // function. Sample usage: Chris@16: // Chris@16: // "Normal" version "Lazy" version Chris@16: // ---------------- -------------- Chris@16: // my_vector.at(5) phoenix::at(arg1, 5) Chris@16: // my_list.size() phoenix::size(arg1) Chris@16: // my_vector1.swap(my_vector2) phoenix::swap(arg1, arg2) Chris@16: // Chris@16: // Notice that member functions with names that clash with a Chris@16: // function in stl algorithms are absent. This will be provided Chris@16: // in Phoenix's algorithm module. Chris@16: // Chris@16: // No support is provided here for lazy versions of operator+=, Chris@16: // operator[] etc. Such operators are not specific to STL containers and Chris@16: // lazy versions can therefore be found in operators.hpp. Chris@16: // Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // Lazy member function implementaions. Chris@16: // Chris@16: // The structs below provide the guts of the implementation. Thereafter, Chris@16: // the corresponding lazy function itself is simply: Chris@16: // Chris@16: // function const assign = stl::assign(); Chris@16: // Chris@16: // The structs provide a nested "result" class template whose Chris@16: // "type" typedef enables the lazy function to ascertain the type Chris@16: // to be returned when it is invoked. Chris@16: // Chris@16: // They also provide operator() member functions with signatures Chris@16: // corresponding to those of the underlying member function of Chris@16: // the STL container. Chris@16: // Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: namespace stl Chris@16: { Chris@16: struct assign Chris@16: { Chris@16: template < Chris@16: typename C Chris@16: , typename Arg1 = fusion::void_ Chris@16: , typename Arg2 = fusion::void_ Chris@16: , typename Arg3 = fusion::void_ Chris@16: > Chris@16: struct result Chris@16: { Chris@16: typedef typename add_reference::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: C& operator()(C& c, Arg1 const& arg1) const Chris@16: { Chris@16: c.assign(arg1); Chris@16: return c; Chris@16: } Chris@16: Chris@16: template Chris@16: C& operator()(C& c, Arg1 const& arg1, Arg2 const& arg2) const Chris@16: { Chris@16: c.assign(arg1, arg2); Chris@16: return c; Chris@16: } Chris@16: Chris@16: template Chris@16: C& operator()( Chris@16: C& c Chris@16: , Arg1 const& arg1 Chris@16: , Arg2 const& arg2 Chris@16: , Arg3 const& arg3) const Chris@16: { Chris@16: return c.assign(arg1, arg2, arg3); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct at Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename const_qualified_reference_of::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c, Index const& i) const Chris@16: { Chris@16: return c.at(i); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct back Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Chris@16: typename const_qualified_reference_of::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c) const Chris@16: { Chris@16: return c.back(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct begin Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename const_qualified_iterator_of::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c) const Chris@16: { Chris@16: return c.begin(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct capacity Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename size_type_of::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C const& c) const Chris@16: { Chris@16: return c.capacity(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct clear Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: template Chris@16: void operator()(C& c) const Chris@16: { Chris@16: return c.clear(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct empty Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef bool type; Chris@16: }; Chris@16: Chris@16: template Chris@16: bool operator()(C const& c) const Chris@16: { Chris@16: return c.empty(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct end Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename const_qualified_iterator_of::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c) const Chris@16: { Chris@16: return c.end(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct erase Chris@16: { Chris@16: // This mouthful can differentiate between the generic erase Chris@16: // functions (Container == std::deque, std::list, std::vector) and Chris@16: // that specific to the two map-types, std::map and std::multimap. Chris@16: // Chris@16: // where C is a std::deque, std::list, std::vector: Chris@16: // Chris@16: // 1) iterator C::erase(iterator where); Chris@16: // 2) iterator C::erase(iterator first, iterator last); Chris@16: // Chris@16: // where M is a std::map or std::multimap: Chris@16: // Chris@16: // 3) size_type M::erase(const Key& keyval); Chris@16: // 4) void M::erase(iterator where); Chris@16: // 5) void M::erase(iterator first, iterator last); Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: // BOOST_MSVC #if branch here in map_erase_result non- Chris@16: // standard behavior. The return type should be void but Chris@16: // VC7.1 prefers to return iterator_of. As a result, Chris@16: // VC7.1 complains of error C2562: Chris@16: // boost::phoenix::stl::erase::operator() 'void' function Chris@16: // returning a value. Oh well... :* Chris@16: Chris@16: typedef Chris@16: boost::mpl::eval_if< Chris@16: boost::is_same::type> Chris@16: #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1500) Chris@16: , iterator_of Chris@16: #else Chris@16: , boost::mpl::identity Chris@16: #endif Chris@16: , size_type_of Chris@16: > Chris@16: map_erase_result; Chris@16: Chris@16: typedef typename Chris@16: boost::mpl::eval_if< Chris@16: has_mapped_type Chris@16: , map_erase_result Chris@16: , iterator_of Chris@16: >::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c, Arg1 const& arg1) const Chris@16: { Chris@16: return c.erase(arg1); Chris@16: } Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c, Arg1 const& arg1, Arg2 const& arg2) const Chris@16: { Chris@16: return c.erase(arg1, arg2); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct front Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename const_qualified_reference_of::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c) const Chris@16: { Chris@16: return c.front(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct get_allocator Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename allocator_type_of::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C const& c) const Chris@16: { Chris@16: return c.get_allocator(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct insert Chris@16: { Chris@16: // This mouthful can differentiate between the generic insert Chris@16: // functions (Container == deque, list, vector) and those Chris@16: // specific to the two map-types, std::map and std::multimap. Chris@16: // Chris@16: // where C is a std::deque, std::list, std::vector: Chris@16: // Chris@16: // 1) iterator C::insert(iterator where, value_type value); Chris@16: // 2) void C::insert( Chris@16: // iterator where, size_type count, value_type value); Chris@16: // 3) template Chris@16: // void C::insert(iterator where, Iter first, Iter last); Chris@16: // Chris@16: // where M is a std::map and MM is a std::multimap: Chris@16: // Chris@16: // 4) pair M::insert(value_type const&); Chris@16: // 5) iterator MM::insert(value_type const&); Chris@16: // Chris@16: // where M is a std::map or std::multimap: Chris@16: // Chris@16: // 6) template Chris@16: // void M::insert(Iter first, Iter last); Chris@16: Chris@16: template < Chris@16: typename C Chris@16: , typename Arg1 Chris@16: , typename Arg2 = fusion::void_ Chris@16: , typename Arg3 = fusion::void_ Chris@16: > Chris@16: class result Chris@16: { Chris@16: struct pair_iterator_bool Chris@16: { Chris@16: typedef typename std::pair type; Chris@16: }; Chris@16: Chris@16: typedef Chris@16: boost::mpl::eval_if< Chris@16: map_insert_returns_pair Chris@16: , pair_iterator_bool Chris@16: , iterator_of Chris@16: > Chris@16: choice_1; Chris@16: Chris@16: typedef Chris@16: boost::mpl::eval_if< Chris@16: boost::mpl::and_< Chris@16: boost::is_same Chris@16: , boost::mpl::not_ > > Chris@16: , iterator_of Chris@16: , boost::mpl::identity Chris@16: > Chris@16: choice_2; Chris@16: Chris@16: public: Chris@16: Chris@16: typedef typename Chris@16: boost::mpl::eval_if< Chris@16: boost::is_same Chris@16: , choice_1 Chris@16: , choice_2 Chris@16: >::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c, Arg1 const& arg1) const Chris@16: { Chris@16: return c.insert(arg1); Chris@16: } Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c, Arg1 const& arg1, Arg2 const& arg2) const Chris@16: { Chris@16: return c.insert(arg1, arg2); Chris@16: } Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()( Chris@16: C& c, Arg1 const& arg1, Arg2 const& arg2, Arg3 const& arg3) const Chris@16: { Chris@16: return c.insert(arg1, arg2, arg3); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct key_comp Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename key_compare_of::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C const& c) const Chris@16: { Chris@16: return c.key_comp(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct max_size Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename size_type_of::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C const& c) const Chris@16: { Chris@16: return c.max_size(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct pop_back Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: template Chris@16: void operator()(C& c) const Chris@16: { Chris@16: return c.pop_back(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct pop_front Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: template Chris@16: void operator()(C& c) const Chris@16: { Chris@16: return c.pop_front(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct push_back Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: template Chris@16: void operator()(C& c, Arg const& data) const Chris@16: { Chris@16: return c.push_back(data); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct push_front Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: template Chris@16: void operator()(C& c, Arg const& data) const Chris@16: { Chris@16: return c.push_front(data); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct rbegin Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename Chris@16: const_qualified_reverse_iterator_of::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c) const Chris@16: { Chris@16: return c.rbegin(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct rend Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename Chris@16: const_qualified_reverse_iterator_of::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C& c) const Chris@16: { Chris@16: return c.rend(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct reserve Chris@16: { Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: template Chris@16: void operator()(C& c, Arg const& count) const Chris@16: { Chris@16: return c.reserve(count); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct resize Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: template Chris@16: void operator()(C& c, Arg1 const& arg1) const Chris@16: { Chris@16: return c.resize(arg1); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(C& c, Arg1 const& arg1, Arg2 const& arg2) const Chris@16: { Chris@16: return c.resize(arg1, arg2); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct size Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename size_type_of::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C const& c) const Chris@16: { Chris@16: return c.size(); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct splice Chris@16: { Chris@16: template < Chris@16: typename C Chris@16: , typename Arg1 Chris@16: , typename Arg2 Chris@16: , typename Arg3 = fusion::void_ Chris@16: , typename Arg4 = fusion::void_ Chris@16: > Chris@16: struct result Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: template Chris@16: void operator()(C& c, Arg1 const& arg1, Arg2& arg2) const Chris@16: { Chris@16: c.splice(arg1, arg2); Chris@16: } Chris@16: Chris@16: template < Chris@16: typename C Chris@16: , typename Arg1 Chris@16: , typename Arg2 Chris@16: , typename Arg3 Chris@16: > Chris@16: void operator()( Chris@16: C& c Chris@16: , Arg1 const& arg1 Chris@16: , Arg2& arg2 Chris@16: , Arg3 const& arg3 Chris@16: ) const Chris@16: { Chris@16: c.splice(arg1, arg2, arg3); Chris@16: } Chris@16: Chris@16: template < Chris@16: typename C Chris@16: , typename Arg1 Chris@16: , typename Arg2 Chris@16: , typename Arg3 Chris@16: , typename Arg4 Chris@16: > Chris@16: void operator()( Chris@16: C& c Chris@16: , Arg1 const& arg1 Chris@16: , Arg2& arg2 Chris@16: , Arg3 const& arg3 Chris@16: , Arg4 const& arg4 Chris@16: ) const Chris@16: { Chris@16: c.splice(arg1, arg2, arg3, arg4); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct value_comp Chris@16: { Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef typename value_compare_of::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename result::type Chris@16: operator()(C const& c) const Chris@16: { Chris@16: return c.value_comp(); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace stl Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // The lazy functions themselves. Chris@16: // Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: function const assign = stl::assign(); Chris@16: function const at = stl::at(); Chris@16: function const back = stl::back(); Chris@16: function const begin = stl::begin(); Chris@16: function const capacity = stl::capacity(); Chris@16: function const clear = stl::clear(); Chris@16: function const empty = stl::empty(); Chris@16: function const end = stl::end(); Chris@16: function const erase = stl::erase(); Chris@16: function const front = stl::front(); Chris@16: function const get_allocator = stl::get_allocator(); Chris@16: function const insert = stl::insert(); Chris@16: function const key_comp = stl::key_comp(); Chris@16: function const max_size = stl::max_size(); Chris@16: function const pop_back = stl::pop_back(); Chris@16: function const pop_front = stl::pop_front(); Chris@16: function const push_back = stl::push_back(); Chris@16: function const push_front = stl::push_front(); Chris@16: function const rbegin = stl::rbegin(); Chris@16: function const rend = stl::rend(); Chris@16: function const reserve = stl::reserve(); Chris@16: function const resize = stl::resize(); Chris@16: function const size = stl::size(); Chris@16: function const splice = stl::splice(); Chris@16: function const value_comp = stl::value_comp(); Chris@16: Chris@16: }} // namespace boost::phoenix Chris@16: Chris@16: #endif // PHOENIX_STL_CONTAINERS_HPP