coherence/lang/FinalView.hpp

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