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

libtdegames

  • libtdegames
  • kgame
kgamepropertyarray.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 __KGAMEPROPERTYARRAY_H_
22 #define __KGAMEPROPERTYARRAY_H_
23 
24 #include <tqdatastream.h>
25 #include <kdebug.h>
26 
27 #include "kgamemessage.h"
28 #include "kgameproperty.h"
29 #include "kgamepropertyhandler.h"
30 
31 
32 template<class type>
33 class KGamePropertyArray : public TQMemArray<type>, public KGamePropertyBase
34 {
35 public:
36  KGamePropertyArray() :TQMemArray<type>(), KGamePropertyBase()
37  {
38  //kdDebug(11001) << "KGamePropertyArray init" << endl;
39  }
40 
41  KGamePropertyArray( int size )
42  {
43  resize(size);
44  }
45 
46  KGamePropertyArray( const KGamePropertyArray<type> &a ) : TQMemArray<type>(a)
47  {
48  }
49 
50  bool resize( uint size )
51  {
52  if (size!=TQMemArray<type>::size())
53  {
54  bool a=true;
55  TQByteArray b;
56  TQDataStream s(b, IO_WriteOnly);
57  KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdResize);
58  s << size ;
59  if (policy()==PolicyClean || policy()==PolicyDirty)
60  {
61  if (mOwner)
62  {
63  mOwner->sendProperty(s);
64  }
65  }
66  if (policy()==PolicyLocal || policy()==PolicyDirty)
67  {
68  extractProperty(b);
69 // a=TQMemArray<type>::resize(size);// FIXME: return value!
70  }
71  return a;
72  }
73  else return true;
74  }
75 
76  void setAt(uint i,type data)
77  {
78  TQByteArray b;
79  TQDataStream s(b, IO_WriteOnly);
80  KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdAt);
81  s << i ;
82  s << data;
83  if (policy()==PolicyClean || policy()==PolicyDirty)
84  {
85  if (mOwner)
86  {
87  mOwner->sendProperty(s);
88  }
89  }
90  if (policy()==PolicyLocal || policy()==PolicyDirty)
91  {
92  extractProperty(b);
93  }
94  //kdDebug(11001) << "KGamePropertyArray setAt send COMMAND for id="<<id() << " type=" << 1 << " at(" << i<<")="<<data << endl;
95  }
96 
97  type at( uint i ) const
98  {
99  return TQMemArray<type>::at(i);
100  }
101 
102  type operator[]( int i ) const
103  {
104  return TQMemArray<type>::at(i);
105  }
106 
107  KGamePropertyArray<type> &operator=(const KGamePropertyArray<type> &a)
108  {
109  return assign(a);
110  }
111 
112  bool truncate( uint pos )
113  {
114  return resize(pos);
115  }
116 
117  bool fill( const type &data, int size = -1 )
118  {
119  bool r=true;
120  TQByteArray b;
121  TQDataStream s(b, IO_WriteOnly);
122  KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdFill);
123  s << data;
124  s << size ;
125  if (policy()==PolicyClean || policy()==PolicyDirty)
126  {
127  if (mOwner)
128  {
129  mOwner->sendProperty(s);
130  }
131  }
132  if (policy()==PolicyLocal || policy()==PolicyDirty)
133  {
134  extractProperty(b);
135 // r=TQMemArray<type>::fill(data,size);//FIXME: return value!
136  }
137  return r;
138  }
139 
140  KGamePropertyArray<type>& assign( const KGamePropertyArray<type>& a )
141  {
142 // note: send() has been replaced by sendProperty so it might be broken now!
143  if (policy()==PolicyClean || policy()==PolicyDirty)
144  {
145  sendProperty();
146  }
147  if (policy()==PolicyLocal || policy()==PolicyDirty)
148  {
149  TQMemArray<type>::assign(a);
150  }
151  return *this;
152  }
153  KGamePropertyArray<type>& assign( const type *a, uint n )
154  {
155  if (policy()==PolicyClean || policy()==PolicyDirty)
156  {
157  sendProperty();
158  }
159  if (policy()==PolicyLocal || policy()==PolicyDirty)
160  {
161  TQMemArray<type>::assign(a,n);
162  }
163  return *this;
164  }
165  KGamePropertyArray<type>& duplicate( const KGamePropertyArray<type>& a )
166  {
167  if (policy()==PolicyClean || policy()==PolicyDirty)
168  {
169  sendProperty();
170  }
171  if (policy()==PolicyLocal || policy()==PolicyDirty)
172  {
173  TQMemArray<type>::duplicate(a);
174  }
175  return *this;
176  }
177  KGamePropertyArray<type>& duplicate( const type *a, uint n )
178  {
179  if (policy()==PolicyClean || policy()==PolicyDirty)
180  {
181  sendProperty();
182  }
183  if (policy()==PolicyLocal || policy()==PolicyDirty)
184  {
185  TQMemArray<type>::duplicate(a,n);
186  }
187  return *this;
188  }
189  KGamePropertyArray<type>& setRawData( const type *a, uint n )
190  {
191  if (policy()==PolicyClean || policy()==PolicyDirty)
192  {
193  sendProperty();
194  }
195  if (policy()==PolicyLocal || policy()==PolicyDirty)
196  {
197  TQMemArray<type>::setRawData(a,n);
198  }
199  return *this;
200  }
201  void sort()
202  {
203  TQByteArray b;
204  TQDataStream s(b, IO_WriteOnly);
205  KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdSort);
206  if (policy()==PolicyLocal || policy()==PolicyDirty)
207  {
208  if (mOwner)
209  {
210  mOwner->sendProperty(s);
211  }
212  }
213  if (policy()==PolicyLocal || policy()==PolicyDirty)
214  {
215  extractProperty(b);
216  }
217  }
218 
219  void load(TQDataStream& s)
220  {
221  //kdDebug(11001) << "KGamePropertyArray load " << id() << endl;
222  type data;
223  for (unsigned int i=0; i<TQMemArray<type>::size(); i++)
224  {
225  s >> data;
226  TQMemArray<type>::at(i)=data;
227  }
228  if (isEmittingSignal())
229  {
230  emitSignal();
231  }
232  }
233  void save(TQDataStream &s)
234  {
235  //kdDebug(11001) << "KGamePropertyArray save "<<id() << endl;
236  for (unsigned int i=0; i<TQMemArray<type>::size(); i++)
237  {
238  s << at(i);
239  }
240  }
241 
242  void command(TQDataStream &s,int cmd,bool)
243  {
244  KGamePropertyBase::command(s, cmd);
245  //kdDebug(11001) << "Array id="<<id()<<" got command ("<<cmd<<") !!!" <<endl;
246  switch(cmd)
247  {
248  case CmdAt:
249  {
250  uint i;
251  type data;
252  s >> i >> data;
253  TQMemArray<type>::at(i)=data;
254  //kdDebug(11001) << "CmdAt:id="<<id()<<" i="<<i<<" data="<<data <<endl;
255  if (isEmittingSignal())
256  {
257  emitSignal();
258  }
259  break;
260  }
261  case CmdResize:
262  {
263  uint size;
264  s >> size;
265  //kdDebug(11001) << "CmdResize:id="<<id()<<" oldsize="<<TQMemArray<type>::size()<<" newsize="<<size <<endl;
266  if (TQMemArray<type>::size() != size)
267  {
268  TQMemArray<type>::resize(size);
269  }
270  break;
271  }
272  case CmdFill:
273  {
274  int size;
275  type data;
276  s >> data >> size;
277  //kdDebug(11001) << "CmdFill:id="<<id()<<"size="<<size <<endl;
278  TQMemArray<type>::fill(data,size);
279  if (isEmittingSignal())
280  {
281  emitSignal();
282  }
283  break;
284  }
285  case CmdSort:
286  {
287  //kdDebug(11001) << "CmdSort:id="<<id()<<endl;
288  TQMemArray<type>::sort();
289  break;
290  }
291  default:
292  kdError(11001) << "Error in KPropertyArray::command: Unknown command " << cmd << endl;
293  break;
294  }
295  }
296 protected:
297  void extractProperty(const TQByteArray& b)
298  {
299  TQDataStream s(b, IO_ReadOnly);
300  int cmd;
301  int propId;
302  KGameMessage::extractPropertyHeader(s, propId);
303  KGameMessage::extractPropertyCommand(s, propId, cmd);
304  command(s, cmd, true);
305  }
306 
307 };
308 
309 #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::sendProperty
bool sendProperty()
Forward the data to the owner of this property which then sends it over network.
Definition: kgameproperty.cpp:124
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.