source: src/Patterns/Registry.hpp@ d1a85d

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

Implemented unit test for Registry pattern.

  • Library version is now 4:0:2, API version is still 1.0.7.
  • So far only empty test was present.
  • 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#include <string>
23
24/**
25 * This template produces a generic registry pattern.
26 *
27 * <h1> Registry Howto </h1>
28 *
29 * The Registry is a class where instances of other classes are stored and be retrieved
30 * by a string token when desired. For this purpose a Registry should always be a singleton
31 * (i.e. use both this Registry and the Singleton pattern to declare a Registry class). It
32 * basically is simply a singleton container of a map, where the pointers to the class
33 * instances are stored by a string key and can be retrieved thereby.
34 *
35 * The available functions are as follows if your class instances to be stored in Registry
36 * are of type 'foo':
37 *
38 * - <code>foo* Registry<foo>::getByName()</code> : returns the instance of a specific
39 * class foo instance as a pointer associated with the given name
40 * - <code>bool Registry<foo>::isPresentByName()</code> : returns whether an instance
41 * of class foo is present under the given name.
42 * - <code>map<string,foo*>::iterator Registry<foo>::getBeginIter()</code> : returns an
43 * iterator to the beginning of the storage map (STL).
44 * - <code>map<string,foo*>::const_iterator Registry<foo>::getBeginIter()</code> : returns a
45 * constant iterator to the beginning of the storage map (STL).
46 * - <code>map<string,foo*>::const_iterator Registry<foo>::getEndIter()</code> : returns an
47 * iterator to the one step past the last element of the storage map (STL).
48 * - <code>map<string,foo*>::const_iterator Registry<foo>::getEndIter()</code> : returns a
49 * constant iterator to the one step past the last element of the storage map (STL).
50 *
51 * In order to use this pattern, additionally to the requirements of the Singleton pattern,
52 * do this:
53 * -# in the declaration derive your class from Registry<foo>, where foo is the class to be
54 * stored
55 * -# in the definition add CONSTRUCT_REGISTRY(foo) to the code such that the templated
56 * functions get instantiated there (otherwise you'll get undefined reference errors).
57 *
58 */
59
60template <class T>
61class Registry
62{
63public:
64 Registry();
65 ~Registry();
66
67 typedef typename std::map<const std::string,T*> instance_map;
68 typedef typename std::map<const std::string,T*>::iterator iterator;
69 typedef typename std::map<const std::string,T*>::const_iterator const_iterator;
70
71 T* getByName(const std::string name) const;
72 bool isPresentByName(const std::string name) const;
73 void registerInstance(T*);
74 void unregisterInstance(T*);
75 void cleanup();
76
77 iterator getBeginIter();
78 const_iterator getBeginIter() const;
79 iterator getEndIter();
80 const_iterator getEndIter() const;
81
82private:
83 instance_map InstanceMap;
84};
85
86template <class T> std::ostream& operator<<(std::ostream& ost, const Registry<T>& m);
87
88#endif /* REGISTRY_HPP_ */
Note: See TracBrowser for help on using the repository browser.