This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mapFunction.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_USD_PCP_MAP_FUNCTION_H
8#define PXR_USD_PCP_MAP_FUNCTION_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/pcp/api.h"
12#include "pxr/usd/sdf/path.h"
14#include "pxr/usd/sdf/pathExpression.h"
15
16#include <atomic>
17#include <memory>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
65{
66public:
68 typedef std::map<SdfPath, SdfPath, SdfPath::FastLessThan> PathMap;
69 typedef std::pair<SdfPath, SdfPath> PathPair;
70 typedef std::vector<PathPair> PathPairVector;
71
73 PcpMapFunction() = default;
74
81 PCP_API
82 static PcpMapFunction
83 Create(const PathMap &sourceToTargetMap,
84 const SdfLayerOffset &offset);
85
87 PCP_API
88 static const PcpMapFunction &Identity();
89
91 PCP_API
92 static const PathMap &IdentityPathMap();
93
95 PCP_API
96 void Swap(PcpMapFunction &map);
97 void swap(PcpMapFunction &map) { Swap(map); }
98
100 PCP_API
101 bool operator==(const PcpMapFunction &map) const;
102
104 PCP_API
105 bool operator!=(const PcpMapFunction &map) const;
106
109 PCP_API
110 bool IsNull() const;
111
114 PCP_API
115 bool IsIdentity() const;
116
120 PCP_API
122
125 bool HasRootIdentity() const { return _data.hasRootIdentity; }
126
129 PCP_API
131
134 PCP_API
136
150 PCP_API
153 const SdfPathExpression &pathExpr,
154 std::vector<SdfPathExpression::PathPattern>
155 *unmappedPatterns = nullptr,
156 std::vector<SdfPathExpression::ExpressionReference>
157 *unmappedRefs = nullptr
158 ) const;
159
173 PCP_API
176 const SdfPathExpression &pathExpr,
177 std::vector<SdfPathExpression::PathPattern>
178 *unmappedPatterns = nullptr,
179 std::vector<SdfPathExpression::ExpressionReference>
180 *unmappedRefs = nullptr
181 ) const;
182
186 PCP_API
188
192 PCP_API
194
198 PCP_API
200
202 PCP_API
204
206 const SdfLayerOffset &GetTimeOffset() const { return _offset; }
207
210 PCP_API
211 std::string GetString() const;
212
214 PCP_API
215 size_t Hash() const;
216
217private:
218
219 PCP_API
220 PcpMapFunction(PathPair const *sourceToTargetBegin,
221 PathPair const *sourceToTargetEnd,
222 SdfLayerOffset offset,
223 bool hasRootIdentity);
224
225 PCP_API
227 _MapPathExpressionImpl(
228 bool invert,
229 const SdfPathExpression &pathExpr,
230 std::vector<SdfPathExpression::PathPattern> *unmappedPatterns,
231 std::vector<SdfPathExpression::ExpressionReference> *unmappedRefs
232 ) const;
233
234private:
235 friend PcpMapFunction *Pcp_MakeIdentity();
236
237 static const int _MaxLocalPairs = 2;
238 struct _Data final {
239 _Data() {};
240
241 _Data(PathPair const *begin, PathPair const *end, bool hasRootIdentity)
242 : numPairs(end-begin)
243 , hasRootIdentity(hasRootIdentity) {
244 if (numPairs == 0)
245 return;
246 if (numPairs <= _MaxLocalPairs) {
247 std::uninitialized_copy(begin, end, localPairs);
248 }
249 else {
250 new (&remotePairs) std::shared_ptr<PathPair>(
251 new PathPair[numPairs], std::default_delete<PathPair[]>());
252 std::copy(begin, end, remotePairs.get());
253 }
254 }
255
256 _Data(_Data const &other)
257 : numPairs(other.numPairs)
258 , hasRootIdentity(other.hasRootIdentity) {
259 if (numPairs <= _MaxLocalPairs) {
260 std::uninitialized_copy(
261 other.localPairs,
262 other.localPairs + other.numPairs, localPairs);
263 }
264 else {
265 new (&remotePairs) std::shared_ptr<PathPair>(other.remotePairs);
266 }
267 }
268 _Data(_Data &&other)
269 : numPairs(other.numPairs)
270 , hasRootIdentity(other.hasRootIdentity) {
271 if (numPairs <= _MaxLocalPairs) {
272 PathPair *dst = localPairs;
273 PathPair *src = other.localPairs;
274 PathPair *srcEnd = other.localPairs + other.numPairs;
275 for (; src != srcEnd; ++src, ++dst) {
276 ::new (static_cast<void*>(std::addressof(*dst)))
277 PathPair(std::move(*src));
278 }
279 }
280 else {
281 new (&remotePairs)
282 std::shared_ptr<PathPair>(std::move(other.remotePairs));
283 }
284 }
285 _Data &operator=(_Data const &other) {
286 if (this != &other) {
287 this->~_Data();
288 new (this) _Data(other);
289 }
290 return *this;
291 }
292 _Data &operator=(_Data &&other) {
293 if (this != &other) {
294 this->~_Data();
295 new (this) _Data(std::move(other));
296 }
297 return *this;
298 }
299 ~_Data() {
300 if (numPairs <= _MaxLocalPairs) {
301 for (PathPair *p = localPairs; numPairs--; ++p) {
302 p->~PathPair();
303 }
304 }
305 else {
306 remotePairs.~shared_ptr<PathPair>();
307 }
308 }
309
310 bool IsNull() const {
311 return numPairs == 0 && !hasRootIdentity;
312 }
313
314 PathPair const *begin() const {
315 return numPairs <= _MaxLocalPairs ? localPairs : remotePairs.get();
316 }
317
318 PathPair const *end() const {
319 return begin() + numPairs;
320 }
321
322 bool operator==(_Data const &other) const {
323 return numPairs == other.numPairs &&
324 hasRootIdentity == other.hasRootIdentity &&
325 std::equal(begin(), end(), other.begin());
326 }
327
328 bool operator!=(_Data const &other) const {
329 return !(*this == other);
330 }
331
332 template <class HashState>
333 friend void TfHashAppend(HashState &h, _Data const &data){
334 h.Append(data.hasRootIdentity);
335 h.Append(data.numPairs);
336 h.AppendRange(std::begin(data), std::end(data));
337 }
338
339 union {
340 PathPair localPairs[_MaxLocalPairs > 0 ? _MaxLocalPairs : 1];
341 std::shared_ptr<PathPair> remotePairs;
342 };
343 typedef int PairCount;
344 PairCount numPairs = 0;
345 bool hasRootIdentity = false;
346 };
347
348 // Specialize TfHashAppend for PcpMapFunction.
349 template <typename HashState>
350 friend inline
351 void TfHashAppend(HashState& h, const PcpMapFunction& x){
352 h.Append(x._data);
353 h.Append(x._offset);
354 }
355
356 _Data _data;
357 SdfLayerOffset _offset;
358};
359
360// Specialize hash_value for PcpMapFunction.
361inline
362size_t hash_value(const PcpMapFunction& x)
363{
364 return TfHash{}(x);
365}
366
367PXR_NAMESPACE_CLOSE_SCOPE
368
369#endif // PXR_USD_PCP_MAP_FUNCTION_H
A function that maps values from one namespace (and time domain) to another.
Definition: mapFunction.h:65
static PCP_API PcpMapFunction Create(const PathMap &sourceToTargetMap, const SdfLayerOffset &offset)
Constructs a map function with the given arguments.
PCP_API void Swap(PcpMapFunction &map)
Swap the contents of this map function with map.
PCP_API SdfPathExpression MapTargetToSource(const SdfPathExpression &pathExpr, std::vector< SdfPathExpression::PathPattern > *unmappedPatterns=nullptr, std::vector< SdfPathExpression::ExpressionReference > *unmappedRefs=nullptr) const
Map all path pattern prefix paths and expression reference paths in the target namespace to the sourc...
PCP_API SdfPath MapSourceToTarget(const SdfPath &path) const
Map a path in the source namespace to the target.
bool HasRootIdentity() const
Return true if the map function maps the absolute root path to the absolute root path,...
Definition: mapFunction.h:125
PCP_API bool IsIdentity() const
Return true if the map function is the identity function.
PCP_API size_t Hash() const
Return a size_t hash for this map function.
PCP_API SdfPathExpression MapSourceToTarget(const SdfPathExpression &pathExpr, std::vector< SdfPathExpression::PathPattern > *unmappedPatterns=nullptr, std::vector< SdfPathExpression::ExpressionReference > *unmappedRefs=nullptr) const
Map all path pattern prefix paths and expression reference paths in the source namespace to the targe...
PCP_API std::string GetString() const
Returns a string representation of this mapping for debugging purposes.
static PCP_API const PathMap & IdentityPathMap()
Returns an identity path mapping.
PcpMapFunction()=default
Construct a null function.
PCP_API SdfPath MapTargetToSource(const SdfPath &path) const
Map a path in the target namespace to the source.
PCP_API bool operator!=(const PcpMapFunction &map) const
Inequality.
const SdfLayerOffset & GetTimeOffset() const
The time offset of the mapping.
Definition: mapFunction.h:206
std::map< SdfPath, SdfPath, SdfPath::FastLessThan > PathMap
A mapping from path to path.
Definition: mapFunction.h:68
static PCP_API const PcpMapFunction & Identity()
Construct an identity map function.
PCP_API PcpMapFunction ComposeOffset(const SdfLayerOffset &newOffset) const
Compose this map function over a hypothetical map function that has an identity path mapping and offs...
PCP_API PcpMapFunction GetInverse() const
Return the inverse of this map function.
PCP_API PcpMapFunction Compose(const PcpMapFunction &f) const
Compose this map over the given map function.
PCP_API bool IsIdentityPathMapping() const
Return true if the map function uses the identity path mapping.
PCP_API bool operator==(const PcpMapFunction &map) const
Equality.
PCP_API bool IsNull() const
Return true if this map function is the null function.
PCP_API PathMap GetSourceToTargetMap() const
The set of path mappings, from source to target.
Represents a time offset and scale between layers.
Definition: layerOffset.h:44
Objects of this class represent a logical expression syntax tree consisting of SdfPathPattern s,...
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:460
size_t hash_value(const half h)
Overload hash_value for half.
Definition: half.h:28