1/*
2 * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef RESOLVABLE_FAMILY_H
6#define RESOLVABLE_FAMILY_H
7
8
9#include <util/khash.h>
10#include <util/OpenHashTable.h>
11
12#include "Resolvable.h"
13
14
15class ResolvableFamily {
16public:
17			void				AddResolvable(Resolvable* resolvable,
18									ResolvableDependencyList&
19										dependenciesToUpdate);
20			void				RemoveResolvable(Resolvable* resolvable,
21									ResolvableDependencyList&
22										dependenciesToUpdate);
23
24			bool				ResolveDependency(Dependency* dependency);
25
26			const char*			Name() const;
27
28			bool				IsLastResolvable(Resolvable* resolvable) const;
29
30			ResolvableFamily*&	HashLink()	{ return fHashLink; }
31
32private:
33			ResolvableFamily*	fHashLink;
34			FamilyResolvableList fResolvables;
35};
36
37
38inline const char*
39ResolvableFamily::Name() const
40{
41	Resolvable* head = fResolvables.Head();
42	return head != NULL ? head->Name() : NULL;
43}
44
45
46inline bool
47ResolvableFamily::IsLastResolvable(Resolvable* resolvable) const
48{
49	return fResolvables.Head() == resolvable
50		&& fResolvables.Tail() == resolvable;
51}
52
53
54// #pragma mark - ResolvableFamilyHashDefinition
55
56
57struct ResolvableFamilyHashDefinition {
58	typedef const char*			KeyType;
59	typedef	ResolvableFamily	ValueType;
60
61	size_t HashKey(const char* key) const
62	{
63		return key != NULL ? hash_hash_string(key) : 0;
64	}
65
66	size_t Hash(const ResolvableFamily* value) const
67	{
68		return HashKey(value->Name());
69	}
70
71	bool Compare(const char* key, const ResolvableFamily* value) const
72	{
73		return strcmp(value->Name(), key) == 0;
74	}
75
76	ResolvableFamily*& GetLink(ResolvableFamily* value) const
77	{
78		return value->HashLink();
79	}
80};
81
82
83typedef BOpenHashTable<ResolvableFamilyHashDefinition>
84	ResolvableFamilyHashTable;
85
86
87#endif	// RESOLVABLE_FAMILY_H
88