1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2014, Rene Gollent, rene@gollent.com.
4 * Distributed under the terms of the MIT License.
5 */
6#ifndef GLOBAL_TYPE_LOOKUP_H
7#define GLOBAL_TYPE_LOOKUP_H
8
9
10#include <image.h>
11#include <Locker.h>
12
13#include <Referenceable.h>
14#include <util/OpenHashTable.h>
15
16
17class BString;
18class Type;
19class TypeLookupConstraints;
20
21
22enum global_type_cache_scope {
23	GLOBAL_TYPE_CACHE_SCOPE_GLOBAL,
24	GLOBAL_TYPE_CACHE_SCOPE_COMPILATION_UNIT
25};
26
27
28class GlobalTypeCache : public BReferenceable {
29public:
30								GlobalTypeCache();
31								~GlobalTypeCache();
32
33			status_t			Init();
34
35	inline	bool				Lock();
36	inline	void				Unlock();
37
38			// cache must be locked
39			Type*				GetType(const BString& name,
40									const TypeLookupConstraints &constraints
41									) const;
42			Type*				GetTypeByID(const BString& id) const;
43			status_t			AddType(Type* type);
44			void				RemoveType(Type* type);
45
46			// cache locked by method
47			void				RemoveTypes(image_id imageID);
48
49private:
50			struct TypeEntry;
51			struct TypeEntryHashDefinitionByName;
52			struct TypeEntryHashDefinitionByID;
53
54			typedef BOpenHashTable<TypeEntryHashDefinitionByName> NameTable;
55			typedef BOpenHashTable<TypeEntryHashDefinitionByID> IDTable;
56
57private:
58			BLocker				fLock;
59			NameTable*			fTypesByName;
60			IDTable*			fTypesByID;
61};
62
63
64class GlobalTypeLookup {
65public:
66	virtual						~GlobalTypeLookup();
67
68	virtual	status_t			GetType(GlobalTypeCache* cache,
69									const BString& name,
70									const TypeLookupConstraints& constraints,
71									Type*& _type) = 0;
72									// returns a reference
73
74	virtual	bool				HasType(GlobalTypeCache* cache,
75									const BString& name,
76									const TypeLookupConstraints& constraints)
77									= 0;
78};
79
80
81bool
82GlobalTypeCache::Lock()
83{
84	return fLock.Lock();
85}
86
87
88void
89GlobalTypeCache::Unlock()
90{
91	fLock.Unlock();
92}
93
94
95#endif	// GLOBAL_TYPE_LOOKUP_H
96