Oracle® Fusion Middleware C++ API Reference for Oracle Coherence
12c (12.2.1.4.0)

E90870-01

coherence/lang/WeakView.hpp

00001 /*
00002 * WeakView.hpp
00003 *
00004 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_WEAK_VIEW_HPP
00017 #define COH_WEAK_VIEW_HPP
00018 
00019 #include "coherence/lang/compatibility.hpp"
00020 
00021 #include "coherence/lang/Object.hpp"
00022 #include "coherence/lang/TypedHandle.hpp"
00023 #include "coherence/lang/MemberView.hpp"
00024 #include "coherence/lang/WeakReference.hpp"
00025 
00026 #include <ostream>
00027 
00028 COH_OPEN_NAMESPACE2(coherence,lang)
00029 
00030 
00031 /**
00032 * WeakViews are a TypedHandle like wrapper around WeakReferences.
00033 * WeakReferences allow one Object to safely reference another without blocking
00034 * it from being destroyed. WeakReferences are necessary when building Object
00035 * graphs to avoid leaking the Object graph due to cyclical references.
00036 *
00037 * WeakReferences are automatically NULL'd out when their referenced Object
00038 * is destroyed. Additionally if the weakly referenced Object becomes only
00039 * referenced via Views, then the WeakReference and corresponding WeakHandle
00040 * will only return View to the Object, if an attempt is made to obtain a
00041 * Handle a NULL will be returned instead.
00042 *
00043 * WeakViews are wrappers for MemberView, and provide a thread-safe handle
00044 * implementation.  As such they require references to both the referenced
00045 * "child" Object, as well as the owning "parent" Object. As with MemberView
00046 * the parent object must inherit from Object.
00047 *
00048 * Note: In the rare case that a WeakView is declared via the mutable
00049 *       keyword, the WeakView must be informed of this fact by setting
00050 *       fMutable to true during construction.
00051 *
00052 * @see MemberView
00053 *
00054 * @author mf  2007.07.05
00055 */
00056 template<class T>
00057 class WeakView
00058     {
00059     // ----- typedefs -------------------------------------------------------
00060 
00061     public:
00062         /**
00063         * The type of Object which this handle weakly references.
00064         */
00065         typedef const T ValueType;
00066 
00067         /**
00068         * The associated non-weak Handle type.
00069         */
00070         typedef typename T::Handle ValueHandle;
00071 
00072         /**
00073         * The associated non-weak View type.
00074         */
00075         typedef typename T::View ValueView;
00076 
00077 
00078     // ----- constructors ---------------------------------------------------
00079 
00080     public:
00081         /**
00082         * Construct a WeakView referencing NULL.
00083         *
00084         * @param oGuardian  reference to the WeakView's parent
00085         */
00086         WeakView(const Object& oGuardian)
00087             : m_vWeak(oGuardian, WeakReference::valueOf(NULL)),
00088               m_cpWeak(NULL)
00089             {
00090             }
00091 
00092         /**
00093         * Construct a WeakView referencing an object.
00094         *
00095         * @param oGuardian  reference to the WeakView's parent
00096         * @param that       the object to reference
00097         */
00098         WeakView(const Object& oGuardian, const TypedHandle<const T>& that)
00099             : m_vWeak(oGuardian, WeakReference::valueOf(that)),
00100               m_cpWeak(get_pointer(that))
00101             {
00102             }
00103 
00104         /**
00105         * Construct a WeakView referencing an object.
00106         *
00107         * @param oGuardian  reference to the WeakView's parent
00108         * @param that       the object to reference
00109         * @param fMutable   true if the member is declared as mutable, false
00110         *                   if declared as const
00111         */
00112         WeakView(const Object& oGuardian, const TypedHandle<const T>& that, bool fMutable)
00113             : m_vWeak(oGuardian, WeakReference::valueOf(that), fMutable),
00114               m_cpWeak(get_pointer(that))
00115             {
00116             }
00117 
00118 
00119     // ----- operators ------------------------------------------------------
00120 
00121     public:
00122         /**
00123         * Re-assign the WeakView to weakly reference a new Object.
00124         *
00125         * @param that  the object to reference
00126         */
00127         WeakView& operator=(const TypedHandle<const T>& that)
00128             {
00129             WeakReference::View vRef   = WeakReference::valueOf(that);
00130             const T*            cpThat = get_pointer(that);
00131 
00132             // update m_vWeak and m_cpWeak under a single write lock
00133                 {
00134                 SynchronizedMemberWriteBlock guard(get_guardian(m_vWeak));
00135                 guard.setMember(m_vWeak, vRef);
00136                 m_cpWeak = cpThat;
00137                 }
00138 
00139             return *this;
00140             }
00141 
00142         /**
00143         * Re-assign the WeakView to weakly reference a new Object.
00144         *
00145         * @param that  the object to reference
00146         */
00147         WeakView& operator=(const WeakView& that)
00148             {
00149             return operator=((ValueView) that); // assign from snapshot
00150             }
00151 
00152         /**
00153         * Return a non-weak View to the weakly referenced Object, or a
00154         * View to NULL if the weakly referenced Object has been
00155         * destroyed.
00156         *
00157         * @return ValueView
00158         */
00159         operator ValueView() const
00160             {
00161             WeakReference::View vRef;
00162             const T*            cpWeak;
00163 
00164             // read m_vWeak and m_cpWeak under a single read lock
00165                 {
00166                 SynchronizedMemberReadBlock guard(get_guardian(m_vWeak));
00167                 vRef   = guard.getMember(m_vWeak);
00168                 cpWeak = m_cpWeak;
00169                 }
00170 
00171             Object::View v = vRef->get();
00172             return NULL == v
00173                 ? TypedHandle<const T>() // Object has been collected
00174                 : TypedHandle<const T>(cpWeak, v);
00175             }
00176 
00177         /**
00178         * Return a View to the referenced Object.
00179         *
00180         * @return a View to the referenced Object
00181         */
00182         template<class PT>
00183         operator TypedHandle<const PT>() const
00184             {
00185             return (ValueView) *this;
00186             }
00187 
00188         /**
00189         * Return a TypedHolder to the referenced Object.
00190         *
00191         * @return a TypedHolder to the referenced Object
00192         */
00193         template<class PT>
00194         operator TypedHolder<PT>() const
00195             {
00196             return (ValueView) *this;
00197             }
00198 
00199         /**
00200         * Dereference the WeakView returning a non-weak View..
00201         *
00202         * If the non-weak View is non-NULL then it is guaranteed
00203         * that the Object will continue to exist for at least the
00204         * life of the returned View.
00205         *
00206         * @return a non-weak View to the referenced Object
00207         */
00208         ValueView operator->() const
00209             {
00210             return (ValueView) *this;
00211             }
00212 
00213         /**
00214         * Dereference this handle, returning <tt>T&</tt>.
00215         *
00216         * @return a raw <tt>T&</tt> reference to the referenced Object
00217         *
00218         * @throws NullPointerException if the this handle is @c NULL
00219         */
00220         const T& operator*() const
00221             {
00222             return *operator->();
00223             }
00224 
00225     // ----- data members ---------------------------------------------------
00226 
00227     protected:
00228         /**
00229         * The View to the associated WeakReference.
00230         */
00231         MemberView<WeakReference> m_vWeak;
00232 
00233         /**
00234         * Raw pointer to the weakly referenced object.
00235         */
00236         const T* m_cpWeak;
00237     };
00238 
00239 
00240 // ----- non-member operators and functions ---------------------------------
00241 
00242 /**
00243 * Output a human-readable description of the given
00244 * WeakView<T> to the specified stream.
00245 *
00246 * @param out  the stream used to output the description
00247 * @param wv   the WeakView<T> to describe
00248 *
00249 * @return the supplied stream
00250 */
00251 template <typename Char, typename Traits, class T>
00252 COH_INLINE std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, const WeakView<T>& wv)
00253     {
00254     typename WeakView<T>::ValueView v = wv;
00255     out << v;
00256     return out;
00257     }
00258 
00259 /**
00260 * Assign the specified handle to NULL.
00261 *
00262 * @param wv the handle to clear
00263 */
00264 template<class T> void clear_handle(WeakView<T>& wv)
00265     {
00266     wv = NULL;
00267     }
00268 
00269 /**
00270 * Return true if the supplied handle equals NULL.
00271 *
00272 * @param wv  the weak handle to test
00273 *
00274 * @return true iff the supplied handle equals NULL
00275 */
00276 template<class T>
00277 bool is_null(const WeakView<T>& wv)
00278     {
00279     return wv == NULL;
00280     }
00281 
00282 /**
00283 * Perform a dynamic cast the pointer associated with the WeakView
00284 * to a the specified handle/view type.
00285 *
00286 * @param wv      the WeakView from which to perform the cast
00287 * @param fThrow  true if an exception is to be thrown on a failed cast
00288 *
00289 * @return the casted pointer, or NULL if the cast fails and fThrow is false
00290 *
00291 * @throws ClassCastException if the cast fails and fThrow is true
00292 */
00293 template<class D, class T>
00294 D cast(const WeakView<T>& wv, bool fThrow = true)
00295     {
00296     return cast<D>((typename WeakView<T>::ValueView) wv, fThrow);
00297     }
00298 
00299 /**
00300 * Perform an instanceof check on a handle or view.
00301 *
00302 * @param wv  the WeakView from which to perform the test
00303 *
00304 * @return true if the supplied handle is an instance of the specified type
00305 */
00306 template<class D, class T>
00307 bool instanceof(const WeakView<T>& wv)
00308     {
00309     return NULL != cast<D>(wv, false);
00310     }
00311 
00312 COH_CLOSE_NAMESPACE2
00313 
00314 #endif // COH_WEAK_VIEW_HPP
Copyright © 2000, 2019, Oracle and/or its affiliates. All rights reserved.