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

libtdegames

  • libtdegames
  • kgame
kgamepropertyhandler.cpp
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  $Id$
22 */
23 
24 #include "kgamepropertyhandler.h"
25 #include "kgameproperty.h"
26 #include "kgamemessage.h"
27 
28 #include <tqmap.h>
29 #include <tqptrqueue.h>
30 
31 #include <tdelocale.h>
32 #include <typeinfo>
33 
34 #define KPLAYERHANDLER_LOAD_COOKIE 6239
35 
36 //---------------------- KGamePropertyHandler -----------------------------------
37 class KGamePropertyHandlerPrivate
38 {
39 public:
40  KGamePropertyHandlerPrivate()
41  {
42  }
43 
44  TQMap<int, TQString> mNameMap;
45  TQIntDict<KGamePropertyBase> mIdDict;
46  int mUniqueId;
47  int mId;
48  KGamePropertyBase::PropertyPolicy mDefaultPolicy;
49  bool mDefaultUserspace;
50  int mIndirectEmit;
51  TQPtrQueue<KGamePropertyBase> mSignalQueue;
52 };
53 
54 KGamePropertyHandler::KGamePropertyHandler(int id, const TQObject* receiver, const char * sendf, const char *emitf, TQObject* parent) : TQObject(parent)
55 {
56  init();
57  registerHandler(id,receiver,sendf,emitf);
58 }
59 
60 KGamePropertyHandler::KGamePropertyHandler(TQObject* parent) : TQObject(parent)
61 {
62  init();
63 }
64 
65 KGamePropertyHandler::~KGamePropertyHandler()
66 {
67  clear();
68  delete d;
69 }
70 
71 void KGamePropertyHandler::init()
72 {
73  kdDebug(11001) << k_funcinfo << ": this=" << this << endl;
74  d = new KGamePropertyHandlerPrivate; // for future use - is BC important to us?
75  d->mId = 0;
76  d->mUniqueId=KGamePropertyBase::IdAutomatic;
77  d->mDefaultPolicy=KGamePropertyBase::PolicyLocal;
78  d->mDefaultUserspace=true;
79  d->mIndirectEmit=0;
80 }
81 
82 
83 int KGamePropertyHandler::id() const
84 {
85  return d->mId;
86 }
87 
88 void KGamePropertyHandler::setId(int id)
89 {
90  d->mId = id;
91 }
92 
93 void KGamePropertyHandler::registerHandler(int id,const TQObject * receiver, const char * sendf, const char *emitf)
94 {
95  setId(id);
96  if (receiver && sendf) {
97  kdDebug(11001) << "Connecting TQ_SLOT " << sendf << endl;
98  connect(this, TQ_SIGNAL(signalSendMessage(int, TQDataStream &, bool*)), receiver, sendf);
99  }
100  if (receiver && emitf) {
101  kdDebug(11001) << "Connecting TQ_SLOT " << emitf << endl;
102  connect(this, TQ_SIGNAL(signalPropertyChanged(KGamePropertyBase *)), receiver, emitf);
103  }
104 }
105 
106 bool KGamePropertyHandler::processMessage(TQDataStream &stream, int id, bool isSender)
107 {
108 // kdDebug(11001) << k_funcinfo << ": id=" << id << " mId=" << d->mId << endl;
109  if (id != d->mId) {
110  return false; // Is the message meant for us?
111  }
112  KGamePropertyBase* p;
113  int propertyId;
114  KGameMessage::extractPropertyHeader(stream, propertyId);
115 // kdDebug(11001) << k_funcinfo << ": Got property " << propertyId << endl;
116  if (propertyId==KGamePropertyBase::IdCommand) {
117  int cmd;
118  KGameMessage::extractPropertyCommand(stream, propertyId, cmd);
119 //kdDebug(11001) << k_funcinfo << ": Got COMMAND for id= "<<propertyId <<endl;
120  p = d->mIdDict.find(propertyId);
121  if (p) {
122  if (!isSender || p->policy()==KGamePropertyBase::PolicyClean) {
123  p->command(stream, cmd, isSender);
124  }
125  } else {
126  kdError(11001) << k_funcinfo << ": (cmd): property " << propertyId << " not found" << endl;
127  }
128  return true;
129  }
130  p = d->mIdDict.find(propertyId);
131  if (p) {
132  //kdDebug(11001) << k_funcinfo << ": Loading " << propertyId << endl;
133  if (!isSender || p->policy()==KGamePropertyBase::PolicyClean) {
134  p->load(stream);
135  }
136  } else {
137  kdError(11001) << k_funcinfo << ": property " << propertyId << " not found" << endl;
138  }
139  return true;
140 }
141 
142 bool KGamePropertyHandler::removeProperty(KGamePropertyBase* data)
143 {
144  if (!data) {
145  return false;
146  }
147  d->mNameMap.erase(data->id());
148  return d->mIdDict.remove(data->id());
149 }
150 
151 bool KGamePropertyHandler::addProperty(KGamePropertyBase* data, TQString name)
152 {
153  //kdDebug(11001) << k_funcinfo << ": " << data->id() << endl;
154  if (d->mIdDict.find(data->id())) {
155  // this id already exists
156  kdError(11001) << " -> cannot add property " << data->id() << endl;
157  return false;
158  } else {
159  d->mIdDict.insert(data->id(), data);
160  // if here is a check for "is_debug" or so we can add the strings only in debug mode
161  // and save memory!!
162  if (!name.isNull()) {
163  d->mNameMap[data->id()] = name;
164  //kdDebug(11001) << k_funcinfo << ": nid="<< (data->id()) << " inserted in Map name=" << d->mNameMap[data->id()] <<endl;
165  //kdDebug(11001) << "Typeid=" << typeid(data).name() << endl;
166  //kdDebug(11001) << "Typeid call=" << data->typeinfo()->name() << endl;
167  }
168  }
169  return true;
170 }
171 
172 TQString KGamePropertyHandler::propertyName(int id) const
173 {
174  TQString s;
175  if (d->mIdDict.find(id)) {
176  if (d->mNameMap.contains(id)) {
177  s = i18n("%1 (%2)").arg(d->mNameMap[id]).arg(id);
178  } else {
179  s = i18n("Unnamed - ID: %1").arg(id);
180  }
181  } else {
182  // Should _never_ happen
183  s = i18n("%1 unregistered").arg(id);
184  }
185  return s;
186 }
187 
188 bool KGamePropertyHandler::load(TQDataStream &stream)
189 {
190  // Prevent direct emmiting until all is loaded
191  lockDirectEmit();
192  uint count,i;
193  stream >> count;
194  kdDebug(11001) << k_funcinfo << ": " << count << " KGameProperty objects " << endl;
195  for (i = 0; i < count; i++) {
196  processMessage(stream, id(),false);
197  }
198  TQ_INT16 cookie;
199  stream >> cookie;
200  if (cookie == KPLAYERHANDLER_LOAD_COOKIE) {
201  kdDebug(11001) << " KGamePropertyHandler loaded propertly"<<endl;
202  } else {
203  kdError(11001) << "KGamePropertyHandler loading error. probably format error"<<endl;
204  }
205  // Allow direct emmiting (if no other lock still holds)
206  unlockDirectEmit();
207  return true;
208 }
209 
210 bool KGamePropertyHandler::save(TQDataStream &stream)
211 {
212  kdDebug(11001) << k_funcinfo << ": " << d->mIdDict.count() << " KGameProperty objects " << endl;
213  stream << (uint)d->mIdDict.count();
214  TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
215  while (it.current()) {
216  KGamePropertyBase *base=it.current();
217  if (base) {
218  KGameMessage::createPropertyHeader(stream, base->id());
219  base->save(stream);
220  }
221  ++it;
222  }
223  stream << (TQ_INT16)KPLAYERHANDLER_LOAD_COOKIE;
224  return true;
225 }
226 
227 KGamePropertyBase::PropertyPolicy KGamePropertyHandler::policy()
228 {
229 // kdDebug(11001) << k_funcinfo << ": " << d->mDefaultPolicy << endl;
230  return d->mDefaultPolicy;
231 }
232 void KGamePropertyHandler::setPolicy(KGamePropertyBase::PropertyPolicy p,bool userspace)
233 {
234  // kdDebug(11001) << k_funcinfo << ": " << p << endl;
235  d->mDefaultPolicy=p;
236  d->mDefaultUserspace=userspace;
237  TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
238  while (it.current()) {
239  if (!userspace || it.current()->id()>=KGamePropertyBase::IdUser) {
240  it.current()->setPolicy((KGamePropertyBase::PropertyPolicy)p);
241  }
242  ++it;
243  }
244 }
245 
246 void KGamePropertyHandler::unlockProperties()
247 {
248  TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
249  while (it.current()) {
250  it.current()->unlock();
251  ++it;
252  }
253 }
254 
255 void KGamePropertyHandler::lockProperties()
256 {
257  TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
258  while (it.current()) {
259  it.current()->lock();
260  ++it;
261  }
262 }
263 
264 int KGamePropertyHandler::uniquePropertyId()
265 {
266  return d->mUniqueId++;
267 }
268 
269 void KGamePropertyHandler::flush()
270 {
271  TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
272  while (it.current()) {
273  if (it.current()->isDirty()) {
274  it.current()->sendProperty();
275  }
276  ++it;
277  }
278 }
279 
280 /* Fire all property signal changed which are collected in
281  * the queque
282  **/
283 void KGamePropertyHandler::lockDirectEmit()
284 {
285  d->mIndirectEmit++;
286 }
287 
288 void KGamePropertyHandler::unlockDirectEmit()
289 {
290  // If the flag is <=0 we emit the queued signals
291  d->mIndirectEmit--;
292  if (d->mIndirectEmit<=0)
293  {
294  KGamePropertyBase *prop;
295  while((prop=d->mSignalQueue.dequeue()) != 0)
296  {
297  // kdDebug(11001) << "emmiting signal for " << prop->id() << endl;
298  emit signalPropertyChanged(prop);
299  }
300  }
301 }
302 
303 void KGamePropertyHandler::emitSignal(KGamePropertyBase *prop)
304 {
305  // If the indirect flag is set (load and network transmit)
306  // we cannot emit the signals directly as it can happend that
307  // a sigal causes an access to a property which is e.g. not
308  // yet loaded or received
309 
310  if (d->mIndirectEmit>0)
311  {
312  // Queque the signal
313  d->mSignalQueue.enqueue(prop);
314  }
315  else
316  {
317  // directly emit
318  emit signalPropertyChanged(prop);
319  }
320 }
321 
322 bool KGamePropertyHandler::sendProperty(TQDataStream &s)
323 {
324  bool sent = false;
325  emit signalSendMessage(id(), s, &sent);
326  return sent;
327 }
328 
329 KGamePropertyBase *KGamePropertyHandler::find(int id)
330 {
331  return d->mIdDict.find(id);
332 }
333 
334 void KGamePropertyHandler::clear()
335 {
336  kdDebug(11001) << k_funcinfo << id() << endl;
337  TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
338  while (it.toFirst()) {
339  KGamePropertyBase* p = it.toFirst();
340  p->unregisterData();
341  if (d->mIdDict.find(p->id())) {
342  // shouldn't happen - but if mOwner in KGamePropertyBase is NULL
343  // this might be possible
344  removeProperty(p);
345  }
346  }
347 }
348 
349 TQIntDict<KGamePropertyBase>& KGamePropertyHandler::dict() const
350 {
351  return d->mIdDict;
352 }
353 
354 TQString KGamePropertyHandler::propertyValue(KGamePropertyBase* prop)
355 {
356  if (!prop) {
357  return i18n("NULL pointer");
358  }
359 
360  int id = prop->id();
361  TQString name = propertyName(id);
362  TQString value;
363 
364  const type_info* t = prop->typeinfo();
365  if (*t == typeid(int)) {
366  value = TQString::number(((KGamePropertyInt*)prop)->value());
367  } else if (*t == typeid(unsigned int)) {
368  value = TQString::number(((KGamePropertyUInt *)prop)->value());
369  } else if (*t == typeid(long int)) {
370  value = TQString::number(((KGameProperty<long int> *)prop)->value());
371  } else if (*t == typeid(unsigned long int)) {
372  value = TQString::number(((KGameProperty<unsigned long int> *)prop)->value());
373  } else if (*t == typeid(TQString)) {
374  value = ((KGamePropertyTQString*)prop)->value();
375  } else if (*t == typeid(TQ_INT8)) {
376  value = ((KGamePropertyBool*)prop)->value() ? i18n("True") : i18n("False");
377  } else {
378  emit signalRequestValue(prop, value);
379  }
380 
381  if (value.isNull()) {
382  value = i18n("Unknown");
383  }
384  return value;
385 }
386 
387 void KGamePropertyHandler::Debug()
388 {
389  kdDebug(11001) << "-----------------------------------------------------------" << endl;
390  kdDebug(11001) << "KGamePropertyHandler:: Debug this=" << this << endl;
391 
392  kdDebug(11001) << " Registered properties: (Policy,Lock,Emit,Optimized, Dirty)" << endl;
393  TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
394  while (it.current()) {
395  KGamePropertyBase *p=it.current();
396  kdDebug(11001) << " "<< p->id() << ": p=" << p->policy()
397  << " l="<<p->isLocked()
398  << " e="<<p->isEmittingSignal()
399  << " o=" << p->isOptimized()
400  << " d="<<p->isDirty()
401  << endl;
402  ++it;
403  }
404  kdDebug(11001) << "-----------------------------------------------------------" << endl;
405 }
406 
407 #include "kgamepropertyhandler.moc"
KGamePropertyBase
Base class of KGameProperty.
Definition: kgameproperty.h:43
KGamePropertyBase::save
virtual void save(TQDataStream &s)=0
Write the value into a stream.
KGamePropertyBase::typeinfo
virtual const type_info * typeinfo()
Definition: kgameproperty.h:244
KGamePropertyBase::isOptimized
bool isOptimized() const
See also setOptimize.
Definition: kgameproperty.h:175
KGamePropertyBase::PropertyPolicy
PropertyPolicy
The policy of the property.
Definition: kgameproperty.h:110
KGamePropertyBase::policy
PropertyPolicy policy() const
Definition: kgameproperty.h:150
KGamePropertyBase::isEmittingSignal
bool isEmittingSignal() const
See also setEmittingSignal.
Definition: kgameproperty.h:163
KGamePropertyBase::load
virtual void load(TQDataStream &s)=0
This will read the value of this property from the stream.
KGamePropertyBase::id
int id() const
Definition: kgameproperty.h:238
KGamePropertyBase::command
virtual void command(TQDataStream &stream, int msgid, bool isSender=false)
send a command to advanced properties like arrays
Definition: kgameproperty.cpp:195
KGamePropertyBase::isDirty
bool isDirty() const
Definition: kgameproperty.h:180
KGamePropertyBase::isLocked
bool isLocked() const
A locked property can only be changed by the player who has set the lock.
Definition: kgameproperty.h:187
KGamePropertyHandler::setId
void setId(int id)
Use id as new ID for this KGamePropertyHandler.
Definition: kgamepropertyhandler.cpp:88
KGamePropertyHandler::propertyValue
TQString propertyValue(KGamePropertyBase *property)
In several situations you just want to have a TQString of a KGameProperty object.
Definition: kgamepropertyhandler.cpp:354
KGamePropertyHandler::load
virtual bool load(TQDataStream &stream)
Loads properties from the datastream.
Definition: kgamepropertyhandler.cpp:188
KGamePropertyHandler::setPolicy
void setPolicy(KGamePropertyBase::PropertyPolicy p, bool userspace=true)
Set the policy for all kgame variables which are currently registerd in the KGame proeprty handler.
Definition: kgamepropertyhandler.cpp:232
KGamePropertyHandler::lockProperties
void lockProperties()
Calls KGamePropertyBase::setReadOnly(true) for all properties of this handler.
Definition: kgamepropertyhandler.cpp:255
KGamePropertyHandler::signalSendMessage
void signalSendMessage(int msgid, TQDataStream &, bool *sent)
This signal is emitted when a property needs to be sent.
KGamePropertyHandler::uniquePropertyId
int uniquePropertyId()
returns a unique property ID starting called usually with a base of KGamePropertyBase::IdAutomatic.
Definition: kgamepropertyhandler.cpp:264
KGamePropertyHandler::removeProperty
bool removeProperty(KGamePropertyBase *data)
Removes a property from the handler.
Definition: kgamepropertyhandler.cpp:142
KGamePropertyHandler::flush
void flush()
Sends all properties which are marked dirty over the network.
Definition: kgamepropertyhandler.cpp:269
KGamePropertyHandler::dict
TQIntDict< KGamePropertyBase > & dict() const
Reference to the internal dictionary.
Definition: kgamepropertyhandler.cpp:349
KGamePropertyHandler::unlockProperties
void unlockProperties()
Calls KGamePropertyBase::setReadOnly(false) for all properties of this player.
Definition: kgamepropertyhandler.cpp:246
KGamePropertyHandler::signalRequestValue
void signalRequestValue(KGamePropertyBase *property, TQString &value)
If you call propertyValue with a non-standard KGameProperty it is possible that the value cannot auto...
KGamePropertyHandler::unlockDirectEmit
void unlockDirectEmit()
Removes the lock from the emitting of property signals.
Definition: kgamepropertyhandler.cpp:288
KGamePropertyHandler::find
KGamePropertyBase * find(int id)
Definition: kgamepropertyhandler.cpp:329
KGamePropertyHandler::registerHandler
void registerHandler(int id, const TQObject *receiver, const char *send, const char *emit)
Register the handler with a parent.
Definition: kgamepropertyhandler.cpp:93
KGamePropertyHandler::lockDirectEmit
void lockDirectEmit()
Called by the KGame or KPlayer object or the handler itself to delay emmiting of signals.
Definition: kgamepropertyhandler.cpp:283
KGamePropertyHandler::sendProperty
bool sendProperty(TQDataStream &s)
called by a property to send itself into the datastream.
Definition: kgamepropertyhandler.cpp:322
KGamePropertyHandler::clear
void clear()
Clear the KGamePropertyHandler.
Definition: kgamepropertyhandler.cpp:334
KGamePropertyHandler::save
virtual bool save(TQDataStream &stream)
Saves properties into the datastream.
Definition: kgamepropertyhandler.cpp:210
KGamePropertyHandler::processMessage
bool processMessage(TQDataStream &stream, int id, bool isSender)
Main message process function.
Definition: kgamepropertyhandler.cpp:106
KGamePropertyHandler::emitSignal
void emitSignal(KGamePropertyBase *data)
called by a property to emit a signal This call is simply forwarded to the parent object
Definition: kgamepropertyhandler.cpp:303
KGamePropertyHandler::signalPropertyChanged
void signalPropertyChanged(KGamePropertyBase *)
This is emitted by a property.
KGamePropertyHandler::Debug
void Debug()
Writes some debug output to the console.
Definition: kgamepropertyhandler.cpp:387
KGamePropertyHandler::propertyName
TQString propertyName(int id) const
Definition: kgamepropertyhandler.cpp:172
KGamePropertyHandler::KGamePropertyHandler
KGamePropertyHandler(TQObject *parent=0)
Construct an unregistered KGamePropertyHandler.
Definition: kgamepropertyhandler.cpp:60
KGamePropertyHandler::policy
KGamePropertyBase::PropertyPolicy policy()
Returns the default policy for this property handler.
Definition: kgamepropertyhandler.cpp:227
KGamePropertyHandler::id
int id() const
Definition: kgamepropertyhandler.cpp:83
KGamePropertyHandler::addProperty
bool addProperty(KGamePropertyBase *data, TQString name=0)
Adds a KGameProperty property to the handler.
Definition: kgamepropertyhandler.cpp:151
KGameProperty< int >

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.9.1
This website is maintained by Timothy Pearson.