reSIProcate/DialogUsageManager  9680
EventDispatcher.hxx
Go to the documentation of this file.
00001 #ifndef RESIP_EventDispatcher_hxx
00002 #define RESIP_EventDispatcher_hxx
00003 
00004 #include <vector>
00005 
00006 #include "resip/dum/DumCommand.hxx"
00007 #include "resip/dum/Postable.hxx"
00008 #include "rutil/Mutex.hxx"
00009 #include "rutil/Lock.hxx"
00010 
00011 namespace resip
00012 {
00013 
00014 template<class E>
00015 class EventDispatcher
00016 {
00017    public:
00018       EventDispatcher() 
00019       {
00020       }
00021 
00022       bool dispatch(Message* msg)
00023       {
00024          Lock lock(mMutex);
00025          bool ret = false;
00026          
00027          E* event = dynamic_cast<E*>(msg);
00028          if (event)
00029          {
00030             if(mListeners.size() > 0)
00031             {
00032                ret = true;
00033                unsigned int counter = 1;
00034                for (std::vector<Postable*>::iterator it = mListeners.begin(); it != mListeners.end(); ++it)
00035                {
00036                   if (counter == mListeners.size())
00037                   {
00038                      (*it)->post(msg);
00039                   }
00040                   else
00041                   {
00042                      ++counter;
00043                      (*it)->post(msg->clone());
00044                   }
00045                }
00046             }         
00047          }
00048 
00049          return ret;
00050       }
00051 
00052       void addListener(Postable* listener)
00053       {
00054          Lock lock(mMutex);
00055          std::vector<Postable*>::iterator it = find(listener);
00056          if (it == mListeners.end())
00057          {
00058             mListeners.push_back(listener);
00059          }
00060       }
00061 
00062       void removeListener(Postable* listener)
00063       {
00064          Lock lock(mMutex);
00065          std::vector<Postable*>::iterator it = find(listener);         
00066          if (it != mListeners.end())
00067          {
00068             mListeners.erase(it);
00069          }
00070       }
00071 
00072    private:
00073       std::vector<Postable*> mListeners;
00074       Mutex mMutex;
00075 
00076       std::vector<Postable*>::iterator find(Postable* listener)
00077       {
00078          std::vector<Postable*>::iterator it;
00079          for (it = mListeners.begin(); it != mListeners.end(); ++it)
00080          {
00081             if (listener == *it)
00082             {
00083                break;
00084             }
00085          }
00086          return it;
00087       }
00088 };
00089 
00090 }
00091 
00092 
00093 #endif
00094 
00095 
00096 /* ====================================================================
00097  * The Vovida Software License, Version 1.0 
00098  * 
00099  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00100  * 
00101  * Redistribution and use in source and binary forms, with or without
00102  * modification, are permitted provided that the following conditions
00103  * are met:
00104  * 
00105  * 1. Redistributions of source code must retain the above copyright
00106  *    notice, this list of conditions and the following disclaimer.
00107  * 
00108  * 2. Redistributions in binary form must reproduce the above copyright
00109  *    notice, this list of conditions and the following disclaimer in
00110  *    the documentation and/or other materials provided with the
00111  *    distribution.
00112  * 
00113  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00114  *    and "Vovida Open Communication Application Library (VOCAL)" must
00115  *    not be used to endorse or promote products derived from this
00116  *    software without prior written permission. For written
00117  *    permission, please contact vocal@vovida.org.
00118  *
00119  * 4. Products derived from this software may not be called "VOCAL", nor
00120  *    may "VOCAL" appear in their name, without prior written
00121  *    permission of Vovida Networks, Inc.
00122  * 
00123  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00124  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00125  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00126  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00127  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00128  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00129  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00130  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00131  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00132  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00133  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00134  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00135  * DAMAGE.
00136  * 
00137  * ====================================================================
00138  * 
00139  * This software consists of voluntary contributions made by Vovida
00140  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00141  * Inc.  For more information on Vovida Networks, Inc., please see
00142  * <http://www.vovida.org/>.
00143  *
00144  */