Line data Source code
1 : /* 2 : * Distributed under the OpenDDS License. 3 : * See: http://www.opendds.org/license.html 4 : */ 5 : 6 : #ifndef OPENDDS_DCPS_XTYPES_EXTERNAL_H 7 : #define OPENDDS_DCPS_XTYPES_EXTERNAL_H 8 : 9 : #include <dds/DCPS/unique_ptr.h> 10 : 11 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL 12 : 13 : namespace OpenDDS { 14 : namespace XTypes { 15 : 16 : /// Simplified version of the XTypes 1.3 mapping External Members (Modern C++) 17 : /// See section 7.5.1.2.3.2. Differences from spec: 18 : /// - this version uses unique ownership instead of shared ownership 19 : /// - default constructed External<T> has a default constructed T, not empty 20 : /// - shared_ptr constructor, get_shared_ptr, is_locked, lock are not provided 21 : /// - External<T> can be constructed from (or assigned from) a T which is copied 22 : /// If/when this class is generated by opendds_idl for @external, closer 23 : /// alignment to the spec may be desired. However, the simplified version is 24 : /// useful for the common use case of @external (tree-like structures). 25 : template <typename T> 26 : class External { 27 : public: 28 : External(); 29 : explicit External(T* p, bool locked = false); 30 : // External(shared_ptr<T> p); 31 : External(const External& other); 32 : // use compiler-generated destructor ~External(); 33 : External& operator=(const External& other); 34 : 35 : // Additions to XTypes spec: 36 : External(const T& value); 37 : External& operator=(const T& value); 38 : 39 240 : T& operator*() { return *ptr_; } 40 16325 : const T& operator*() const { return *ptr_; } 41 : 42 : T* get() { return ptr_.get(); } 43 : const T* get() const { return ptr_.get(); } 44 : 45 : T* operator->() { return ptr_.get(); } 46 : const T* operator->() const { return ptr_.get(); } 47 : // shared_ptr<T> get_shared_ptr(); 48 : 49 : bool operator==(const External& other) const { return ptr_ == other.ptr_; } 50 : bool operator!=(const External& other) const { return ptr_ != other.ptr_; } 51 : bool operator<(const External& other) const { return ptr_ < other.ptr_; } 52 : operator bool() const { return ptr_; } 53 : // bool is_locked() const; 54 : // void lock(); 55 : 56 : private: 57 : DCPS::unique_ptr<T> ptr_; 58 : }; 59 : 60 : template <typename T> 61 4309 : External<T>::External() 62 4309 : : ptr_(new T) 63 4309 : {} 64 : 65 : template <typename T> 66 87 : External<T>::External(const T& value) 67 87 : : ptr_(new T(value)) 68 87 : {} 69 : 70 : template <typename T> 71 : External<T>::External(T* p, bool) 72 : : ptr_(p) 73 : {} 74 : 75 : template <typename T> 76 : External<T>::External(const External& other) 77 : : ptr_(new T(*other)) 78 : {} 79 : 80 : template <typename T> 81 4021 : External<T>& External<T>::operator=(const External& other) 82 : { 83 4021 : ptr_.reset(new T(*other)); 84 4021 : return *this; 85 : } 86 : 87 : template <typename T> 88 0 : External<T>& External<T>::operator=(const T& value) 89 : { 90 0 : ptr_.reset(new T(value)); 91 0 : return *this; 92 : } 93 : 94 : } // namespace OpenDDS 95 : } // namespace XTypes 96 : 97 : OPENDDS_END_VERSIONED_NAMESPACE_DECL 98 : 99 : #endif // OPENDDS_DCPS_XTYPES_EXTERNAL_H