kodi
NptMessaging.h
1 /*****************************************************************
2 |
3 | Neptune - Messaging System
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
18 |
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
30  ****************************************************************/
31 
32 #ifndef _NPT_MESSAGING_H_
33 #define _NPT_MESSAGING_H_
34 
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NptConstants.h"
39 #include "NptTypes.h"
40 #include "NptResults.h"
41 #include "NptList.h"
42 #include "NptThreads.h"
43 #include "NptDynamicCast.h"
44 
45 /*----------------------------------------------------------------------
46 | forward references
47 +---------------------------------------------------------------------*/
48 class NPT_Message;
49 
50 /*----------------------------------------------------------------------
51 | NPT_MessageHandler
52 +---------------------------------------------------------------------*/
54 {
55 public:
56  NPT_IMPLEMENT_DYNAMIC_CAST(NPT_MessageHandler)
57 
58  // methods
59  virtual ~NPT_MessageHandler() {}
60 
61  // default message handler
62  virtual void OnMessage(NPT_Message*) {}
63 
64  // this method is a central point of handling for received messages.
65  // it can be overloaded by subclasses that wish to process all
66  // incoming messages
67  virtual NPT_Result HandleMessage(NPT_Message* message);
68 };
69 
70 /*----------------------------------------------------------------------
71 | NPT_MessageHandlerProxy
72 +---------------------------------------------------------------------*/
74 {
75 public:
76  NPT_IMPLEMENT_DYNAMIC_CAST_D(NPT_MessageHandlerProxy, NPT_MessageHandler)
77 
78 
90 
91  // destructor
92  ~NPT_MessageHandlerProxy() override;
93 
94  // NPT_MessageHandler methods
95  void OnMessage(NPT_Message*) override;
96  NPT_Result HandleMessage(NPT_Message* message) override;
97 
104  void DetachHandler();
105 
109  void AddReference();
110 
114  void Release();
115 
116 private:
117  // members
118  NPT_MessageHandler* m_Handler;
119  NPT_Cardinal m_ReferenceCount;
120  NPT_Mutex m_Lock;
121 };
122 
123 /*----------------------------------------------------------------------
124 | NPT_Messsage
125 +---------------------------------------------------------------------*/
127 {
128 public:
129  // types
130  typedef const char* Type;
131 
132  // static members
133  static Type const MessageType;
134 
135  // methods
136  virtual ~NPT_Message() {}
137  virtual Type GetType() { return MessageType; }
138  virtual NPT_Result Dispatch(NPT_MessageHandler* handler) {
139  return DefaultDeliver(handler);
140  }
141  // this method should really be called 'Deliver', but this would
142  // cause a problem when subclasses overload it
143  virtual NPT_Result DefaultDeliver(NPT_MessageHandler* handler) {
144  handler->OnMessage(this);
145  return NPT_SUCCESS;
146  }
147 };
148 
149 /*----------------------------------------------------------------------
150 | NPT_TerminateMesssage
151 +---------------------------------------------------------------------*/
153 {
154 public:
155  // methods
156  NPT_Result Dispatch(NPT_MessageHandler* /*handler*/) override {
157  return NPT_ERROR_TERMINATED;
158  }
159 };
160 
161 /*----------------------------------------------------------------------
162 | NPT_MessageQueue
163 +---------------------------------------------------------------------*/
165 {
166 public:
167  // methods
168  virtual ~NPT_MessageQueue() {}
169  virtual NPT_Result PumpMessage(NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) = 0;
170  virtual NPT_Result QueueMessage(NPT_Message* message,
171  NPT_MessageHandler* handler) = 0;
172 };
173 
174 /*----------------------------------------------------------------------
175 | NPT_MessageReceiver
176 +---------------------------------------------------------------------*/
178 {
179 public:
180  // methods
181  NPT_MessageReceiver() : m_Queue(NULL), m_Handler(NULL) {}
183  m_Queue(NULL), m_Handler(handler) {}
185  m_Queue(queue), m_Handler(NULL) {}
187  NPT_MessageQueue* queue) :
188  m_Queue(queue), m_Handler(handler) {}
189  virtual ~NPT_MessageReceiver() {}
190  NPT_Result SetQueue(NPT_MessageQueue* queue) {
191  m_Queue = queue;
192  return NPT_SUCCESS;
193  }
194  NPT_Result SetHandler(NPT_MessageHandler* handler) {
195  m_Handler = handler;
196  return NPT_SUCCESS;
197  }
198  virtual NPT_Result PostMessage(NPT_Message* message) {
199  if (m_Queue) {
200  return m_Queue->QueueMessage(message, m_Handler);
201  } else {
202  return NPT_FAILURE;
203  }
204  }
205 
206 protected:
207  // members
208  NPT_MessageQueue* m_Queue;
209  NPT_MessageHandler* m_Handler;
210 };
211 
212 /*----------------------------------------------------------------------
213 | NPT_MessageBroadcaster
214 +---------------------------------------------------------------------*/
216 {
217 public:
218  // methods
219  NPT_MessageBroadcaster(NPT_Message* message) : m_Message(message) {}
220  NPT_Result operator()(NPT_MessageReceiver*& receiver) const {
221  receiver->PostMessage(m_Message);
222  return NPT_SUCCESS;
223  }
224 
225 private:
226  // members
227  NPT_Message* m_Message;
228 };
229 
230 #endif // _NPT_MESSAGING_H_
Definition: NptMessaging.h:215
Definition: NptThreads.h:76
Definition: NptMessaging.h:126
Definition: NptMessaging.h:73
Definition: NptMessaging.h:164
Definition: NptMessaging.h:152
Definition: NptMessaging.h:177
Definition: NptMessaging.h:53