HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
handle.h
Go to the documentation of this file.
1 //
2 // Copyright 2020 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HGI_HANDLE_H
8 #define PXR_IMAGING_HGI_HANDLE_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hgi/api.h"
12 
13 #include <stdint.h>
14 
16 
17 /// \class HgiHandle
18 ///
19 /// Handle that contains a hgi object and unique id.
20 ///
21 /// The unique id is used to compare two handles to guard against pointer
22 /// aliasing, where the same memory address is used to create a similar object,
23 /// but it is not actually the same object.
24 ///
25 /// Handle is not a shared or weak_ptr and destruction of the contained object
26 /// should be explicitely managed by the client via the HgiDestroy*** functions.
27 ///
28 /// If shared/weak ptr functionality is desired, the client creating Hgi objects
29 /// can wrap the returned handle in a shared_ptr.
30 ///
31 template<class T>
32 class HgiHandle
33 {
34 public:
35  HgiHandle() : _ptr(nullptr), _id(0) {}
36  HgiHandle(T* obj, uint64_t id) : _ptr(obj), _id(id) {}
37 
38  T*
39  Get() const {
40  return _ptr;
41  }
42 
43  uint64_t GetId() const {
44  return _id;
45  }
46 
47  // Note this only checks if a ptr is set, it does not offer weak_ptr safety.
48  explicit operator bool() const {return _ptr!=nullptr;}
49 
50  // Pointer access operator
51  T* operator ->() const {return _ptr;}
52 
53  bool operator==(const HgiHandle& other) const {
54  return _id == other._id;
55  }
56 
57  bool operator!=(const HgiHandle& other) const {
58  return !(*this == other);
59  }
60 
61 private:
62  T* _ptr;
63  uint64_t _id;
64 };
65 
66 
68 
69 #endif
bool operator!=(const HgiHandle &other) const
Definition: handle.h:57
T * operator->() const
Definition: handle.h:51
OutGridT const XformOp bool bool
T * Get() const
Definition: handle.h:39
HgiHandle(T *obj, uint64_t id)
Definition: handle.h:36
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
uint64_t GetId() const
Definition: handle.h:43
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HgiHandle()
Definition: handle.h:35
bool operator==(const HgiHandle &other) const
Definition: handle.h:53