xref: /haiku/src/kits/app/Invoker.cpp (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		Invoker.cpp
23 //	Author:			Marc Flerackers (mflerackers@androme.be)
24 //	Description:	BInvoker class defines a protocol for objects that
25 //					post messages to a "target".
26 //------------------------------------------------------------------------------
27 
28 // Standard Includes -----------------------------------------------------------
29 
30 // System Includes -------------------------------------------------------------
31 #include <Invoker.h>
32 #include <Errors.h>
33 
34 // Project Includes ------------------------------------------------------------
35 
36 // Local Includes --------------------------------------------------------------
37 
38 // Local Defines ---------------------------------------------------------------
39 
40 // Globals ---------------------------------------------------------------------
41 
42 //------------------------------------------------------------------------------
43 BInvoker::BInvoker(BMessage *message, BMessenger messenger)
44 	:	fMessage(message),
45 		fMessenger(messenger),
46 		fReplyTo(NULL),
47 		fTimeout(B_INFINITE_TIMEOUT),
48 		fNotifyKind(0)
49 {
50 }
51 //------------------------------------------------------------------------------
52 BInvoker::BInvoker(BMessage *message, const BHandler *handler,
53 				   const BLooper *looper)
54 	:	fMessage(message),
55 		fMessenger(BMessenger(handler, looper)),
56 		fReplyTo(NULL),
57 		fTimeout(B_INFINITE_TIMEOUT),
58 		fNotifyKind(0)
59 {
60 }
61 //------------------------------------------------------------------------------
62 BInvoker::BInvoker()
63 	:	fMessage(NULL),
64 		fReplyTo(NULL),
65 		fTimeout(B_INFINITE_TIMEOUT),
66 		fNotifyKind(0)
67 {
68 }
69 //------------------------------------------------------------------------------
70 BInvoker::~BInvoker()
71 {
72 	delete fMessage;
73 }
74 //------------------------------------------------------------------------------
75 status_t BInvoker::SetMessage(BMessage *message)
76 {
77 	if (fMessage == message)
78 		return B_OK;
79 
80 	if (fMessage)
81 		delete fMessage;
82 
83 	fMessage = message;
84 
85 	return B_OK;
86 }
87 //------------------------------------------------------------------------------
88 BMessage *BInvoker::Message() const
89 {
90 	return fMessage;
91 }
92 //------------------------------------------------------------------------------
93 uint32 BInvoker::Command() const
94 {
95 	if (fMessage)
96 		return fMessage->what;
97 	else
98 		return 0;
99 }
100 //------------------------------------------------------------------------------
101 status_t BInvoker::SetTarget(BMessenger messenger)
102 {
103 	fMessenger = messenger;
104 
105 	return B_OK;
106 }
107 //------------------------------------------------------------------------------
108 status_t BInvoker::SetTarget(const BHandler *handler, const BLooper *looper)
109 {
110 	fMessenger = BMessenger(handler, looper);
111 
112 	return B_OK;
113 }
114 //------------------------------------------------------------------------------
115 bool BInvoker::IsTargetLocal() const
116 {
117 	return fMessenger.IsTargetLocal();
118 }
119 //------------------------------------------------------------------------------
120 BHandler *BInvoker::Target(BLooper **looper) const
121 {
122 	return fMessenger.Target(looper);
123 }
124 //------------------------------------------------------------------------------
125 BMessenger BInvoker::Messenger() const
126 {
127 	return fMessenger;
128 }
129 //------------------------------------------------------------------------------
130 status_t BInvoker::SetHandlerForReply(BHandler *replyHandler)
131 {
132 	fReplyTo = replyHandler;
133 
134 	return B_OK;
135 }
136 //------------------------------------------------------------------------------
137 BHandler *BInvoker::HandlerForReply() const
138 {
139 	return fReplyTo;
140 }
141 //------------------------------------------------------------------------------
142 status_t BInvoker::Invoke(BMessage *message)
143 {
144 	if (!message)
145 		message = Message();
146 
147 	if (!message)
148 		return B_BAD_VALUE;
149 
150 	return fMessenger.SendMessage(message, fReplyTo, fTimeout);
151 }
152 //------------------------------------------------------------------------------
153 status_t BInvoker::InvokeNotify(BMessage *message, uint32 kind)
154 {
155 	if (fNotifyKind != 0)
156 		return B_WOULD_BLOCK;
157 
158 	BeginInvokeNotify(kind);
159 
160 	status_t err = Invoke(message);
161 
162 	EndInvokeNotify();
163 
164 	return err;
165 }
166 //------------------------------------------------------------------------------
167 status_t BInvoker::SetTimeout(bigtime_t timeout)
168 {
169 	fTimeout = timeout;
170 
171 	return B_OK;
172 }
173 //------------------------------------------------------------------------------
174 bigtime_t BInvoker::Timeout() const
175 {
176 	return fTimeout;
177 }
178 //------------------------------------------------------------------------------
179 uint32 BInvoker::InvokeKind(bool *notify)
180 {
181 	if (notify)
182 		*notify = (fNotifyKind != 0) ? true : false;
183 
184 	if (fNotifyKind != 0)
185 		return fNotifyKind;
186 	else
187 		return B_CONTROL_INVOKED;
188 }
189 //------------------------------------------------------------------------------
190 void BInvoker::BeginInvokeNotify(uint32 kind)
191 {
192 	fNotifyKind = kind;
193 }
194 //------------------------------------------------------------------------------
195 void BInvoker::EndInvokeNotify()
196 {
197 	fNotifyKind = 0;
198 }
199 //------------------------------------------------------------------------------
200 void BInvoker::_ReservedInvoker1() {}
201 void BInvoker::_ReservedInvoker2() {}
202 void BInvoker::_ReservedInvoker3() {}
203 //------------------------------------------------------------------------------
204 BInvoker::BInvoker(const BInvoker &)
205 {
206 }
207 //------------------------------------------------------------------------------
208 BInvoker &BInvoker::operator=(const BInvoker &)
209 {
210 	return *this;
211 }
212 //------------------------------------------------------------------------------
213