reSIProcate/DialogUsageManager  9680
HandleManager.cxx
Go to the documentation of this file.
00001 #include <cassert>
00002 #include "rutil/Logger.hxx"
00003 #include "rutil/Inserter.hxx"
00004 #include "resip/dum/HandleManager.hxx"
00005 #include "resip/dum/HandleException.hxx"
00006 
00007 using namespace resip;
00008 #define RESIPROCATE_SUBSYSTEM Subsystem::DUM
00009 
00010 HandleManager::HandleManager() : 
00011    mShuttingDown(false),
00012    mLastId(Handled::npos)
00013 {
00014 }
00015 
00016 HandleManager::~HandleManager()
00017 {
00018    // !jf! do nothing?
00019    // !dcm! -- this is the best we can do w/out a back-ptr to each handle
00020    // DUM currently cleans up properly, so not an issue unless users make their
00021    // own handled objects, could clean up memeory, but the app will crash first
00022    // handle deference regardless.
00023    if (!mHandleMap.empty())
00024    {
00025       DebugLog ( << "&&&&&& HandleManager::~HandleManager: Deleting handlemanager that still has Handled objects: " );
00026       DebugLog ( << InserterP(mHandleMap));   
00027       //throw HandleException("Deleting handlemanager that still has Handled objects", __FILE__, __LINE__);
00028    }
00029 }
00030 
00031 Handled::Id
00032 HandleManager::create(Handled* handled)
00033 {
00034    mHandleMap[++mLastId] = handled;
00035    return mLastId;
00036 }
00037 
00038 void HandleManager::shutdownWhenEmpty()
00039 {
00040    mShuttingDown = true;
00041    if (mHandleMap.empty())
00042    {
00043       onAllHandlesDestroyed();      
00044    }
00045    else
00046    {
00047       DebugLog (<< "Shutdown waiting for all usages to be deleted (" << mHandleMap.size() << ")");
00048 #if 1
00049       for (HandleMap::iterator i=mHandleMap.begin() ; i != mHandleMap.end(); ++i)
00050       {
00051          DebugLog (<< i->first << " -> " << *(i->second));
00052       }
00053 #endif
00054    }
00055 }
00056 
00057 #if 0
00058 // !jf! this will leak if there are active usages
00059 void HandleManager::onAllHandlesDestroyed()
00060 {
00061    WarningLog(<< "Forcing shutdown " << mHandleMap.size() << " active usages");
00062    for (HandleMap::const_iterator i = mHandleMap.begin();
00063         i != mHandleMap.end(); ++i)
00064    {
00065       InfoLog(<< "Handled left at force shutdown: " << *i->second);
00066    }
00067    mHandleMap.clear();
00068 }
00069 #endif
00070 
00071 void
00072 HandleManager::remove(Handled::Id id)
00073 {
00074    HandleMap::iterator i = mHandleMap.find(id);
00075    assert (i != mHandleMap.end());
00076    mHandleMap.erase(i);
00077    if (mShuttingDown)
00078    {
00079       if(mHandleMap.empty())
00080       {
00081          onAllHandlesDestroyed();      
00082       }
00083       else
00084       {
00085          DebugLog (<< "Waiting for usages to be deleted (" << mHandleMap.size() << ")");      
00086       }
00087    }
00088 }
00089 
00090 void
00091 HandleManager::dumpHandles() const
00092 {
00093    DebugLog (<< "Waiting for usages to be deleted (" << mHandleMap.size() << ")");
00094    for (HandleMap::const_iterator i=mHandleMap.begin() ; i != mHandleMap.end(); ++i)
00095    {
00096       DebugLog (<< i->first << " -> " << *(i->second));
00097    }
00098 }
00099 
00100 bool
00101 HandleManager::isValidHandle(Handled::Id id) const
00102 {
00104    return mHandleMap.count(id) != 0;
00105 }
00106 
00107 Handled*
00108 HandleManager::getHandled(Handled::Id id) const
00109 {
00110    HandleMap::const_iterator i = mHandleMap.find(id);
00111    if (i == mHandleMap.end())
00112    {
00113       InfoLog (<< "Reference to stale handle: " << id);
00114       assert(0);
00115       throw HandleException("Stale handle", __FILE__, __LINE__);
00116    }
00117    else
00118    {
00119       assert(i->second);
00120       return i->second;
00121    }
00122 }
00123 
00124 
00125 /* ====================================================================
00126 
00127  * The Vovida Software License, Version 1.0 
00128 
00129  * 
00130 
00131  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00132 
00133  * 
00134 
00135  * Redistribution and use in source and binary forms, with or without
00136 
00137  * modification, are permitted provided that the following conditions
00138 
00139  * are met:
00140 
00141  * 
00142 
00143  * 1. Redistributions of source code must retain the above copyright
00144 
00145  *    notice, this list of conditions and the following disclaimer.
00146 
00147  * 
00148 
00149  * 2. Redistributions in binary form must reproduce the above copyright
00150 
00151  *    notice, this list of conditions and the following disclaimer in
00152 
00153  *    the documentation and/or other materials provided with the
00154 
00155  *    distribution.
00156 
00157  * 
00158 
00159  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00160 
00161  *    and "Vovida Open Communication Application Library (VOCAL)" must
00162 
00163  *    not be used to endorse or promote products derived from this
00164 
00165  *    software without prior written permission. For written
00166 
00167  *    permission, please contact vocal@vovida.org.
00168 
00169  *
00170 
00171  * 4. Products derived from this software may not be called "VOCAL", nor
00172 
00173  *    may "VOCAL" appear in their name, without prior written
00174 
00175  *    permission of Vovida Networks, Inc.
00176 
00177  * 
00178 
00179  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00180 
00181  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00182 
00183  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00184 
00185  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00186 
00187  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00188 
00189  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00190 
00191  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00192 
00193  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00194 
00195  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00196 
00197  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00198 
00199  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00200 
00201  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00202 
00203  * DAMAGE.
00204 
00205  * 
00206 
00207  * ====================================================================
00208 
00209  * 
00210 
00211  * This software consists of voluntary contributions made by Vovida
00212 
00213  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00214 
00215  * Inc.  For more information on Vovida Networks, Inc., please see
00216 
00217  * <http://www.vovida.org/>.
00218 
00219  *
00220 
00221  */
00222