knowL: Knowledge Libraries
Loading...
Searching...
No Matches
ConstExplicitlySharedDataPointer.h
1#ifndef _KNOW_CORE_CONSTEXPLICITLYSHAREDDATAPOINTER_H_
2#define _KNOW_CORE_CONSTEXPLICITLYSHAREDDATAPOINTER_H_
3
4#include <QSharedDataPointer>
5
6namespace knowCore
7{
12 template <class T>
13 class ConstExplicitlySharedDataPointer
14 {
15 public:
16 typedef T Type;
17 typedef T* pointer;
18
19 inline const T& operator*() const
20 {
21 return *d;
22 }
23 inline const T* operator->() const
24 {
25 return d;
26 }
27 inline operator bool() const
28 {
29 return d;
30 }
31 inline const T* data() const
32 {
33 return d;
34 }
35 inline const T* constData() const
36 {
37 return d;
38 }
39
40 inline bool operator==(const ConstExplicitlySharedDataPointer<T>& other) const
41 {
42 return d == other.d;
43 }
44 inline bool operator!=(const ConstExplicitlySharedDataPointer<T>& other) const
45 {
46 return d != other.d;
47 }
48
49 inline ConstExplicitlySharedDataPointer()
50 {
51 d = 0;
52 }
53 inline ~ConstExplicitlySharedDataPointer()
54 {
55 if(d && !d->ref.deref())
56 {
57 delete d;
58 }
59 }
60
61 inline ConstExplicitlySharedDataPointer(const T* data) : d(const_cast<T*>(data))
62 {
63 if(d)
64 {
65 d->ref.ref();
66 }
67 }
68 inline ConstExplicitlySharedDataPointer(const ConstExplicitlySharedDataPointer& o) : d(o.d)
69 {
70 if(d)
71 {
72 d->ref.ref();
73 }
74 }
75 template<typename _T2_, std::enable_if_t<std::is_convertible_v<_T2_*, T*>, int> = 0>
76 inline ConstExplicitlySharedDataPointer(const ConstExplicitlySharedDataPointer<_T2_>& o) : d(const_cast<_T2_*>(o.data()))
77 {
78 if(d)
79 {
80 d->ref.ref();
81 }
82 }
83 inline ConstExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T>& o) : d(const_cast<T*>(o.data()))
84 {
85 if(d)
86 {
87 d->ref.ref();
88 }
89 }
90 inline ConstExplicitlySharedDataPointer<T>& operator=(const ConstExplicitlySharedDataPointer<T>& o)
91 {
92 if(o.d != d)
93 {
94 if(o.d)
95 {
96 o.d->ref.ref();
97 }
98
99 if(d && !d->ref.deref())
100 {
101 delete d;
102 }
103
104 d = o.d;
105 }
106
107 return *this;
108 }
109 inline ConstExplicitlySharedDataPointer& operator=(T* o)
110 {
111 if(o != d)
112 {
113 if(o)
114 {
115 o->ref.ref();
116 }
117
118 if(d && !d->ref.deref())
119 {
120 delete d;
121 }
122
123 d = o;
124 }
125
126 return *this;
127 }
128 template<typename _T2_>
129 inline std::enable_if_t<std::is_convertible_v<_T2_*, T*>, ConstExplicitlySharedDataPointer<_T2_>> d_cast() const
130 {
131 return ConstExplicitlySharedDataPointer<_T2_>(dynamic_cast<_T2_*>(d));
132 }
133 template<typename _T2_>
134 inline std::enable_if_t<std::is_convertible_v<_T2_*, T*>, ConstExplicitlySharedDataPointer<_T2_>> s_cast() const
135 {
136 return ConstExplicitlySharedDataPointer<_T2_>(static_cast<_T2_*>(d));
137 }
138
139 inline bool operator!() const
140 {
141 return !d;
142 }
143
144 inline void swap(ConstExplicitlySharedDataPointer& other)
145 {
146 qSwap(d, other.d);
147 }
148
149 private:
150 T* d;
151 };
152
153}
154
155#endif