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 #include "coherence/lang/SynchronizedMemberWriteBlock.hpp"
00024
00025
00026 COH_OPEN_NAMESPACE2(coherence,lang)
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 template<class T>
00038 class FinalHolder
00039 : public MemberHolder<T>
00040 {
00041
00042
00043 public:
00044
00045
00046
00047 typedef T ValueType;
00048
00049
00050
00051
00052 typedef typename T::Handle ValueHandle;
00053
00054
00055
00056
00057 typedef typename T::View ValueView;
00058
00059
00060
00061
00062 typedef typename T::Holder ValueHolder;
00063
00064
00065
00066
00067 public:
00068
00069
00070
00071
00072
00073 FinalHolder(const Object& oGuardian)
00074 : MemberHolder<T>(oGuardian)
00075 {
00076 }
00077
00078
00079
00080
00081
00082
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
00095
00096
00097
00098
00099
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
00113
00114 FinalHolder(const FinalHolder&);
00115
00116
00117
00118
00119 public:
00120
00121
00122
00123
00124
00125 operator ValueView() const
00126 {
00127 return getFinal();
00128 }
00129
00130
00131
00132
00133
00134
00135 template<class PT>
00136 operator TypedHandle<const PT>() const
00137 {
00138 return getFinal();
00139 }
00140
00141
00142
00143
00144
00145
00146 template<class PT>
00147 operator TypedHolder<PT>() const
00148 {
00149 return getFinal();
00150 }
00151
00152
00153
00154
00155
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
00180
00181 FinalHolder& operator=(const TypedHolder<T>& that);
00182
00183
00184
00185
00186 FinalHolder& operator=(const FinalHolder<T>& that);
00187
00188
00189
00190 protected:
00191
00192
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( true);
00206 }
00207
00208
00209
00210
00211 protected:
00212
00213
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
00245
00246
00247
00248
00249
00250
00251
00252
00253
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)
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, false);
00280 if (NULL == hThat)
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( false)
00286 ? NULL : cpo);
00287 }
00288 else
00289 {
00290 T* po = get_pointer(hThat);
00291 MemberHolder<T>::m_fView = false;
00292 MemberHolder<T>::m_po =
00293 NULL == po->_attach(false) ? NULL : po;
00294 }
00295 MemberHolder<T>::m_nMutability = MemberHolder<T>::safe_immutable;
00296 }
00297 }
00298
00299
00300
00301
00302
00303
00304 template<class _T, class OH> friend void initialize(FinalHolder<_T>& fh,
00305 OH that);
00306 };
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
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
00330
00331
00332
00333
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
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
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