• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/hpp/dds/core/Reference.hpp

Go to the documentation of this file.
00001 #ifndef OMG_DDS_CORE_REFERENCE_HPP_
00002 #define OMG_DDS_CORE_REFERENCE_HPP_
00003 
00004 /* Copyright 2010, Object Management Group, Inc.
00005  * Copyright 2010, PrismTech, Inc.
00006  * Copyright 2010, Real-Time Innovations, Inc.
00007  * All rights reserved.
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License");
00010  * you may not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  *     http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS,
00017  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
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 //TODO: Move required parts in the "detail" namespace
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     // -- disallow dynamic allocation for reference types
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 } } /* namespace dds / namespace core */
00270 
00271 #endif /* OMG_DDS_CORE_REFERENCE_HPP_ */
00272 

Generated on Tue Nov 9 2010 02:16:21 for dds-psm-cxx-doc by  doxygen 1.7.1