19320567eSIngo Weinhold/*
29320567eSIngo Weinhold * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
39320567eSIngo Weinhold * Distributed under the terms of the MIT License.
49320567eSIngo Weinhold */
59320567eSIngo Weinhold#ifndef DEPENDENCY_FAMILY_H
69320567eSIngo Weinhold#define DEPENDENCY_FAMILY_H
79320567eSIngo Weinhold
89320567eSIngo Weinhold
99320567eSIngo Weinhold#include <util/OpenHashTable.h>
109320567eSIngo Weinhold
119320567eSIngo Weinhold#include "Dependency.h"
129320567eSIngo Weinhold
139320567eSIngo Weinhold
149320567eSIngo Weinholdclass DependencyFamily {
159320567eSIngo Weinholdpublic:
169320567eSIngo Weinhold			void				AddDependency(Dependency* dependency);
179320567eSIngo Weinhold			void				RemoveDependency(Dependency* dependency);
189320567eSIngo Weinhold
195165b6b1SIngo Weinhold			void				AddDependenciesToList(
205165b6b1SIngo Weinhold									ResolvableDependencyList& list) const;
215165b6b1SIngo Weinhold
22d07c930cSIngo Weinhold			String				Name() const;
239320567eSIngo Weinhold
249320567eSIngo Weinhold			bool				IsLastDependency(Dependency* dependency) const;
259320567eSIngo Weinhold
269320567eSIngo Weinhold			DependencyFamily*&	HashLink()	{ return fHashLink; }
279320567eSIngo Weinhold
289320567eSIngo Weinholdprivate:
299320567eSIngo Weinhold			DependencyFamily*	fHashLink;
309320567eSIngo Weinhold			FamilyDependencyList fDependencies;
319320567eSIngo Weinhold};
329320567eSIngo Weinhold
339320567eSIngo Weinhold
349320567eSIngo Weinholdinline void
359320567eSIngo WeinholdDependencyFamily::AddDependency(Dependency* dependency)
369320567eSIngo Weinhold{
379320567eSIngo Weinhold	fDependencies.Add(dependency);
389320567eSIngo Weinhold	dependency->SetFamily(this);
399320567eSIngo Weinhold}
409320567eSIngo Weinhold
419320567eSIngo Weinhold
429320567eSIngo Weinholdinline void
439320567eSIngo WeinholdDependencyFamily::RemoveDependency(Dependency* dependency)
449320567eSIngo Weinhold{
459320567eSIngo Weinhold	dependency->SetFamily(NULL);
469320567eSIngo Weinhold	fDependencies.Remove(dependency);
479320567eSIngo Weinhold}
489320567eSIngo Weinhold
499320567eSIngo Weinhold
505165b6b1SIngo Weinholdinline void
515165b6b1SIngo WeinholdDependencyFamily::AddDependenciesToList(ResolvableDependencyList& list) const
525165b6b1SIngo Weinhold{
535165b6b1SIngo Weinhold	for (FamilyDependencyList::ConstIterator it = fDependencies.GetIterator();
545165b6b1SIngo Weinhold			Dependency* dependency = it.Next();) {
555165b6b1SIngo Weinhold		list.Add(dependency);
565165b6b1SIngo Weinhold	}
575165b6b1SIngo Weinhold}
585165b6b1SIngo Weinhold
595165b6b1SIngo Weinhold
60d07c930cSIngo Weinholdinline String
619320567eSIngo WeinholdDependencyFamily::Name() const
629320567eSIngo Weinhold{
639320567eSIngo Weinhold	Dependency* head = fDependencies.Head();
64d07c930cSIngo Weinhold	return head != NULL ? head->Name() : String();
659320567eSIngo Weinhold}
669320567eSIngo Weinhold
679320567eSIngo Weinhold
689320567eSIngo Weinholdinline bool
699320567eSIngo WeinholdDependencyFamily::IsLastDependency(Dependency* dependency) const
709320567eSIngo Weinhold{
719320567eSIngo Weinhold	return fDependencies.Head() == dependency
729320567eSIngo Weinhold		&& fDependencies.Tail() == dependency;
739320567eSIngo Weinhold}
749320567eSIngo Weinhold
759320567eSIngo Weinhold
769320567eSIngo Weinhold// #pragma mark - DependencyFamilyHashDefinition
779320567eSIngo Weinhold
789320567eSIngo Weinhold
799320567eSIngo Weinholdstruct DependencyFamilyHashDefinition {
80d07c930cSIngo Weinhold	typedef String				KeyType;
819320567eSIngo Weinhold	typedef	DependencyFamily	ValueType;
829320567eSIngo Weinhold
83d07c930cSIngo Weinhold	size_t HashKey(const String& key) const
849320567eSIngo Weinhold	{
85d07c930cSIngo Weinhold		return key.Hash();
869320567eSIngo Weinhold	}
879320567eSIngo Weinhold
889320567eSIngo Weinhold	size_t Hash(const DependencyFamily* value) const
899320567eSIngo Weinhold	{
90d07c930cSIngo Weinhold		return value->Name().Hash();
919320567eSIngo Weinhold	}
929320567eSIngo Weinhold
93d07c930cSIngo Weinhold	bool Compare(const String& key, const DependencyFamily* value) const
949320567eSIngo Weinhold	{
95d07c930cSIngo Weinhold		return key == value->Name();
969320567eSIngo Weinhold	}
979320567eSIngo Weinhold
989320567eSIngo Weinhold	DependencyFamily*& GetLink(DependencyFamily* value) const
999320567eSIngo Weinhold	{
1009320567eSIngo Weinhold		return value->HashLink();
1019320567eSIngo Weinhold	}
1029320567eSIngo Weinhold};
1039320567eSIngo Weinhold
1049320567eSIngo Weinhold
1059320567eSIngo Weinholdtypedef BOpenHashTable<DependencyFamilyHashDefinition>
1069320567eSIngo Weinhold	DependencyFamilyHashTable;
1079320567eSIngo Weinhold
1089320567eSIngo Weinhold
1099320567eSIngo Weinhold#endif	// DEPENDENCY_FAMILY_H
110