OpenDDS  Snapshot(2023/04/28-20:55)
External.h
Go to the documentation of this file.
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 
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  T& operator*() { return *ptr_; }
40  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:
58 };
59 
60 template <typename T>
62  : ptr_(new T)
63 {}
64 
65 template <typename T>
67  : ptr_(new T(value))
68 {}
69 
70 template <typename T>
72  : ptr_(p)
73 {}
74 
75 template <typename T>
77  : ptr_(new T(*other))
78 {}
79 
80 template <typename T>
82 {
83  ptr_.reset(new T(*other));
84  return *this;
85 }
86 
87 template <typename T>
89 {
90  ptr_.reset(new T(value));
91  return *this;
92 }
93 
94 } // namespace OpenDDS
95 } // namespace XTypes
96 
98 
99 #endif // OPENDDS_DCPS_XTYPES_EXTERNAL_H
bool operator!=(const External &other) const
Definition: External.h:50
const LogLevel::Value value
Definition: debug.cpp:61
DCPS::unique_ptr< T > ptr_
Definition: External.h:57
bool operator==(const External &other) const
Definition: External.h:49
const T * operator->() const
Definition: External.h:46
const T & operator*() const
Definition: External.h:40
External & operator=(const External &other)
Definition: External.h:81
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
bool operator<(const External &other) const
Definition: External.h:51
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28