HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scoped.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_BASE_TF_SCOPED_H
8 #define PXR_BASE_TF_SCOPED_H
9 
10 #include "pxr/pxr.h"
11 
12 #include <functional>
13 
15 
16 /// \class TfScoped
17 /// \ingroup group_tf_Multithreading
18 ///
19 /// Execute code on exiting scope.
20 ///
21 /// A \c TfScoped executes code when destroyed. It's useful when cleanup code
22 /// should be executed when exiting the scope because it gets executed no
23 /// matter how the scope is exited (e.g. normal execution, return, exceptions,
24 /// etc).
25 ///
26 /// \code
27 /// int func(bool x) {
28 /// TfScoped scope(cleanup);
29 /// return func2(x); // call cleanup after calling func2
30 /// }
31 /// \endcode
32 ///
33 template <typename T = std::function<void ()> >
34 class TfScoped {
35  TfScoped(TfScoped const &) = delete;
36  TfScoped &operator=(TfScoped const &) = delete;
37 public:
38  /// The type of the function executed on destruction.
39  typedef T Procedure;
40 
41  /// Execute \p leave when this object goes out of scope.
42  explicit TfScoped(const Procedure& leave) : _onExit(leave) { }
43 
44  ~TfScoped() { _onExit(); }
45 
46 private:
47  // Can't put these on the heap. No implementation needed.
48  static void *operator new(::std::size_t size);
49 
50 private:
51  Procedure _onExit;
52 };
53 
54 // Specialization of TfScoped for member functions.
55 template <typename T>
56 class TfScoped<void (T::*)()> {
57  TfScoped(TfScoped const &) = delete;
58  TfScoped &operator=(TfScoped const &) = delete;
59 public:
60  /// The type of the function executed on destruction.
61  typedef void (T::*Procedure)();
62 
63  /// Execute \p leave on \p obj when this object goes out of scope.
64  explicit TfScoped(T* obj, const Procedure& leave) :
65  _obj(obj), _onExit(leave) { }
66 
67  ~TfScoped() { (_obj->*_onExit)(); }
68 
69 private:
70  // Can't put these on the heap. No implementation needed.
71  static void *operator new(::std::size_t size);
72 
73 private:
74  T* _obj;
75  Procedure _onExit;
76 };
77 
78 // Specialization of TfScoped for functions taking one pointer argument.
79 template <typename T>
80 class TfScoped<void (*)(T*)> {
81  TfScoped(TfScoped const &) = delete;
82  TfScoped &operator=(TfScoped const &) = delete;
83 public:
84  /// The type of the function executed on destruction.
85  typedef void (*Procedure)(T*);
86 
87  /// Execute \p leave, passing \p obj, when this object goes out of scope.
88  explicit TfScoped(const Procedure& leave, T* obj) :
89  _obj(obj), _onExit(leave) { }
90 
91  ~TfScoped() { _onExit(_obj); }
92 
93 private:
94  // Can't put these on the heap. No implementation needed.
95  static void *operator new(::std::size_t size);
96 
97 private:
98  T* _obj;
99  Procedure _onExit;
100 };
101 
102 /// \class TfScopedVar
103 ///
104 /// Reset variable on exiting scope.
105 ///
106 /// A \c TfScopedVar sets a variable to a value when created then restores its
107 /// original value when destroyed. For example:
108 ///
109 /// \code
110 /// int func(bool x) {
111 /// TfScopedVar<bool> scope(x, true); // set x to true
112 /// return func2(x); // restore x after calling func2
113 /// }
114 /// \endcode
115 template <typename T>
116 class TfScopedVar {
117  TfScopedVar(TfScopedVar const &) = delete;
118  TfScopedVar &operator=(TfScopedVar const &) = delete;
119 public:
120  /// Set/reset variable
121  ///
122  /// Sets \p x to \p val immediately and restores its old value when this
123  /// goes out of scope.
124  explicit TfScopedVar(T& x, const T& val) :
125  _x(&x),
126  _old(x)
127  {
128  x = val;
129  }
130 
131  ~TfScopedVar() { *_x = _old; }
132 
133 private:
134  // Can't put these on the heap. No implementation needed.
135  static void *operator new(::std::size_t size);
136 
137 private:
138  T* _x;
139  T _old;
140 };
141 
142 /// \class TfScopedAutoVar
143 ///
144 /// Reset variable on exiting scope.
145 ///
146 /// A \c TfScopedAutoVar sets a variable to a value when created then restores
147 /// its original value when destroyed.
148 ///
149 /// For example:
150 /// \code
151 /// int func(bool x) {
152 /// TfScopedAutoVar scope(x, true); // set x to true
153 /// return func2(x); // restore x after calling func2
154 /// }
155 /// \endcode
156 ///
157 /// This differs from \c TfScopedVar in that it's not a template class, the
158 /// value type is deduced automatically and it allocates memory on the heap.
159 /// If performance is critical or memory must not be allocated then use \c
160 /// TfScopedVar instead.
161 ///
162 /// \see TfScopedVar
164  TfScopedAutoVar(TfScopedAutoVar const &) = delete;
165  TfScopedAutoVar &operator=(TfScopedAutoVar const &) = delete;
166 public:
167  /// Set/reset variable
168  ///
169  /// Sets \p x to \p val immediately and restores its old value when this
170  /// goes out of scope.
171  template <typename T>
172  explicit TfScopedAutoVar(T& x, const T& val) :
173  _scope(std::bind(&TfScopedAutoVar::_Set<T>, &x, x))
174  {
175  x = val;
176  }
177 
178 private:
179  // Restore value function
180  template <typename T>
181  static void _Set(T* x, const T& val)
182  {
183  *x = val;
184  }
185 
186  // Can't put these on the heap. No implementation needed.
187  static void *operator new(::std::size_t size);
188 
189 private:
190  TfScoped<> _scope;
191 };
192 
194 
195 #endif // PXR_BASE_TF_SCOPED_H
~TfScopedVar()
Definition: scoped.h:131
void
Definition: png.h:1083
~TfScoped()
Definition: scoped.h:44
TfScoped(const Procedure &leave, T *obj)
Execute leave, passing obj, when this object goes out of scope.
Definition: scoped.h:88
TfScoped(T *obj, const Procedure &leave)
Execute leave on obj when this object goes out of scope.
Definition: scoped.h:64
GLint GLenum GLint x
Definition: glcorearb.h:409
T Procedure
The type of the function executed on destruction.
Definition: scoped.h:39
GLsizeiptr size
Definition: glcorearb.h:664
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
LeafData & operator=(const LeafData &)=delete
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
TfScopedVar(T &x, const T &val)
Definition: scoped.h:124
TfScoped(const Procedure &leave)
Execute leave when this object goes out of scope.
Definition: scoped.h:42
TfScopedAutoVar(T &x, const T &val)
Definition: scoped.h:172