DEADSOFTWARE

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