source: src/Patterns/Registry.hpp@ cdf2e4

Last change on this file since cdf2e4 was 70672e3, checked in by Frederik Heber <heber@…>, 15 years ago

Added config.h to all header files.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*
2 * Registry.hpp
3 *
4 * Based on initial ActionRegistry code by Till Crueger.
5 *
6 * The registry pattern is basically just a singleton map, wherein instantiations
7 * of a class can be registered, unregistered and retrieved.
8 *
9 * Created on: Jul 28, 2010
10 * Author: heber
11 */
12
13#ifndef REGISTRY_HPP_
14#define REGISTRY_HPP_
15
16// include config.h
17#ifdef HAVE_CONFIG_H
18#include <config.h>
19#endif
20
21#include <map>
22
23/**
24 * This template produces a generic registry pattern.
25 *
26 * <h1> Registry Howto </h1>
27 *
28 * The Registry is a class where instances of other classes are stored and be retrieved
29 * by a string token when desired. For this purpose a Registry should always be a singleton
30 * (i.e. use both this Registry and the Singleton pattern to declare a Registry class). It
31 * basically is simply a singleton container of a map, where the pointers to the class
32 * instances are stored by a string key and can be retrieved thereby.
33 *
34 * The available functions are as follows if your class instances to be stored in Registry
35 * are of type 'foo':
36 *
37 * - <code>foo* Registry<foo>::getByName()</code> : returns the instance of a specific
38 * class foo instance as a pointer associated with the given name
39 * - <code>bool Registry<foo>::isPresentByName()</code> : returns whether an instance
40 * of class foo is present under the given name.
41 * - <code>map<string,foo*>::iterator Registry<foo>::getBeginIter()</code> : returns an
42 * iterator to the beginning of the storage map (STL).
43 * - <code>map<string,foo*>::const_iterator Registry<foo>::getBeginIter()</code> : returns a
44 * constant iterator to the beginning of the storage map (STL).
45 * - <code>map<string,foo*>::const_iterator Registry<foo>::getEndIter()</code> : returns an
46 * iterator to the one step past the last element of the storage map (STL).
47 * - <code>map<string,foo*>::const_iterator Registry<foo>::getEndIter()</code> : returns a
48 * constant iterator to the one step past the last element of the storage map (STL).
49 *
50 * In order to use this pattern, additionally to the requirements of the Singleton pattern,
51 * do this:
52 * -# in the declaration derive your class from Registry<foo>, where foo is the class to be
53 * stored
54 * -# in the definition add CONSTRUCT_REGISTRY(foo) to the code such that the templated
55 * functions get instantiated there (otherwise you'll get undefined reference errors).
56 *
57 */
58
59template <class T>
60class Registry
61{
62public:
63 Registry();
64 ~Registry();
65
66 typedef typename std::map<const std::string,T*> instance_map;
67 typedef typename std::map<const std::string,T*>::iterator iterator;
68 typedef typename std::map<const std::string,T*>::const_iterator const_iterator;
69
70 T* getByName(const std::string name) const;
71 bool isPresentByName(const std::string name) const;
72 void registerInstance(T*);
73 void unregisterInstance(T*);
74 void cleanup();
75
76 iterator getBeginIter();
77 const_iterator getBeginIter() const;
78 iterator getEndIter();
79 const_iterator getEndIter() const;
80
81private:
82 instance_map InstanceMap;
83};
84
85template <class T> std::ostream& operator<<(std::ostream& ost, const Registry<T>& m);
86
87#endif /* REGISTRY_HPP_ */
Note: See TracBrowser for help on using the repository browser.