libkdegames Library API Documentation

kgamepropertyarray.h

00001 /* 00002 This file is part of the KDE games library 00003 Copyright (C) 2001 Martin Heni (martin@heni-online.de) 00004 Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de) 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library 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 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 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 //kdDebug(11001) << "KGamePropertyArray init" << endl; 00039 } 00040 00041 KGamePropertyArray( int size ) 00042 { 00043 resize(size); 00044 } 00045 00046 KGamePropertyArray( const KGamePropertyArray<type> &a ) : QMemArray<type>(a) 00047 { 00048 QMemArray<type>::send(); 00049 } 00050 00051 bool resize( uint size ) 00052 { 00053 if (size!=QMemArray<type>::size()) 00054 { 00055 bool a=true; 00056 QByteArray b; 00057 QDataStream s(b, IO_WriteOnly); 00058 KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdResize); 00059 s << size ; 00060 if (policy()==PolicyClean || policy()==PolicyDirty) 00061 { 00062 if (mOwner) 00063 { 00064 mOwner->sendProperty(s); 00065 } 00066 } 00067 if (policy()==PolicyLocal || policy()==PolicyDirty) 00068 { 00069 extractProperty(b); 00070 // a=QMemArray<type>::resize(size);// FIXME: return value! 00071 } 00072 return a; 00073 } 00074 else return true; 00075 } 00076 00077 void setAt(uint i,type data) 00078 { 00079 QByteArray b; 00080 QDataStream s(b, IO_WriteOnly); 00081 KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdAt); 00082 s << i ; 00083 s << data; 00084 if (policy()==PolicyClean || policy()==PolicyDirty) 00085 { 00086 if (mOwner) 00087 { 00088 mOwner->sendProperty(s); 00089 } 00090 } 00091 if (policy()==PolicyLocal || policy()==PolicyDirty) 00092 { 00093 extractProperty(b); 00094 } 00095 //kdDebug(11001) << "KGamePropertyArray setAt send COMMAND for id="<<id() << " type=" << 1 << " at(" << i<<")="<<data << endl; 00096 } 00097 00098 type at( uint i ) const 00099 { 00100 return QMemArray<type>::at(i); 00101 } 00102 00103 type operator[]( int i ) const 00104 { 00105 return QMemArray<type>::at(i); 00106 } 00107 00108 KGamePropertyArray<type> &operator=(const KGamePropertyArray<type> &a) 00109 { 00110 return assign(a); 00111 } 00112 00113 bool truncate( uint pos ) 00114 { 00115 return resize(pos); 00116 } 00117 00118 bool fill( const type &data, int size = -1 ) 00119 { 00120 bool r=true; 00121 QByteArray b; 00122 QDataStream s(b, IO_WriteOnly); 00123 KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdFill); 00124 s << data; 00125 s << size ; 00126 if (policy()==PolicyClean || policy()==PolicyDirty) 00127 { 00128 if (mOwner) 00129 { 00130 mOwner->sendProperty(s); 00131 } 00132 } 00133 if (policy()==PolicyLocal || policy()==PolicyDirty) 00134 { 00135 extractProperty(b); 00136 // r=QMemArray<type>::fill(data,size);//FIXME: return value! 00137 } 00138 return r; 00139 } 00140 00141 KGamePropertyArray<type>& assign( const KGamePropertyArray<type>& a ) 00142 { 00143 // note: send() has been replaced by sendProperty so it might be broken now! 00144 if (policy()==PolicyClean || policy()==PolicyDirty) 00145 { 00146 sendProperty(); 00147 } 00148 if (policy()==PolicyLocal || policy()==PolicyDirty) 00149 { 00150 QMemArray<type>::assign(a); 00151 } 00152 return *this; 00153 } 00154 KGamePropertyArray<type>& assign( const type *a, uint n ) 00155 { 00156 if (policy()==PolicyClean || policy()==PolicyDirty) 00157 { 00158 sendProperty(); 00159 } 00160 if (policy()==PolicyLocal || policy()==PolicyDirty) 00161 { 00162 QMemArray<type>::assign(a,n); 00163 } 00164 return *this; 00165 } 00166 KGamePropertyArray<type>& duplicate( const KGamePropertyArray<type>& a ) 00167 { 00168 if (policy()==PolicyClean || policy()==PolicyDirty) 00169 { 00170 sendProperty(); 00171 } 00172 if (policy()==PolicyLocal || policy()==PolicyDirty) 00173 { 00174 QMemArray<type>::duplicate(a); 00175 } 00176 return *this; 00177 } 00178 KGamePropertyArray<type>& duplicate( const type *a, uint n ) 00179 { 00180 if (policy()==PolicyClean || policy()==PolicyDirty) 00181 { 00182 sendProperty(); 00183 } 00184 if (policy()==PolicyLocal || policy()==PolicyDirty) 00185 { 00186 QMemArray<type>::duplicate(a,n); 00187 } 00188 return *this; 00189 } 00190 KGamePropertyArray<type>& setRawData( const type *a, uint n ) 00191 { 00192 if (policy()==PolicyClean || policy()==PolicyDirty) 00193 { 00194 sendProperty(); 00195 } 00196 if (policy()==PolicyLocal || policy()==PolicyDirty) 00197 { 00198 QMemArray<type>::setRawData(a,n); 00199 } 00200 return *this; 00201 } 00202 void sort() 00203 { 00204 QByteArray b; 00205 QDataStream s(b, IO_WriteOnly); 00206 KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdSort); 00207 if (policy()==PolicyLocal || policy()==PolicyDirty) 00208 { 00209 if (mOwner) 00210 { 00211 mOwner->sendProperty(s); 00212 } 00213 } 00214 if (policy()==PolicyLocal || policy()==PolicyDirty) 00215 { 00216 extractProperty(b); 00217 } 00218 } 00219 00220 void load(QDataStream& s) 00221 { 00222 //kdDebug(11001) << "KGamePropertyArray load " << id() << endl; 00223 type data; 00224 for (unsigned int i=0; i<QMemArray<type>::size(); i++) 00225 { 00226 s >> data; 00227 QMemArray<type>::at(i)=data; 00228 } 00229 if (isEmittingSignal()) 00230 { 00231 emitSignal(); 00232 } 00233 } 00234 void save(QDataStream &s) 00235 { 00236 //kdDebug(11001) << "KGamePropertyArray save "<<id() << endl; 00237 for (unsigned int i=0; i<QMemArray<type>::size(); i++) 00238 { 00239 s << at(i); 00240 } 00241 } 00242 00243 void command(QDataStream &s,int cmd,bool) 00244 { 00245 KGamePropertyBase::command(s, cmd); 00246 //kdDebug(11001) << "Array id="<<id()<<" got command ("<<cmd<<") !!!" <<endl; 00247 switch(cmd) 00248 { 00249 case CmdAt: 00250 { 00251 uint i; 00252 type data; 00253 s >> i >> data; 00254 QMemArray<type>::at(i)=data; 00255 //kdDebug(11001) << "CmdAt:id="<<id()<<" i="<<i<<" data="<<data <<endl; 00256 if (isEmittingSignal()) 00257 { 00258 emitSignal(); 00259 } 00260 break; 00261 } 00262 case CmdResize: 00263 { 00264 uint size; 00265 s >> size; 00266 //kdDebug(11001) << "CmdResize:id="<<id()<<" oldsize="<<QMemArray<type>::size()<<" newsize="<<size <<endl; 00267 if (QMemArray<type>::size() != size) 00268 { 00269 QMemArray<type>::resize(size); 00270 } 00271 break; 00272 } 00273 case CmdFill: 00274 { 00275 int size; 00276 type data; 00277 s >> data >> size; 00278 //kdDebug(11001) << "CmdFill:id="<<id()<<"size="<<size <<endl; 00279 QMemArray<type>::fill(data,size); 00280 if (isEmittingSignal()) 00281 { 00282 emitSignal(); 00283 } 00284 break; 00285 } 00286 case CmdSort: 00287 { 00288 //kdDebug(11001) << "CmdSort:id="<<id()<<endl; 00289 QMemArray<type>::sort(); 00290 break; 00291 } 00292 default: 00293 kdError(11001) << "Error in KPropertyArray::command: Unknown command " << cmd << endl; 00294 break; 00295 } 00296 } 00297 protected: 00298 void extractProperty(const QByteArray& b) 00299 { 00300 QDataStream s(b, IO_ReadOnly); 00301 int cmd; 00302 int propId; 00303 KGameMessage::extractPropertyHeader(s, propId); 00304 KGameMessage::extractPropertyCommand(s, propId, cmd); 00305 command(s, cmd, true); 00306 } 00307 00308 }; 00309 00310 #endif
KDE Logo
This file is part of the documentation for libkdegames Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 13 12:48:55 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003