reSIProcate/DialogUsageManager  9680
Handle.hxx
Go to the documentation of this file.
00001 #if !defined(RESIP_HANDLE_HXX)
00002 #define RESIP_HANDLE_HXX
00003 
00004 #include <iosfwd>
00005 #include <cassert>
00006 #include "resip/dum/Handled.hxx"
00007 #include "resip/dum/HandleManager.hxx"
00008 #include "resip/dum/HandleException.hxx"
00009 
00010 namespace resip
00011 {
00012 
00013 template <class T>
00014 class Handle
00015 {
00016    public:
00017       Handle(HandleManager& ham, Handled::Id id) : mHam(&ham), mId(id)
00018       {
00019       }
00020 
00021       Handle() : mHam(0), mId(0)
00022       {
00023       }
00024 
00025       bool isValid() const
00026       {
00027          if (!mHam)
00028          {
00029             return false;
00030          }
00031          else
00032          {
00033             return mHam->isValidHandle(mId);
00034          }
00035       }
00036       
00037       // throws if not found
00038       T* get()
00039       {
00040          if (!mHam)
00041          {
00042             //assert(0);
00043             throw HandleException("Reference to unitialized handle.", __FILE__, __LINE__);
00044          }
00045          return static_cast<T*>(mHam->getHandled(mId));
00046       }
00047 
00048       const T* get() const
00049       {
00050          if (!mHam)
00051          {
00052             //assert(0);
00053             throw HandleException("Reference to unitialized handle.", __FILE__, __LINE__);
00054          }
00055          return static_cast<T*>(mHam->getHandled(mId));
00056       }
00057       
00058       T* operator->()
00059       {
00060          return get();
00061       }
00062 
00063       const T* operator->() const
00064       {
00065          return get();
00066       }
00067 
00068       T& operator*()
00069       {
00070          return *get();
00071       }
00072 
00073       const T& operator*() const
00074       {
00075          return *get();
00076       }
00077       
00078 
00079       Handled::Id getId() const
00080       {
00081          return mId;
00082       }
00083 
00084       static Handle<T> NotValid()
00085       {
00086          static Handle<T> notValid;
00087          return notValid;
00088       }
00089       
00090       bool operator==(const Handle<T>& other)
00091       {
00092          return (mHam == other.mHam) && (mId == other.mId);
00093       }
00094       
00095       // !nash! to be able to use Handle in Set or Map container
00096       bool operator<(const Handle<T>& other) const
00097       {
00098          assert(mHam);
00099          assert(other.mHam);
00100          return mId < other.mId;
00101       }
00102 
00103    private:
00104       HandleManager* mHam;
00105 
00106    protected:
00107       Handled::Id mId;
00108 
00109       friend class Handled;
00110 };
00111 
00112 }
00113 
00114 #endif
00115 
00116 /* ====================================================================
00117  * The Vovida Software License, Version 1.0 
00118  * 
00119  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00120  * 
00121  * Redistribution and use in source and binary forms, with or without
00122  * modification, are permitted provided that the following conditions
00123  * are met:
00124  * 
00125  * 1. Redistributions of source code must retain the above copyright
00126  *    notice, this list of conditions and the following disclaimer.
00127  * 
00128  * 2. Redistributions in binary form must reproduce the above copyright
00129  *    notice, this list of conditions and the following disclaimer in
00130  *    the documentation and/or other materials provided with the
00131  *    distribution.
00132  * 
00133  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00134  *    and "Vovida Open Communication Application Library (VOCAL)" must
00135  *    not be used to endorse or promote products derived from this
00136  *    software without prior written permission. For written
00137  *    permission, please contact vocal@vovida.org.
00138  *
00139  * 4. Products derived from this software may not be called "VOCAL", nor
00140  *    may "VOCAL" appear in their name, without prior written
00141  *    permission of Vovida Networks, Inc.
00142  * 
00143  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00144  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00145  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00146  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00147  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00148  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00149  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00150  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00151  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00152  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00153  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00154  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00155  * DAMAGE.
00156  * 
00157  * ====================================================================
00158  * 
00159  * This software consists of voluntary contributions made by Vovida
00160  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00161  * Inc.  For more information on Vovida Networks, Inc., please see
00162  * <http://www.vovida.org/>.
00163  *
00164  */