• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

libkdegames/libkdegamesprivate/kgame

  • sources
  • kde-4.14
  • kdegames
  • libkdegames
  • libkdegamesprivate
  • kgame
kgameproperty.h
Go to the documentation of this file.
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
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 <QtCore/QDataStream>
25 
26 #include <kdebug.h>
27 #include <typeinfo>
28 #include "../libkdegamesprivate_export.h"
29 
30 class KGame;
31 class KPlayer;
32 class KGamePropertyHandler;
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 
74  enum PropertyCommandIds
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 
112  enum PropertyPolicy
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 
138  KGamePropertyBase();
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>
585 class KGameProperty : public KGamePropertyBase
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 
608  KGameProperty() : KGamePropertyBase() { init(); }
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  kError(11001) << "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  virtual void save(QDataStream &stream)
759  {
760  stream << mData;
761  }
762 
768  const type& value() const
769  {
770  return mData;
771  }
772 
784  virtual void load(QDataStream& s)
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  virtual const type_info* typeinfo() { return &typeid(type); }
838 
839 private:
840  void init() { }
841 
842 private:
843  type mData;
844 };
845 
846 
847 typedef KGameProperty<int> KGamePropertyInt;
848 typedef KGameProperty<unsigned int> KGamePropertyUInt;
849 typedef KGameProperty<QString> KGamePropertyQString;
850 typedef KGameProperty<qint8> KGamePropertyBool;
851 
852 #endif
KGameProperty::KGameProperty
KGameProperty()
This constructor does nothing.
Definition: kgameproperty.h:608
KGamePropertyBase::setEmittingSignal
void setEmittingSignal(bool p)
Sets this property to emit a signal on value changed.
Definition: kgameproperty.h:160
KGamePropertyBase::Flags
Definition: kgameproperty.h:359
KGameProperty::setValue
void setValue(type v)
Set the value depending on the current policy (see setConsistent).
Definition: kgameproperty.h:619
QByteArray
KGameProperty::setLocal
bool setLocal(type v)
This function sets the value of the property directly, i.e.
Definition: kgameproperty.h:719
KGamePropertyBase::isLocked
bool isLocked() const
A locked property can only be changed by the player who has set the lock.
Definition: kgameproperty.h:190
KGameProperty::~KGameProperty
virtual ~KGameProperty()
Definition: kgameproperty.h:610
QDataStream
KGamePropertyHandler
A collection class for KGameProperty objects.
Definition: kgamepropertyhandler.h:73
KGamePropertyBase::setOptimized
void setOptimized(bool p)
Sets this property to try to optimize signal and network handling by not sending it out when the prop...
Definition: kgameproperty.h:172
KGamePropertyBase::setDirty
void setDirty(bool d)
Sets the "dirty" flag of the property.
Definition: kgameproperty.h:318
KGamePropertyBase::PropertyPolicy
PropertyPolicy
The policy of the property.
Definition: kgameproperty.h:112
KGameProperty::changeValue
void changeValue(type v)
This function does both, change the local value and change the network value.
Definition: kgameproperty.h:748
KGameProperty::load
virtual void load(QDataStream &s)
Reads from a stream and assigns the read value to this object.
Definition: kgameproperty.h:784
KGamePropertyInt
KGameProperty< int > KGamePropertyInt
Definition: kgameproperty.h:847
KGamePropertyBase::PropertyCommandIds
PropertyCommandIds
Commands for advanced properties (qint8)
Definition: kgameproperty.h:74
KGameProperty::typeinfo
virtual const type_info * typeinfo()
Definition: kgameproperty.h:837
KGamePropertyBool
KGameProperty< qint8 > KGamePropertyBool
Definition: kgameproperty.h:850
KGameProperty::send
bool send(type v)
This function sends a new value over network.
Definition: kgameproperty.h:674
KGamePropertyBase::isDirty
bool isDirty() const
Definition: kgameproperty.h:183
KGameProperty::KGameProperty
KGameProperty(int id, KGamePropertyHandler *owner)
Constructs a KGameProperty object.
Definition: kgameproperty.h:601
KGamePropertyQString
KGameProperty< QString > KGamePropertyQString
Definition: kgameproperty.h:849
KGamePropertyBase::Flags::flag
char flag
Definition: kgameproperty.h:360
KGamePropertyUInt
KGameProperty< unsigned int > KGamePropertyUInt
Definition: kgameproperty.h:848
KGamePropertyBase::typeinfo
virtual const type_info * typeinfo()
Definition: kgameproperty.h:246
KGamePropertyBase::setPolicy
void setPolicy(PropertyPolicy p)
Changes the consistency policy of a property.
Definition: kgameproperty.h:148
QString
KGamePropertyBase::IdCommand
Definition: kgameproperty.h:65
KPlayer
Base class for a game player.
Definition: kplayer.h:69
KGamePropertyBase::isOptimized
bool isOptimized() const
See also setOptimize.
Definition: kgameproperty.h:178
KGamePropertyBase
Base class of KGameProperty.
Definition: kgameproperty.h:45
KGameProperty::value
const type & value() const
Definition: kgameproperty.h:768
KGameProperty::save
virtual void save(QDataStream &stream)
Saves the object to a stream.
Definition: kgameproperty.h:758
KGame
The main KDE game object.
Definition: kgame.h:60
KGamePropertyBase::PropertyDataIds
PropertyDataIds
Definition: kgameproperty.h:48
KGamePropertyBase::policy
PropertyPolicy policy() const
Definition: kgameproperty.h:153
KGameProperty::operator=
const type & operator=(const KGameProperty &property)
This copies the data of property to the KGameProperty object.
Definition: kgameproperty.h:822
KGamePropertyBase::mOwner
KGamePropertyHandler * mOwner
Definition: kgameproperty.h:354
KGameProperty::operator=
const type & operator=(const type &t)
This calls setValue to change the value of the property.
Definition: kgameproperty.h:811
KGameProperty
A class for network transparent games.
Definition: kgameproperty.h:585
KGamePropertyBase::id
int id() const
Definition: kgameproperty.h:241
KGamePropertyBase::isEmittingSignal
bool isEmittingSignal() const
See also setEmittingSignal.
Definition: kgameproperty.h:166
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:54 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdegames/libkdegamesprivate/kgame

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

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal