1 // GnashAlgorithm.h: useful templates and functors for generic algorithms
3 // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifndef GNASH_ALGORITHM_H
21 #define GNASH_ALGORITHM_H
24 #include <boost/checked_delete.hpp>
25 #include <boost/intrusive_ptr.hpp>
26 #include <boost/shared_ptr.hpp>
27 #include <boost/bind.hpp>
31 /// Retrieve the second element of a container with std::pairs.
35 typedef typename
T::second_type result_type
;
37 const result_type
& operator()(const T
& pair
) const {
42 /// Retrieve the first element of a container with std::pairs.
46 typedef typename
T::first_type result_type
;
48 const result_type
& operator()(const T
& pair
) const {
53 /// Return a pointer to a type
57 const T
* operator()(const T
& t
) {
62 /// Recurse to the base type of a pointer.
70 struct RemovePointer
<T
*>
72 typedef typename RemovePointer
<T
>::value_type value_type
;
76 struct RemovePointer
<boost::intrusive_ptr
<T
> >
78 typedef typename RemovePointer
<T
>::value_type value_type
;
82 struct RemovePointer
<boost::shared_ptr
<T
> >
84 typedef typename RemovePointer
<T
>::value_type value_type
;
87 /// Erase elements from an associative container based on a predicate
89 /// This removes elements from a container such as a map if they fulfil a
90 /// particular condition. Because keys of associative container are const,
91 /// we can't do this using iterators, because we can't write to them.
92 template<typename Container
, typename Predicate
>
93 void EraseIf(Container
& c
, Predicate p
)
95 typedef typename
Container::iterator iterator
;
97 for (iterator i
= c
.begin(), e
= c
.end(); i
!= e
; ) {
98 iterator stored
= i
++;
99 if (p(*stored
)) c
.erase(stored
);
104 /// Get the size of an array without passing a pointer by mistake
105 template<typename T
, size_t N
>
113 /// Delete a pointer safely
115 /// Any depth of pointers-to-pointers (up to maximum template recursion) can
116 /// be passed to this struct. The type of the pointee is deduced and passed
117 /// to boost::checked_deleter, which ensures that the type is fully known
118 /// at the point of deletion. It does not, of course, check that the pointer
119 /// was allocated with new, so this isn't completely idiot-proof.
121 struct CheckedDeleter
126 struct CheckedDeleter
<T
**>
128 /// Typedef for use in boost::bind.
129 typedef typename CheckedDeleter
<T
*>::result_type result_type
;
131 void operator()(T
** p
) const {
132 CheckedDeleter
<T
*>()(*p
);
137 struct CheckedDeleter
<T
*>
139 /// Typedef for use in boost::bind.
140 typedef void result_type
;
142 void operator()(T
* p
) const {
143 boost::checked_delete
<T
>(p
);
148 /// Call a functor on the second element of each element in a range.
150 /// @tparam T An iterator type satisfying the requirements of a
152 /// @tparam U The type of the functor op.
153 /// @param begin The start of the range to call op on.
154 /// @param end The end of the range to call op on.
155 /// @param op The function to call on each second element.
156 template<typename T
, typename U
>
158 foreachSecond(T begin
, T end
, U op
)
160 typedef SecondElement
<typename
std::iterator_traits
<T
>::value_type
> S
;
161 std::for_each(begin
, end
, boost::bind(op
, boost::bind(S(), _1
)));
164 /// Call a functor on the first element of each element in a range.
166 /// @tparam T An iterator type satisfying the requirements of a
168 /// @tparam U The type of the functor op.
169 /// @param begin The start of the range to call op on.
170 /// @param end The end of the range to call op on.
171 /// @param op The function to call on each second element.
172 template<typename T
, typename U
>
174 foreachFirst(T begin
, T end
, U op
)
176 typedef FirstElement
<typename
std::iterator_traits
<T
>::value_type
> S
;
177 std::for_each(begin
, end
, boost::bind(op
, boost::bind(S(), _1
)));
180 /// Safely call delete on each element in a range.
182 /// This checks that the type is fully known, but cannot check whether the
183 /// pointer was allocated with new. Pointers allocated with new[] or any other
184 /// allocation function should never be passed to this function.
186 /// @param begin The start of the range to call delete on.
187 /// @param end The end of the range to call delete on.
190 deleteChecked(T begin
, T end
)
192 typedef typename
std::iterator_traits
<T
>::value_type value_type
;
193 std::for_each(begin
, end
, CheckedDeleter
<value_type
>());
196 /// Safely call delete on each second element in a range of pairs.
198 /// This checks that the type is fully known, but cannot check whether the
199 /// pointer was allocated with new. Pointers allocated with new[] or any other
200 /// allocation function should never be passed to this function.
202 /// @param begin The start of the range to call delete on.
203 /// @param end The end of the range to call delete on.
206 deleteSecondElements(T begin
, T end
)
208 typedef SecondElement
<typename
std::iterator_traits
<T
>::value_type
> S
;
209 foreachSecond(begin
, end
, CheckedDeleter
<typename
S::result_type
>());