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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_WORK_UTILS_H
8 #define PXR_BASE_WORK_UTILS_H
9 
10 ///\file work/utils.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/base/work/api.h"
15 
16 #include <utility>
17 
19 
22 
23 template <class T>
25  void operator()() const { /* do nothing */ }
26  T obj;
27 };
28 
29 // Helper for swap-based asynchronous destruction that synthesizes move
30 // construction and assignment using swap.
31 template <class T>
33  Work_AsyncSwapDestroyHelper() = default;
34 
37  Work_AsyncSwapDestroyHelper const&) = delete;
38 
40  : obj()
41  {
42  using std::swap;
43  swap(obj, other.obj);
44  }
45 
48  {
49  using std::swap;
50  swap(obj, other.obj);
51  return *this;
52  }
53 
54  void operator()() const { /* do nothing */ }
55  T obj;
56 };
57 
58 /// Swap \p obj with a default-constructed T instance, return and arrange
59 /// for the swapped-out instance to be destroyed asynchronously. This means
60 /// that any code that obj's destructor might invoke must be safe to run both
61 /// concurrently with other code and at any point in the future. This might not
62 /// be true, for example, if obj's destructor might try to update some other
63 /// data structure that could be destroyed by the time obj's destruction occurs.
64 /// Be careful.
65 template <class T>
67 {
68  using std::swap;
70  swap(helper.obj, obj);
72  WorkRunDetachedTask(std::move(helper));
73 }
74 
75 /// Like WorkSwapDestroyAsync() but instead, move from \p obj, leaving it
76 /// in a moved-from state instead of a default constructed state.
77 template <class T>
79 {
80  Work_AsyncMoveDestroyHelper<T> helper { std::move(obj) };
82  WorkRunDetachedTask(std::move(helper));
83 }
84 
86 
87 #endif // PXR_BASE_WORK_UTILS_H
void swap(ArAssetInfo &lhs, ArAssetInfo &rhs)
Definition: assetInfo.h:57
Work_AsyncSwapDestroyHelper & operator=(Work_AsyncSwapDestroyHelper const &)=delete
void operator()() const
Definition: utils.h:54
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:1699
void operator()() const
Definition: utils.h:25
void swap(T &lhs, T &rhs)
Definition: pugixml.cpp:7440
#define WORK_API
Definition: api.h:23
Work_AsyncSwapDestroyHelper & operator=(Work_AsyncSwapDestroyHelper &&other)
Definition: utils.h:46
PXR_NAMESPACE_OPEN_SCOPE WORK_API bool Work_ShouldSynchronizeAsyncDestroyCalls()
void WorkRunDetachedTask(Fn &&fn)
Definition: detachedTask.h:45
Work_AsyncSwapDestroyHelper(Work_AsyncSwapDestroyHelper &&other)
Definition: utils.h:39
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void WorkSwapDestroyAsync(T &obj)
Definition: utils.h:66
void WorkMoveDestroyAsync(T &obj)
Definition: utils.h:78