kgamepropertylist.h

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, 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   }
00051 
00052   uint findIterator(Iterator me)
00053   {
00054     Iterator it;
00055     uint cnt=0;
00056     for( it = begin(); it != end(); ++it )
00057     {
00058       if (me==it) 
00059       {
00060         return cnt;
00061       }
00062       cnt++;
00063     }
00064     return count();
00065   }
00066 
00067   Iterator insert( Iterator it, const type& d )
00068   {
00069     it=QValueList<type>::insert(it,d);
00070 
00071     QByteArray b;
00072     QDataStream s(b, IO_WriteOnly);
00073     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdInsert);
00074     int i=findIterator(it);
00075     s << i;
00076     s << d;
00077     if (policy() == PolicyClean || policy() == PolicyDirty)
00078     {
00079       if (mOwner)
00080       {
00081         mOwner->sendProperty(s);
00082       }
00083     }
00084     if (policy() == PolicyDirty || policy() == PolicyLocal)
00085     {
00086       extractProperty(b);
00087     }
00088     return it;
00089   }
00090 
00091   void  prepend( const type& d) { insert(begin(),d); }
00092   
00093   void  append( const type& d ) 
00094   {
00095     QByteArray b;
00096     QDataStream s(b, IO_WriteOnly);
00097     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdAppend);
00098     s << d;
00099     if (policy() == PolicyClean || policy() == PolicyDirty)
00100     {
00101       if (mOwner)
00102       {
00103         mOwner->sendProperty(s);
00104       }
00105     }
00106     if (policy() == PolicyDirty || policy() == PolicyLocal)
00107     {
00108       extractProperty(b);
00109     }
00110   }
00111 
00112   Iterator erase( Iterator it )
00113   {
00114     QByteArray b;
00115     QDataStream s(b, IO_WriteOnly);
00116     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdRemove);
00117     int i=findIterator(it);
00118     s << i;
00119     if (policy() == PolicyClean || policy() == PolicyDirty)
00120     {
00121       if (mOwner)
00122       {
00123         mOwner->sendProperty(s);
00124       }
00125     }
00126     if (policy() == PolicyDirty || policy() == PolicyLocal)
00127     {
00128       extractProperty(b);
00129     }
00130     //TODO: return value - is it correct for PolicyLocal|PolicyDirty?
00131 //    return QValueList<type>::remove(it);
00132     return it;
00133   }
00134 
00135   Iterator remove( Iterator it )
00136   {
00137     return erase(it);
00138   }
00139 
00140   void remove( const type& d )
00141   {
00142     Iterator it=find(d);
00143     remove(it);
00144   }
00145 
00146   void clear()
00147   {
00148     QByteArray b;
00149     QDataStream s(b, IO_WriteOnly);
00150     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdClear);
00151     if (policy() == PolicyClean || policy() == PolicyDirty)
00152     {
00153       if (mOwner)
00154       {
00155         mOwner->sendProperty(s);
00156       }
00157     }
00158     if (policy() == PolicyDirty || policy() == PolicyLocal)
00159     {
00160       extractProperty(b);
00161     }
00162   }
00163 
00164   void load(QDataStream& s)
00165   {
00166     kdDebug(11001) << "KGamePropertyList load " << id() << endl;
00167     QValueList<type>::clear();
00168     uint size;
00169     type data;
00170     s >> size;
00171 
00172     for (unsigned int i=0;i<size;i++)
00173     {
00174       s >> data;
00175       QValueList<type>::append(data);
00176     }
00177     if (isEmittingSignal()) emitSignal();
00178   }
00179 
00180   void save(QDataStream &s)
00181   {
00182     kdDebug(11001) << "KGamePropertyList save "<<id() << endl;
00183     type data;
00184     uint size=count();
00185     s << size;
00186     Iterator it;
00187     for( it = begin(); it != end(); ++it )
00188     {
00189       data=*it;
00190       s << data;
00191     }
00192   }
00193 
00194   void command(QDataStream &s,int cmd,bool)
00195   {
00196     KGamePropertyBase::command(s, cmd);
00197     kdDebug(11001) << "---> LIST id="<<id()<<" got command ("<<cmd<<") !!!" <<endl; 
00198     Iterator it;
00199     switch(cmd)
00200     {
00201       case CmdInsert:
00202       {
00203         uint i;
00204         type data;
00205         s >> i >> data;
00206         it=at(i);
00207         QValueList<type>::insert(it,data);
00208 //        kdDebug(11001) << "CmdInsert:id="<<id()<<" i="<<i<<" data="<<data <<endl; 
00209         if (isEmittingSignal()) emitSignal();
00210         break;
00211       }
00212       case CmdAppend:
00213       {
00214         type data;
00215         s >> data;
00216         QValueList<type>::append(data);
00217 //        kdDebug(11001) << "CmdAppend:id=" << id() << " data=" << data << endl; 
00218         if (isEmittingSignal()) emitSignal();
00219         break;
00220       }
00221       case CmdRemove:
00222       {
00223         uint i;
00224         s >> i;
00225         it=at(i);
00226         QValueList<type>::remove(it);
00227         kdDebug(11001) << "CmdRemove:id="<<id()<<" i="<<i <<endl; 
00228         if (isEmittingSignal()) emitSignal();
00229         break;
00230       }
00231       case CmdClear:
00232       {
00233         QValueList<type>::clear();
00234         kdDebug(11001) << "CmdClear:id="<<id()<<endl; 
00235         if (isEmittingSignal()) emitSignal();
00236         break;
00237       }
00238       default: 
00239         kdDebug(11001) << "Error in KPropertyList::command: Unknown command " << cmd << endl;
00240     }
00241   }
00242 
00243 protected:
00244   void extractProperty(const QByteArray& b)
00245   // this is called for Policy[Dirty|Local] after putting the stuff into the
00246   // stream
00247   {
00248     QDataStream s(b, IO_ReadOnly);
00249     int cmd;
00250     int propId;
00251     KGameMessage::extractPropertyHeader(s, propId);
00252     KGameMessage::extractPropertyCommand(s, propId, cmd);
00253     command(s, cmd, true);
00254   }
00255 
00256 };
00257 
00258 #endif

Generated on Wed Aug 23 18:04:18 2006 for libkdegames by  doxygen 1.4.6