| [cc04b7] | 1 | /* | 
|---|
|  | 2 | * ActionRegistry.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Jan 7, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [112b09] | 8 | #include "Helpers/MemDebug.hpp" | 
|---|
|  | 9 |  | 
|---|
| [cc04b7] | 10 | #include "Actions/ActionRegistry.hpp" | 
|---|
|  | 11 | #include "Actions/Action.hpp" | 
|---|
|  | 12 |  | 
|---|
| [e73a8a2] | 13 | #include "Patterns/Singleton_impl.hpp" | 
|---|
|  | 14 |  | 
|---|
| [cc04b7] | 15 | #include <string> | 
|---|
| [d56640] | 16 | #include "Helpers/Assert.hpp" | 
|---|
| [cc04b7] | 17 | #include <iostream> | 
|---|
|  | 18 |  | 
|---|
|  | 19 | using namespace std; | 
|---|
|  | 20 |  | 
|---|
| [d0fbec] | 21 | /** Constructor for class ActionRegistry. | 
|---|
|  | 22 | */ | 
|---|
| [cc04b7] | 23 | ActionRegistry::ActionRegistry() | 
|---|
|  | 24 | { | 
|---|
|  | 25 | } | 
|---|
|  | 26 |  | 
|---|
| [d0fbec] | 27 | /** Destructor for class ActionRegistry. | 
|---|
|  | 28 | */ | 
|---|
| [cc04b7] | 29 | ActionRegistry::~ActionRegistry() | 
|---|
|  | 30 | { | 
|---|
| [12b845] | 31 | map<const string,Action*>::iterator iter; | 
|---|
| [cf1a07] | 32 | for(iter=actionMap.begin();iter!=actionMap.end();++iter) { | 
|---|
| [12b845] | 33 | delete iter->second; | 
|---|
|  | 34 | } | 
|---|
| [cf1a07] | 35 | actionMap.clear(); | 
|---|
| [cc04b7] | 36 | } | 
|---|
|  | 37 |  | 
|---|
| [d0fbec] | 38 | /** Returns pointer to an action named by \a name. | 
|---|
|  | 39 | * \param name name of action | 
|---|
|  | 40 | * \return pointer to Action | 
|---|
|  | 41 | */ | 
|---|
| [cc04b7] | 42 | Action* ActionRegistry::getActionByName(const std::string name){ | 
|---|
|  | 43 | map<const string,Action*>::iterator iter; | 
|---|
|  | 44 | iter = actionMap.find(name); | 
|---|
| [d56640] | 45 | ASSERT(iter!=actionMap.end(),"Query for an action not stored in registry"); | 
|---|
| [cc04b7] | 46 | return iter->second; | 
|---|
|  | 47 | } | 
|---|
|  | 48 |  | 
|---|
| [d0fbec] | 49 | /** States whether action is present or not. | 
|---|
|  | 50 | * \note This iss needed as ActionRegistry::getActionByName() ASSERT()s that action is in map. | 
|---|
|  | 51 | * \param name name of action | 
|---|
|  | 52 | * \return true - Action present, false - Action absent | 
|---|
|  | 53 | */ | 
|---|
| [1e6913] | 54 | bool ActionRegistry::isActionByNamePresent(const std::string name){ | 
|---|
|  | 55 | map<const string,Action*>::iterator iter; | 
|---|
|  | 56 | iter = actionMap.find(name); | 
|---|
|  | 57 | return iter!=actionMap.end(); | 
|---|
|  | 58 | } | 
|---|
|  | 59 |  | 
|---|
| [d0fbec] | 60 | /** Registers an Action with the ActionRegistry. | 
|---|
|  | 61 | * \param *action pointer to Action. | 
|---|
|  | 62 | */ | 
|---|
| [cc04b7] | 63 | void ActionRegistry::registerAction(Action* action){ | 
|---|
|  | 64 | pair<map<const string,Action*>::iterator,bool> ret; | 
|---|
| [d0fbec] | 65 | //cout << "Trying to register action with name " << action->getName() << "." << endl; | 
|---|
| [cc04b7] | 66 | ret = actionMap.insert(pair<const string,Action*>(action->getName(),action)); | 
|---|
| [d56640] | 67 | ASSERT(ret.second,"Two actions with the same name added to registry"); | 
|---|
| [cc04b7] | 68 | } | 
|---|
|  | 69 |  | 
|---|
| [d0fbec] | 70 | /** Unregisters an Action. | 
|---|
|  | 71 | * \param *action pointer to Action. | 
|---|
|  | 72 | */ | 
|---|
| [97ebf8] | 73 | void ActionRegistry::unregisterAction(Action* action){ | 
|---|
| [d0fbec] | 74 | //cout << "Unregistering action with name " << action->getName() << "." << endl; | 
|---|
| [97ebf8] | 75 | actionMap.erase(action->getName()); | 
|---|
|  | 76 | } | 
|---|
|  | 77 |  | 
|---|
| [d0fbec] | 78 | /** Returns an iterator pointing to the start of the map of Action's. | 
|---|
|  | 79 | * \return begin iterator | 
|---|
|  | 80 | */ | 
|---|
| [fa56f0] | 81 | std::map<const std::string,Action*>::iterator ActionRegistry::getBeginIter() | 
|---|
|  | 82 | { | 
|---|
|  | 83 | return actionMap.begin(); | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
| [d0fbec] | 86 | /** Returns an iterator pointing to the end of the map of Action's. | 
|---|
|  | 87 | * \return end iterator | 
|---|
|  | 88 | */ | 
|---|
| [fa56f0] | 89 | std::map<const std::string,Action*>::iterator ActionRegistry::getEndIter() | 
|---|
|  | 90 | { | 
|---|
|  | 91 | return actionMap.end(); | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
| [d0fbec] | 94 | /** Returns a const iterator pointing to the start of the map of Action's. | 
|---|
|  | 95 | * \return constant begin iterator | 
|---|
|  | 96 | */ | 
|---|
|  | 97 | std::map<const std::string,Action*>::const_iterator ActionRegistry::getBeginIter() const | 
|---|
|  | 98 | { | 
|---|
|  | 99 | return actionMap.begin(); | 
|---|
|  | 100 | } | 
|---|
|  | 101 |  | 
|---|
|  | 102 | /** Returns a const iterator pointing to the end of the map of Action's. | 
|---|
|  | 103 | * \return constant end iterator | 
|---|
|  | 104 | */ | 
|---|
|  | 105 | std::map<const std::string,Action*>::const_iterator ActionRegistry::getEndIter() const | 
|---|
|  | 106 | { | 
|---|
|  | 107 | return actionMap.end(); | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | /** Prints the contents of the ActionRegistry \a &m to \a &ost. | 
|---|
|  | 111 | * \param &ost output stream | 
|---|
|  | 112 | * \param &m reference to ActionRegistry | 
|---|
|  | 113 | * \return reference to the above out stream for concatenation | 
|---|
|  | 114 | */ | 
|---|
|  | 115 | ostream& operator<<(ostream& ost, const ActionRegistry& m) | 
|---|
|  | 116 | { | 
|---|
|  | 117 | ost << "ActionRegistry contains:" << endl; | 
|---|
|  | 118 | for (std::map<const std::string,Action*>::const_iterator iter = m.getBeginIter(); iter != m.getEndIter(); ++iter) { | 
|---|
|  | 119 | ost << "\t" << iter->first << " with pointer " << iter->second << endl; | 
|---|
|  | 120 | } | 
|---|
|  | 121 | return ost; | 
|---|
|  | 122 | }; | 
|---|
|  | 123 |  | 
|---|
|  | 124 |  | 
|---|
|  | 125 |  | 
|---|
| [e73a8a2] | 126 | CONSTRUCT_SINGLETON(ActionRegistry) | 
|---|