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
15class DependencyFamily {
16public:
17			void				AddDependency(Dependency* dependency);
18			void				RemoveDependency(Dependency* dependency);
19
20			void				AddDependenciesToList(
21									ResolvableDependencyList& list) const;
22
23			const char*			Name() const;
24
25			bool				IsLastDependency(Dependency* dependency) const;
26
27			DependencyFamily*&	HashLink()	{ return fHashLink; }
28
29private:
30			DependencyFamily*	fHashLink;
31			FamilyDependencyList fDependencies;
32};
33
34
35inline void
36DependencyFamily::AddDependency(Dependency* dependency)
37{
38	fDependencies.Add(dependency);
39	dependency->SetFamily(this);
40}
41
42
43inline void
44DependencyFamily::RemoveDependency(Dependency* dependency)
45{
46	dependency->SetFamily(NULL);
47	fDependencies.Remove(dependency);
48}
49
50
51inline void
52DependencyFamily::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
61inline const char*
62DependencyFamily::Name() const
63{
64	Dependency* head = fDependencies.Head();
65	return head != NULL ? head->Name() : NULL;
66}
67
68
69inline bool
70DependencyFamily::IsLastDependency(Dependency* dependency) const
71{
72	return fDependencies.Head() == dependency
73		&& fDependencies.Tail() == dependency;
74}
75
76
77// #pragma mark - DependencyFamilyHashDefinition
78
79
80struct DependencyFamilyHashDefinition {
81	typedef const char*			KeyType;
82	typedef	DependencyFamily	ValueType;
83
84	size_t HashKey(const char* key) const
85	{
86		return key != NULL ? hash_hash_string(key) : 0;
87	}
88
89	size_t Hash(const DependencyFamily* value) const
90	{
91		return HashKey(value->Name());
92	}
93
94	bool Compare(const char* key, const DependencyFamily* value) const
95	{
96		return strcmp(value->Name(), key) == 0;
97	}
98
99	DependencyFamily*& GetLink(DependencyFamily* value) const
100	{
101		return value->HashLink();
102	}
103};
104
105
106typedef BOpenHashTable<DependencyFamilyHashDefinition>
107	DependencyFamilyHashTable;
108
109
110#endif	// DEPENDENCY_FAMILY_H
111