• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

GnashAlgorithm.h

Go to the documentation of this file.
00001 // GnashAlgorithm.h: useful templates and functors for generic algorithms
00002 //
00003 //   Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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 } // namespace gnash
00214 
00215 #endif
00216 

Generated on Thu Sep 30 2010 14:34:58 for Gnash by  doxygen 1.7.1