DEADSOFTWARE

fix gcc 4.0
[odcread.git] / typeregister / typeregister.h
1 #ifndef _CLASSREGISTER_H_
2 #define _CLASSREGISTER_H_
4 #include <map>
5 #include <string>
7 #include "oberon.h"
9 namespace odc {
10 class Store;
11 class TypeProxyBase; // forward declaration
13 /**
14 * Register of Oberon/BlackBox types.
15 * Each type has a registered name, which refers to the full BlackBox type name (including any modules).
16 * It also (optionally) has a supertype (also referred to by the full BlackBox type name).
17 */
18 class TypeRegister {
19 static TypeRegister *s_instance;
21 std::map<std::string, TypeProxyBase *> d_map;
23 TypeRegister();
24 TypeRegister(const TypeRegister &other); // NI
25 TypeRegister &operator=(const TypeRegister &other); // NI
27 public:
28 /**
29 * Get an instance of the type register (singleton pattern).
30 */
31 static TypeRegister &getInstance();
33 /**
34 * Register a new type.
35 */
36 void add(const std::string &name, TypeProxyBase *proxy);
38 /**
39 * Get a type's proxy.
40 */
41 const TypeProxyBase *get(const std::string &name);
42 };
44 /**
45 * Proxy to represent a BlackBox type. Has a name and optional supertype name.
46 * Can instantiate new instances of the type.
47 */
48 class TypeProxyBase {
49 public:
50 /**
51 * Create a new TypeProxy and register it with the TypeRegister.
52 */
53 TypeProxyBase(const std::string &name);
54 /**
55 * @return The full BlackBox type name (including modules).
56 */
57 virtual const std::string &getName() const = 0;
58 /**
59 * @return The full BlackBox type name of the supertype (if applicable, otherwise a 0-pointer).
60 */
61 virtual const std::string *getSuper() const = 0;
62 /**
63 * @return A new instance of the type, with the given ID.
64 */
65 virtual Store *newInstance(INTEGER id) const = 0;
66 };
68 /**
69 * Proxy for a toplevel type (no supertype).
70 * T: the class to proxy for.
71 * Requires T to have a static const std::string TYPENAME.
72 */
73 template <class T> class TopTypeProxy : public TypeProxyBase {
74 public:
75 TopTypeProxy(): TypeProxyBase(T::TYPENAME) {}
76 virtual const std::string &getName() const {
77 return T::TYPENAME;
78 }
79 virtual const std::string *getSuper() const {
80 return 0;
81 }
82 virtual Store *newInstance(INTEGER id) const {
83 return new T(id);
84 }
85 };
87 /**
88 * Proxy for a derived type (with supertype).
89 * T: the class to proxy for.
90 * S: the supertype.
91 * Requires T, S to have a static const std::string TYPENAME.
92 */
93 template <class T, class S> class TypeProxy : public TypeProxyBase {
94 public:
95 TypeProxy(): TypeProxyBase(T::TYPENAME) {}
96 virtual const std::string &getName() const {
97 return T::TYPENAME;
98 }
99 virtual const std::string *getSuper() const {
100 return &S::TYPENAME;
102 virtual Store *newInstance(INTEGER id) const {
103 return new T(id);
105 };
108 #endif // _CLASSREGISTER_H_