libkdegames Library API Documentation

kgamepropertylist.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 __KGAMEPROPERTYLIST_H_
00022 #define __KGAMEPROPERTYLIST_H_
00023 
00024 #include <qvaluelist.h>
00025 
00026 #include <kdebug.h>
00027 
00028 #include "kgamemessage.h"
00029 #include "kgameproperty.h"
00030 #include "kgamepropertyhandler.h"
00031 
00032 // AB: also see README.LIB!
00033 
00034 template<class type>
00035 class KGamePropertyList : public QValueList<type>, public KGamePropertyBase
00036 {
00037 public:
00041     typedef QValueListIterator<type> Iterator;
00042     typedef QValueListConstIterator<type> ConstIterator;
00043 
00044   KGamePropertyList() :QValueList<type>(), KGamePropertyBase()
00045   {
00046   }
00047 
00048   KGamePropertyList( const KGamePropertyList<type> &a ) : QValueList<type>(a)
00049   {
00050     send();
00051   }
00052 
00053   uint findIterator(Iterator me)
00054   {
00055     Iterator it;
00056     uint cnt=0;
00057     for( it = begin(); it != end(); ++it )
00058     {
00059       if (me==it) 
00060       {
00061         return cnt;
00062       }
00063       cnt++;
00064     }
00065     return count();
00066   }
00067 
00068   Iterator insert( Iterator it, const type& d )
00069   {
00070     it=QValueList<type>::insert(it,d);
00071 
00072     QByteArray b;
00073     QDataStream s(b, IO_WriteOnly);
00074     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdInsert);
00075     int i=findIterator(it);
00076     s << i;
00077     s << d;
00078     if (policy() == PolicyClean || policy() == PolicyDirty)
00079     {
00080       if (mOwner)
00081       {
00082         mOwner->sendProperty(s);
00083       }
00084     }
00085     if (policy() == PolicyDirty || policy() == PolicyLocal)
00086     {
00087       extractProperty(b);
00088     }
00089     return it;
00090   }
00091 
00092   void  prepend( const type& d) { insert(begin(),d); }
00093   
00094   void  append( const type& d ) 
00095   {
00096     QByteArray b;
00097     QDataStream s(b, IO_WriteOnly);
00098     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdAppend);
00099     s << d;
00100     if (policy() == PolicyClean || policy() == PolicyDirty)
00101     {
00102       if (mOwner)
00103       {
00104         mOwner->sendProperty(s);
00105       }
00106     }
00107     if (policy() == PolicyDirty || policy() == PolicyLocal)
00108     {
00109       extractProperty(b);
00110     }
00111   }
00112 
00113   Iterator erase( Iterator it )
00114   {
00115     QByteArray b;
00116     QDataStream s(b, IO_WriteOnly);
00117     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdRemove);
00118     int i=findIterator(it);
00119     s << i;
00120     if (policy() == PolicyClean || policy() == PolicyDirty)
00121     {
00122       if (mOwner)
00123       {
00124         mOwner->sendProperty(s);
00125       }
00126     }
00127     if (policy() == PolicyDirty || policy() == PolicyLocal)
00128     {
00129       extractProperty(b);
00130     }
00131     //TODO: return value - is it correct for PolicyLocal|PolicyDirty?
00132 //    return QValueList<type>::remove(it);
00133     return it;
00134   }
00135 
00136   Iterator remove( Iterator it )
00137   {
00138     return erase(it);
00139   }
00140 
00141   void remove( const type& d )
00142   {
00143     Iterator it=find(d);
00144     remove(it);
00145   }
00146 
00147   void clear()
00148   {
00149     QByteArray b;
00150     QDataStream s(b, IO_WriteOnly);
00151     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdClear);
00152     if (policy() == PolicyClean || policy() == PolicyDirty)
00153     {
00154       if (mOwner)
00155       {
00156         mOwner->sendProperty(s);
00157       }
00158     }
00159     if (policy() == PolicyDirty || policy() == PolicyLocal)
00160     {
00161       extractProperty(b);
00162     }
00163   }
00164 
00165   void load(QDataStream& s)
00166   {
00167     kdDebug(11001) << "KGamePropertyList load " << id() << endl;
00168     QValueList<type>::clear();
00169     uint size;
00170     type data;
00171     s >> size;
00172 
00173     for (unsigned int i=0;i<size;i++)
00174     {
00175       s >> data;
00176       QValueList<type>::append(data);
00177     }
00178     if (isEmittingSignal()) emitSignal();
00179   }
00180 
00181   void save(QDataStream &s)
00182   {
00183     kdDebug(11001) << "KGamePropertyList save "<<id() << endl;
00184     type data;
00185     uint size=count();
00186     s << size;
00187     Iterator it;
00188     for( it = begin(); it != end(); ++it )
00189     {
00190       data=*it;
00191       s << data;
00192     }
00193   }
00194 
00195   void command(QDataStream &s,int cmd,bool)
00196   {
00197     KGamePropertyBase::command(s, cmd);
00198     kdDebug(11001) << "---> LIST id="<<id()<<" got command ("<<cmd<<") !!!" <<endl; 
00199     Iterator it;
00200     switch(cmd)
00201     {
00202       case CmdInsert:
00203       {
00204         uint i;
00205         type data;
00206         s >> i >> data;
00207         it=at(i);
00208         QValueList<type>::insert(it,data);
00209 //        kdDebug(11001) << "CmdInsert:id="<<id()<<" i="<<i<<" data="<<data <<endl; 
00210         if (isEmittingSignal()) emitSignal();
00211         break;
00212       }
00213       case CmdAppend:
00214       {
00215         type data;
00216     s >> data;
00217         QValueList<type>::append(data);
00218 //        kdDebug(11001) << "CmdAppend:id=" << id() << " data=" << data << endl; 
00219         if (isEmittingSignal()) emitSignal();
00220     break;
00221       }
00222       case CmdRemove:
00223       {
00224         uint i;
00225         s >> i;
00226         it=at(i);
00227         QValueList<type>::remove(it);
00228         kdDebug(11001) << "CmdRemove:id="<<id()<<" i="<<i <<endl; 
00229         if (isEmittingSignal()) emitSignal();
00230         break;
00231       }
00232       case CmdClear:
00233       {
00234         QValueList<type>::clear();
00235         kdDebug(11001) << "CmdClear:id="<<id()<<endl; 
00236         if (isEmittingSignal()) emitSignal();
00237         break;
00238       }
00239       default: 
00240         kdDebug(11001) << "Error in KPropertyList::command: Unknown command " << cmd << endl;
00241     }
00242   }
00243 
00244 protected:
00245   void extractProperty(const QByteArray& b)
00246   // this is called for Policy[Dirty|Local] after putting the stuff into the
00247   // stream
00248   {
00249     QDataStream s(b, IO_ReadOnly);
00250     int cmd;
00251     int propId;
00252     KGameMessage::extractPropertyHeader(s, propId);
00253     KGameMessage::extractPropertyCommand(s, propId, cmd);
00254     command(s, cmd, true);
00255   }
00256 
00257 };
00258 
00259 #endif
KDE Logo
This file is part of the documentation for libkdegames Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Mar 12 22:53:43 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003