libkdegames/kgame
kgamepropertyarray.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __KGAMEPROPERTYARRAY_H_
00022 #define __KGAMEPROPERTYARRAY_H_
00023
00024 #include <QtCore/QDataStream>
00025
00026 #include <QtCore/QVector>
00027 #include <kdebug.h>
00028
00029 #include "kgamemessage.h"
00030 #include "kgameproperty.h"
00031 #include "kgamepropertyhandler.h"
00032
00036 template<class type>
00037 class KGamePropertyArray : public QVector<type>, public KGamePropertyBase
00038 {
00039 public:
00040 KGamePropertyArray() :QVector<type>(), KGamePropertyBase()
00041 {
00042
00043 }
00044
00045 KGamePropertyArray( int size )
00046 {
00047 resize(size);
00048 }
00049
00050 KGamePropertyArray( const KGamePropertyArray<type> &a ) : QVector<type>(a)
00051 {
00052 }
00053
00054 bool resize( int size )
00055 {
00056 if (size!= QVector<type>::size())
00057 {
00058 bool a=true;
00059 QByteArray b;
00060 QDataStream s(&b, QIODevice::WriteOnly);
00061 KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdResize);
00062 s << size ;
00063 if (policy()==PolicyClean || policy()==PolicyDirty)
00064 {
00065 if (mOwner)
00066 {
00067 mOwner->sendProperty(s);
00068 }
00069 }
00070 if (policy()==PolicyLocal || policy()==PolicyDirty)
00071 {
00072 extractProperty(b);
00073
00074 }
00075 return a;
00076 }
00077 else return true;
00078 }
00079
00080 void setAt(int i,type data)
00081 {
00082 QByteArray b;
00083 QDataStream s(&b, QIODevice::WriteOnly);
00084 KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdAt);
00085 s << i ;
00086 s << data;
00087 if (policy()==PolicyClean || policy()==PolicyDirty)
00088 {
00089 if (mOwner)
00090 {
00091 mOwner->sendProperty(s);
00092 }
00093 }
00094 if (policy()==PolicyLocal || policy()==PolicyDirty)
00095 {
00096 extractProperty(b);
00097 }
00098
00099 }
00100
00101 const type& at( int i ) const
00102 {
00103 return QVector<type>::at(i);
00104 }
00105
00106 const type& operator[]( int i ) const
00107 {
00108 return QVector<type>::operator[](i);
00109 }
00110
00111 type& operator[]( int i )
00112 {
00113 return QVector<type>::operator[](i);
00114 }
00115
00116 KGamePropertyArray<type> &operator=(const KGamePropertyArray<type> &a)
00117 {
00118 return assign(a);
00119 }
00120
00121 bool truncate( int pos )
00122 {
00123 return resize(pos);
00124 }
00125
00126 bool fill( const type &data, int size = -1 )
00127 {
00128 bool r=true;
00129 QByteArray b;
00130 QDataStream s(&b, QIODevice::WriteOnly);
00131 KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdFill);
00132 s << data;
00133 s << size ;
00134 if (policy()==PolicyClean || policy()==PolicyDirty)
00135 {
00136 if (mOwner)
00137 {
00138 mOwner->sendProperty(s);
00139 }
00140 }
00141 if (policy()==PolicyLocal || policy()==PolicyDirty)
00142 {
00143 extractProperty(b);
00144
00145 }
00146 return r;
00147 }
00148
00149 KGamePropertyArray<type>& assign( const KGamePropertyArray<type>& a )
00150 {
00151
00152 if (policy()==PolicyClean || policy()==PolicyDirty)
00153 {
00154 sendProperty();
00155 }
00156 if (policy()==PolicyLocal || policy()==PolicyDirty)
00157 {
00158 QVector<type>::assign(a);
00159 }
00160 return *this;
00161 }
00162 KGamePropertyArray<type>& assign( const type *a, int n )
00163 {
00164 if (policy()==PolicyClean || policy()==PolicyDirty)
00165 {
00166 sendProperty();
00167 }
00168 if (policy()==PolicyLocal || policy()==PolicyDirty)
00169 {
00170 QVector<type>::assign(a,n);
00171 }
00172 return *this;
00173 }
00174 KGamePropertyArray<type>& duplicate( const KGamePropertyArray<type>& a )
00175 {
00176 if (policy()==PolicyClean || policy()==PolicyDirty)
00177 {
00178 sendProperty();
00179 }
00180 if (policy()==PolicyLocal || policy()==PolicyDirty)
00181 {
00182 QVector<type>::duplicate(a);
00183 }
00184 return *this;
00185 }
00186 KGamePropertyArray<type>& duplicate( const type *a, int n )
00187 {
00188 if (policy()==PolicyClean || policy()==PolicyDirty)
00189 {
00190 sendProperty();
00191 }
00192 if (policy()==PolicyLocal || policy()==PolicyDirty)
00193 {
00194 QVector<type>::duplicate(a,n);
00195 }
00196 return *this;
00197 }
00198 KGamePropertyArray<type>& setRawData( const type *a, int n )
00199 {
00200 if (policy()==PolicyClean || policy()==PolicyDirty)
00201 {
00202 sendProperty();
00203 }
00204 if (policy()==PolicyLocal || policy()==PolicyDirty)
00205 {
00206 QVector<type>::setRawData(a,n);
00207 }
00208 return *this;
00209 }
00210 void sort()
00211 {
00212 QByteArray b;
00213 QDataStream s(b, QIODevice::WriteOnly);
00214 KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdSort);
00215 if (policy()==PolicyLocal || policy()==PolicyDirty)
00216 {
00217 if (mOwner)
00218 {
00219 mOwner->sendProperty(s);
00220 }
00221 }
00222 if (policy()==PolicyLocal || policy()==PolicyDirty)
00223 {
00224 extractProperty(b);
00225 }
00226 }
00227
00228 void load(QDataStream& s)
00229 {
00230
00231 type data;
00232 for (int i=0; i<QVector<type>::size(); i++)
00233 {
00234 s >> data;
00235 QVector<type>::replace(i,data);
00236 }
00237 if (isEmittingSignal())
00238 {
00239 emitSignal();
00240 }
00241 }
00242 void save(QDataStream &s)
00243 {
00244
00245 for (int i=0; i<QVector<type>::size(); i++)
00246 {
00247 s << at(i);
00248 }
00249 }
00250
00251 void command(QDataStream &stream,int msgid, bool isSender)
00252 {
00253 Q_UNUSED(isSender);
00254 KGamePropertyBase::command(stream, msgid);
00255
00256 switch(msgid)
00257 {
00258 case CmdAt:
00259 {
00260 uint i;
00261 type data;
00262 stream >> i >> data;
00263 QVector<type>::replace( i, data );
00264
00265 if (isEmittingSignal())
00266 {
00267 emitSignal();
00268 }
00269 break;
00270 }
00271 case CmdResize:
00272 {
00273 uint size;
00274 stream >> size;
00275
00276 if (( uint )QVector<type>::size() != size)
00277 {
00278 QVector<type>::resize(size);
00279 }
00280 break;
00281 }
00282 case CmdFill:
00283 {
00284 int size;
00285 type data;
00286 stream >> data >> size;
00287
00288 QVector<type>::fill(data,size);
00289 if (isEmittingSignal())
00290 {
00291 emitSignal();
00292 }
00293 break;
00294 }
00295 case CmdSort:
00296 {
00297
00298 qSort( *this );
00299 break;
00300 }
00301 default:
00302 kError(11001) << "Error in KPropertyArray::command: Unknown command" << msgid;
00303 break;
00304 }
00305 }
00306 protected:
00307 void extractProperty(const QByteArray& b)
00308 {
00309 QByteArray _b(b);
00310 QDataStream s(&_b, QIODevice::ReadOnly);
00311 int cmd;
00312 int propId;
00313 KGameMessage::extractPropertyHeader(s, propId);
00314 KGameMessage::extractPropertyCommand(s, propId, cmd);
00315 command(s, cmd, true);
00316 }
00317
00318 };
00319
00320 #endif