00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef _qtpod_itunesdb_utils_H_
00025 #define _qtpod_itunesdb_utils_H_
00026
00027
00028
00029
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 }
00163
00164 }
00165
00166 #endif