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