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

libtdegames

  • libtdegames
  • kgame
kgamepropertylist.h
1 /*
2  This file is part of the TDE games library
3  Copyright (C) 2001 Martin Heni (martin@heni-online.de)
4  Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.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 __KGAMEPROPERTYLIST_H_
22 #define __KGAMEPROPERTYLIST_H_
23 
24 #include <tqvaluelist.h>
25 
26 #include <kdebug.h>
27 
28 #include "kgamemessage.h"
29 #include "kgameproperty.h"
30 #include "kgamepropertyhandler.h"
31 
32 // AB: also see README.LIB!
33 
34 template<class type>
35 class KGamePropertyList : public TQValueList<type>, public KGamePropertyBase
36 {
37 public:
41  typedef TQValueListIterator<type> Iterator;
42  typedef TQValueListConstIterator<type> ConstIterator;
43 
44  KGamePropertyList() :TQValueList<type>(), KGamePropertyBase()
45  {
46  }
47 
48  KGamePropertyList( const KGamePropertyList<type> &a ) : TQValueList<type>(a)
49  {
50  }
51 
52  uint findIterator(Iterator me)
53  {
54  Iterator it;
55  uint cnt=0;
56  for( it = this->begin(); it != this->end(); ++it )
57  {
58  if (me==it)
59  {
60  return cnt;
61  }
62  cnt++;
63  }
64  return this->count();
65  }
66 
67  Iterator insert( Iterator it, const type& d )
68  {
69  it=TQValueList<type>::insert(it,d);
70 
71  TQByteArray b;
72  TQDataStream s(b, IO_WriteOnly);
73  KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdInsert);
74  int i=findIterator(it);
75  s << i;
76  s << d;
77  if (policy() == PolicyClean || policy() == PolicyDirty)
78  {
79  if (mOwner)
80  {
81  mOwner->sendProperty(s);
82  }
83  }
84  if (policy() == PolicyDirty || policy() == PolicyLocal)
85  {
86  extractProperty(b);
87  }
88  return it;
89  }
90 
91  void prepend( const type& d) { insert(this->begin(),d); }
92 
93  void append( const type& d )
94  {
95  TQByteArray b;
96  TQDataStream s(b, IO_WriteOnly);
97  KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdAppend);
98  s << d;
99  if (policy() == PolicyClean || policy() == PolicyDirty)
100  {
101  if (mOwner)
102  {
103  mOwner->sendProperty(s);
104  }
105  }
106  if (policy() == PolicyDirty || policy() == PolicyLocal)
107  {
108  extractProperty(b);
109  }
110  }
111 
112  Iterator erase( Iterator it )
113  {
114  TQByteArray b;
115  TQDataStream s(b, IO_WriteOnly);
116  KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdRemove);
117  int i=findIterator(it);
118  s << i;
119  if (policy() == PolicyClean || policy() == PolicyDirty)
120  {
121  if (mOwner)
122  {
123  mOwner->sendProperty(s);
124  }
125  }
126  if (policy() == PolicyDirty || policy() == PolicyLocal)
127  {
128  extractProperty(b);
129  }
130  //TODO: return value - is it correct for PolicyLocal|PolicyDirty?
131 // return TQValueList<type>::remove(it);
132  return it;
133  }
134 
135  Iterator remove( Iterator it )
136  {
137  return erase(it);
138  }
139 
140  void remove( const type& d )
141  {
142  Iterator it=find(d);
143  remove(it);
144  }
145 
146  void clear()
147  {
148  TQByteArray b;
149  TQDataStream s(b, IO_WriteOnly);
150  KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdClear);
151  if (policy() == PolicyClean || policy() == PolicyDirty)
152  {
153  if (mOwner)
154  {
155  mOwner->sendProperty(s);
156  }
157  }
158  if (policy() == PolicyDirty || policy() == PolicyLocal)
159  {
160  extractProperty(b);
161  }
162  }
163 
164  void load(TQDataStream& s)
165  {
166  kdDebug(11001) << "KGamePropertyList load " << id() << endl;
167  TQValueList<type>::clear();
168  uint size;
169  type data;
170  s >> size;
171 
172  for (unsigned int i=0;i<size;i++)
173  {
174  s >> data;
175  TQValueList<type>::append(data);
176  }
177  if (isEmittingSignal()) emitSignal();
178  }
179 
180  void save(TQDataStream &s)
181  {
182  kdDebug(11001) << "KGamePropertyList save "<<id() << endl;
183  type data;
184  uint size=this->count();
185  s << size;
186  Iterator it;
187  for( it = this->begin(); it != this->end(); ++it )
188  {
189  data=*it;
190  s << data;
191  }
192  }
193 
194  void command(TQDataStream &s,int cmd,bool)
195  {
196  KGamePropertyBase::command(s, cmd);
197  kdDebug(11001) << "---> LIST id="<<id()<<" got command ("<<cmd<<") !!!" <<endl;
198  Iterator it;
199  switch(cmd)
200  {
201  case CmdInsert:
202  {
203  uint i;
204  type data;
205  s >> i >> data;
206  it=this->at(i);
207  TQValueList<type>::insert(it,data);
208 // kdDebug(11001) << "CmdInsert:id="<<id()<<" i="<<i<<" data="<<data <<endl;
209  if (isEmittingSignal()) emitSignal();
210  break;
211  }
212  case CmdAppend:
213  {
214  type data;
215  s >> data;
216  TQValueList<type>::append(data);
217 // kdDebug(11001) << "CmdAppend:id=" << id() << " data=" << data << endl;
218  if (isEmittingSignal()) emitSignal();
219  break;
220  }
221  case CmdRemove:
222  {
223  uint i;
224  s >> i;
225  it=this->at(i);
226  TQValueList<type>::remove(it);
227  kdDebug(11001) << "CmdRemove:id="<<id()<<" i="<<i <<endl;
228  if (isEmittingSignal()) emitSignal();
229  break;
230  }
231  case CmdClear:
232  {
233  TQValueList<type>::clear();
234  kdDebug(11001) << "CmdClear:id="<<id()<<endl;
235  if (isEmittingSignal()) emitSignal();
236  break;
237  }
238  default:
239  kdDebug(11001) << "Error in KPropertyList::command: Unknown command " << cmd << endl;
240  }
241  }
242 
243 protected:
244  void extractProperty(const TQByteArray& b)
245  // this is called for Policy[Dirty|Local] after putting the stuff into the
246  // stream
247  {
248  TQDataStream s(b, IO_ReadOnly);
249  int cmd;
250  int propId;
251  KGameMessage::extractPropertyHeader(s, propId);
252  KGameMessage::extractPropertyCommand(s, propId, cmd);
253  command(s, cmd, true);
254  }
255 
256 };
257 
258 #endif
KGamePropertyHandler::sendProperty
bool sendProperty(TQDataStream &s)
called by a property to send itself into the datastream.
Definition: kgamepropertyhandler.cpp:322
KGamePropertyBase::save
virtual void save(TQDataStream &s)=0
Write the value into a stream.
KGamePropertyBase::id
int id() const
Definition: kgameproperty.h:238
KGamePropertyBase::emitSignal
void emitSignal()
Causes the parent object to emit a signal on value change.
Definition: kgameproperty.cpp:185
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
Base class of KGameProperty.
Definition: kgameproperty.h:42
KGamePropertyBase::isEmittingSignal
bool isEmittingSignal() const
See also setEmittingSignal.
Definition: kgameproperty.h:163
KGamePropertyBase::policy
PropertyPolicy policy() const
Definition: kgameproperty.h:150
KGamePropertyBase::load
virtual void load(TQDataStream &s)=0
This will read the value of this property from the stream.

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.