xref: /haiku/src/add-ons/kernel/file_systems/packagefs/resolvables/DependencyFamily.h (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 /*
2  * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef DEPENDENCY_FAMILY_H
6 #define DEPENDENCY_FAMILY_H
7 
8 
9 #include <util/khash.h>
10 #include <util/OpenHashTable.h>
11 
12 #include "Dependency.h"
13 
14 
15 class DependencyFamily {
16 public:
17 			void				AddDependency(Dependency* dependency);
18 			void				RemoveDependency(Dependency* dependency);
19 
20 			void				AddDependenciesToList(
21 									ResolvableDependencyList& list) const;
22 
23 			String				Name() const;
24 
25 			bool				IsLastDependency(Dependency* dependency) const;
26 
27 			DependencyFamily*&	HashLink()	{ return fHashLink; }
28 
29 private:
30 			DependencyFamily*	fHashLink;
31 			FamilyDependencyList fDependencies;
32 };
33 
34 
35 inline void
36 DependencyFamily::AddDependency(Dependency* dependency)
37 {
38 	fDependencies.Add(dependency);
39 	dependency->SetFamily(this);
40 }
41 
42 
43 inline void
44 DependencyFamily::RemoveDependency(Dependency* dependency)
45 {
46 	dependency->SetFamily(NULL);
47 	fDependencies.Remove(dependency);
48 }
49 
50 
51 inline void
52 DependencyFamily::AddDependenciesToList(ResolvableDependencyList& list) const
53 {
54 	for (FamilyDependencyList::ConstIterator it = fDependencies.GetIterator();
55 			Dependency* dependency = it.Next();) {
56 		list.Add(dependency);
57 	}
58 }
59 
60 
61 inline String
62 DependencyFamily::Name() const
63 {
64 	Dependency* head = fDependencies.Head();
65 	return head != NULL ? head->Name() : String();
66 }
67 
68 
69 inline bool
70 DependencyFamily::IsLastDependency(Dependency* dependency) const
71 {
72 	return fDependencies.Head() == dependency
73 		&& fDependencies.Tail() == dependency;
74 }
75 
76 
77 // #pragma mark - DependencyFamilyHashDefinition
78 
79 
80 struct DependencyFamilyHashDefinition {
81 	typedef String				KeyType;
82 	typedef	DependencyFamily	ValueType;
83 
84 	size_t HashKey(const String& key) const
85 	{
86 		return key.Hash();
87 	}
88 
89 	size_t Hash(const DependencyFamily* value) const
90 	{
91 		return value->Name().Hash();
92 	}
93 
94 	bool Compare(const String& key, const DependencyFamily* value) const
95 	{
96 		return key == value->Name();
97 	}
98 
99 	DependencyFamily*& GetLink(DependencyFamily* value) const
100 	{
101 		return value->HashLink();
102 	}
103 };
104 
105 
106 typedef BOpenHashTable<DependencyFamilyHashDefinition>
107 	DependencyFamilyHashTable;
108 
109 
110 #endif	// DEPENDENCY_FAMILY_H
111