Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef GNASH_ALGORITHM_H
00021 #define GNASH_ALGORITHM_H
00022
00023 #include <algorithm>
00024 #include <boost/checked_delete.hpp>
00025 #include <boost/intrusive_ptr.hpp>
00026 #include <boost/shared_ptr.hpp>
00027 #include <boost/bind.hpp>
00028
00029 namespace gnash {
00030
00032 template<typename T>
00033 struct SecondElement
00034 {
00035 typedef typename T::second_type result_type;
00036
00037 const result_type& operator()(const T& pair) const {
00038 return pair.second;
00039 }
00040 };
00041
00043 template<typename T>
00044 struct FirstElement
00045 {
00046 typedef typename T::first_type result_type;
00047
00048 const result_type& operator()(const T& pair) const {
00049 return pair.first;
00050 }
00051 };
00052
00054 template<typename T>
00055 struct CreatePointer
00056 {
00057 const T* operator()(const T& t) {
00058 return &t;
00059 }
00060 };
00061
00063 template<typename T>
00064 struct RemovePointer
00065 {
00066 typedef T value_type;
00067 };
00068
00069 template<typename T>
00070 struct RemovePointer<T*>
00071 {
00072 typedef typename RemovePointer<T>::value_type value_type;
00073 };
00074
00075 template<typename T>
00076 struct RemovePointer<boost::intrusive_ptr<T> >
00077 {
00078 typedef typename RemovePointer<T>::value_type value_type;
00079 };
00080
00081 template<typename T>
00082 struct RemovePointer<boost::shared_ptr<T> >
00083 {
00084 typedef typename RemovePointer<T>::value_type value_type;
00085 };
00086
00088
00092 template<typename Container, typename Predicate>
00093 void EraseIf(Container& c, Predicate p)
00094 {
00095 typedef typename Container::iterator iterator;
00096
00097 for (iterator i = c.begin(), e = c.end(); i != e; ) {
00098 iterator stored = i++;
00099 if (p(*stored)) c.erase(stored);
00100 }
00101 }
00102
00103
00105 template<typename T, size_t N>
00106 size_t
00107 arraySize(T(&)[N])
00108 {
00109 return N;
00110 }
00111
00112
00114
00120 template<typename T>
00121 struct CheckedDeleter
00122 {
00123 };
00124
00125 template<typename T>
00126 struct CheckedDeleter<T**>
00127 {
00129 typedef typename CheckedDeleter<T*>::result_type result_type;
00130
00131 void operator()(T** p) const {
00132 CheckedDeleter<T*>()(*p);
00133 }
00134 };
00135
00136 template<typename T>
00137 struct CheckedDeleter<T*>
00138 {
00140 typedef void result_type;
00141
00142 void operator()(T* p) const {
00143 boost::checked_delete<T>(p);
00144 }
00145 };
00146
00147
00149
00156 template<typename T, typename U>
00157 void
00158 foreachSecond(T begin, T end, U op)
00159 {
00160 typedef SecondElement<typename std::iterator_traits<T>::value_type> S;
00161 std::for_each(begin, end, boost::bind(op, boost::bind(S(), _1)));
00162 }
00163
00165
00172 template<typename T, typename U>
00173 void
00174 foreachFirst(T begin, T end, U op)
00175 {
00176 typedef FirstElement<typename std::iterator_traits<T>::value_type> S;
00177 std::for_each(begin, end, boost::bind(op, boost::bind(S(), _1)));
00178 }
00179
00181
00185
00188 template<typename T>
00189 void
00190 deleteChecked(T begin, T end)
00191 {
00192 typedef typename std::iterator_traits<T>::value_type value_type;
00193 std::for_each(begin, end, CheckedDeleter<value_type>());
00194 }
00195
00197
00201
00204 template<typename T>
00205 void
00206 deleteSecondElements(T begin, T end)
00207 {
00208 typedef SecondElement<typename std::iterator_traits<T>::value_type> S;
00209 foreachSecond(begin, end, CheckedDeleter<typename S::result_type>());
00210 }
00211
00212
00213 }
00214
00215 #endif
00216