00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
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 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 template<class T>
00036 class FinalHolder
00037     : public MemberHolder<T>
00038     {
00039     
00040 
00041     public:
00042 
00043 
00044 
00045         typedef T ValueType;
00046 
00047 
00048 
00049 
00050         typedef typename T::Handle ValueHandle;
00051 
00052 
00053 
00054 
00055         typedef typename T::View ValueView;
00056 
00057 
00058 
00059 
00060         typedef typename T::Holder ValueHolder;
00061 
00062 
00063     
00064 
00065     public:
00066 
00067 
00068 
00069 
00070 
00071         FinalHolder(const Object& oGuardian)
00072             : MemberHolder<T>(oGuardian)
00073             {
00074             }
00075 
00076 
00077 
00078 
00079 
00080 
00081 
00082         FinalHolder(const Object& oGuardian, const TypedHolder<T>& that)
00083             : MemberHolder<T>(oGuardian, that)
00084             {
00085             }
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095         FinalHolder(const Object& oGuardian, const ValueView& that, bool fMutable)
00096             : MemberHolder<T>(oGuardian, that, fMutable)
00097             {
00098             }
00099 
00100 
00101     
00102 
00103     public:
00104 
00105 
00106 
00107 
00108 
00109         operator ValueView() const
00110             {
00111             return ValueView(MemberHolder<T>::m_po);
00112             }
00113 
00114 
00115 
00116 
00117 
00118 
00119         template<class PT>
00120         operator TypedHandle<const PT>() const
00121             {
00122             return MemberHolder<T>::m_po;
00123             }
00124 
00125 
00126 
00127 
00128 
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 
00142 
00143 
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 
00158 
00159         FinalHolder& operator=(const TypedHolder<T>& that);
00160 
00161 
00162 
00163 
00164         FinalHolder& operator=(const FinalHolder<T>& that);
00165 
00166 
00167     
00168 
00169     protected:
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 
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,  false);
00203                 if (NULL == hThat) 
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( false)
00209                                 ? NULL : cpo);
00210                     }
00211                 else 
00212                     {
00213                     T* po = get_pointer(hThat);
00214                     MemberHolder<T>::m_fView = false;
00215                     MemberHolder<T>::m_po    =
00216                         NULL == po->_attach(false) ? NULL : po;
00217                     }
00218                 }
00219             }
00220 
00221     
00222 
00223 
00224 
00225 
00226     template<class _T, class OH> friend void initialize(FinalHolder<_T>& fh,
00227                                               OH that);
00228     };
00229 
00230 
00231 
00232 
00233 
00234 
00235 
00236 
00237 
00238 
00239 
00240 
00241 
00242 
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 
00252 
00253 
00254 
00255 
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 
00265 
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 
00274 
00275 
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