• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
  • kgame
kgameproperty.h
1 /*
2  This file is part of the TDE games library
3  Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
4  Copyright (C) 2001 Martin Heni (martin@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 <tqdatastream.h>
25 
26 #include <kdebug.h>
27 #include <typeinfo>
28 #include <tdemacros.h>
29 class KGame;
30 class KPlayer;
31 class KGamePropertyHandler;
32 using namespace std;
33 
42 class TDE_EXPORT KGamePropertyBase
43 {
44 public:
45  enum PropertyDataIds { // these belong to KPlayer/KGame!
46  //KPlayer
47  IdGroup=1,
48  IdUserId=2,
49  IdAsyncInput=3,
50  IdTurn=4,
51  IdName=5,
52 
53  //KGame
54  IdGameStatus=6,
55  IdMaxPlayer=7,
56  IdMinPlayer=8,
57 
58  // Input Grabbing
59  IdGrabInput=16,
60  IdReleaseInput=17,
61 
62  IdCommand, // Reserved for internal use
63  IdUser=256,
64 
65  IdAutomatic=0x7000 // Id's from here on are automatically given (16bit)
66  };
67 
71  enum PropertyCommandIds
72  {
73  // General
74  CmdLock=1,
75 
76  // Array
77  CmdAt=51,
78  CmdResize=52,
79  CmdFill=53,
80  CmdSort=54,
81  // List (could be the same id's actually)
82  CmdInsert=61,
83  CmdAppend=62,
84  CmdRemove=63,
85  CmdClear=64
86  };
87 
109  enum PropertyPolicy
110  {
111  PolicyUndefined = 0,
112  PolicyClean = 1,
113  PolicyDirty = 2,
114  PolicyLocal = 3
115  };
116 
117 
126  KGamePropertyBase(int id, KGamePropertyHandler* owner);
127 
128  KGamePropertyBase(int id, KGame* parent);
129  KGamePropertyBase(int id, KPlayer* parent);
130 
135  KGamePropertyBase();
136 
137  virtual ~KGamePropertyBase();
138 
145  void setPolicy(PropertyPolicy p) { mFlags.bits.policy = p; }
146 
150  PropertyPolicy policy() const { return (PropertyPolicy)mFlags.bits.policy; }
151 
157  void setEmittingSignal(bool p) { mFlags.bits.emitsignal=p; }
158 
163  bool isEmittingSignal() const { return mFlags.bits.emitsignal; }
164 
169  void setOptimized(bool p) { mFlags.bits.optimize = p ; }
170 
175  bool isOptimized() const { return mFlags.bits.optimize; }
176 
180  bool isDirty() const { return mFlags.bits.dirty; }
181 
187  bool isLocked() const { return mFlags.bits.locked; }
188 
200  bool lock();
201 
213  bool unlock(bool force=false);
214 
220  virtual void load(TQDataStream& s) = 0;
221 
225  virtual void save(TQDataStream& s) = 0;
226 
233  virtual void command(TQDataStream &stream, int msgid, bool isSender=false);
234 
238  int id() const { return mId; }
239 
244  virtual const type_info* typeinfo() { return &typeid(this); }
245 
264  int registerData(int id, KGamePropertyHandler* owner,PropertyPolicy p, TQString name=0);
265 
270  int registerData(int id, KGamePropertyHandler* owner, TQString name=0);
271 
276  int registerData(int id, KGame* owner, TQString name=0);
277 
282  int registerData(int id, KPlayer* owner, TQString name=0);
283 
290  int registerData(KGamePropertyHandler* owner,PropertyPolicy p=PolicyUndefined, TQString name=0);
291 
292  void unregisterData();
293 
294 
295 protected:
306  void setLock(bool l);
307 
316  void setDirty(bool d) { mFlags.bits.dirty = d ; }
317 
329  bool sendProperty();
330 
344  bool sendProperty(const TQByteArray& b);
345 
349  void emitSignal();
350 
351 protected:
352  KGamePropertyHandler* mOwner;
353 
354  // Having this as a union of the bitfield and the char
355  // allows us to stream this quantity easily (if we need to)
356  // At the moment it is not yet transmitted
357  union Flags {
358  char flag;
359  struct {
360  // unsigned char dosave : 1; // do save this property
361  // unsigned char delaytransmit : 1; // do not send immediately on
362  // change but a KPlayer:TQTimer
363  // sends it later on - fast
364  // changing variables
365  unsigned char emitsignal : 1; // KPlayer notifies on variable change (true)
366  //unsigned char readonly : 1; // whether the property can be changed (false)
367  unsigned char optimize : 1; // whether the property tries to optimize send/emit (false)
368  unsigned char dirty: 1; // whether the property dirty (setLocal() was used)
369  unsigned char policy : 2; // whether the property is always consistent (see PropertyPolicy)
370  unsigned char locked: 1; // whether the property is locked (true)
371  } bits;
372  } mFlags;
373 
374 private:
375  friend class KGamePropertyHandler;
376  void init();
377 
378 private:
379  int mId;
380 
381 };
382 
580 template<class type>
581 class KGameProperty : public KGamePropertyBase
582 {
583 public:
596 // TODO: ID: Very ugly - better use something like parent()->propertyId() or so which assigns a free id automatically.
597  KGameProperty(int id, KGamePropertyHandler* owner) : KGamePropertyBase(id, owner) { init(); }
598 
604  KGameProperty() : KGamePropertyBase() { init(); }
605 
606  virtual ~KGameProperty() {}
607 
615  void setValue(type v)
616  {
617  switch (policy()) {
618  case PolicyClean:
619  send(v);
620  break;
621  case PolicyDirty:
622  changeValue(v);
623  break;
624  case PolicyLocal:
625  setLocal(v);
626  break;
627  default: // NEVER!
628  kdError(11001) << "Undefined Policy in property " << id() << endl;
629  return;
630  }
631  }
632 
633 
670  bool send(type v)
671  {
672  if (isOptimized() && mData == v) {
673  return true;
674  }
675  if (isLocked()) {
676  return false;
677  }
678  TQByteArray b;
679  TQDataStream stream(b, IO_WriteOnly);
680  stream << v;
681  if (!sendProperty(b)) {
682  setLocal(v);
683  return false;
684  }
685  return true;
686  }
687 
715  bool setLocal(type v)
716  {
717  if (isOptimized() && mData == v) {
718  return false;
719  }
720  if (isLocked()) {
721  return false;
722  }
723  mData = v;
724  setDirty(true);
725  if (isEmittingSignal()) {
726  emitSignal();
727  }
728  return true;
729  }
730 
744  void changeValue(type v)
745  {
746  send(v);
747  setLocal(v);
748  }
749 
754  virtual void save(TQDataStream &stream)
755  {
756  stream << mData;
757  }
758 
764  const type& value() const
765  {
766  return mData;
767  }
768 
780  virtual void load(TQDataStream& s)
781  {
782  s >> mData;
783  setDirty(false);
784  if (isEmittingSignal()) {
785  emitSignal();
786  }
787  }
788 
807  const type& operator=(const type& t)
808  {
809  setValue(t);
810  return value();
811  }
812 
818  const type& operator=(const KGameProperty& property)
819  {
820  setValue(property.value());
821  return value();
822  }
823 
831  operator type() const { return value(); }
832 
833  virtual const type_info* typeinfo() { return &typeid(type); }
834 
835 private:
836  void init() { }
837 
838 private:
839  type mData;
840 };
841 
842 
843 typedef KGameProperty<int> KGamePropertyInt;
844 typedef KGameProperty<unsigned int> KGamePropertyUInt;
845 typedef KGameProperty<TQString> KGamePropertyTQString;
846 typedef KGameProperty<TQ_INT8> KGamePropertyBool;
847 
848 #endif
KGamePropertyBase::isOptimized
bool isOptimized() const
See also setOptimize.
Definition: kgameproperty.h:175
KGameProperty::KGameProperty
KGameProperty()
This constructor does nothing.
Definition: kgameproperty.h:604
KGamePropertyBase::setEmittingSignal
void setEmittingSignal(bool p)
Sets this property to emit a signal on value changed.
Definition: kgameproperty.h:157
KGameProperty::setValue
void setValue(type v)
Set the value depending on the current policy (see setConsistent).
Definition: kgameproperty.h:615
KGamePropertyBase::isDirty
bool isDirty() const
Definition: kgameproperty.h:180
KGameProperty::setLocal
bool setLocal(type v)
This function sets the value of the property directly, i.e.
Definition: kgameproperty.h:715
KGamePropertyHandler
A collection class for KGameProperty objects.
Definition: kgamepropertyhandler.h:72
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:169
KGamePropertyBase::setDirty
void setDirty(bool d)
Sets the "dirty" flag of the property.
Definition: kgameproperty.h:316
KGamePropertyBase::id
int id() const
Definition: kgameproperty.h:238
KGamePropertyBase::PropertyPolicy
PropertyPolicy
The policy of the property.
Definition: kgameproperty.h:109
KGameProperty::changeValue
void changeValue(type v)
This function does both, change the local value and change the network value.
Definition: kgameproperty.h:744
std
KGamePropertyBase::PropertyCommandIds
PropertyCommandIds
Commands for advanced properties (TQ_INT8)
Definition: kgameproperty.h:71
KGameProperty::value
const type & value() const
Definition: kgameproperty.h:764
KGameProperty::typeinfo
virtual const type_info * typeinfo()
Definition: kgameproperty.h:833
KGameProperty::send
bool send(type v)
This function sends a new value over network.
Definition: kgameproperty.h:670
KGameProperty::KGameProperty
KGameProperty(int id, KGamePropertyHandler *owner)
Constructs a KGameProperty object.
Definition: kgameproperty.h:597
KGamePropertyBase::typeinfo
virtual const type_info * typeinfo()
Definition: kgameproperty.h:244
KGamePropertyBase::setPolicy
void setPolicy(PropertyPolicy p)
Changes the consistency policy of a property.
Definition: kgameproperty.h:145
KGameProperty::save
virtual void save(TQDataStream &stream)
Saves the object to a stream.
Definition: kgameproperty.h:754
KGamePropertyBase::isLocked
bool isLocked() const
A locked property can only be changed by the player who has set the lock.
Definition: kgameproperty.h:187
KPlayer
Base class for a game player.
Definition: kplayer.h:69
KGamePropertyBase
Base class of KGameProperty.
Definition: kgameproperty.h:42
KGameProperty::load
virtual void load(TQDataStream &s)
Reads from a stream and assigns the read value to this object.
Definition: kgameproperty.h:780
KGame
The main KDE game object.
Definition: kgame.h:62
KGamePropertyBase::isEmittingSignal
bool isEmittingSignal() const
See also setEmittingSignal.
Definition: kgameproperty.h:163
KGameProperty::operator=
const type & operator=(const KGameProperty &property)
This copies the data of property to the KGameProperty object.
Definition: kgameproperty.h:818
KGamePropertyBase::policy
PropertyPolicy policy() const
Definition: kgameproperty.h:150
KGameProperty::operator=
const type & operator=(const type &t)
This calls setValue to change the value of the property.
Definition: kgameproperty.h:807
KGameProperty
A class for network transparent games.
Definition: kgameproperty.h:581

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.8.13
This website is maintained by Timothy Pearson.