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 typedef T
* result_type
;
58 result_type
operator()(T
& t
) {
63 /// Erase elements from an associative container based on a predicate
65 /// This removes elements from a container such as a map if they fulfil a
66 /// particular condition. Because keys of associative container are const,
67 /// we can't do this using iterators, because we can't write to them.
68 template<typename Container
, typename Predicate
>
69 void EraseIf(Container
& c
, Predicate p
)
71 typedef typename
Container::iterator iterator
;
73 for (iterator i
= c
.begin(), e
= c
.end(); i
!= e
; ) {
74 iterator stored
= i
++;
75 if (p(*stored
)) c
.erase(stored
);
80 /// Get the size of an array without passing a pointer by mistake
81 template<typename T
, size_t N
>
89 /// Delete a pointer safely
91 /// Any depth of pointers-to-pointers (up to maximum template recursion) can
92 /// be passed to this struct. The type of the pointee is deduced and passed
93 /// to boost::checked_deleter, which ensures that the type is fully known
94 /// at the point of deletion. It does not, of course, check that the pointer
95 /// was allocated with new, so this isn't completely idiot-proof.
102 struct CheckedDeleter
<T
**>
104 /// Typedef for use in boost::bind.
105 typedef typename CheckedDeleter
<T
*>::result_type result_type
;
107 void operator()(T
** p
) const {
108 CheckedDeleter
<T
*>()(*p
);
113 struct CheckedDeleter
<T
*>
115 /// Typedef for use in boost::bind.
116 typedef void result_type
;
118 void operator()(T
* p
) const {
119 boost::checked_delete
<T
>(p
);
124 /// Call a functor on the second element of each element in a range.
126 /// @tparam T An iterator type satisfying the requirements of a
128 /// @tparam U The type of the functor op.
129 /// @param begin The start of the range to call op on.
130 /// @param end The end of the range to call op on.
131 /// @param op The function to call on each second element.
132 template<typename T
, typename U
>
134 foreachSecond(T begin
, T end
, U op
)
136 typedef SecondElement
<typename
std::iterator_traits
<T
>::value_type
> S
;
137 std::for_each(begin
, end
, boost::bind(op
, boost::bind(S(), _1
)));
140 /// Safely call delete on each element in a range.
142 /// This checks that the type is fully known, but cannot check whether the
143 /// pointer was allocated with new. Pointers allocated with new[] or any other
144 /// allocation function should never be passed to this function.
146 /// @param begin The start of the range to call delete on.
147 /// @param end The end of the range to call delete on.
150 deleteChecked(T begin
, T end
)
152 typedef typename
std::iterator_traits
<T
>::value_type value_type
;
153 std::for_each(begin
, end
, CheckedDeleter
<value_type
>());