Go to the documentation of this file.00001 #ifndef OMG_DDS_CORE_REFERENCE_HPP_
00002 #define OMG_DDS_CORE_REFERENCE_HPP_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <dds/core/corefwd.hpp>
00023 #include <dds/core/Exception.hpp>
00024 #include <dds/core/refmacros.hpp>
00025 #include <dds/core/types.hpp>
00026
00027
00028
00029 namespace dds { namespace core {
00030
00031
00036 template <typename DELEGATE>
00037 class Reference
00038 {
00039 public:
00040 DECLARE_TYPE_TRAITS(DELEGATE)
00041
00042 protected:
00046 Reference() : impl_() {
00047 OMG_DDS_LOG("MM", "Reference()");
00048 }
00049
00055 Reference(const Reference& ref) : impl_(ref.delegate()) {
00056 OMG_DDS_LOG("MM", "Reference(const Reference& ref)");
00057 }
00058
00064 template <typename D>
00065 Reference(const Reference<D>& ref)
00066 {
00067 OMG_DDS_STATIC_ASSERT((dds::core::is_base_of<DELEGATE_T, D>::value));
00068 OMG_DDS_LOG("MM", "Reference(const Reference<D>& ref)");
00069 impl_ = ref.delegate();
00070 }
00071
00072 template <typename R>
00073 Reference(const R& that) {
00074 OMG_DDS_STATIC_ASSERT((dds::core::is_base_of<DELEGATE_T, typename R::DELEGATE_T>::value));
00075 impl_ = that.delegate();
00076 }
00077
00078 explicit Reference(DELEGATE_T* p) : impl_(p) {
00079 OMG_DDS_LOG("MM", "Reference(DELEGATE_T* p)");
00080 }
00081
00082 Reference(const DELEGATE_REF_T& ref) : impl_(ref) {
00083 OMG_DDS_LOG("MM", "Reference(const DELEGATE_REF_T& ref)");
00084 }
00085
00086 public:
00090 ~Reference() {
00091 OMG_DDS_LOG("MM", "~Reference()");
00092 }
00093
00094 public:
00095 operator DELEGATE_REF_T() const {
00096 return impl_;
00097 }
00098
00106 template <typename R>
00107 bool operator==(const R& ref) const {
00108 return this->delegate() == ref.delegate();
00109 }
00110
00118 template <typename R>
00119 bool operator!=(const R& ref) const {
00120 return this->delegate() != ref.delegate();
00121 }
00122
00123 template <typename D>
00124 Reference& operator=(const Reference<D>& that) {
00125 OMG_DDS_STATIC_ASSERT((dds::core::is_base_of<DELEGATE_T, D>::value));
00126 if (this != (Reference*)&that) {
00127 *this = Reference<DELEGATE_T>(that);
00128 }
00129 return *this;
00130 }
00131
00132 template <typename R>
00133 Reference& operator=(const R& rhs) {
00134 OMG_DDS_STATIC_ASSERT((dds::core::is_base_of< DELEGATE_T, typename R::DELEGATE_T>::value));
00135 if (this != (Reference*)&rhs)
00136 *this = Reference<DELEGATE_T>(rhs);
00137 return *this;
00138 }
00139
00150 Reference&
00151 operator=(const null_type) {
00152 DELEGATE_REF_T tmp;
00153 impl_ = tmp;
00154 return *this;
00155 }
00156
00160 bool is_nil() const {
00161 return this->delegate().get() == 0;
00162 }
00163
00176 bool
00177 operator==(const null_type) const {
00178 return this->is_nil();
00179 }
00180
00193 bool operator!=(const null_type nil) const {
00194 return !(this->is_nil());
00195 }
00196
00197 private:
00198
00199 void* operator new(size_t);
00200
00201
00202
00203 public:
00210 const DELEGATE_REF_T& delegate() const {
00211 return impl_;
00212 }
00213
00220 DELEGATE_REF_T& delegate() {
00221 return impl_;
00222 }
00223
00240 DELEGATE* operator->() {
00241 return impl_.get();
00242 }
00243
00260 const DELEGATE* operator->() const {
00261 return impl_.get();
00262 }
00263
00264 protected:
00265 DELEGATE_REF_T impl_;
00266 };
00267
00268
00269 } }
00270
00271 #endif
00272