HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.h
Go to the documentation of this file.
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_WORK_UTILS_H
25 #define PXR_BASE_WORK_UTILS_H
26 
27 ///\file work/utils.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/base/work/api.h"
32 
33 #include <utility>
34 
36 
39 
40 template <class T>
42  void operator()() const { /* do nothing */ }
43  T obj;
44 };
45 
46 // Helper for swap-based asynchronous destruction that synthesizes move
47 // construction and assignment using swap.
48 template <class T>
50  Work_AsyncSwapDestroyHelper() = default;
51 
54  Work_AsyncSwapDestroyHelper const&) = delete;
55 
57  : obj()
58  {
59  using std::swap;
60  swap(obj, other.obj);
61  }
62 
65  {
66  using std::swap;
67  swap(obj, other.obj);
68  return *this;
69  }
70 
71  void operator()() const { /* do nothing */ }
72  T obj;
73 };
74 
75 /// Swap \p obj with a default-constructed T instance, return and arrange
76 /// for the swapped-out instance to be destroyed asynchronously. This means
77 /// that any code that obj's destructor might invoke must be safe to run both
78 /// concurrently with other code and at any point in the future. This might not
79 /// be true, for example, if obj's destructor might try to update some other
80 /// data structure that could be destroyed by the time obj's destruction occurs.
81 /// Be careful.
82 template <class T>
84 {
85  using std::swap;
87  swap(helper.obj, obj);
89  WorkRunDetachedTask(std::move(helper));
90 }
91 
92 /// Like WorkSwapDestroyAsync() but instead, move from \p obj, leaving it
93 /// in a moved-from state instead of a default constructed state.
94 template <class T>
96 {
97  Work_AsyncMoveDestroyHelper<T> helper { std::move(obj) };
99  WorkRunDetachedTask(std::move(helper));
100 }
101 
103 
104 #endif // PXR_BASE_WORK_UTILS_H
void swap(ArAssetInfo &lhs, ArAssetInfo &rhs)
Definition: assetInfo.h:60
Work_AsyncSwapDestroyHelper & operator=(Work_AsyncSwapDestroyHelper const &)=delete
void operator()() const
Definition: utils.h:71
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
Definition: UT_ArraySet.h:1629
void operator()() const
Definition: utils.h:42
void swap(T &lhs, T &rhs)
Definition: pugixml.cpp:7172
GLhandleARB obj
Definition: glew.h:6236
#define WORK_API
Definition: api.h:40
Work_AsyncSwapDestroyHelper & operator=(Work_AsyncSwapDestroyHelper &&other)
Definition: utils.h:63
PXR_NAMESPACE_OPEN_SCOPE WORK_API bool Work_ShouldSynchronizeAsyncDestroyCalls()
void WorkRunDetachedTask(Fn &&fn)
Definition: detachedTask.h:59
Work_AsyncSwapDestroyHelper(Work_AsyncSwapDestroyHelper &&other)
Definition: utils.h:56
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
void WorkSwapDestroyAsync(T &obj)
Definition: utils.h:83
void WorkMoveDestroyAsync(T &obj)
Definition: utils.h:95