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

E26041-01

coherence/lang/FinalHolder.hpp

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