[084729c] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * Graveyard.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Sep 5, 2013
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
[9eb71b3] | 35 | //#include "CodePatterns/MemDebug.hpp"
|
---|
[084729c] | 36 |
|
---|
| 37 | #include "CodePatterns/Observer/Graveyard.hpp"
|
---|
| 38 | #include "CodePatterns/Observer/ObserverLog.hpp"
|
---|
| 39 |
|
---|
| 40 | #include <boost/bind.hpp>
|
---|
| 41 |
|
---|
| 42 | #include "CodePatterns/Log.hpp"
|
---|
| 43 |
|
---|
| 44 | Graveyard::Graveyard() :
|
---|
| 45 | graveyard_informer( boost::bind(&Graveyard::ObserverLeft, this, _1) )
|
---|
| 46 | {}
|
---|
| 47 |
|
---|
| 48 | Graveyard::~Graveyard()
|
---|
| 49 | {
|
---|
| 50 | if (graveyard.size() != 0)
|
---|
| 51 | ELOG(1, "There are still zombies left in the graveyard.");
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | void Graveyard::turnZombie(Observable *&_observable)
|
---|
| 55 | {
|
---|
| 56 | // LOG(1, "Adding Observable "+toString(_observable)+".");
|
---|
| 57 | #ifdef LOG_OBSERVER
|
---|
| 58 | observerLog().addMessage(0) << "++ Turning " << observerLog().getName(_observable)
|
---|
| 59 | << " into a Zombie.";
|
---|
| 60 | #endif
|
---|
| 61 | Zombie::ptr newzombie(new Zombie(_observable, &graveyard_informer));
|
---|
| 62 | if (newzombie->getNumberOfObservers() != 0)
|
---|
| 63 | graveyard.insert( std::make_pair(newzombie->getPtr(), newzombie) );
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | void Graveyard::ObserverLeft(const Observable *_observable)
|
---|
| 67 | {
|
---|
| 68 | graveyard_t::iterator iter = graveyard.find(_observable);
|
---|
| 69 | if (iter != graveyard.end()) {
|
---|
| 70 | #ifdef LOG_OBSERVER
|
---|
| 71 | observerLog().addMessage(0) << "++ Zombie " << observerLog().getName(const_cast<Observable *>(_observable))
|
---|
| 72 | << " has " << iter->second->getNumberOfObservers() << " Observers left.";
|
---|
| 73 | #endif
|
---|
| 74 | // remove zombie if no Observers are left
|
---|
| 75 | if (iter->second->getNumberOfObservers() == 0)
|
---|
| 76 | graveyard.erase(iter);
|
---|
| 77 | } else {
|
---|
| 78 | ELOG(1, "Zombie to Observable "+toString(_observable)+" is not known.");
|
---|
| 79 | }
|
---|
| 80 | }
|
---|