171f75cdcSIngo Weinhold/*
271f75cdcSIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3d21bcbb0SRene Gollent * Copyright 2014, Rene Gollent, rene@gollent.com.
471f75cdcSIngo Weinhold * Distributed under the terms of the MIT License.
571f75cdcSIngo Weinhold */
671f75cdcSIngo Weinhold#ifndef GLOBAL_TYPE_LOOKUP_H
771f75cdcSIngo Weinhold#define GLOBAL_TYPE_LOOKUP_H
871f75cdcSIngo Weinhold
971f75cdcSIngo Weinhold
10d054be0dSIngo Weinhold#include <image.h>
1171f75cdcSIngo Weinhold#include <Locker.h>
1271f75cdcSIngo Weinhold
1371f75cdcSIngo Weinhold#include <Referenceable.h>
1471f75cdcSIngo Weinhold#include <util/OpenHashTable.h>
1571f75cdcSIngo Weinhold
1671f75cdcSIngo Weinhold
1771f75cdcSIngo Weinholdclass BString;
1871f75cdcSIngo Weinholdclass Type;
19185d2cdcSRene Gollentclass TypeLookupConstraints;
2071f75cdcSIngo Weinhold
2171f75cdcSIngo Weinhold
2259ea286fSIngo Weinholdenum global_type_cache_scope {
2359ea286fSIngo Weinhold	GLOBAL_TYPE_CACHE_SCOPE_GLOBAL,
2459ea286fSIngo Weinhold	GLOBAL_TYPE_CACHE_SCOPE_COMPILATION_UNIT
2559ea286fSIngo Weinhold};
2659ea286fSIngo Weinhold
2759ea286fSIngo Weinhold
28c3e066cfSIngo Weinholdclass GlobalTypeCache : public BReferenceable {
2971f75cdcSIngo Weinholdpublic:
30d054be0dSIngo Weinhold								GlobalTypeCache();
31d054be0dSIngo Weinhold								~GlobalTypeCache();
3271f75cdcSIngo Weinhold
3371f75cdcSIngo Weinhold			status_t			Init();
3471f75cdcSIngo Weinhold
3571f75cdcSIngo Weinhold	inline	bool				Lock();
3671f75cdcSIngo Weinhold	inline	void				Unlock();
3771f75cdcSIngo Weinhold
38d054be0dSIngo Weinhold			// cache must be locked
3967737919SRene Gollent			Type*				GetType(const BString& name,
4067737919SRene Gollent									const TypeLookupConstraints &constraints
4167737919SRene Gollent									) const;
4259ea286fSIngo Weinhold			Type*				GetTypeByID(const BString& id) const;
4359ea286fSIngo Weinhold			status_t			AddType(Type* type);
4459ea286fSIngo Weinhold			void				RemoveType(Type* type);
45d054be0dSIngo Weinhold
46d054be0dSIngo Weinhold			// cache locked by method
47d054be0dSIngo Weinhold			void				RemoveTypes(image_id imageID);
4871f75cdcSIngo Weinhold
4971f75cdcSIngo Weinholdprivate:
5071f75cdcSIngo Weinhold			struct TypeEntry;
5159ea286fSIngo Weinhold			struct TypeEntryHashDefinitionByName;
5259ea286fSIngo Weinhold			struct TypeEntryHashDefinitionByID;
5371f75cdcSIngo Weinhold
5459ea286fSIngo Weinhold			typedef BOpenHashTable<TypeEntryHashDefinitionByName> NameTable;
5559ea286fSIngo Weinhold			typedef BOpenHashTable<TypeEntryHashDefinitionByID> IDTable;
5671f75cdcSIngo Weinhold
5771f75cdcSIngo Weinholdprivate:
5871f75cdcSIngo Weinhold			BLocker				fLock;
5959ea286fSIngo Weinhold			NameTable*			fTypesByName;
6059ea286fSIngo Weinhold			IDTable*			fTypesByID;
6171f75cdcSIngo Weinhold};
6271f75cdcSIngo Weinhold
6371f75cdcSIngo Weinhold
6471f75cdcSIngo Weinholdclass GlobalTypeLookup {
6571f75cdcSIngo Weinholdpublic:
663c9dd7d7SAxel Dörfler	virtual						~GlobalTypeLookup();
6771f75cdcSIngo Weinhold
68d054be0dSIngo Weinhold	virtual	status_t			GetType(GlobalTypeCache* cache,
69185d2cdcSRene Gollent									const BString& name,
70185d2cdcSRene Gollent									const TypeLookupConstraints& constraints,
71185d2cdcSRene Gollent									Type*& _type) = 0;
7271f75cdcSIngo Weinhold									// returns a reference
73d21bcbb0SRene Gollent
74d21bcbb0SRene Gollent	virtual	bool				HasType(GlobalTypeCache* cache,
75d21bcbb0SRene Gollent									const BString& name,
76d21bcbb0SRene Gollent									const TypeLookupConstraints& constraints)
77d21bcbb0SRene Gollent									= 0;
7871f75cdcSIngo Weinhold};
7971f75cdcSIngo Weinhold
8071f75cdcSIngo Weinhold
8171f75cdcSIngo Weinholdbool
82d054be0dSIngo WeinholdGlobalTypeCache::Lock()
8371f75cdcSIngo Weinhold{
8471f75cdcSIngo Weinhold	return fLock.Lock();
8571f75cdcSIngo Weinhold}
8671f75cdcSIngo Weinhold
8771f75cdcSIngo Weinhold
8871f75cdcSIngo Weinholdvoid
89d054be0dSIngo WeinholdGlobalTypeCache::Unlock()
9071f75cdcSIngo Weinhold{
9171f75cdcSIngo Weinhold	fLock.Unlock();
9271f75cdcSIngo Weinhold}
9371f75cdcSIngo Weinhold
9471f75cdcSIngo Weinhold
9571f75cdcSIngo Weinhold#endif	// GLOBAL_TYPE_LOOKUP_H
96