reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
EventDispatcher.hxx
Go to the documentation of this file.
1#ifndef RESIP_EventDispatcher_hxx
2#define RESIP_EventDispatcher_hxx
3
4#include <vector>
5
8#include "rutil/Mutex.hxx"
9#include "rutil/Lock.hxx"
10
11namespace resip
12{
13
14template<class E>
16{
17 public:
19 {
20 }
21
22 bool dispatch(Message* msg)
23 {
24 E* event = dynamic_cast<E*>(msg);
25 if (!event) return false;
26
27 Lock lock(mMutex);
28 const std::size_t size = mListeners.size();
29 if (size == 0) return false;
30
31 for (std::size_t i = 0; i < size; ++i)
32 {
33 if ((i + 1) == size)
34 {
35 mListeners[i]->post(msg);
36 }
37 else
38 {
39 mListeners[i]->post(msg->clone());
40 }
41 }
42 return true;
43 }
44
45 void addListener(Postable* listener)
46 {
47 Lock lock(mMutex);
48 std::vector<Postable*>::iterator it = find(listener);
49 if (it == mListeners.end())
50 {
51 mListeners.push_back(listener);
52 }
53 }
54
55 void removeListener(Postable* listener)
56 {
57 Lock lock(mMutex);
58 std::vector<Postable*>::iterator it = find(listener);
59 if (it != mListeners.end())
60 {
61 mListeners.erase(it);
62 }
63 }
64
65 private:
66 std::vector<Postable*> mListeners;
67 Mutex mMutex;
68
69 std::vector<Postable*>::iterator find(Postable* listener)
70 {
71 std::vector<Postable*>::iterator it;
72 for (it = mListeners.begin(); it != mListeners.end(); ++it)
73 {
74 if (listener == *it)
75 {
76 break;
77 }
78 }
79 return it;
80 }
81};
82
83}
84
85
86#endif
87
88
89/* ====================================================================
90 * The Vovida Software License, Version 1.0
91 *
92 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
93 *
94 * Redistribution and use in source and binary forms, with or without
95 * modification, are permitted provided that the following conditions
96 * are met:
97 *
98 * 1. Redistributions of source code must retain the above copyright
99 * notice, this list of conditions and the following disclaimer.
100 *
101 * 2. Redistributions in binary form must reproduce the above copyright
102 * notice, this list of conditions and the following disclaimer in
103 * the documentation and/or other materials provided with the
104 * distribution.
105 *
106 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
107 * and "Vovida Open Communication Application Library (VOCAL)" must
108 * not be used to endorse or promote products derived from this
109 * software without prior written permission. For written
110 * permission, please contact vocal@vovida.org.
111 *
112 * 4. Products derived from this software may not be called "VOCAL", nor
113 * may "VOCAL" appear in their name, without prior written
114 * permission of Vovida Networks, Inc.
115 *
116 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
117 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
118 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
119 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
120 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
121 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
122 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
123 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
124 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
125 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
126 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
127 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
128 * DAMAGE.
129 *
130 * ====================================================================
131 *
132 * This software consists of voluntary contributions made by Vovida
133 * Networks, Inc. and many individuals on behalf of Vovida Networks,
134 * Inc. For more information on Vovida Networks, Inc., please see
135 * <http://www.vovida.org/>.
136 *
137 */
void removeListener(Postable *listener)
void addListener(Postable *listener)
bool dispatch(Message *msg)
The base-class used for message-passing.
Definition Message.hxx:21
virtual Message * clone() const =0
dcm! – add UnusedChecking(_enum) below;
std::mutex Mutex
A semaphore that can be locked by only one thread at a time.
Definition Mutex.hxx:37
std::unique_lock< Mutex > Lock
A convenience class to lock a Lockable (such as a Mutex) on object creation, and unlock on destructio...
Definition Lock.hxx:28