1/*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stephan A��mus <superstippi@gmx.de>
7 */
8
9#ifndef OBSERVABLE_H
10#define OBSERVABLE_H
11
12#include <List.h>
13
14class Observer;
15
16class Observable {
17 public:
18								Observable();
19	virtual						~Observable();
20
21			bool				AddObserver(Observer* observer);
22			bool				RemoveObserver(Observer* observer);
23
24			int32				CountObservers() const;
25			Observer*			ObserverAtFast(int32 index) const;
26
27	 		void				Notify() const;
28
29			void				SuspendNotifications(bool suspend);
30
31			bool				HasPendingNotifications() const
32									{ return fPendingNotifications; }
33
34 private:
35			BList				fObservers;
36
37			int32				fSuspended;
38	mutable	bool				fPendingNotifications;
39};
40
41class AutoNotificationSuspender {
42 public:
43								AutoNotificationSuspender(Observable* object)
44									: fObject(object)
45								{
46									fObject->SuspendNotifications(true);
47								}
48
49	virtual						~AutoNotificationSuspender()
50								{
51									fObject->SuspendNotifications(false);
52								}
53 private:
54			Observable*			fObject;
55};
56
57#endif // OBSERVABLE_H
58