All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
shared.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 USD_SHARED_H
25 #define USD_SHARED_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/api.h"
29 
30 #include <boost/functional/hash.hpp>
31 #include <boost/smart_ptr/intrusive_ptr.hpp>
32 #include <atomic>
33 
34 PXR_NAMESPACE_OPEN_SCOPE
35 
36 
37 // Implementation storage + refcount for Usd_Shared.
38 template <class T>
39 struct Usd_Counted {
40  constexpr Usd_Counted() : count(0) {}
41  explicit Usd_Counted(T const &data) : data(data), count(0) {}
42  explicit Usd_Counted(T &&data) : data(std::move(data)), count(0) {}
43 
44  friend inline void
45  intrusive_ptr_add_ref(Usd_Counted const *c) {
46  c->count.fetch_add(1, std::memory_order_relaxed);
47  }
48  friend inline void
49  intrusive_ptr_release(Usd_Counted const *c) {
50  if (c->count.fetch_sub(1, std::memory_order_release) == 1) {
51  std::atomic_thread_fence(std::memory_order_acquire);
52  delete c;
53  }
54  }
55 
56  T data;
57  mutable std::atomic_int count;
58 };
59 
60 struct Usd_EmptySharedTagType {};
61 constexpr Usd_EmptySharedTagType Usd_EmptySharedTag{};
62 
63 // This class provides a simple way to share a data object between clients. It
64 // can be used to do simple copy-on-write, etc.
65 template <class T>
66 struct Usd_Shared
67 {
68  // Construct a Usd_Shared with a value-initialized T instance.
69  Usd_Shared() : _held(new Usd_Counted<T>()) {}
70  // Create a copy of \p obj.
71  explicit Usd_Shared(T const &obj) : _held(new Usd_Counted<T>(obj)) {}
72  // Move from \p obj.
73  explicit Usd_Shared(T &&obj) : _held(new Usd_Counted<T>(std::move(obj))) {}
74 
75  // Create an empty shared, which may not be accessed via Get(),
76  // GetMutable(), IsUnique(), Clone(), or MakeUnique(). This is useful when
77  // using the insert() or emplace() methods on associative containers, to
78  // avoid allocating a temporary in case the object is already present in the
79  // container.
80  Usd_Shared(Usd_EmptySharedTagType) {}
81 
82  // Return a const reference to the shared data.
83  T const &Get() const { return _held->data; }
84  // Return a mutable reference to the shared data.
85  T &GetMutable() const { return _held->data; }
86  // Return true if no other Usd_Shared instance shares this instance's data.
87  bool IsUnique() const { return _held->count == 1; }
88  // Make a new copy of the held data and refer to it.
89  void Clone() { _held.reset(new Usd_Counted<T>(Get())); }
90  // Ensure this Usd_Shared instance has unique data. Equivalent to:
91  // \code
92  // if (not shared.IsUnique()) { shared.Clone(); }
93  // \endcode
94  void MakeUnique() { if (!IsUnique()) Clone(); }
95 
96  // Equality and inequality.
97  bool operator==(Usd_Shared const &other) const {
98  return _held == other._held || _held->data == other._held->data;
99  }
100  bool operator!=(Usd_Shared const &other) const { return *this != other; }
101 
102  // Swap.
103  void swap(Usd_Shared &other) { _held.swap(other._held); }
104  friend inline void swap(Usd_Shared &l, Usd_Shared &r) { l.swap(r); }
105 
106  // hash_value.
107  friend inline size_t hash_value(Usd_Shared const &sh) {
108  using boost::hash_value;
109  return hash_value(sh._held->data);
110  }
111 private:
112  boost::intrusive_ptr<Usd_Counted<T>> _held;
113 };
114 
115 
116 PXR_NAMESPACE_CLOSE_SCOPE
117 
118 #endif // USD_SHARED_H
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.