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

E80355-01

coherence/lang/FinalView.hpp

00001 /*
00002 * FinalView.hpp
00003 *
00004 * Copyright (c) 2000, 2017, 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_FINAL_VIEW_HPP
00017 #define COH_FINAL_VIEW_HPP
00018 
00019 #include "coherence/lang/compatibility.hpp"
00020 
00021 #include "coherence/lang/MemberView.hpp"
00022 #include "coherence/lang/TypedHandle.hpp"
00023 #include "coherence/lang/SynchronizedMemberWriteBlock.hpp"
00024 
00025 COH_OPEN_NAMESPACE2(coherence,lang)
00026 
00027 /**
00028 * FinalView is an immutable thread-safe view intended for use as a data-member
00029 * within Objects.
00030 *
00031 * @author mf  2008.12.01
00032 *
00033 * @see FinalHandle
00034 * @see FinalHolder
00035 */
00036 template<class T>
00037 class FinalView
00038     : public MemberView<T>
00039     {
00040     // ----- typedefs -------------------------------------------------------
00041 
00042     public:
00043         /**
00044         * The type of the values the holder can reference.
00045         */
00046         typedef T ValueType;
00047 
00048         /**
00049         * The View type for the referenced Object.
00050         */
00051         typedef typename T::View ValueView;
00052 
00053 
00054     // -------- constructors ------------------------------------------------
00055 
00056     public:
00057         /**
00058         * Construct a new FinalView referencing NULL via a handle.
00059         *
00060         * @param oGuardian  the object that protects this member
00061         */
00062         FinalView(const Object& oGuardian)
00063             : MemberView<T>(oGuardian)
00064             {
00065             }
00066 
00067         /**
00068         * Construct a new FinalView referencing specified Object.
00069         *
00070         * @param oGuardian  the object that protects this member
00071         * @param that       the object to reference
00072         */
00073         FinalView(const Object& oGuardian, const ValueView& that)
00074             : MemberView<T>(oGuardian, that)
00075             {
00076             if (MemberView<T>::m_cpo)
00077                 {
00078                 MemberView<T>::m_nMutability = MemberView<T>::safe_immutable;
00079                 }
00080             }
00081 
00082         /**
00083         * Construct a new FinalView referencing specified Object.
00084         *
00085         * @param oGuardian  the object that protects this member
00086         * @param that       the object to reference
00087         * @param fMutable   true if the member is declared as mutable, false
00088         *                   if declared as const
00089         */
00090         FinalView(const Object& oGuardian, const ValueView& that, bool fMutable)
00091             : MemberView<T>(oGuardian, that, fMutable)
00092             {
00093             if (MemberView<T>::m_cpo)
00094                 {
00095                 MemberView<T>::m_nMutability = MemberView<T>::safe_immutable;
00096                 }
00097             }
00098 
00099     private:
00100         /**
00101         * Blocked copy constructor.
00102         */
00103         FinalView(const FinalView&);
00104 
00105 
00106     // ----- operators ------------------------------------------------------
00107 
00108     public:
00109         /**
00110         * Return a View to the referenced Object.
00111         *
00112         * @return a View to the referenced Object
00113         */
00114         operator ValueView() const
00115             {
00116             return getFinal();
00117             }
00118 
00119         /**
00120         * Return a View to the referenced Object.
00121         *
00122         * @return a View to the referenced Object
00123         */
00124         template<class PT>
00125         operator TypedHandle<const PT>() const
00126             {
00127             return getFinal();
00128             }
00129 
00130         /**
00131         * Return a TypedHolder to the referenced Object.
00132         *
00133         * @return a TypedHolder to the referenced Object
00134         */
00135         template<class PT>
00136         operator TypedHolder<PT>() const
00137             {
00138             return getFinal();
00139             }
00140 
00141         /**
00142         * Dereference the FinalView.
00143         *
00144         * @return a const pointer to the referenced Object
00145         */
00146         const T* operator->() const
00147             {
00148             const T* cpo = MemberView<T>::m_cpo;
00149             if (MemberView<T>::m_nMutability < MemberView<T>::safe_immutable)
00150                 {
00151                 MemberView<T>::readBarrier();
00152                 if (cpo == NULL)
00153                     {
00154                     cpo = MemberView<T>::m_cpo;
00155                     MemberView<T>::readBarrier();
00156                     }
00157                 }
00158 
00159             if (NULL == cpo)
00160                 {
00161                 coh_throw_npe(typeid(const T));
00162                 }
00163             return cpo;
00164             }
00165 
00166     private:
00167         /**
00168         * Blocked assignment operator.
00169         */
00170         FinalView& operator=(const ValueView& that);
00171 
00172         /**
00173         * Blocked assignment operator.
00174         */
00175         FinalView& operator=(const FinalView<T>& that);
00176 
00177 
00178     // ----- SmartMember interface ------------------------------------------
00179 
00180     protected:
00181         /**
00182         * {@inheritDoc}
00183         */
00184         virtual size64_t retained() const
00185             {
00186             const T* cpo = MemberView<T>::m_cpo;
00187             if (cpo == NULL &&
00188                 MemberView<T>::m_nMutability < MemberView<T>::safe_immutable)
00189                 {
00190                 MemberView<T>::readBarrier();
00191                 cpo = MemberView<T>::m_cpo;
00192                 }
00193 
00194             return cpo == NULL
00195                     ? 0
00196                     : cpo->sizeOf(/*fDeep*/ true);
00197             }
00198 
00199 
00200     // ----- helper methods -------------------------------------------------
00201 
00202     protected:
00203         /**
00204          * {@inheritDoc}
00205          */
00206         ValueView getFinal() const
00207             {
00208             const T* cpo = MemberView<T>::m_cpo;
00209             if (MemberView<T>::m_prev == NULL)
00210                 {
00211                 if (cpo == NULL &&
00212                     MemberView<T>::m_nMutability < MemberView<T>::safe_immutable)
00213                     {
00214                     MemberView<T>::readBarrier();
00215                     cpo = MemberView<T>::m_cpo;
00216                     }
00217 
00218                 return ValueView(cpo);
00219                 }
00220             else
00221                 {
00222                 return TypedHandle<const T>(cpo, *this);
00223                 }
00224             }
00225 
00226         /**
00227         * Initialize the value of a FinalView after it is
00228         * constructed.  A FinalView may be initialized only if the
00229         * following conditions hold:
00230         * <ul>
00231         *   <li> The FinalView has not already been initialized
00232         *   (either during construction, or via <tt>initialize()</tt>)
00233         *   <li> The FinalView has not escaped
00234         * </ul>
00235         *
00236         * @param that  the value to initialize this FinalView to
00237         */
00238         void initialize(ValueView that)
00239             {
00240             if (MemberView<T>::m_nMutability >= MemberView<T>::forever_immutable)
00241                 {
00242                 coh_throw_illegal_state(
00243                     "attempt to initialize const FinalView");
00244                 }
00245             else if (MemberView<T>::m_prev == NULL)
00246                 {
00247                 SynchronizedMemberWriteBlock block(MemberView<T>::getGuardian());
00248                 if (MemberView<T>::m_cpo != NULL) // sync'd check
00249                     {
00250                     coh_throw_illegal_state(
00251                         "attempt to multiply initialize FinalView");
00252                     }
00253                 MemberView<T>::setEscaped(that, &block);
00254                 }
00255             else if (MemberView<T>::m_cpo != NULL)
00256                 {
00257                 coh_throw_illegal_state(
00258                     "attempt to multiply initialize FinalView");
00259                 }
00260             else
00261                 {
00262                 MemberView<T>::m_cpo = get_pointer(that);
00263                 if (MemberView<T>::m_cpo)
00264                     {
00265                     MemberView<T>::link(that);
00266                     MemberView<T>::m_nMutability = MemberView<T>::safe_immutable;
00267                     }
00268                 }
00269             }
00270 
00271     // ----- friends --------------------------------------------------------
00272 
00273     /**
00274     * @internal
00275     */
00276     template<class _T, class V> friend void initialize(FinalView<_T>& fh,
00277                                               V that);
00278     };
00279 
00280 /**
00281 * Perform a dynamic cast the pointer associated with the FinalView
00282 * to a the specified handle/view type.
00283 *
00284 * @param v       the FinalView from which to perform the cast
00285 * @param fThrow  true if an exception is to be thrown on a failed cast
00286 *
00287 * @return the casted pointer, or NULL if the cast fails and fThrow is false
00288 *
00289 * @throws ClassCastException if the cast fails and fThrow is true
00290 */
00291 template<class D, class T>
00292 D cast(const FinalView<T>& v, bool fThrow = true)
00293     {
00294     return cast<D>((typename FinalView<T>::ValueView) v, fThrow);
00295     }
00296 
00297 /**
00298 * Perform an instanceof check on a FinalView.
00299 *
00300 * @param v   the FinalView from which to perform the test
00301 *
00302 * @return true if the supplied handle is an instance of the specified type
00303 */
00304 template<class D, class T>
00305 bool instanceof(const FinalView<T>& v)
00306     {
00307     return NULL != cast<D>(v, false);
00308     }
00309 
00310 /**
00311 * Initialize the value of a FinalView after it is constructed.  A
00312 * FinalView may be initialized only if the following conditions
00313 * hold:
00314 * <ul>
00315 *   <li> The FinalView has not already been initialized to a
00316 *   non-NULL value (either during construction, or via
00317 *   <tt>initialize()</tt>)
00318 * </ul>
00319 *
00320 * @param h     the FinalView to initialize
00321 * @param that  the value to initialize this FinalView to
00322 */
00323 template<class T, class V>
00324 void initialize(FinalView<T>& h, V that)
00325     {
00326     h.initialize(that);
00327     }
00328 
00329 
00330 COH_CLOSE_NAMESPACE2
00331 
00332 #endif // COH_FINAL_VIEW_HPP
Copyright © 2000, 2017, Oracle and/or its affiliates. All rights reserved.