1/*
2 * Copyright 2013-2014, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Ingo Weinhold <ingo_weinhold@gmx.de>
7 */
8#ifndef PACKAGE_H
9#define PACKAGE_H
10
11
12#include <set>
13
14#include "PackageFile.h"
15
16
17using namespace BPackageKit;
18
19
20class Package {
21public:
22								Package(PackageFile* file);
23								~Package();
24
25			PackageFile*		File() const
26									{ return fFile; }
27
28			const node_ref&		NodeRef() const
29									{ return fFile->NodeRef(); }
30			const BString&		FileName() const
31									{ return fFile->FileName(); }
32			NotOwningEntryRef	EntryRef() const
33									{ return fFile->EntryRef(); }
34
35			const BPackageInfo & Info() const
36									{ return fFile->Info(); }
37
38			BString				RevisionedName() const
39									{ return fFile->RevisionedName(); }
40			BString				RevisionedNameThrows() const
41									{ return fFile->RevisionedNameThrows(); }
42
43			bool				IsActive() const
44									{ return fActive; }
45			void				SetActive(bool active)
46									{ fActive = active; }
47
48			Package*			Clone() const;
49
50			Package*&			FileNameHashTableNext()
51									{ return fFileNameHashTableNext; }
52			Package*&			NodeRefHashTableNext()
53									{ return fNodeRefHashTableNext; }
54
55private:
56			PackageFile*		fFile;
57			bool				fActive;
58			Package*			fFileNameHashTableNext;
59			Package*			fNodeRefHashTableNext;
60};
61
62
63struct PackageFileNameHashDefinition {
64	typedef const char*		KeyType;
65	typedef	Package			ValueType;
66
67	size_t HashKey(const char* key) const
68	{
69		return BString::HashValue(key);
70	}
71
72	size_t Hash(const Package* value) const
73	{
74		return HashKey(value->FileName());
75	}
76
77	bool Compare(const char* key, const Package* value) const
78	{
79		return value->FileName() == key;
80	}
81
82	Package*& GetLink(Package* value) const
83	{
84		return value->FileNameHashTableNext();
85	}
86};
87
88
89struct PackageNodeRefHashDefinition {
90	typedef node_ref		KeyType;
91	typedef	Package			ValueType;
92
93	size_t HashKey(const node_ref& key) const
94	{
95		return (size_t)key.device + 17 * (size_t)key.node;
96	}
97
98	size_t Hash(const Package* value) const
99	{
100		return HashKey(value->NodeRef());
101	}
102
103	bool Compare(const node_ref& key, const Package* value) const
104	{
105		return key == value->NodeRef();
106	}
107
108	Package*& GetLink(Package* value) const
109	{
110		return value->NodeRefHashTableNext();
111	}
112};
113
114
115typedef BOpenHashTable<PackageFileNameHashDefinition> PackageFileNameHashTable;
116typedef BOpenHashTable<PackageNodeRefHashDefinition> PackageNodeRefHashTable;
117typedef std::set<Package*> PackageSet;
118
119
120#endif	// PACKAGE_H
121