KDEGames

kgameproperty.h
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001 Andreas Beckermann ([email protected])
4  Copyright (C) 2001 Martin Heni (kde at heni-online.de)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #ifndef __KGAMEPROPERTY_H_
22 #define __KGAMEPROPERTY_H_
23 
24 #include <QDataStream>
25 #include <QDebug>
26 
27 #include <typeinfo>
28 #include "libkdegamesprivate_export.h"
29 
30 class KGame;
31 class KPlayer;
33 using namespace std;
34 
45 class KDEGAMESPRIVATE_EXPORT KGamePropertyBase
46 {
47 public:
48  enum PropertyDataIds { // these belong to KPlayer/KGame!
49  //KPlayer
50  IdGroup=1,
51  IdUserId=2,
52  IdAsyncInput=3,
53  IdTurn=4,
54  IdName=5,
55 
56  //KGame
57  IdGameStatus=6,
58  IdMaxPlayer=7,
59  IdMinPlayer=8,
60 
61  // Input Grabbing
62  IdGrabInput=16,
63  IdReleaseInput=17,
64 
65  IdCommand, // Reserved for internal use
66  IdUser=256,
67 
68  IdAutomatic=0x7000 // Id's from here on are automatically given (16bit)
69  };
70 
75  {
76  // General
77  CmdLock=1,
78 
79  // Array
80  CmdAt=51,
81  CmdResize=52,
82  CmdFill=53,
83  CmdSort=54,
84  // List (could be the same id's actually)
85  CmdInsert=61,
86  CmdAppend=62,
87  CmdRemove=63,
88  CmdClear=64
89  };
90 
113  {
114  PolicyUndefined = 0,
115  PolicyClean = 1,
116  PolicyDirty = 2,
117  PolicyLocal = 3
118  };
119 
120 
129  KGamePropertyBase(int id, KGamePropertyHandler* owner);
130 
131  KGamePropertyBase(int id, KGame* parent);
132  KGamePropertyBase(int id, KPlayer* parent);
133 
139 
140  virtual ~KGamePropertyBase();
141 
148  void setPolicy(PropertyPolicy p) { mFlags.bits.policy = p; }
149 
153  PropertyPolicy policy() const { return (PropertyPolicy)mFlags.bits.policy; }
154 
160  void setEmittingSignal(bool p) { mFlags.bits.emitsignal=p; }
161 
166  bool isEmittingSignal() const { return mFlags.bits.emitsignal; }
167 
172  void setOptimized(bool p) { mFlags.bits.optimize = p ; }
173 
178  bool isOptimized() const { return mFlags.bits.optimize; }
179 
183  bool isDirty() const { return mFlags.bits.dirty; }
184 
190  bool isLocked() const { return mFlags.bits.locked; }
191 
203  bool lock();
204 
216  bool unlock(bool force=false);
217 
223  virtual void load(QDataStream& s) = 0;
224 
228  virtual void save(QDataStream& s) = 0;
229 
236  virtual void command(QDataStream &stream, int msgid, bool isSender=false);
237 
241  int id() const { return mId; }
242 
246  virtual const type_info* typeinfo() { return &typeid(this); }
247 
266  int registerData(int id, KGamePropertyHandler* owner,PropertyPolicy p, const QString& name=QString());
267 
272  int registerData(int id, KGamePropertyHandler* owner, const QString& name=QString());
273 
278  int registerData(int id, KGame* owner, const QString& name=QString());
279 
284  int registerData(int id, KPlayer* owner, const QString& name=QString());
285 
292  int registerData(KGamePropertyHandler* owner,PropertyPolicy p=PolicyUndefined, const QString& name=QString() );
293 
294  void unregisterData();
295 
296 
297 protected:
308  void setLock(bool l);
309 
318  void setDirty(bool d) { mFlags.bits.dirty = d ; }
319 
331  bool sendProperty();
332 
346  bool sendProperty(const QByteArray& b);
347 
351  void emitSignal();
352 
353 protected:
354  KGamePropertyHandler* mOwner;
355 
356  // Having this as a union of the bitfield and the char
357  // allows us to stream this quantity easily (if we need to)
358  // At the moment it is not yet transmitted
359  union Flags {
360  char flag;
361  struct {
362  // unsigned char dosave : 1; // do save this property
363  // unsigned char delaytransmit : 1; // do not send immediately on
364  // change but a KPlayer:QTimer
365  // sends it later on - fast
366  // changing variables
367  unsigned char emitsignal : 1; // KPlayer notifies on variable change (true)
368  //unsigned char readonly : 1; // whether the property can be changed (false)
369  unsigned char optimize : 1; // whether the property tries to optimize send/emit (false)
370  unsigned char dirty: 1; // whether the property dirty (setLocal() was used)
371  unsigned char policy : 2; // whether the property is always consistent (see PropertyPolicy)
372  unsigned char locked: 1; // whether the property is locked (true)
373  } bits;
374  } mFlags;
375 
376 private:
377  friend class KGamePropertyHandler;
378  void init();
379 
380 private:
381  int mId;
382 
383 };
384 
584 template<class type>
586 {
587 public:
600 // TODO: ID: Very ugly - better use something like parent()->propertyId() or so which assigns a free id automatically.
601  KGameProperty(int id, KGamePropertyHandler* owner) : KGamePropertyBase(id, owner) { init(); }
602 
609 
610  virtual ~KGameProperty() {}
611 
619  void setValue(type v)
620  {
621  switch (policy()) {
622  case PolicyClean:
623  send(v);
624  break;
625  case PolicyDirty:
626  changeValue(v);
627  break;
628  case PolicyLocal:
629  setLocal(v);
630  break;
631  default: // NEVER!
632  qCritical() << "Undefined Policy in property" << id();
633  return;
634  }
635  }
636 
637 
674  bool send(type v)
675  {
676  if (isOptimized() && mData == v) {
677  return true;
678  }
679  if (isLocked()) {
680  return false;
681  }
682  QByteArray b;
683  QDataStream stream(&b, QIODevice::WriteOnly);
684  stream << v;
685  if (!sendProperty(b)) {
686  setLocal(v);
687  return false;
688  }
689  return true;
690  }
691 
719  bool setLocal(type v)
720  {
721  if (isOptimized() && mData == v) {
722  return false;
723  }
724  if (isLocked()) {
725  return false;
726  }
727  mData = v;
728  setDirty(true);
729  if (isEmittingSignal()) {
730  emitSignal();
731  }
732  return true;
733  }
734 
748  void changeValue(type v)
749  {
750  send(v);
751  setLocal(v);
752  }
753 
758  void save(QDataStream &stream) override
759  {
760  stream << mData;
761  }
762 
768  const type& value() const
769  {
770  return mData;
771  }
772 
784  void load(QDataStream& s) override
785  {
786  s >> mData;
787  setDirty(false);
788  if (isEmittingSignal()) {
789  emitSignal();
790  }
791  }
792 
811  const type& operator=(const type& t)
812  {
813  setValue(t);
814  return value();
815  }
816 
822  const type& operator=(const KGameProperty& property)
823  {
824  setValue(property.value());
825  return value();
826  }
827 
835  operator type() const { return value(); }
836 
837  const type_info* typeinfo() override { return &typeid(type); }
838 
839 private:
840  void init() { }
841 
842 private:
843  type mData;
844 };
845 
846 
851 
852 #endif
KGameProperty()
This constructor does nothing.
void setEmittingSignal(bool p)
Sets this property to emit a signal on value changed.
void setValue(type v)
Set the value depending on the current policy (see setConsistent).
bool setLocal(type v)
This function sets the value of the property directly, i.e.
bool isLocked() const
A locked property can only be changed by the player who has set the lock.
A collection class for KGameProperty objects.
void setOptimized(bool p)
Sets this property to try to optimize signal and network handling by not sending it out when the prop...
void setDirty(bool d)
Sets the "dirty" flag of the property.
PropertyPolicy
The policy of the property.
void changeValue(type v)
This function does both, change the local value and change the network value.
void save(QDataStream &stream) override
Saves the object to a stream.
PropertyCommandIds
Commands for advanced properties (qint8)
Definition: kgameproperty.h:74
const type_info * typeinfo() override
bool send(type v)
This function sends a new value over network.
bool isDirty() const
KGameProperty(int id, KGamePropertyHandler *owner)
Constructs a KGameProperty object.
void load(QDataStream &s) override
Reads from a stream and assigns the read value to this object.
virtual const type_info * typeinfo()
void setPolicy(PropertyPolicy p)
Changes the consistency policy of a property.
Base class for a game player.
Definition: kplayer.h:69
bool isOptimized() const
See also setOptimize.
Base class of KGameProperty.
Definition: kgameproperty.h:45
const type & value() const
QCA_EXPORT void init()
The main KDE game object.
Definition: kgame.h:64
PropertyPolicy policy() const
const type & operator=(const KGameProperty &property)
This copies the data of property to the KGameProperty object.
const type & operator=(const type &t)
This calls setValue to change the value of the property.
A class for network transparent games.
int id() const
bool isEmittingSignal() const
See also setEmittingSignal.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Nov 26 2020 22:36:18 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.