/* * Registry_impl.hpp * * Created on: Jul 28, 2010 * Author: heber */ #ifndef REGISTRY_IMPL_HPP_ #define REGISTRY_IMPL_HPP_ #include "Helpers/MemDebug.hpp" #include "Patterns/Registry.hpp" #include "Patterns/Singleton_impl.hpp" #include "Helpers/Assert.hpp" #include /** Constructor for class Registry. */ template Registry::Registry() {} /** Destructor for class Registry. */ template Registry::~Registry() {} /** Returns pointer to an instance named by \a name. * \param name name of instance * \return pointer to instance */ template T* Registry::getByName(const std::string name) const { typename std::map::const_iterator iter; iter = InstanceMap.find(name); ASSERT(iter!=InstanceMap.end(),"Query for an instance "+name+" not stored in registry"); return iter->second; } /** States whether instance is present or not. * \note This is needed as Registry::getByName() ASSERT()s that instance is in std::map. * \param name name of instance * \return true - present, false - instance absent */ template bool Registry::isPresentByName(const std::string name) const { typename std::map::const_iterator iter; iter = InstanceMap.find(name); return iter!=InstanceMap.end(); } /** Registers an instance with the Registry. * \param *instance pointer to T. */ template void Registry::registerInstance(T* instance){ std::pair::iterator,bool> ret; //std::cout << "Trying to register instance of type " << typeid(T).name() << " with name " << instance->getName() << "." << std::endl; ret = InstanceMap.insert(typename std::pair(instance->getName(),instance)); ASSERT(ret.second,"Two instances with the same name "+instance->getName()+" added to registry"); } /** Unregisters an instance. * \param *instance pointer to instance. */ template void Registry::unregisterInstance(T* instance){ //std::cout << "Unregistering instance of type " << typeid(T).name() << " with name " << instance->getName() << "." << std::endl; InstanceMap.erase(instance->getName()); } /** Removes every instance from the registry. */ template void Registry::cleanup() { typename std::map::iterator iter; for(iter=InstanceMap.begin();iter!=InstanceMap.end();++iter) { //std::cerr << "Removing instance "+iter->first+" from registry" << std::endl; delete iter->second; } InstanceMap.clear(); } /** Returns an iterator pointing to the start of the std::map of instance's. * \return begin iterator */ template typename std::map::iterator Registry::getBeginIter() { return InstanceMap.begin(); } /** Returns an iterator pointing to the end of the std::map of instance's. * \return end iterator */ template typename std::map::iterator Registry::getEndIter() { return InstanceMap.end(); } /** Returns a const iterator pointing to the start of the std::map of instance's. * \return constant begin iterator */ template typename std::map::const_iterator Registry::getBeginIter() const { return InstanceMap.begin(); } /** Returns a const iterator pointing to the end of the std::map of instance's. * \return constant end iterator */ template typename std::map::const_iterator Registry::getEndIter() const { return InstanceMap.end(); } /** Prints the contents of the Registry \a &m to \a &ost. * \param &ost output stream * \param &m reference to Registry * \return reference to the above out stream for concatenation */ template std::ostream& operator<<(std::ostream& ost, const Registry& m) { ost << "Registry contains:" << std::endl; for (typename std::map::const_iterator iter = m.getBeginIter(); iter != m.getEndIter(); ++iter) { ost << "\t" << iter->first << " with pointer " << iter->second << std::endl; } return ost; }; /** * This define allows simple instantiation of the necessary registryfunctions * at a chosen place. */ #define CONSTRUCT_REGISTRY(InstanceType) \ template InstanceType* Registry::getByName(const std::string) const; \ template bool Registry::isPresentByName(const std::string) const; \ template void Registry::registerInstance(InstanceType*); \ template void Registry::unregisterInstance(InstanceType*); \ template std::map::iterator Registry::getBeginIter(); \ template std::map::const_iterator Registry::getBeginIter() const; \ template std::map::iterator Registry::getEndIter(); \ template std::map::const_iterator Registry::getEndIter() const; #endif /* REGISTRY_IMPL_HPP_ */