All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
scoped.h
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
14PXR_NAMESPACE_OPEN_SCOPE
15
33template <typename T = std::function<void ()> >
34class TfScoped {
35 TfScoped(TfScoped const &) = delete;
36 TfScoped &operator=(TfScoped const &) = delete;
37public:
39 typedef T Procedure;
40
42 explicit TfScoped(const Procedure& leave) : _onExit(leave) { }
43
44 ~TfScoped() { _onExit(); }
45
46private:
47 // Can't put these on the heap. No implementation needed.
48 static void *operator new(::std::size_t size);
49
50private:
51 Procedure _onExit;
52};
53
54// Specialization of TfScoped for member functions.
55template <typename T>
56class TfScoped<void (T::*)()> {
57 TfScoped(TfScoped const &) = delete;
58 TfScoped &operator=(TfScoped const &) = delete;
59public:
61 typedef void (T::*Procedure)();
62
64 explicit TfScoped(T* obj, const Procedure& leave) :
65 _obj(obj), _onExit(leave) { }
66
67 ~TfScoped() { (_obj->*_onExit)(); }
68
69private:
70 // Can't put these on the heap. No implementation needed.
71 static void *operator new(::std::size_t size);
72
73private:
74 T* _obj;
75 Procedure _onExit;
76};
77
78// Specialization of TfScoped for functions taking one pointer argument.
79template <typename T>
80class TfScoped<void (*)(T*)> {
81 TfScoped(TfScoped const &) = delete;
82 TfScoped &operator=(TfScoped const &) = delete;
83public:
85 typedef void (*Procedure)(T*);
86
88 explicit TfScoped(const Procedure& leave, T* obj) :
89 _obj(obj), _onExit(leave) { }
90
91 ~TfScoped() { _onExit(_obj); }
92
93private:
94 // Can't put these on the heap. No implementation needed.
95 static void *operator new(::std::size_t size);
96
97private:
98 T* _obj;
99 Procedure _onExit;
100};
101
115template <typename T>
117 TfScopedVar(TfScopedVar const &) = delete;
118 TfScopedVar &operator=(TfScopedVar const &) = delete;
119public:
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
133private:
134 // Can't put these on the heap. No implementation needed.
135 static void *operator new(::std::size_t size);
136
137private:
138 T* _x;
139 T _old;
140};
141
164 TfScopedAutoVar(TfScopedAutoVar const &) = delete;
165 TfScopedAutoVar &operator=(TfScopedAutoVar const &) = delete;
166public:
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
178private:
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
189private:
190 TfScoped<> _scope;
191};
192
193PXR_NAMESPACE_CLOSE_SCOPE
194
195#endif // PXR_BASE_TF_SCOPED_H
Reset variable on exiting scope.
Definition: scoped.h:163
TfScopedAutoVar(T &x, const T &val)
Set/reset variable.
Definition: scoped.h:172
Execute code on exiting scope.
Definition: scoped.h:34
TfScoped(const Procedure &leave)
Execute leave when this object goes out of scope.
Definition: scoped.h:42
T Procedure
The type of the function executed on destruction.
Definition: scoped.h:39
Reset variable on exiting scope.
Definition: scoped.h:116
TfScopedVar(T &x, const T &val)
Set/reset variable.
Definition: scoped.h:124
STL namespace.