utils.h

00001  /*****************************************************************************
00002  *   Copyright (C) 2006 by Michael Schulze                                    *
00003  *   mike.s@genion.de                                                         *
00004  *                                                                            *
00005  *  The code contained in this file is free software; you can redistribute    *
00006  *  it and/or modify it under the terms of the GNU Lesser General Public      *
00007  *  License as published by the Free Software Foundation; either version      *
00008  *  2.1 of the License, or (at your option) any later version.                *
00009  *                                                                            *
00010  *  This file 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 GNU         *
00013  *  Lesser General Public License for more details.                           *
00014  *                                                                            *
00015  *  You should have received a copy of the GNU Lesser General Public          *
00016  *  License along with this code; if not, write to the Free Software          *
00017  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *
00018  *                                                                            *
00019  *  iTunes and iPod are trademarks of Apple                                   *
00020  *                                                                            *
00021  *  This product is not supported/written/published by Apple!                 *
00022  *****************************************************************************/
00023 
00024 #ifndef _qtpod_itunesdb_utils_H_
00025 #define _qtpod_itunesdb_utils_H_
00026 
00027 // here you can enable usage of the boost shared_ptr impl if using an older libstdc++
00028 // uncomment the following line to use the boost shared_ptr
00029 // #define QTPOD_USE_BOOST_SHAREDPTR 1
00030 
00031 #ifdef QTPOD_USE_BOOST_SHAREDPTR
00032 
00033 #include <boost/shared_ptr.hpp>
00034 #define QTPOD_SHARED_PTR_IMPL_DEF boost::shared_ptr
00035 
00036 #else
00037 
00038 #include <tr1/memory>
00039 #define QTPOD_SHARED_PTR_IMPL_DEF std::tr1::shared_ptr
00040 
00041 #endif
00042 
00043 #define MAC_EPOCH_DELTA 2082844800
00044 
00045 namespace itunesdb {
00046 
00047 namespace utils {
00048 
00056 template <class Iter, class TUnaryPredicate>
00057 Iter findFirst( Iter pos, Iter end, const TUnaryPredicate& predicate ) {
00058     for (; pos != end; ++pos) {
00059         if (predicate(*pos)) {
00060             return pos;
00061         }
00062     }
00063     return end;
00064 }
00065 
00069 template <class Iter, class TUnaryFunction>
00070 Iter max( Iter pos, Iter end, const TUnaryFunction& fn ) {
00071     Iter currentMax = pos;
00072     for ( ; pos != end; ++pos ) {
00073         if ( fn( *pos ) > fn ( *currentMax ) ) {
00074             currentMax = pos;
00075         }
00076     }
00077     return currentMax;
00078 }
00079 
00080 template <class ContainerT, class Iter>
00081 ContainerT& appendAll( ContainerT& container, Iter begin, Iter end ) {
00082     Iter elemIter = begin;
00083     for ( ; elemIter != end; ++elemIter ) {
00084         container.append( *elemIter );
00085     }
00086     return container;
00087 }
00088 
00092 template <class Iter, class TUnaryPredicate>
00093 class FilteredIterator {
00094 public:
00098     FilteredIterator(Iter start, Iter end, TUnaryPredicate predicate)
00099         : _pos_(start), _end_(end), _next_(NULL), _unarypredicate_(predicate) {}
00100 
00104     bool hasNext() {
00105         calcNext();
00106         return _next_ != _end_;
00107     }
00108 
00113     Iter next() {
00114         calcNext();
00115         Iter result = _next_;
00116         _next_ = _end_;
00117         ++_pos_;
00118         return result;
00119     }
00120 private:
00121     void calcNext() {
00122         while ( _pos_ != _end_ && _next_ == _end_ ) {
00123             if (_unarypredicate_(*_pos_)) {
00124                 _next_ = _pos_;
00125             } else {
00126                 ++_pos_;
00127             }
00128         }
00129     }
00130     Iter _pos_;
00131     Iter _end_;
00132     Iter _next_;
00133     TUnaryPredicate _unarypredicate_;
00134 };
00135 
00139 class NonCopyAble {
00140 private:
00141     NonCopyAble( const NonCopyAble& ) {}
00142 protected:
00143     NonCopyAble() {}
00144 public:
00145     virtual ~NonCopyAble() {}
00146 };
00147 
00148 
00152 template <class Stream_T>
00153 inline void seekRelative( Stream_T& stream, uint numbytes ) {
00154     if ( numbytes > 0 ) {
00155         char * buffer = new char[ numbytes ];
00156         stream.readRawBytes( buffer, numbytes );
00157         delete [] buffer;
00158     }
00159 }
00160 
00161 
00162 } // utils
00163 
00164 } // itunesdb
00165 
00166 #endif

Generated on Tue Dec 12 16:39:27 2006 for libqtpod.kdevelop by  doxygen 1.5.1