• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames/kgame

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

libkdegames/kgame

Skip menu "libkdegames/kgame"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal