/* * ActionRegistry.cpp * * Created on: Jan 7, 2010 * Author: crueger */ #include "Helpers/MemDebug.hpp" #include "Actions/ActionRegistry.hpp" #include "Actions/Action.hpp" #include "Patterns/Singleton_impl.hpp" #include #include "Helpers/Assert.hpp" #include using namespace std; /** Constructor for class ActionRegistry. */ ActionRegistry::ActionRegistry() { } /** Destructor for class ActionRegistry. */ ActionRegistry::~ActionRegistry() { map::iterator iter; for(iter=actionMap.begin();iter!=actionMap.end();++iter) { delete iter->second; } actionMap.clear(); } /** Returns pointer to an action named by \a name. * \param name name of action * \return pointer to Action */ Action* ActionRegistry::getActionByName(const std::string name){ map::iterator iter; iter = actionMap.find(name); ASSERT(iter!=actionMap.end(),"Query for an action not stored in registry"); return iter->second; } /** States whether action is present or not. * \note This iss needed as ActionRegistry::getActionByName() ASSERT()s that action is in map. * \param name name of action * \return true - Action present, false - Action absent */ bool ActionRegistry::isActionByNamePresent(const std::string name){ map::iterator iter; iter = actionMap.find(name); return iter!=actionMap.end(); } /** Registers an Action with the ActionRegistry. * \param *action pointer to Action. */ void ActionRegistry::registerAction(Action* action){ pair::iterator,bool> ret; //cout << "Trying to register action with name " << action->getName() << "." << endl; ret = actionMap.insert(pair(action->getName(),action)); ASSERT(ret.second,"Two actions with the same name added to registry"); } /** Unregisters an Action. * \param *action pointer to Action. */ void ActionRegistry::unregisterAction(Action* action){ //cout << "Unregistering action with name " << action->getName() << "." << endl; actionMap.erase(action->getName()); } /** Returns an iterator pointing to the start of the map of Action's. * \return begin iterator */ std::map::iterator ActionRegistry::getBeginIter() { return actionMap.begin(); } /** Returns an iterator pointing to the end of the map of Action's. * \return end iterator */ std::map::iterator ActionRegistry::getEndIter() { return actionMap.end(); } /** Returns a const iterator pointing to the start of the map of Action's. * \return constant begin iterator */ std::map::const_iterator ActionRegistry::getBeginIter() const { return actionMap.begin(); } /** Returns a const iterator pointing to the end of the map of Action's. * \return constant end iterator */ std::map::const_iterator ActionRegistry::getEndIter() const { return actionMap.end(); } /** Prints the contents of the ActionRegistry \a &m to \a &ost. * \param &ost output stream * \param &m reference to ActionRegistry * \return reference to the above out stream for concatenation */ ostream& operator<<(ostream& ost, const ActionRegistry& m) { ost << "ActionRegistry contains:" << endl; for (std::map::const_iterator iter = m.getBeginIter(); iter != m.getEndIter(); ++iter) { ost << "\t" << iter->first << " with pointer " << iter->second << endl; } return ost; }; CONSTRUCT_SINGLETON(ActionRegistry)