00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 #ifndef COH_BOX_HANDLE_HPP
00017 #define COH_BOX_HANDLE_HPP
00018 
00019 #include "coherence/lang/compatibility.hpp"
00020 
00021 #include "coherence/lang/Object.hpp"
00022 #include "coherence/lang/TypedHandle.hpp"
00023 
00024 COH_OPEN_NAMESPACE2(coherence,lang)
00025 
00026 
00027 
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 template<class T, class B = typename T::BoxedType, bool fStrict = true>
00053 class BoxHandle
00054     : public TypedHandle<T>
00055     {
00056     
00057 
00058     public:
00059         
00060 
00061 
00062         typedef B BoxedType;
00063 
00064 
00065     
00066 
00067     public:
00068 
00069 
00070 
00071         BoxHandle()
00072             : TypedHandle<T>()
00073             {
00074             }
00075 
00076 
00077 
00078 
00079         BoxHandle(const B& b)
00080             : TypedHandle<T>(T::create(b))
00081             {
00082             }
00083 
00084 
00085 
00086 
00087         template<class O> BoxHandle(const TypedHandle<O>& that)
00088             : TypedHandle<T>(that)
00089             {
00090             }
00091 
00092 
00093 
00094 
00095         BoxHandle(T* o)
00096             : TypedHandle<T>(o)
00097             {
00098             }
00099 
00100 
00101 
00102 
00103         BoxHandle(const BoxHandle& h)
00104             : TypedHandle<T>(h)
00105             {
00106             }
00107 
00108     
00109 
00110     public:
00111 
00112 
00113 
00114         BoxHandle& operator=(const BoxedType& b)
00115             {
00116             TypedHandle<T>::operator=(T::create(b));
00117             return *this;
00118             }
00119 
00120 
00121 
00122 
00123 
00124 
00125         operator BoxedType() const
00126             {
00127             const T* pT = TypedHandle<T>::get();
00128             if (NULL == pT)
00129                 {
00130                 if (fStrict)
00131                     {
00132                     coh_throw_npe(typeid(T));
00133                     }
00134                 return BoxedType();
00135                 }
00136             return (BoxedType) *pT;
00137             }
00138     };
00139 
00140 template<class T, class B, bool S>
00141 bool operator==(const BoxHandle<T, B, S>& h1, const BoxHandle<T, B, S>& h2)
00142     {
00143     return operator==(Object::Holder(h1), Object::Holder(h2));
00144     }
00145 
00146 template<class T, class B, bool S>
00147 bool operator==(const BoxHandle<T, B, S>& h, const B& b)
00148     {
00149     const T* pT = get_pointer(h);
00150     if (S && pT == NULL)
00151         {
00152         coh_throw_npe(typeid(T));
00153         }
00154     return NULL == pT ? B() == b : ((B) *pT) == b;
00155     }
00156 
00157 template<class T, class B, bool S>
00158 bool operator==(const B& b, const BoxHandle<T, B, S>& h)
00159     {
00160     return operator==(h, b);
00161     }
00162 
00163 template<class T, class B, bool S>
00164 bool operator!=(const BoxHandle<T, B, S>& h1, const BoxHandle<T, B, S>& h2)
00165     {
00166     return !operator==(h1, h2);
00167     }
00168 
00169 template<class T, class B, bool S>
00170 bool operator!=(const BoxHandle<T, B, S>& h, const B& b)
00171     {
00172     return !operator==(h, b);
00173     }
00174 
00175 template<class T, class B, bool S>
00176 bool operator!=(const B& b, const BoxHandle<T, B, S>& h)
00177     {
00178     return !operator==(h, b);
00179     }
00180 
00181 COH_CLOSE_NAMESPACE2
00182 
00183 #endif // COH_BOX_HANDLE_HPP