1 ///////////////////////////////////////////////////////////
2 // Softwarebauelemente II, Aufgabe O2.1
3 //
4 // author: Stephan Brumme
5 // last changes: February 26, 2001
6
7
8 #include "House.h"
9
10 // needed for std::find
11 #include <algorithm>
12 #include <strstream>
13
14
15 // constructs a new house using its date of foundation (default: today)
16 CHouse::CHouse(const CDate& dtDateOfFoundation)
17 {
18 m_dtDateOfFoundation = dtDateOfFoundation;
19 m_Set.empty();
20 m_itCursor = m_Set.begin();
21 }
22
23
24 ostream& operator<<(ostream& mystream, const CHouse& house)
25 {
26 return house.OutStream(mystream);
27 }
28
29
30 // used to perform proper display in derived classes
31 ostream& CHouse::OutStream(ostream& mystream, bool bDebug) const
{
32 if (!bDebug)
33 {
34 // some general information
35 mystream << "The house was founded on " << m_dtDateOfFoundation
<< " and consists of "
36 << m_Set.size() << " rooms" << endl;
37 }
38 else
{
39 // print any information about this class
40 mystream << "DEBUG info for '" << ClassnameOf() << "'" << endl
41 << " m_dtDateOfFoundation=" << m_dtDateOfFoundation
<< " rooms=" << m_Set.size() << endl;
42 }
43
44 // stream all rooms
45 if (m_Set.size() > 0)
46 {
47 // this iterator needs to be const ...
48 TSetConstCursor itCursor;
49 for (itCursor = m_Set.begin(); itCursor != m_Set.end(); itCursor++)
50 mystream << " " << *itCursor << endl;
51 }
52
53 return mystream;
54 }
55
56
57 // copy constructor
58 CHouse& CHouse::operator =(const CHouse &house)
59 {
60 m_dtDateOfFoundation = house.m_dtDateOfFoundation;
61 m_itCursor = house.m_itCursor;
62 // container performs the whole copy internally
63 m_Set = house.m_Set;
64
65 return *this;
66 }
67
68
69 // virtual, see operator=
70 bool CHouse::Copy(const CHouse *house)
71 {
72 // invalid class
73 if (house == NULL || house == this)
74 return false;
75
76 // use non virtual reference based copy
77 // return value isn't needed
78 operator=(*house);
79
80 // we're done
81 return true;
82 }
83
84
85 // compare two houses
86 bool CHouse::operator ==(const CHouse &house) const
{
87 // compare date of foundation
88 if (!(m_dtDateOfFoundation == house.m_dtDateOfFoundation))
89 return false;
90
91 // find each room of the given house in our house
92 TSetConstCursor itCursor;
93 for (itCursor = house.m_Set.begin(); itCursor != house.m_Set.end(); itCursor++)
94 // I re-implement the find method because of keeping this method const
95 if (std::find(m_Set.begin(), m_Set.end(), *itCursor) == m_Set.end())
96 return false;
97
98 // all rooms were found
99 return true;
100 }
101
102
103 // virtual, see operator==
104 bool CHouse::EqualValue(const CHouse *house) const
{
105 // invalid class
106 if (house == NULL)
107 return false;
108
109 // use non virtual reference based copy
110 return operator==(*house);
111 }
112
113
114 // virtual, just compares the pointers
115 bool CHouse::Equal(const CHouse *house) const
{
116 return (house == this);
117 }
118
119
120 // return date of foundation
121 CDate CHouse::GetDateOfFoundation() const
{
122 return m_dtDateOfFoundation;
123 }
124
125
126 // return number of stored rooms
127 int CHouse::Card() const
{
128 return m_Set.size();
129 }
130
131
132 // insert a new room into the set, TRUE if successful
133 // will fail if room already exists
134 bool CHouse::Insert(const CRoom &room)
135 {
136 // there must be some memory to grow the set
137 // and the room must not be part of the list
138 if (m_Set.size() < m_Set.max_size() && !Find(room))
139 {
140 // insert at the tail of the list
141 m_Set.push_back(room);
142 return true;
143 }
144 else
return false;
145 }
146
147
148 // return first stored room, TRUE if successful
149 bool CHouse::GetFirst(CRoom &room)
150 {
151 // set must not be empty
152 if (m_Set.empty())
153 return false;
154
155 // set cursor to first room
156 m_itCursor = m_Set.begin();
157 // get this room
158 room = *m_itCursor;
159
160 return true;
161 }
162
163
164 // return next room, TRUE if successful
165 bool CHouse::GetNext(CRoom &room)
166 {
167 // set must not be empty, end must not be reached
168 if (m_Set.empty() && m_itCursor != m_Set.end())
169 return false;
170
171 // iterate to next object
172 m_itCursor++;
173 // get this object
174 room = *m_itCursor;
175
176 return true;
177 }
178
179
180 // look for a room and set cursor, TRUE if successful
181 bool CHouse::Find(const CRoom &room)
182 {
183 // set must not be empty
184 if (m_Set.empty())
185 return false;
186
187 // create new iterator
188 TSetCursor itCursor;
189
190 // use STL's find
191 itCursor = std::find(m_Set.begin(), m_Set.end(), room);
192
193 // change m_itCursor if room was found
194 if (itCursor != m_Set.end())
195 {
196 m_itCursor = itCursor;
197 return true;
198 }
199 else
return false;
200 }
201
202
203 // return the room the cursor points to, TRUE if successful
204 bool CHouse::GetCurrent(CRoom &room) const
{
205 // set must not be empty
206 if (m_Set.empty())
207 return false;
208
209 // return current room
210 room = *m_itCursor;
211 return true;
212 }
213
214
215 // erase current room, TRUE if successful
216 bool CHouse::Scratch()
217 {
218 // set must not be empty
219 if (m_Set.empty())
220 return false;
221
222 // erase room, set cursor to next room
223 m_itCursor = m_Set.erase(m_itCursor);
224 return true;
225 }
226
227