/////////////////////////////////////////////////////////// // Softwarebauelemente II, Aufgabe O2.1 // // author: Stephan Brumme // last changes: February 26, 2001 #include "House.h" // needed for std::find #include #include // constructs a new house using its date of foundation (default: today) CHouse::CHouse(const CDate& dtDateOfFoundation) { m_dtDateOfFoundation = dtDateOfFoundation; m_Set.empty(); m_itCursor = m_Set.begin(); } ostream& operator<<(ostream& mystream, const CHouse& house) { return house.OutStream(mystream); } // used to perform proper display in derived classes ostream& CHouse::OutStream(ostream& mystream, bool bDebug) const { if (!bDebug) { // some general information mystream << "The house was founded on " << m_dtDateOfFoundation << " and consists of " << m_Set.size() << " rooms" << endl; } else { // print any information about this class mystream << "DEBUG info for '" << ClassnameOf() << "'" << endl << " m_dtDateOfFoundation=" << m_dtDateOfFoundation << " rooms=" << m_Set.size() << endl; } // stream all rooms if (m_Set.size() > 0) { // this iterator needs to be const ... TSetConstCursor itCursor; for (itCursor = m_Set.begin(); itCursor != m_Set.end(); itCursor++) mystream << " " << *itCursor << endl; } return mystream; } // copy constructor CHouse& CHouse::operator =(const CHouse &house) { m_dtDateOfFoundation = house.m_dtDateOfFoundation; m_itCursor = house.m_itCursor; // container performs the whole copy internally m_Set = house.m_Set; return *this; } // virtual, see operator= bool CHouse::Copy(const CHouse *house) { // invalid class if (house == NULL || house == this) return false; // use non virtual reference based copy // return value isn't needed operator=(*house); // we're done return true; } // compare two houses bool CHouse::operator ==(const CHouse &house) const { // compare date of foundation if (!(m_dtDateOfFoundation == house.m_dtDateOfFoundation)) return false; // find each room of the given house in our house TSetConstCursor itCursor; for (itCursor = house.m_Set.begin(); itCursor != house.m_Set.end(); itCursor++) // I re-implement the find method because of keeping this method const if (std::find(m_Set.begin(), m_Set.end(), *itCursor) == m_Set.end()) return false; // all rooms were found return true; } // virtual, see operator== bool CHouse::EqualValue(const CHouse *house) const { // invalid class if (house == NULL) return false; // use non virtual reference based copy return operator==(*house); } // virtual, just compares the pointers bool CHouse::Equal(const CHouse *house) const { return (house == this); } // return date of foundation CDate CHouse::GetDateOfFoundation() const { return m_dtDateOfFoundation; } // return number of stored rooms int CHouse::Card() const { return m_Set.size(); } // insert a new room into the set, TRUE if successful // will fail if room already exists bool CHouse::Insert(const CRoom &room) { // there must be some memory to grow the set // and the room must not be part of the list if (m_Set.size() < m_Set.max_size() && !Find(room)) { // insert at the tail of the list m_Set.push_back(room); return true; } else return false; } // return first stored room, TRUE if successful bool CHouse::GetFirst(CRoom &room) { // set must not be empty if (m_Set.empty()) return false; // set cursor to first room m_itCursor = m_Set.begin(); // get this room room = *m_itCursor; return true; } // return next room, TRUE if successful bool CHouse::GetNext(CRoom &room) { // set must not be empty, end must not be reached if (m_Set.empty() && m_itCursor != m_Set.end()) return false; // iterate to next object m_itCursor++; // get this object room = *m_itCursor; return true; } // look for a room and set cursor, TRUE if successful bool CHouse::Find(const CRoom &room) { // set must not be empty if (m_Set.empty()) return false; // create new iterator TSetCursor itCursor; // use STL's find itCursor = std::find(m_Set.begin(), m_Set.end(), room); // change m_itCursor if room was found if (itCursor != m_Set.end()) { m_itCursor = itCursor; return true; } else return false; } // return the room the cursor points to, TRUE if successful bool CHouse::GetCurrent(CRoom &room) const { // set must not be empty if (m_Set.empty()) return false; // return current room room = *m_itCursor; return true; } // erase current room, TRUE if successful bool CHouse::Scratch() { // set must not be empty if (m_Set.empty()) return false; // erase room, set cursor to next room m_itCursor = m_Set.erase(m_itCursor); return true; }