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