00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef COH_WEAK_HOLDER_HPP
00017 #define COH_WEAK_HOLDER_HPP
00018
00019 #include "coherence/lang/compatibility.hpp"
00020
00021 #include "coherence/lang/Object.hpp"
00022 #include "coherence/lang/TypedHandle.hpp"
00023 #include "coherence/lang/TypedHolder.hpp"
00024 #include "coherence/lang/MemberHolder.hpp"
00025 #include "coherence/lang/WeakReference.hpp"
00026
00027 COH_OPEN_NAMESPACE2(coherence,lang)
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 template<class T>
00056 class WeakHolder
00057 {
00058
00059
00060 public:
00061
00062
00063
00064 typedef const T ValueType;
00065
00066
00067
00068
00069 typedef typename T::Handle ValueHandle;
00070
00071
00072
00073
00074 typedef typename T::View ValueView;
00075
00076
00077
00078
00079 typedef typename T::Holder ValueHolder;
00080
00081
00082
00083
00084 public:
00085
00086
00087
00088
00089
00090 WeakHolder(const Object& oGuardian)
00091 : m_ohWeak(oGuardian, WeakReference::valueOf(NULL)),
00092 m_cpWeak(NULL)
00093 {
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 WeakHolder(const Object& oGuardian, const TypedHolder<T>& that)
00103 : m_ohWeak(oGuardian, WeakReference::valueOf(that)),
00104 m_cpWeak(get_pointer(that))
00105 {
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 WeakHolder(const Object& oGuardian, const TypedHolder<T>& that,
00117 bool fMutable)
00118 : m_ohWeak(oGuardian, WeakReference::valueOf(that), fMutable),
00119 m_cpWeak(get_pointer(that))
00120 {
00121 }
00122
00123
00124
00125
00126 public:
00127
00128
00129
00130
00131
00132 WeakHolder& operator=(const TypedHolder<T>& that)
00133 {
00134 WeakReference::Holder ohRef = WeakReference::valueOf(that);
00135 const T* cpThat = get_pointer(that);
00136
00137
00138 {
00139 SynchronizedMemberWriteBlock guard(get_guardian(m_ohWeak));
00140 guard.setMember(m_ohWeak, ohRef);
00141 m_cpWeak = cpThat;
00142 }
00143
00144 return *this;
00145 }
00146
00147
00148
00149
00150
00151
00152 WeakHolder& operator=(const WeakHolder& that)
00153 {
00154 return operator=((ValueHolder) that);
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164 operator ValueView() const
00165 {
00166 WeakReference::View vRef;
00167 const T* cpWeak;
00168
00169
00170 {
00171 SynchronizedMemberReadBlock guard(get_guardian(m_ohWeak));
00172 vRef = guard.getMember(m_ohWeak);
00173 cpWeak = m_cpWeak;
00174 }
00175
00176 Object::View v = vRef->get();
00177 return NULL == v
00178 ? TypedHandle<const T>()
00179 : TypedHandle<const T>(cpWeak, v);
00180 }
00181
00182
00183
00184
00185
00186
00187 template<class PT>
00188 operator TypedHandle<const PT>() const
00189 {
00190 return (ValueView) *this;
00191 }
00192
00193
00194
00195
00196
00197
00198 template<class PT>
00199 operator TypedHolder<PT>() const
00200 {
00201 WeakReference::Holder ohRef;
00202 const T* cpWeak;
00203
00204
00205 {
00206 SynchronizedMemberReadBlock guard(get_guardian(m_ohWeak));
00207 ohRef = guard.getMember(m_ohWeak);
00208 cpWeak = m_cpWeak;
00209 }
00210
00211 WeakReference::Handle hRef = cast<WeakReference::Handle>(ohRef,
00212 false);
00213 Object::Holder oh = NULL == hRef
00214 ? ohRef->get()
00215 : hRef->get();
00216
00217 if (NULL == oh)
00218 {
00219 return NULL;
00220 }
00221 else if (NULL == hRef)
00222 {
00223 return TypedHandle<const T>(cpWeak, oh);
00224 }
00225 else
00226 {
00227 return TypedHandle<T>(const_cast<T*>(cpWeak), oh);
00228 }
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 ValueView operator->() const
00241 {
00242 return (ValueView) *this;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252 const T& operator*() const
00253 {
00254 return *operator->();
00255 }
00256
00257
00258
00259 protected:
00260
00261
00262
00263 MemberHolder<WeakReference> m_ohWeak;
00264
00265
00266
00267
00268 const T* m_cpWeak;
00269 };
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 template<class T>
00284 std::ostream& operator<<(std::ostream& out, const WeakHolder<T>& woh)
00285 {
00286 typename WeakHolder<T>::ValueView v = woh;
00287 out << v;
00288 return out;
00289 }
00290
00291
00292
00293
00294
00295
00296 template<class T> void clear_handle(WeakHolder<T>& woh)
00297 {
00298 woh = NULL;
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308 template<class T>
00309 bool is_null(const WeakHolder<T>& woh)
00310 {
00311 return woh == NULL;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 template<class D, class T>
00326 D cast(const WeakHolder<T>& woh, bool fThrow = true)
00327 {
00328 return cast<D>((TypedHolder<T>) woh, fThrow);
00329 }
00330
00331
00332
00333
00334
00335
00336
00337
00338 template<class D, class T>
00339 bool instanceof(const WeakHolder<T>& woh)
00340 {
00341 return NULL != cast<D>(woh, false);
00342 }
00343
00344 COH_CLOSE_NAMESPACE2
00345
00346 #endif // COH_WEAK_HOLDER_HPP
Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.