registration.hh
Go to the documentation of this file.
#pragma once
#include <string>
#include <map>
template <typename TItem>
class registration { // TODO Refine the naming, derive from std::map<std::string, TItem*>
std::map<std::string, TItem*> _items;
static registration<TItem>* _instance;
private:
registration(): _items() { }; // can not be created manually
public:
static registration<TItem>& get_instance();
public:
void add_item(const std::string& type, TItem* f);
TItem * get_item(const std::string& type);
}; // End of class registration
template <typename TItem>
registration<TItem>* registration<TItem>::_instance = nullptr;
// static functions
template <typename TItem>
registration<TItem>& registration<TItem>::get_instance()
{
return (_instance != nullptr) ? *_instance : *(_instance = new registration());
}
template <typename TItem>
void registration<TItem>::add_item(const std::string & type, TItem * f)
{
_items[type] = f;
}
template <typename TItem>
TItem* registration<TItem>::get_item(const std::string & type)
{
typename std::map<std::string, TItem*>::const_iterator it =_items.find(type);
if (it == _items.cend()) {
return nullptr;
}
return it->second;
} // End of class registration
registration::add_item
void add_item(const std::string &type, TItem *f)
Definition: registration.hh:48
registration::get_instance
static registration< TItem > & get_instance()
Definition: registration.hh:42
registration
This class provides a way to register items and its object reference The registered items are accessi...
Definition: registration.hh:23
registration::_items
std::map< std::string, TItem * > _items
Definition: registration.hh:24
registration::get_item
TItem * get_item(const std::string &type)
Definition: registration.hh:54
registration::_instance
static registration< TItem > * _instance
Definition: registration.hh:26
registration::registration
registration()
Definition: registration.hh:28