Loading...
Searching...
No Matches
scoped.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_BASE_TF_SCOPED_H
25#define PXR_BASE_TF_SCOPED_H
26
27#include "pxr/pxr.h"
28
29#include <functional>
30
31PXR_NAMESPACE_OPEN_SCOPE
32
50template <typename T = std::function<void ()> >
51class TfScoped {
52 TfScoped(TfScoped const &) = delete;
53 TfScoped &operator=(TfScoped const &) = delete;
54public:
56 typedef T Procedure;
57
59 explicit TfScoped(const Procedure& leave) : _onExit(leave) { }
60
61 ~TfScoped() { _onExit(); }
62
63private:
64 // Can't put these on the heap. No implementation needed.
65 static void *operator new(::std::size_t size);
66
67private:
68 Procedure _onExit;
69};
70
71// Specialization of TfScoped for member functions.
72template <typename T>
73class TfScoped<void (T::*)()> {
74 TfScoped(TfScoped const &) = delete;
75 TfScoped &operator=(TfScoped const &) = delete;
76public:
78 typedef void (T::*Procedure)();
79
81 explicit TfScoped(T* obj, const Procedure& leave) :
82 _obj(obj), _onExit(leave) { }
83
84 ~TfScoped() { (_obj->*_onExit)(); }
85
86private:
87 // Can't put these on the heap. No implementation needed.
88 static void *operator new(::std::size_t size);
89
90private:
91 T* _obj;
92 Procedure _onExit;
93};
94
95// Specialization of TfScoped for functions taking one pointer argument.
96template <typename T>
97class TfScoped<void (*)(T*)> {
98 TfScoped(TfScoped const &) = delete;
99 TfScoped &operator=(TfScoped const &) = delete;
100public:
102 typedef void (*Procedure)(T*);
103
105 explicit TfScoped(const Procedure& leave, T* obj) :
106 _obj(obj), _onExit(leave) { }
107
108 ~TfScoped() { _onExit(_obj); }
109
110private:
111 // Can't put these on the heap. No implementation needed.
112 static void *operator new(::std::size_t size);
113
114private:
115 T* _obj;
116 Procedure _onExit;
117};
118
132template <typename T>
134 TfScopedVar(TfScopedVar const &) = delete;
135 TfScopedVar &operator=(TfScopedVar const &) = delete;
136public:
141 explicit TfScopedVar(T& x, const T& val) :
142 _x(&x),
143 _old(x)
144 {
145 x = val;
146 }
147
148 ~TfScopedVar() { *_x = _old; }
149
150private:
151 // Can't put these on the heap. No implementation needed.
152 static void *operator new(::std::size_t size);
153
154private:
155 T* _x;
156 T _old;
157};
158
181 TfScopedAutoVar(TfScopedAutoVar const &) = delete;
182 TfScopedAutoVar &operator=(TfScopedAutoVar const &) = delete;
183public:
188 template <typename T>
189 explicit TfScopedAutoVar(T& x, const T& val) :
190 _scope(std::bind(&TfScopedAutoVar::_Set<T>, &x, x))
191 {
192 x = val;
193 }
194
195private:
196 // Restore value function
197 template <typename T>
198 static void _Set(T* x, const T& val)
199 {
200 *x = val;
201 }
202
203 // Can't put these on the heap. No implementation needed.
204 static void *operator new(::std::size_t size);
205
206private:
207 TfScoped<> _scope;
208};
209
210PXR_NAMESPACE_CLOSE_SCOPE
211
212#endif // PXR_BASE_TF_SCOPED_H
Reset variable on exiting scope.
Definition: scoped.h:180
TfScopedAutoVar(T &x, const T &val)
Set/reset variable.
Definition: scoped.h:189
Execute code on exiting scope.
Definition: scoped.h:51
TfScoped(const Procedure &leave)
Execute leave when this object goes out of scope.
Definition: scoped.h:59
T Procedure
The type of the function executed on destruction.
Definition: scoped.h:56
Reset variable on exiting scope.
Definition: scoped.h:133
TfScopedVar(T &x, const T &val)
Set/reset variable.
Definition: scoped.h:141
STL namespace.