Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Registry_impl.hpp

    r311d688 rb59da6  
    1515
    1616#include "Helpers/Assert.hpp"
    17 #include <iosfwd>
     17#include <iostream>
    1818
    1919/** Constructor for class Registry.
     
    2525 */
    2626template <class T> Registry<T>::~Registry()
    27 {
    28   typename std::map<const std::string,T*>::iterator iter;
    29   for(iter=InstanceMap.begin();iter!=InstanceMap.end();++iter) {
    30     delete iter->second;
    31   }
    32   InstanceMap.clear();
    33 }
     27{}
    3428
    3529/** Returns pointer to an instance named by \a name.
     
    3731 * \return pointer to instance
    3832 */
    39 template <class T> T* Registry<T>::getByName(const std::string name){
    40   typename std::map<const std::string,T*>::iterator iter;
     33template <class T> T* Registry<T>::getByName(const std::string name) const
     34{
     35  typename std::map<const std::string,T*>::const_iterator iter;
    4136  iter = InstanceMap.find(name);
    42   ASSERT(iter!=InstanceMap.end(),"Query for an instance not stored in registry");
     37  ASSERT(iter!=InstanceMap.end(),"Query for an instance "+name+" not stored in registry");
    4338  return iter->second;
    4439}
     
    4742 * \note This is needed as Registry<T>::getByName() ASSERT()s that instance is in std::map.
    4843 * \param name name of instance
    49  * \return true - v present, false - instance absent
     44 * \return true - present, false - instance absent
    5045 */
    51 template <class T>bool Registry<T>::isPresentByName(const std::string name){
    52   typename std::map<const std::string,T*>::iterator iter;
     46template <class T>bool Registry<T>::isPresentByName(const std::string name) const
     47{
     48  typename std::map<const std::string,T*>::const_iterator iter;
    5349  iter = InstanceMap.find(name);
    5450  return iter!=InstanceMap.end();
     
    6056template <class T>void Registry<T>::registerInstance(T* instance){
    6157  std::pair<typename std::map<const std::string,T*>::iterator,bool> ret;
    62   //cout << "Trying to register instance with name " << instance->getName() << "." << endl;
     58  //std::cout << "Trying to register instance of type " << typeid(T).name() << " with name " << instance->getName() << "." << std::endl;
    6359  ret = InstanceMap.insert(typename std::pair<const std::string,T*>(instance->getName(),instance));
    64   ASSERT(ret.second,"Two instances with the same name added to registry");
     60  ASSERT(ret.second,"Two instances with the same name "+instance->getName()+" added to registry");
    6561}
    6662
     
    6965 */
    7066template <class T>void Registry<T>::unregisterInstance(T* instance){
    71   //cout << "Unregistering instance with name " << instance->getName() << "." << endl;
     67  //std::cout << "Unregistering instance of type " << typeid(T).name() << " with name " << instance->getName() << "." << std::endl;
    7268  InstanceMap.erase(instance->getName());
    7369}
     70
     71/** Removes every instance from the registry.
     72 */
     73template <class T>void Registry<T>::cleanup()
     74{
     75  typename std::map<const std::string,T*>::iterator iter;
     76  for(iter=InstanceMap.begin();iter!=InstanceMap.end();++iter) {
     77    delete iter->second;
     78  }
     79  InstanceMap.clear();
     80}
     81
    7482
    7583/** Returns an iterator pointing to the start of the std::map of instance's.
     
    128136 * at a chosen place.
    129137 */
    130 #define CONSTRUCT_REGISTRY(name) \
    131     template name* Registry<name>::getByName(const std::string name); \
    132     template bool Registry<name>::isPresentByName(const std::string name); \
    133     template void Registry<name>::registerInstance(name*); \
    134     template void Registry<name>::unregisterInstance(name*); \
    135     template std::map<const std::string,name*>::iterator Registry<name>::getBeginIter(); \
    136     template std::map<const std::string,name*>::const_iterator Registry<name>::getBeginIter() const; \
    137     template std::map<const std::string,name*>::iterator Registry<name>::getEndIter(); \
    138     template std::map<const std::string,name*>::const_iterator Registry<name>::getEndIter() const;
     138#define CONSTRUCT_REGISTRY(InstanceType) \
     139    template InstanceType* Registry<InstanceType>::getByName(const std::string) const; \
     140    template bool Registry<InstanceType>::isPresentByName(const std::string) const; \
     141    template void Registry<InstanceType>::registerInstance(InstanceType*); \
     142    template void Registry<InstanceType>::unregisterInstance(InstanceType*); \
     143    template std::map<const std::string,InstanceType*>::iterator Registry<InstanceType>::getBeginIter(); \
     144    template std::map<const std::string,InstanceType*>::const_iterator Registry<InstanceType>::getBeginIter() const; \
     145    template std::map<const std::string,InstanceType*>::iterator Registry<InstanceType>::getEndIter(); \
     146    template std::map<const std::string,InstanceType*>::const_iterator Registry<InstanceType>::getEndIter() const;
    139147
    140148
Note: See TracChangeset for help on using the changeset viewer.