Nui
gobject.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 namespace Nui
4 {
5  namespace Detail
6  {
7  template <typename T>
8  T* referenceGObject(T* ptr)
9  {
10  if (ptr)
11  g_object_ref_sink(ptr);
12  return ptr;
13  }
14 
15  template <typename T>
16  void unreferenceGObject(T* ptr)
17  {
18  if (ptr)
19  g_object_unref(ptr);
20  }
21  }
22 
23  template <typename T>
25  {
26  public:
27  struct AdoptFlag
28  {};
29 
30  using value_type = T;
32 
34  : ptr_{nullptr}
35  {}
36 
38  : ptr_{ptr}
39  {
40  if (ptr_)
42  }
43 
45  : ptr_{other.ptr_}
46  {
47  if (ptr_)
49  }
50 
51  template <typename U>
53  : ptr_{other.get()}
54  {
55  if (ptr_)
57  }
58 
60  : ptr_{other.release()}
61  {}
62 
64  {
66  }
67 
68  void clear()
69  {
70  using std::swap;
71  T* ptr = nullptr;
72  swap(ptr_, ptr);
73  if (ptr)
74  derefGPtr(ptr);
75  }
76 
78  {
79  pointer_type ptr = nullptr;
80  using std::swap;
81  swap(ptr_, ptr);
82  return ptr;
83  }
84 
85  T* get() const
86  {
87  return ptr_;
88  }
89  T& operator*() const
90  {
91  return *ptr_;
92  }
93  T* operator->() const
94  {
95  return ptr_;
96  }
97 
98  explicit operator bool() const
99  {
100  return ptr_ != nullptr;
101  }
102  bool operator!() const
103  {
104  return ptr_ == nullptr;
105  }
106 
108  {
109  pointer_type optr = other.get();
110  if (optr)
112  pointer_type ptr = ptr_;
113  ptr_ = optr;
114  if (ptr)
116  return *this;
117  }
119  {
120  GObjectReference(std::move(other)).swap(*this);
121  return *this;
122  }
124  {
125  pointer_type ptr = ptr_;
126  if (optr)
128  ptr_ = optr;
129  if (ptr)
131  return *this;
132  }
133  template <typename U>
135  {
136  GObjectReference(other).swap(*this);
137  return *this;
138  }
139 
140  void swap(GObjectReference& other)
141  {
142  using std::swap;
143  swap(ptr_, other.ptr_);
144  }
146  {
147  return GObjectReference<T>(ptr, AdoptFlag{});
148  }
149 
150  private:
151  GObjectReference(pointer_type ptr, AdoptFlag)
152  : ptr_{ptr}
153  {}
154 
155  private:
156  T* ptr_;
157  };
158 
159  template <typename T>
161  {
162  lhs.swap(rhs);
163  }
164 
165  template <typename T, typename U>
167  {
168  return lhs.get() == rhs.get();
169  }
170  template <typename T, typename U>
171  bool operator==(GObjectReference<T> const& lhs, T* rhs)
172  {
173  return lhs.get() == rhs;
174  }
175  template <typename T, typename U>
176  bool operator==(T* lhs, GObjectReference<U> const& rhs)
177  {
178  return lhs == rhs.get();
179  }
180 
181  template <typename T, typename U>
183  {
184  return lhs.get() != rhs.get();
185  }
186  template <typename T, typename U>
187  bool operator!=(GObjectReference<T> const& lhs, T* rhs)
188  {
189  return lhs.get() != rhs;
190  }
191  template <typename T, typename U>
192  bool operator!=(T* lhs, GObjectReference<U> const& rhs)
193  {
194  return lhs != rhs.get();
195  }
196 
197  template <typename T, typename U>
199  {
200  return GObjectReference<T>(static_cast<T*>(ptr.get()));
201  }
202  template <typename T, typename U>
204  {
205  return GObjectReference<T>(dynamic_cast<T*>(ptr.get()));
206  }
207 }
Definition: gobject.hpp:25
value_type * pointer_type
Definition: gobject.hpp:31
void swap(GObjectReference &other)
Definition: gobject.hpp:140
GObjectReference & operator=(pointer_type optr)
Definition: gobject.hpp:123
T & operator*() const
Definition: gobject.hpp:89
GObjectReference & operator=(GObjectReference const &other)
Definition: gobject.hpp:107
static GObjectReference< T > adoptReference(T *ptr)
Definition: gobject.hpp:145
GObjectReference(GObjectReference &&other)
Definition: gobject.hpp:59
GObjectReference(GObjectReference const &other)
Definition: gobject.hpp:44
~GObjectReference()
Definition: gobject.hpp:63
GObjectReference(GObjectReference< U > const &other)
Definition: gobject.hpp:52
GObjectReference()
Definition: gobject.hpp:33
T * get() const
Definition: gobject.hpp:85
T * operator->() const
Definition: gobject.hpp:93
bool operator!() const
Definition: gobject.hpp:102
T value_type
Definition: gobject.hpp:30
GObjectReference(pointer_type ptr)
Definition: gobject.hpp:37
pointer_type release()
Definition: gobject.hpp:77
GObjectReference & operator=(GObjectReference &&other)
Definition: gobject.hpp:118
GObjectReference & operator=(GObjectReference< U > const &other)
Definition: gobject.hpp:134
void clear()
Definition: gobject.hpp:68
T * referenceGObject(T *ptr)
Definition: gobject.hpp:8
void unreferenceGObject(T *ptr)
Definition: gobject.hpp:16
Definition: file_dialog.hpp:6
void swap(GObjectReference< T > &lhs, GObjectReference< T > &rhs)
Definition: gobject.hpp:160
bool operator==(GObjectReference< T > const &lhs, GObjectReference< U > const &rhs)
Definition: gobject.hpp:166
GObjectReference< T > static_pointer_cast(GObjectReference< U > const &ptr)
Definition: gobject.hpp:198
bool operator!=(GObjectReference< T > const &lhs, GObjectReference< U > const &rhs)
Definition: gobject.hpp:182
GObjectReference< T > dynamic_pointer_cast(GObjectReference< U > const &ptr)
Definition: gobject.hpp:203
Definition: gobject.hpp:28