1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2010, Rene Gollent, rene@gollent.com.
4 * Distributed under the terms of the MIT License.
5 */
6#ifndef FUNCTION_H
7#define FUNCTION_H
8
9#include <util/DoublyLinkedList.h>
10#include <util/OpenHashTable.h>
11
12#include "FunctionInstance.h"
13#include "LocatableFile.h"
14
15
16class FileSourceCode;
17
18
19class Function : public BReferenceable, private LocatableFile::Listener {
20public:
21	class Listener;
22
23public:
24								Function();
25								~Function();
26
27			// team must be locked to access the instances
28			FunctionInstance*	FirstInstance() const
29									{ return fInstances.Head(); }
30			FunctionInstance*	LastInstance() const
31									{ return fInstances.Tail(); }
32			const FunctionInstanceList& Instances() const
33									{ return fInstances; }
34
35			const BString&		Name() const
36									{ return FirstInstance()->Name(); }
37			const BString&		PrettyName() const
38									{ return FirstInstance()->PrettyName(); }
39			LocatableFile*		SourceFile() const
40									{ return FirstInstance()->SourceFile(); }
41			SourceLocation		GetSourceLocation() const
42									{ return FirstInstance()
43										->GetSourceLocation(); }
44
45			FunctionID*			GetFunctionID() const
46									{ return FirstInstance()->GetFunctionID(); }
47									// returns a reference
48
49			// mutable attributes follow (locking required)
50			FileSourceCode*		GetSourceCode() const	{ return fSourceCode; }
51			function_source_state SourceCodeState() const
52									{ return fSourceCodeState; }
53			void				SetSourceCode(FileSourceCode* source,
54									function_source_state state);
55
56			void				AddListener(Listener* listener);
57			void				RemoveListener(Listener* listener);
58
59			// package private
60			void				AddInstance(FunctionInstance* instance);
61			void				RemoveInstance(FunctionInstance* instance);
62
63			void				NotifySourceCodeChanged();
64
65			void				LocatableFileChanged(LocatableFile* file);
66
67private:
68			typedef DoublyLinkedList<Listener> ListenerList;
69
70private:
71			FunctionInstanceList fInstances;
72			FileSourceCode*		fSourceCode;
73			function_source_state fSourceCodeState;
74			ListenerList		fListeners;
75			int32				fNotificationsDisabled;
76
77public:
78			// BOpenHashTable support
79			Function*			fNext;
80};
81
82
83class Function::Listener : public DoublyLinkedListLinkImpl<Listener> {
84public:
85	virtual						~Listener();
86
87	virtual	void				FunctionSourceCodeChanged(Function* function);
88									// called with lock held
89};
90
91
92#endif	// FUNCTION_H
93