Gnash  0.8.10
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, 2011, 2012
00004 //   Free Software Foundation, Inc.
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 
00021 #ifndef GNASH_ALGORITHM_H
00022 #define GNASH_ALGORITHM_H
00023 
00024 #include <algorithm>
00025 #include <boost/bind.hpp>
00026 
00027 namespace gnash {
00028 
00030 template<typename T>
00031 struct CreatePointer
00032 {
00033     typedef T* result_type;
00034     result_type operator()(T& t) { 
00035         return &t;
00036     }
00037 };
00038 
00040 //
00044 template<typename Container, typename Predicate>
00045 void EraseIf(Container& c, Predicate p)
00046 {
00047     typedef typename Container::iterator iterator;
00048 
00049     for (iterator i = c.begin(), e = c.end(); i != e; ) {
00050         iterator stored = i++;
00051         if (p(*stored)) c.erase(stored);
00052     }
00053 }
00054 
00056 template<typename T, size_t N>
00057 size_t
00058 arraySize(T(&)[N])
00059 {
00060     return N;
00061 }
00062 
00064 //
00071 template<typename T, typename U>
00072 void
00073 foreachSecond(T begin, T end, U op)
00074 {
00075     typedef typename std::iterator_traits<T>::value_type value_type;
00076 
00077     std::for_each(begin, end, boost::bind(op,
00078                 boost::bind(&value_type::second, _1)));
00079 }
00080 
00081 } // namespace gnash
00082 
00083 #endif
00084