guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gptr.hpp
1 //
2 // Smart Pointers.
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef guit_ptr_hpp
9 #define guit_ptr_hpp
10 #include <gobject.hpp>
11 namespace guit {
12 
15 template <typename T> class gptr {
16 public:
17  gptr() noexcept : p_{} {}
18  gptr(T* p) : p_(p) {gaddPtr(p_,true);}
19  gptr(T& p) : p_(&p) {gaddPtr(p_,true);}
20  gptr(gptr const& b) : p_(b.p_) {gaddPtr(p_,true);}
21  gptr(gptr const&& b) : p_(b.p_) {gaddPtr(p_,true);}
22  template <typename B> gptr(gptr<B> const& b) : p_(b.p_) {gaddPtr(p_,true);}
23  template <typename B> gptr(gptr<B> const&& b) : p_(b.p_) {gaddPtr(p_,true);}
24 
25  ~gptr() {gremovePtr(p_);}
26 
27  gptr& operator=(T* p) {reset(p); return *this;}
28  gptr& operator=(T& p) {reset(&p); return *this;}
29  gptr& operator=(gptr<T> const& b) {reset(b.p_); return *this;}
30  gptr& operator=(gptr<T> const&& b) {reset(b.p_); return *this;}
31  template <class B> gptr& operator=(B* p) {reset(p); return *this;}
32  template <class B> gptr& operator=(gptr<B> const& b) {reset(b.p_); return *this;}
33  template <class B> gptr& operator=(gptr<B> const&& b) {reset(b.p_); return *this;}
34 
36  void reset() {gptr().swap(*this);}
37  void reset(T* b) {gptr(b).swap(*this);}
38 
40  void swap(gptr& b) noexcept {T* tmp = p_; p_ = b.p_; b.p_ = tmp;}
41 
43  unsigned int useCount() const noexcept {return p_ ? p_->refcount_ : 0;}
44 
46  T* get() const noexcept {return p_;}
47 
49  operator T*() const noexcept {return p_;}
50 
52  T* operator->() const noexcept {return p_;}
53 
55  T& operator*() const noexcept {return *p_;}
56 
58  auto operator~() const {return ~(*p_);}
59 
61  bool operator!() const noexcept {return p_ == nullptr;}
62 
64  auto& operator++(int) const {return (*p_)++;}
65 
67  auto& operator--(int) const {return (*p_)--;}
68 
70  auto& operator<<(const char* child) const {return *p_ << child;}
71  auto& operator<<(GString const& child) const {return *p_ << child;}
72  auto& operator<<(GStrings const& child) const {return *p_ << child;}
73  auto& operator<<(Gadget& child) const {return *p_ << child;}
74  auto& operator<<(Gadget* child) const {return *p_ << child;}
75  auto& operator<<(GProp& child) const {return *p_ << child;}
76  auto& operator<<(GProp* child) const {return *p_ << child;}
77  auto& operator<<(class GPropList const& list) const {return *p_ << list;}
78 
80  template <typename C> auto& operator<<=(C& from) {return *p_ <<= from;}
81 
83  template <typename C> auto& operator>>(C& to) {return *p_ >> to;}
84 
85 protected:
86  template <class X> friend class gptr;
87  T* p_{};
88 };
89 
90 template<class T>
91 inline bool operator==(gptr<T> const & a, std::nullptr_t b) {
92  return a.get() == b;
93 }
94 
95 template<class T>
96 inline bool operator!=(gptr<T> const & a, std::nullptr_t b) {
97  return a.get() != b;
98 }
99 
100 template<class T, class U>
101 inline bool operator==(gptr<T> const & a, U const * b) {
102  return a.get() == b;
103 }
104 
105 template<class T, class U>
106 inline bool operator!=(gptr<T> const & a, U const * b) {
107  return a.get() != b;
108 }
109 
110 template<class T, class U>
111 inline bool operator==(gptr<T> const & a, U const & b) {
112  return a.get() == &b;
113 }
114 
115 template<class T, class U>
116 inline bool operator!=(gptr<T> const & a, U const & b) {
117  return a.get() != &b;
118 }
119 
120 template<class T, class U>
121 inline bool operator==(gptr<T> const & a, gptr<U> const & b) {
122  return a.get() == b.get();
123 }
124 
125 template<class T, class U>
126 inline bool operator!=(gptr<T> const & a, gptr<U> const & b) {
127  return a.get() != b.get();
128 }
129 
130 
131 
132 template <typename T> class gnew : public gptr<T> {
133 public:
134  gnew() : gptr<T>(new T) {}
135 
136  template <typename A>
137  gnew(A arg) : gptr<T>(new T(arg)) {}
138 };
139 
140 
141 
149 template <typename T> class gprotect : public gptr<T> {
150  void init(T* p) {this->p_ = p; gaddPtr(p,false);}
151 public:
152  gprotect() noexcept {}
153  gprotect(T* p) {init(p);}
154  gprotect(T& p) {init(&p);}
155  gprotect(gprotect const& b) {init(b.p_);}
156  gprotect(gprotect const&& b) {init(b.p_);}
157  template <typename B> gprotect(gprotect<B> const& b) {init(b.p_);}
158  template <typename B> gprotect(gprotect<B> const&& b) {init(b.p_);}
159 
160  gprotect& operator=(T* p) {reset(p); return *this;}
161  gprotect& operator=(T& p) {reset(&p); return *this;}
162  gprotect& operator=(gprotect<T> const& b) {reset(b.p_); return *this;}
163  gprotect& operator=(gprotect<T> const&& b) {reset(b.p_); return *this;}
164  template <class B> gprotect& operator=(B* p) {reset(p); return *this;}
165  template <class B> gprotect& operator=(gprotect<B> const& b) {reset(b.p_); return *this;}
166  template <class B> gprotect& operator=(gprotect<B> const&& b) {reset(b.p_); return *this;}
167 
168  void reset() {gprotect().swap(*this);}
169  void reset(T* b) {gprotect(b).swap(*this);}
170  void swap(gprotect& b) noexcept {T* tmp = this->p_; this->p_ = b.p_; b.p_ = tmp;}
171 };
172 
173 }
174 #endif