1 ///////////////////////////////////////////////////////////////////////////////
4 // Copyright 2008 Eric Niebler. Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_XPRESSIVE_DETAIL_CORE_RESULTS_CACHE_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_CORE_RESULTS_CACHE_HPP_EAN_10_04_2005
11 // MS compatible compilers support #pragma once
17 #include <boost/detail/workaround.hpp>
18 #include <boost/assert.hpp>
19 #include <boost/xpressive/detail/detail_fwd.hpp>
20 #include <boost/xpressive/detail/core/list.hpp>
21 #include <boost/xpressive/detail/core/access.hpp>
22 #include <boost/xpressive/match_results.hpp>
24 namespace boost { namespace xpressive { namespace detail
27 ///////////////////////////////////////////////////////////////////////////////
29 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
30 template<typename BidiIter>
32 : detail::list<match_results<BidiIter> >
34 friend struct results_cache<BidiIter>;
35 friend struct match_results<BidiIter>;
38 template<typename BidiIter>
40 : private detail::list<match_results<BidiIter> >
42 friend struct results_cache<BidiIter>;
43 friend struct xpressive::match_results<BidiIter>;
44 typedef list<xpressive::match_results<BidiIter> > base_type;
46 typedef typename base_type::iterator iterator;
47 typedef typename base_type::const_iterator const_iterator;
48 typedef typename base_type::pointer pointer;
49 typedef typename base_type::const_pointer const_pointer;
50 typedef typename base_type::reference reference;
51 typedef typename base_type::const_reference const_reference;
52 typedef typename base_type::size_type size_type;
53 using base_type::begin;
55 using base_type::size;
56 using base_type::empty;
57 using base_type::front;
58 using base_type::back;
62 ///////////////////////////////////////////////////////////////////////////////
65 // cache storage for reclaimed match_results structs
66 template<typename BidiIter>
69 typedef core_access<BidiIter> access;
71 match_results<BidiIter> &append_new(nested_results<BidiIter> &out)
73 if(this->cache_.empty())
75 out.push_back(match_results<BidiIter>());
79 BOOST_ASSERT(access::get_nested_results(this->cache_.back()).empty());
80 out.splice(out.end(), this->cache_, --this->cache_.end());
85 // move the last match_results struct into the cache
86 void reclaim_last(nested_results<BidiIter> &out)
88 BOOST_ASSERT(!out.empty());
89 // first, reclaim any nested results
90 nested_results<BidiIter> &nested = access::get_nested_results(out.back());
93 this->reclaim_all(nested);
95 // then, reclaim the last match_results
96 this->cache_.splice(this->cache_.end(), out, --out.end());
99 // move the last n match_results structs into the cache
100 void reclaim_last_n(nested_results<BidiIter> &out, std::size_t count)
102 for(; 0 != count; --count)
104 this->reclaim_last(out);
108 void reclaim_all(nested_results<BidiIter> &out)
110 typedef typename nested_results<BidiIter>::iterator iter_type;
112 // first, recursively reclaim all the nested results
113 for(iter_type begin = out.begin(); begin != out.end(); ++begin)
115 nested_results<BidiIter> &nested = access::get_nested_results(*begin);
119 this->reclaim_all(nested);
123 // next, reclaim the results themselves
124 this->cache_.splice(this->cache_.end(), out);
129 nested_results<BidiIter> cache_;
132 }}} // namespace boost::xpressive::detail