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 } 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 // a=QMemArray<type>::resize(size);// FIXME: return value! 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 //kdDebug(11001) << "KGamePropertyArray setAt send COMMAND for id="<<id() << " type=" << 1 << " at(" << i<<")="<<data << endl; 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 // r=QMemArray<type>::fill(data,size);//FIXME: return value! 00136 } 00137 return r; 00138 } 00139 00140 KGamePropertyArray<type>& assign( const KGamePropertyArray<type>& a ) 00141 { 00142 // note: send() has been replaced by sendProperty so it might be broken now! 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 //kdDebug(11001) << "KGamePropertyArray load " << id() << endl; 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 //kdDebug(11001) << "KGamePropertyArray save "<<id() << endl; 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 //kdDebug(11001) << "Array id="<<id()<<" got command ("<<cmd<<") !!!" <<endl; 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 //kdDebug(11001) << "CmdAt:id="<<id()<<" i="<<i<<" data="<<data <<endl; 00255 if (isEmittingSignal()) 00256 { 00257 emitSignal(); 00258 } 00259 break; 00260 } 00261 case CmdResize: 00262 { 00263 uint size; 00264 s >> size; 00265 //kdDebug(11001) << "CmdResize:id="<<id()<<" oldsize="<<QMemArray<type>::size()<<" newsize="<<size <<endl; 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 //kdDebug(11001) << "CmdFill:id="<<id()<<"size="<<size <<endl; 00278 QMemArray<type>::fill(data,size); 00279 if (isEmittingSignal()) 00280 { 00281 emitSignal(); 00282 } 00283 break; 00284 } 00285 case CmdSort: 00286 { 00287 //kdDebug(11001) << "CmdSort:id="<<id()<<endl; 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
KDE Logo
This file is part of the documentation for libkdegames Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Aug 26 00:21:40 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003