OW_SharedLibraryReference.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00035 #ifndef OW_SHARED_LIBRARY_REFERENCE_HPP_
00036 #define OW_SHARED_LIBRARY_REFERENCE_HPP_
00037 #include "OW_config.h"
00038 #include "OW_SharedLibrary.hpp"
00039 
00040 namespace OW_NAMESPACE
00041 {
00042 
00043 template <class T>
00044 class SharedLibraryReference
00045 {
00046 public:
00047    typedef T element_type;
00048 
00049    SharedLibraryReference(const SharedLibraryRef& lib, const T& obj)
00050    : m_sharedLib(lib), m_obj(obj)
00051    {}
00052    SharedLibraryReference(const SharedLibraryRef& lib, typename T::element_type* obj)
00053    : m_sharedLib(lib), m_obj(T(obj))
00054    {}
00055    SharedLibraryReference(const SharedLibraryReference<T>& arg)
00056    : m_sharedLib(arg.m_sharedLib), m_obj(arg.m_obj)
00057    {
00058    }
00059    /* construct out of a reference to a derived type.  U should be
00060    derived from T */
00061    template <class U>
00062    SharedLibraryReference(const SharedLibraryReference<U>& arg)
00063    : m_sharedLib(arg.m_sharedLib), m_obj(arg.m_obj)
00064    {
00065    }
00066    SharedLibraryReference()
00067    : m_sharedLib(), m_obj()
00068    {}
00069    SharedLibraryReference<T>& operator=(const SharedLibraryReference<T>& arg)
00070    {
00071       m_obj = arg.m_obj;
00072       m_sharedLib = arg.m_sharedLib;
00073       return *this;
00074    }
00075    ~SharedLibraryReference()
00076    {
00077       try
00078       {
00079          m_obj = 0;
00080          m_sharedLib = 0;
00081       }
00082       catch (...)
00083       {
00084          // don't let exceptions escape
00085       }
00086    }
00087    SharedLibraryRef getLibRef() const
00088    {
00089       return m_sharedLib;
00090    }
00091    typename T::element_type* operator->() const
00092    {
00093       return &*m_obj;
00094    }
00095    T get() const
00096    {
00097       return m_obj;
00098    }
00099    
00100    typedef T SharedLibraryReference::*safe_bool;
00101    operator safe_bool () const
00102       {  return (m_obj) ? &SharedLibraryReference::m_obj : 0; }
00103    bool operator!() const
00104       {  return !m_obj; }
00105    void setNull()
00106    {
00107       m_obj = 0;
00108       m_sharedLib = 0;
00109    }
00110    
00111    template <class U>
00112    SharedLibraryReference<U> cast_to() const
00113    {
00114       SharedLibraryReference<U> rval;
00115       rval.m_sharedLib = m_sharedLib;
00116       rval.m_obj = m_obj.cast_to<U>();
00117       return rval;
00118    }
00119 
00120    OW_DEPRECATED bool isNull() const // in 3.1.0
00121    {
00122       return !m_obj;
00123    }
00124 
00125 #if !defined(__GNUC__) || __GNUC__ > 2 // causes gcc 2.95 to ICE
00126    /* This is so cast_to will work */
00127    template <class U> friend class SharedLibraryReference;
00128 
00129 private:
00130 #endif
00131 
00132    SharedLibraryRef m_sharedLib;
00133    T m_obj;
00134 };
00135 
00136 } // end namespace OW_NAMESPACE
00137 
00138 #endif

Generated on Thu Feb 9 08:48:11 2006 for openwbem by  doxygen 1.4.6