kDB: Knowledge DataBase
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 { return *d; }
20 inline const T* operator->() const { return d; }
21 inline operator bool() const { return d; }
22 inline const T* data() const { return d; }
23 inline const T* constData() const { return d; }
24
25 inline bool operator==(const ConstExplicitlySharedDataPointer<T>& other) const
26 {
27 return d == other.d;
28 }
29 inline bool operator!=(const ConstExplicitlySharedDataPointer<T>& other) const
30 {
31 return d != other.d;
32 }
33
34 inline ConstExplicitlySharedDataPointer() { d = 0; }
35 inline ~ConstExplicitlySharedDataPointer()
36 {
37 if(d && !d->ref.deref())
38 {
39 delete d;
40 }
41 }
42
43 inline ConstExplicitlySharedDataPointer(const T* data) : d(const_cast<T*>(data))
44 {
45 if(d)
46 {
47 d->ref.ref();
48 }
49 }
50 inline ConstExplicitlySharedDataPointer(const ConstExplicitlySharedDataPointer& o) : d(o.d)
51 {
52 if(d)
53 {
54 d->ref.ref();
55 }
56 }
57 template<typename _T2_>
58 requires(std::is_convertible_v<_T2_*, T*>)
59 inline ConstExplicitlySharedDataPointer(const ConstExplicitlySharedDataPointer<_T2_>& o)
60 : d(const_cast<_T2_*>(o.data()))
61 {
62 if(d)
63 {
64 d->ref.ref();
65 }
66 }
67 inline ConstExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T>& o)
68 : d(const_cast<T*>(o.data()))
69 {
70 if(d)
71 {
72 d->ref.ref();
73 }
74 }
75 inline ConstExplicitlySharedDataPointer<T>&
76 operator=(const ConstExplicitlySharedDataPointer<T>& o)
77 {
78 if(o.d != d)
79 {
80 if(o.d)
81 {
82 o.d->ref.ref();
83 }
84
85 if(d && !d->ref.deref())
86 {
87 delete d;
88 }
89
90 d = o.d;
91 }
92
93 return *this;
94 }
95 inline ConstExplicitlySharedDataPointer& operator=(T* o)
96 {
97 if(o != d)
98 {
99 if(o)
100 {
101 o->ref.ref();
102 }
103
104 if(d && !d->ref.deref())
105 {
106 delete d;
107 }
108
109 d = o;
110 }
111
112 return *this;
113 }
114 template<typename _T2_>
115 requires(std::is_convertible_v<_T2_*, T*>)
116 inline ConstExplicitlySharedDataPointer<_T2_> d_cast() const
117 {
118 return ConstExplicitlySharedDataPointer<_T2_>(dynamic_cast<_T2_*>(d));
119 }
120 template<typename _T2_>
121 requires(std::is_convertible_v<_T2_*, T*>)
122 inline ConstExplicitlySharedDataPointer<_T2_> s_cast() const
123 {
124 return ConstExplicitlySharedDataPointer<_T2_>(static_cast<_T2_*>(d));
125 }
126
127 inline bool operator!() const { return !d; }
128
129 inline void swap(ConstExplicitlySharedDataPointer& other) { qSwap(d, other.d); }
130 private:
131 T* d;
132 };
133
134} // namespace knowCore
135
136#endif