remotemono
RMonoHandle_Def.h
1 /*
2  Copyright 2020 David "Alemarius Nexus" Lerch
3 
4  This file is part of RemoteMono.
5 
6  RemoteMono is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  RemoteMono is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with RemoteMono. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include "../config.h"
23 
24 #include <cstddef>
25 #include "RMonoHandle_FwdDef.h"
26 #include "RMonoTypes.h"
27 
28 
29 
30 namespace remotemono
31 {
32 
33 
34 
35 
36 class RMonoAPIBase;
37 
38 
40 {
41 public:
42  virtual void forceDelete() = 0;
43 };
44 
45 
46 
47 
48 
49 inline void RMonoObjectHandleDelete(rmono_gchandle gchandle, RMonoAPIBase* mono);
50 
51 
52 
53 
54 struct RMonoHandleTag {};
55 
62 template <
63  typename HandleT,
64  void (*deleter)(HandleT, RMonoAPIBase*),
65  HandleT invalidHandle
66  >
68 {
69 public:
70  typedef HandleT HandleType;
72 
73 private:
74  struct Data : public RMonoHandleBackendBase
75  {
76  enum Flags
77  {
78  FlagOwned = 0x01
79  };
80 
81  Data(HandleT handle, RMonoAPIBase* mono, bool owned)
82  : handle(handle), mono(mono), flags(owned ? FlagOwned : 0)
83  {
84  if (owned) {
85  registerBackend();
86  }
87  }
88 
89  ~Data()
90  {
91  if (mono) {
92  if ((flags & FlagOwned) != 0) {
93  unregisterBackend();
94  deleter(handle, mono);
95  }
96  }
97  }
98 
99  inline void registerBackend();
100  inline void unregisterBackend();
101 
102  bool takeOwnership()
103  {
104  if ((flags & FlagOwned) != 0) {
105  unregisterBackend();
106  flags &= ~FlagOwned;
107  return true;
108  }
109  return false;
110  }
111 
112  virtual void forceDelete()
113  {
114  if ((flags & FlagOwned) != 0) {
115  deleter(handle, mono);
116  flags = 0;
117  mono = nullptr;
118  handle = invalidHandle;
119  }
120  }
121 
122  HandleT handle;
123  RMonoAPIBase* mono;
124  uint8_t flags;
125  typename std::list<RMonoHandleBackendBase*>::iterator regIt; // TODO: Find a way to get this type from RMonoAPIBase
126  };
127 
128 public:
133 
137  RMonoHandle(std::nullptr_t) {}
138 
147  RMonoHandle(HandleType handle, RMonoAPIBase* mono, bool owned)
148  : d(handle == invalidHandle ? std::shared_ptr<Data>() : std::make_shared<Data>(handle, mono, owned)) {}
149 
153  RMonoHandle(const Self& other) : d(other.d) {}
154 
158  RMonoHandle(Self&& other) : d(std::move(other.d)) {}
159 
160 
164  Self& operator=(const Self& other)
165  {
166  if (this != &other)
167  d = other.d;
168  return *this;
169  }
170 
174  Self& operator=(Self&& other)
175  {
176  if (this != &other)
177  d = std::move(other.d);
178  return *this;
179  }
180 
181 
185  bool operator==(const Self& other) const { return **this == *other; }
186 
190  bool operator!=(const Self& other) const { return !(*this == other); }
191 
192 
196  HandleType operator*() const { return d ? d->handle : invalidHandle; }
197 
201  RMonoAPIBase* getMonoAPI() { return d ? d->mono : nullptr; }
202 
203 
211  {
212  return d ? d->takeOwnership() : false;
213  }
214 
215 
219  void reset() { d.reset(); }
220 
221 
226  bool isValid() const { return (bool) d; }
227 
231  bool isNull() const { return !isValid(); }
232 
236  operator bool() const { return isValid(); }
237 
238 protected:
239  std::shared_ptr<Data> d;
240 };
241 
242 
243 
244 
245 
247 
273 template <typename RawPtrT>
276  public RMonoObjectHandleTag
277 {
278 public:
281 
282  typedef RawPtrT RawPtr;
283 
284 public:
289 
293  RMonoObjectHandle(std::nullptr_t) : RMonoHandle(nullptr) {}
294 
303  RMonoObjectHandle(rmono_gchandle gchandle, RMonoAPIBase* mono, bool owned = true) : RMonoHandle(gchandle, mono, owned) {}
304 
308  RMonoObjectHandle(const Self& other) : RMonoHandle(other) {}
309 
313  RMonoObjectHandle(Self&& other) : RMonoHandle(std::move(other)) {}
314 
315 
319  Self& operator=(const Self& other) { Base::operator=(other); return *this; }
320 
324  Self& operator=(Self&& other) { Base::operator=(std::move(other)); return *this; }
325 
326 
332  inline bool operator==(const Self& other) const;
333 
339  bool operator!=(const Self& other) const { return !(*this == other); }
340 
341 
345  inline Self pin() const;
346 
351  inline Self clone() const;
352 
357  inline RawPtr raw() const;
358 };
359 
360 
361 
362 }
363 
364 
365 
366 
367 namespace std
368 {
369 
370 
371 template <
372  typename HandleT,
373  void (*deleter)(HandleT, remotemono::RMonoAPIBase*),
374  HandleT invalidHandle
375 >
377 {
379 
380  std::size_t operator()(HandleT const& h) const noexcept
381  {
382  return hh(*h);
383  }
384 
385 private:
386  std::hash<typename HandleT::HandleType> hh;
387 };
388 
389 
390 }
remotemono::RMonoHandle::getMonoAPI
RMonoAPIBase * getMonoAPI()
Definition: RMonoHandle_Def.h:201
remotemono::RMonoHandle::operator=
Self & operator=(const Self &other)
Definition: RMonoHandle_Def.h:164
remotemono::RMonoObjectHandle::RMonoObjectHandle
RMonoObjectHandle()
Definition: RMonoHandle_Def.h:288
remotemono::RMonoHandle::RMonoHandle
RMonoHandle(const Self &other)
Definition: RMonoHandle_Def.h:153
remotemono::RMonoAPIBase
Definition: RMonoAPIBase_Def.h:43
remotemono::RMonoHandle::operator==
bool operator==(const Self &other) const
Definition: RMonoHandle_Def.h:185
remotemono::RMonoHandle::isValid
bool isValid() const
Definition: RMonoHandle_Def.h:226
remotemono::RMonoObjectHandle
Definition: RMonoHandle_Def.h:277
remotemono::RMonoObjectHandle::operator=
Self & operator=(Self &&other)
Definition: RMonoHandle_Def.h:324
remotemono::RMonoHandle::RMonoHandle
RMonoHandle(std::nullptr_t)
Definition: RMonoHandle_Def.h:137
remotemono::RMonoHandle::operator=
Self & operator=(Self &&other)
Definition: RMonoHandle_Def.h:174
remotemono::RMonoObjectHandle::RMonoObjectHandle
RMonoObjectHandle(const Self &other)
Definition: RMonoHandle_Def.h:308
remotemono::RMonoHandle::operator!=
bool operator!=(const Self &other) const
Definition: RMonoHandle_Def.h:190
remotemono::RMonoHandle::operator*
HandleType operator*() const
Definition: RMonoHandle_Def.h:196
remotemono::RMonoHandle::isNull
bool isNull() const
Definition: RMonoHandle_Def.h:231
remotemono::RMonoObjectHandle::RMonoObjectHandle
RMonoObjectHandle(rmono_gchandle gchandle, RMonoAPIBase *mono, bool owned=true)
Definition: RMonoHandle_Def.h:303
remotemono::RMonoObjectHandle::operator=
Self & operator=(const Self &other)
Definition: RMonoHandle_Def.h:319
remotemono::RMonoHandle::reset
void reset()
Definition: RMonoHandle_Def.h:219
remotemono::RMonoHandle::RMonoHandle
RMonoHandle(HandleType handle, RMonoAPIBase *mono, bool owned)
Definition: RMonoHandle_Def.h:147
remotemono::RMonoObjectHandle::RMonoObjectHandle
RMonoObjectHandle(Self &&other)
Definition: RMonoHandle_Def.h:313
remotemono::RMonoHandle::RMonoHandle
RMonoHandle(Self &&other)
Definition: RMonoHandle_Def.h:158
remotemono::RMonoHandle::takeOwnership
bool takeOwnership()
Definition: RMonoHandle_Def.h:210
remotemono::RMonoHandleBackendBase
Definition: RMonoHandle_Def.h:40
remotemono::RMonoObjectHandle::RMonoObjectHandle
RMonoObjectHandle(std::nullptr_t)
Definition: RMonoHandle_Def.h:293
remotemono::RMonoObjectHandleTag
Definition: RMonoHandle_Def.h:246
remotemono::RMonoHandleTag
Definition: RMonoHandle_Def.h:54
remotemono::RMonoHandle::RMonoHandle
RMonoHandle()
Definition: RMonoHandle_Def.h:132
remotemono::RMonoObjectHandle::operator!=
bool operator!=(const Self &other) const
Definition: RMonoHandle_Def.h:339
remotemono::RMonoHandle
Definition: RMonoHandle_Def.h:68