Advanced Strategic Command
abstracttaskcontainer.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.org
3  Copyright (C) 1994-2010 Martin Bickel
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program 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
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #include "abstracttaskcontainer.h"
22 #include "../basestrm.h"
23 
24 
25 AbstractTaskContainer::AbstractTaskContainer() : newTasks(NULL), lastPlayer(-1)
26 {
27  for ( int i = 0; i < GameMap::maxTotalPlayers; ++i )
28  playerTasks[i] = NULL;
29 }
30 
32 {
33  for ( int i = 0; i < GameMap::maxTotalPlayers; ++i ) {
34  delete playerTasks[i];
35  playerTasks[i] = NULL;
36  }
37 
38  delete newTasks;
39  newTasks = NULL;
40 }
41 
43 {
44  stream.writeInt( taskMagic );
45  for ( int i = 0; i < GameMap::maxTotalPlayers; ++i ) {
46  if ( playerTasks[i] ) {
47  stream.writeInt( 1 );
48  playerTasks[i]->writetostream( &stream );
49  } else
50  stream.writeInt( 0 );
51  }
52  stream.writeInt( taskMagic );
53  if ( newTasks ) {
54  stream.writeInt( 1 );
55  newTasks->writetostream( &stream );
56  } else
57  stream.writeInt( 0 );
58 
59  stream.writeInt( lastPlayer );
60  stream.writeInt( taskMagic );
61 
62 }
63 
64 
66 {
67  int magic = stream.readInt();
68  if ( magic != taskMagic )
69  throw new tinvalidversion( stream.getLocation(), taskMagic, magic );
70 
71  for ( int i = 0; i < GameMap::maxTotalPlayers; ++i ) {
72 
73  delete playerTasks[i];
74  playerTasks[i] = NULL;
75 
76  int data = stream.readInt();
77  if ( data ) {
79  playerTasks[i]->readfromstream( &stream );
80  }
81  }
82 
83  magic = stream.readInt();
84  if ( magic != taskMagic )
85  throw new tinvalidversion( stream.getLocation(), taskMagic, magic );
86 
87  delete newTasks;
88  newTasks = NULL;
89 
90  if ( stream.readInt() ) {
91  newTasks = new MemoryStreamStorage();
92  newTasks->readfromstream( &stream );
93  }
94 
95  lastPlayer = stream.readInt();
96 
97  magic = stream.readInt();
98  if ( magic != taskMagic )
99  throw new tinvalidversion( stream.getLocation(), taskMagic, magic );
100 
101 }
void readStorage(tnstream &stream)
An in-memory storage of streamed data.
Definition: basestrm.h:172
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
virtual ASCString getLocation()
returns the location of the stream.
Definition: basestrm.cpp:274
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
MemoryStreamStorage * playerTasks[GameMap::maxTotalPlayers]
The interface for all kinds of IO stream.
void writetostream(tnstream *stream)
persist the contents in another stream
Definition: basestrm.cpp:2303
const int magic
Definition: stack.cpp:36
MemoryStreamStorage * newTasks
void writeStorage(tnstream &stream) const
static const int maxTotalPlayers
Definition: gamemap.h:198
void readfromstream(tnstream *stream)
read persisted contents from another stream
Definition: basestrm.cpp:2316