coherence/lang/FinalHolder.hpp

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