HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_LockedRawPtr.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_LockedRawPtr.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_LOCKEDRAWPTR_H__
12 #define __UT_LOCKEDRAWPTR_H__
13 
14 #include "UT_API.h"
15 #include "UT_Assert.h"
16 #include "UT_UniquePtr.h"
17 #include <SYS/SYS_Compiler.h>
18 
19 
20 /// A template class that allows auto-locking in a getter method. This class
21 /// has pointer semantics and does NOT deal with ownership by design. The
22 /// pointer that you give it *MUST* live longer than the lifetime of this
23 /// class.
24 ///
25 /// EXAMPLE:
26 /// @code
27 /// class B;
28 /// class A
29 /// {
30 /// /// Obtain a ptr to myB that is locked. When it goes out
31 /// /// of scope, it will automatically unlock.
32 /// UT_LockedRawPtr<B,UT_Lock> getLockedB()
33 /// { return UT_LockedRawPtr<B>(myB, myBLock); }
34 /// private:
35 /// B myB;
36 /// UT_Lock myBLock;
37 /// };
38 /// @endcode
39 template <typename T, typename LOCKABLE>
41 {
42 public:
43 
44  typedef typename LOCKABLE::Scope LockScopeT;
45  typedef T ElementT;
46 
48  : myPtr(nullptr)
49  , myLockScope(nullptr)
50  {
51  }
52 
53  /// Construct a locked ptr instance
54  UT_LockedRawPtr(T &ref, LOCKABLE &lock)
55  : myPtr(&ref)
56  , myLockScope(new LockScopeT(lock))
57  {
58  }
59 
60  /// This class is only movable
61  /// @{
63  : myPtr(std::move(other.myPtr))
64  , myLockScope(std::move(other.myLockScope))
65  {
66  }
69  {
70  myPtr = std::move(other.myPtr);
71  myLockScope = std::move(other.myLockScope);
72  return *this;
73  }
74  /// @}
75 
76  /// Pointer semantic accessors
77  /// @{
78  T & operator*() const { UT_ASSERT(myPtr); return *myPtr; }
79  T * operator->() const { UT_ASSERT(myPtr); return myPtr; }
80  SYS_SAFE_BOOL operator bool() const { return (myPtr != 0); }
81  /// @}
82 
83 private:
84  UT_LockedRawPtr(const UT_LockedRawPtr &other);
86 
87  T * myPtr;
88  mutable UT_UniquePtr<LockScopeT> myLockScope;
89 };
90 
91 #endif // __UT_LOCKEDRAWPTR_H__
UT_LockedRawPtr(T &ref, LOCKABLE &lock)
Construct a locked ptr instance.
GLint ref
Definition: glcorearb.h:123
UT_LockedRawPtr & operator=(UT_LockedRawPtr &&other)
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
#define SYS_SAFE_BOOL
Definition: SYS_Compiler.h:56
UT_LockedRawPtr(UT_LockedRawPtr &&other)
T * operator->() const
T & operator*() const
LOCKABLE::Scope LockScopeT