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