source: src/CodePatterns/Registry.hpp@ 163eec

Last change on this file since 163eec was 9b8fa4, checked in by Frederik Heber <heber@…>, 14 years ago

Huge update of file structure to place installation header files into right folder.

  • The problem ist that we desire use as include "CodePatterns/...". For this to work, especially with the new Observer subfolder structure, it has been necessary to place all header files away from their source files into a distinct folder called CodePatterns. This emulates the later, after make install present structure.
  • essentially all source and header files had to be changed to adapt the include.
  • all Makefile.am's had to be changed.
  • nobase_ ... was removed such that header files are installed flat and not creating their subfolder along the process.
  • We placed Observer into its own convenience library and own folder Observer away from Patterns.

Some other changes:

  • FIX: MemDebug.hpp inclusion has been removed in all stubs.
  • Property mode set to 100644
File size: 3.3 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 * -# The type stored in the registry needs to have a std::string getName() function that
58 * returns the name under which the instance is stored.
59 *
60 */
61
62template <class T>
63class Registry
64{
65public:
66 Registry();
67 ~Registry();
68
69 typedef typename std::map<const std::string,T*> instance_map;
70 typedef typename std::map<const std::string,T*>::iterator iterator;
71 typedef typename std::map<const std::string,T*>::const_iterator const_iterator;
72
73 T* getByName(const std::string name) const;
74 bool isPresentByName(const std::string name) const;
75 void registerInstance(T*);
76 void unregisterInstance(T*);
77 void cleanup();
78
79 iterator getBeginIter();
80 const_iterator getBeginIter() const;
81 iterator getEndIter();
82 const_iterator getEndIter() const;
83
84private:
85 instance_map InstanceMap;
86};
87
88template <class T> std::ostream& operator<<(std::ostream& ost, const Registry<T>& m);
89
90#endif /* REGISTRY_HPP_ */
Note: See TracBrowser for help on using the repository browser.