forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layerRegistry.h
152 lines (128 loc) · 6 KB
/
layerRegistry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_SDF_LAYER_REGISTRY_H
#define PXR_USD_SDF_LAYER_REGISTRY_H
/// \file sdf/layerRegistry.h
#include "pxr/pxr.h"
#include "pxr/usd/sdf/assetPathResolver.h"
#include "pxr/usd/sdf/declareHandles.h"
#include "pxr/base/tf/hash.h"
#include <string>
#include <unordered_map>
#include <iosfwd>
PXR_NAMESPACE_OPEN_SCOPE
SDF_DECLARE_HANDLES(SdfLayer);
/// \class Sdf_LayerRegistry
///
/// A class that provides functionality to look up layers by asset path that
/// are tracked by the registry. Currently, when a new SdfLayer is created, it
/// is inserted into the layer registry. This allows SdfLayer::Find/FindOrOpen
/// to locate loaded layers.
///
class Sdf_LayerRegistry
{
Sdf_LayerRegistry(const Sdf_LayerRegistry&) = delete;
Sdf_LayerRegistry& operator=(const Sdf_LayerRegistry&) = delete;
public:
/// Constructor.
Sdf_LayerRegistry();
/// Inserts layer into the registry
void Insert(const SdfLayerHandle& layer, const Sdf_AssetInfo& assetInfo);
/// Updates an existing registry entry if an entry is found for the same
/// layer.
void Update(const SdfLayerHandle& layer, const Sdf_AssetInfo& oldInfo,
const Sdf_AssetInfo& newInfo);
/// Erases the layer from the registry, if found.
void Erase(const SdfLayerHandle& layer, const Sdf_AssetInfo& assetInfo);
/// Returns a layer from the registry, searching first by identifier using
/// FindByIdentifier, then by real path using FindByRealPath. If the layer
/// cannot be found, a null layer handle is returned. If the \p layerPath
/// is relative, it is made absolute by anchoring to the current working
/// directory.
SdfLayerHandle Find(const std::string &layerPath,
const std::string &resolvedPath=std::string()) const;
/// Returns all valid layers held in the registry as a set.
SdfLayerHandleSet GetLayers() const;
private:
// Returns a layer from the registry, consulting the by_identifier index
// with the \p layerPath as provided.
SdfLayerHandle _FindByIdentifier(const std::string& layerPath) const;
// Returns a layer from the registry, consulting the by_repository_path
// index with the \p layerPath as provided.
SdfLayerHandle _FindByRepositoryPath(const std::string& layerPath) const;
// Returns a layer from the registry, consulting the by_real_path index. If
// \p layerPath is an absolute file system path, the index is searched using
// the input path. Otherwise, \p layerPath is resolved and the resulting
// path is used to search the index.
SdfLayerHandle _FindByRealPath(
const std::string& layerPath,
const std::string& resolvedPath=std::string()) const;
// A wrapper around a set of unordered_maps that maps layers
// bidirectionally to their various string representations (realPath,
// identifier, and repositoryPath)
class _Layers final {
public:
_Layers() = default;
using LayersByRealPath =
std::unordered_map<std::string, SdfLayerHandle, TfHash>;
using LayersByIdentifier =
std::unordered_multimap<std::string, SdfLayerHandle, TfHash>;
using LayersByRepositoryPath =
std::unordered_multimap<std::string, SdfLayerHandle, TfHash>;
const LayersByRealPath& ByRealPath() const { return _byRealPath; }
const LayersByIdentifier& ByIdentifier() const {
return _byIdentifier;
}
const LayersByRepositoryPath& ByRepositoryPath() const {
return _byRepositoryPath;
}
// Insert the layer.
// If the insertion is successful, return (the inserted layer, true)
// If insertion is unsuccessful, return the layer that's occupying one
// of the layer's entries and false.
std::pair<SdfLayerHandle, bool> Insert(const SdfLayerHandle& layer,
const Sdf_AssetInfo& assetInfo);
// Update all the aliases (realPath, identifier, repositoryPath)
// for this layer. The layer should already be stored in the container.
// If a layer already occupies the newAssetInfo realPath slot, the
// Update operation results in the layer being evicted from the
// registry, leaving a "dangling layer" outside of registry in user
// space. This is undesirable but matches legacy behavior.
void Update(const SdfLayerHandle& layer,
const Sdf_AssetInfo& oldAssetInfo,
const Sdf_AssetInfo& newAssetInfo);
// Remove this layer (and its aliases)
bool Erase(const SdfLayerHandle& layer,
const Sdf_AssetInfo& assetInfo);
private:
LayersByRealPath _byRealPath;
LayersByIdentifier _byIdentifier;
LayersByRepositoryPath _byRepositoryPath;
};
_Layers _layers;
};
std::ostream&
operator<<(std::ostream& ostr, const Sdf_LayerRegistry& registry);
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_SDF_LAYER_REGISTRY_H