20 #ifndef AKONADI_ITEM_P_H
21 #define AKONADI_ITEM_P_H
23 #include <QtCore/QDateTime>
24 #include <QtCore/QMap>
25 #include <QtCore/QVarLengthArray>
28 #include "itempayloadinternals_p.h"
30 #include <boost/bind.hpp>
45 clone_ptr() : t( 0 ) {}
46 explicit clone_ptr( T * t ) : t( t ) {}
47 clone_ptr(
const clone_ptr & other ) : t ( other.t ? other.t->clone() : 0 ) {}
48 ~clone_ptr() {
delete t; }
49 clone_ptr & operator=(
const clone_ptr & other ) {
50 if (
this != &other ) {
51 clone_ptr copy( other );
56 void swap( clone_ptr & other ) {
60 T * operator->()
const {
return get(); }
61 T & operator*()
const { assert(
get() != 0 );
return *
get(); }
62 T *
get()
const {
return t; }
63 T * release() { T *
const r = t; t = 0;
return r; }
64 void reset( T * other=0 ) {
delete t; t = other; }
67 struct _save_bool {
void f() {}; };
68 typedef void (_save_bool::*save_bool)();
70 operator save_bool ()
const {
return get() ? &_save_bool::f : 0 ; }
74 inline void swap( clone_ptr<T> & lhs, clone_ptr<T> & rhs ) {
78 template <
typename T,
size_t N>
79 class VarLengthArray {
80 QVarLengthArray<T,N> impl;
84 typedef const T* const_iterator;
86 typedef const T* const_pointer;
87 typedef T & reference;
88 typedef const T & const_reference;
90 explicit VarLengthArray(
int size=0 ) : impl( size ) {}
94 void push_back(
const T & t ) { impl.append( t ); }
95 int capacity()
const {
return impl.capacity(); }
96 void clear() { impl.clear(); }
97 size_t size()
const {
return impl.count(); }
98 bool empty()
const {
return impl.isEmpty(); }
99 void pop_back() {
return impl.removeLast(); }
100 void reserve(
size_t n ) { impl.reserve( n ); }
101 void resize(
size_t n ) { impl.resize( n ); }
103 iterator begin() {
return impl.data(); }
104 iterator end() {
return impl.data() + impl.size(); }
105 const_iterator begin()
const {
return impl.data(); }
106 const_iterator end()
const {
return impl.data() + impl.size(); }
107 const_iterator cbegin()
const {
return begin(); }
108 const_iterator cend()
const {
return end(); }
110 reference front() {
return *impl.data(); }
111 reference back() {
return *(impl.data()+impl.size()); }
112 const_reference front()
const {
return *impl.data(); }
113 const_reference back()
const {
return *(impl.data()+impl.size()); }
115 reference operator[](
size_t n ) {
return impl[n]; }
116 const_reference operator[](
size_t n )
const {
return impl[n]; }
119 struct TypedPayload {
120 clone_ptr<PayloadBase> payload;
125 struct BySharedPointerAndMetaTypeID : std::unary_function<TypedPayload,bool> {
128 BySharedPointerAndMetaTypeID(
int spid,
int mtid ) : spid( spid ), mtid( mtid ) {}
129 bool operator()(
const TypedPayload & tp )
const {
130 return ( mtid == -1 || mtid == tp.metaTypeId )
131 && ( spid == -1 || spid == tp.sharedPointerId ) ;
141 inline void swap<Akonadi::_detail::TypedPayload>( Akonadi::_detail::TypedPayload & lhs, Akonadi::_detail::TypedPayload & rhs ) {
142 lhs.payload.swap( rhs.payload );
143 swap( lhs.sharedPointerId, rhs.sharedPointerId );
144 swap( lhs.metaTypeId, rhs.metaTypeId );
150 typedef std::vector<_detail::TypedPayload> PayloadContainer;
155 class QForeachContainer<Akonadi::PayloadContainer> {};
169 mConversionInProgress(
false ),
174 mFlagsOverwritten(
false ),
175 mSizeChanged(
false ),
176 mClearPayload(
false )
184 mFlags = other.mFlags;
185 mRevision = other.mRevision;
187 mModificationTime = other.mModificationTime;
188 mMimeType = other.mMimeType;
189 mLegacyPayload = other.mLegacyPayload;
190 mPayloads = other.mPayloads;
191 mConversionInProgress =
false;
192 mAddedFlags = other.mAddedFlags;
193 mDeletedFlags = other.mDeletedFlags;
194 mFlagsOverwritten = other.mFlagsOverwritten;
195 mSizeChanged = other.mSizeChanged;
196 mCollectionId = other.mCollectionId;
197 mClearPayload = other.mClearPayload;
205 void resetChangeLog()
207 mFlagsOverwritten =
false;
209 mDeletedFlags.clear();
210 mSizeChanged =
false;
218 bool hasMetaTypeId(
int mtid )
const {
219 return std::find_if( mPayloads.begin(), mPayloads.end(),
220 _detail::BySharedPointerAndMetaTypeID( -1, mtid ) )
224 PayloadBase * payloadBaseImpl(
int spid,
int mtid )
const {
225 const PayloadContainer::const_iterator it
226 = std::find_if( mPayloads.begin(), mPayloads.end(),
227 _detail::BySharedPointerAndMetaTypeID( spid, mtid ) );
228 return it == mPayloads.end() ? 0 : it->payload.get() ;
231 bool movePayloadFrom(
ItemPrivate * other,
int mtid )
const {
233 const size_t oldSize = mPayloads.size();
234 PayloadContainer & oPayloads = other->mPayloads;
235 const _detail::BySharedPointerAndMetaTypeID matcher( -1, mtid );
236 const size_t numMatching
237 = std::count_if( oPayloads.begin(), oPayloads.end(), matcher );
238 mPayloads.resize( oldSize + numMatching );
240 for ( PayloadContainer::iterator
241 dst = mPayloads.begin() + oldSize,
242 src = oPayloads.begin(), end = oPayloads.end() ; src != end ; ++src )
243 if ( matcher( *src ) ) {
247 return numMatching > 0 ;
251 std::auto_ptr<PayloadBase> takePayloadBaseImpl(
int spid,
int mtid ) {
252 PayloadContainer::iterator it
253 = std::find_if( mPayloads.begin(), mPayloads.end(),
254 _detail::BySharedPointerAndMetaTypeID( spid, mtid ) );
255 if ( it == mPayloads.end() )
256 return std::auto_ptr<PayloadBase>();
257 std::rotate( it, it + 1, mPayloads.end() );
258 std::auto_ptr<PayloadBase> result( it->payload.release() );
259 mPayloads.pop_back();
264 void setPayloadBaseImpl(
int spid,
int mtid, std::auto_ptr<PayloadBase> p,
bool add )
const {
267 mLegacyPayload.reset();
277 mPayloads.resize( add ? mPayloads.size() + 1 : 1 );
278 _detail::TypedPayload & tp = mPayloads.back();
279 tp.payload.reset( p.release() );
280 tp.sharedPointerId = spid;
281 tp.metaTypeId = mtid;
284 void setLegacyPayloadBaseImpl( std::auto_ptr<PayloadBase> p );
285 void tryEnsureLegacyPayload()
const;
287 mutable _detail::clone_ptr<PayloadBase> mLegacyPayload;
288 mutable PayloadContainer mPayloads;
289 mutable bool mConversionInProgress;
294 QDateTime mModificationTime;
297 Item::Flags mAddedFlags;
298 Item::Flags mDeletedFlags;
299 QSet<QByteArray> mCachedPayloadParts;
300 bool mFlagsOverwritten : 1;
301 bool mSizeChanged : 1;
302 bool mClearPayload : 1;
qint64 Id
Describes the unique id type.