00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
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 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 template<class T>
00037 class FinalView
00038     : public MemberView<T>
00039     {
00040     
00041 
00042     public:
00043 
00044 
00045 
00046         typedef T ValueType;
00047 
00048 
00049 
00050 
00051         typedef typename T::View ValueView;
00052 
00053 
00054     
00055 
00056     public:
00057 
00058 
00059 
00060 
00061 
00062         FinalView(const Object& oGuardian)
00063             : MemberView<T>(oGuardian)
00064             {
00065             }
00066 
00067 
00068 
00069 
00070 
00071 
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 
00084 
00085 
00086 
00087 
00088 
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 
00102 
00103         FinalView(const FinalView&);
00104 
00105 
00106     
00107 
00108     public:
00109 
00110 
00111 
00112 
00113 
00114         operator ValueView() const
00115             {
00116             return getFinal();
00117             }
00118 
00119 
00120 
00121 
00122 
00123 
00124         template<class PT>
00125         operator TypedHandle<const PT>() const
00126             {
00127             return getFinal();
00128             }
00129 
00130 
00131 
00132 
00133 
00134 
00135         template<class PT>
00136         operator TypedHolder<PT>() const
00137             {
00138             return getFinal();
00139             }
00140 
00141 
00142 
00143 
00144 
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 
00169 
00170         FinalView& operator=(const ValueView& that);
00171 
00172 
00173 
00174 
00175         FinalView& operator=(const FinalView<T>& that);
00176 
00177 
00178     
00179 
00180     protected:
00181 
00182 
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( true);
00197             }
00198 
00199 
00200     
00201 
00202     protected:
00203 
00204 
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 
00228 
00229 
00230 
00231 
00232 
00233 
00234 
00235 
00236 
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) 
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     
00272 
00273 
00274 
00275 
00276     template<class _T, class V> friend void initialize(FinalView<_T>& fh,
00277                                               V that);
00278     };
00279 
00280 
00281 
00282 
00283 
00284 
00285 
00286 
00287 
00288 
00289 
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 
00299 
00300 
00301 
00302 
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 
00312 
00313 
00314 
00315 
00316 
00317 
00318 
00319 
00320 
00321 
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