1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * Additional copyright for this file:
8  * Copyright (C) 1995-1997 Presto Studios, Inc.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  */
25 
26 #ifndef PEGASUS_NOTIFICATION_H
27 #define PEGASUS_NOTIFICATION_H
28 
29 #include "common/array.h"
30 #include "common/list.h"
31 
32 #include "pegasus/types.h"
33 #include "pegasus/util.h"
34 
35 namespace Pegasus {
36 
37 class NotificationManager;
38 class NotificationReceiver;
39 
40 struct ReceiverEntry {
41 	NotificationReceiver *receiver;
42 	NotificationFlags mask;
43 };
44 
45 int operator==(const ReceiverEntry &entry1, const ReceiverEntry &entry2);
46 int operator!=(const ReceiverEntry &entry1, const ReceiverEntry &entry2);
47 
48 typedef Common::Array<ReceiverEntry> ReceiverList;
49 
50 /*
51 	A notification can have 32 flags associated with it, which can be user-defined.
52 */
53 
54 class Notification : public IDObject {
55 friend class NotificationManager;
56 
57 public:
58 	Notification(const NotificationID id, NotificationManager *owner);
59 	virtual ~Notification();
60 
61 	// notifyMe will have this receiver notified when any of the specified notification
62 	// flags are set.
63 	// If there is already a notification set for this receiver, notifyMe does a bitwise
64 	// OR with the receiver's current notification flags.
65 
66 	// Can selectively set or clear notification bits by using the flags and mask argument.
67 
68 	void notifyMe(NotificationReceiver*, NotificationFlags flags, NotificationFlags mask);
69 	void cancelNotification(NotificationReceiver *receiver);
70 
71 	void setNotificationFlags(NotificationFlags flags,	NotificationFlags mask);
getNotificationFlags()72 	NotificationFlags getNotificationFlags() { return _currentFlags; }
73 
clearNotificationFlags()74 	void clearNotificationFlags() { setNotificationFlags(0, ~(NotificationFlags)0); }
75 
76 protected:
77 	void checkReceivers();
78 
79 	NotificationManager *_owner;
80 	ReceiverList _receivers;
81 	NotificationFlags _currentFlags;
82 };
83 
84 class NotificationReceiver {
85 friend class Notification;
86 
87 public:
88 	NotificationReceiver();
89 	virtual ~NotificationReceiver();
90 
91 protected:
92 	// receiveNotification is called automatically whenever a notification that this
93 	// receiver depends on has its flags set
94 
95 	virtual void receiveNotification(Notification *, const NotificationFlags);
96 	virtual void newNotification(Notification *notification);
97 
98 private:
99 	Notification *_notification;
100 };
101 
102 typedef Common::List<Notification *> NotificationList;
103 
104 class NotificationManager : public NotificationReceiver {
105 friend class Notification;
106 
107 public:
108 	NotificationManager();
109 	~NotificationManager() override;
110 
111 	void checkNotifications();
112 
113 protected:
114 	void addNotification(Notification *notification);
115 	void removeNotification(Notification *notification);
116 	void detachNotifications();
117 
118 	NotificationList _notifications;
119 };
120 
121 } // End of namespace Pegasus
122 
123 #endif
124