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
registry.h
Go to the documentation of this file.
1//
2// Copyright 2018 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7
8#ifndef PXR_USD_NDR_REGISTRY_H
9#define PXR_USD_NDR_REGISTRY_H
10
12
13#include "pxr/pxr.h"
14#include "pxr/usd/ndr/api.h"
16#include "pxr/usd/ndr/declare.h"
17#include "pxr/usd/ndr/discoveryPlugin.h"
18#include "pxr/usd/ndr/node.h"
19#include "pxr/usd/ndr/nodeDiscoveryResult.h"
22#include <map>
23#include <mutex>
24
25PXR_NAMESPACE_OPEN_SCOPE
26
47class NdrRegistry : public TfWeakBase
48{
49public:
50 using DiscoveryPluginRefPtrVec = NdrDiscoveryPluginRefPtrVector;
51
59 NDR_API
60 void SetExtraDiscoveryPlugins(DiscoveryPluginRefPtrVec plugins);
61
69 NDR_API
70 void SetExtraDiscoveryPlugins(const std::vector<TfType>& pluginTypes);
71
79 NDR_API
81
86 NDR_API
87 void AddDiscoveryResult(const NdrNodeDiscoveryResult& discoveryResult);
88
95 NDR_API
96 void SetExtraParserPlugins(const std::vector<TfType>& pluginTypes);
97
120 NDR_API
121 NdrNodeConstPtr GetNodeFromAsset(const SdfAssetPath &asset,
122 const NdrTokenMap &metadata,
123 const TfToken &subIdentifier=TfToken(),
124 const TfToken &sourceType=TfToken());
125
141 NDR_API
142 NdrNodeConstPtr GetNodeFromSourceCode(const std::string &sourceCode,
143 const TfToken &sourceType,
144 const NdrTokenMap &metadata);
145
150 NDR_API
151 NdrStringVec GetSearchURIs() const;
152
160 NDR_API
161 NdrIdentifierVec
163 NdrVersionFilter filter =
164 NdrVersionFilterDefaultOnly) const;
165
172 NDR_API
173 NdrStringVec GetNodeNames(const TfToken& family = TfToken()) const;
174
195 NDR_API
196 NdrNodeConstPtr GetNodeByIdentifier(const NdrIdentifier& identifier,
197 const NdrTokenVec& sourceTypePriority = NdrTokenVec());
198
201 NDR_API
202 NdrNodeConstPtr GetNodeByIdentifierAndType(const NdrIdentifier& identifier,
203 const TfToken& sourceType);
204
214 NDR_API
215 NdrNodeConstPtr GetNodeByName(const std::string& name,
216 const NdrTokenVec& sourceTypePriority = NdrTokenVec(),
217 NdrVersionFilter filter = NdrVersionFilterDefaultOnly);
218
227 NDR_API
228 NdrNodeConstPtr GetNodeByNameAndType(const std::string& name,
229 const TfToken& sourceType,
230 NdrVersionFilter filter =
231 NdrVersionFilterDefaultOnly);
232
236 NDR_API
237 NdrNodeConstPtrVec GetNodesByIdentifier(const NdrIdentifier& identifier);
238
243 NDR_API
244 NdrNodeConstPtrVec GetNodesByName(const std::string& name,
245 NdrVersionFilter filter =
246 NdrVersionFilterDefaultOnly);
247
254 NDR_API
255 NdrNodeConstPtrVec GetNodesByFamily(const TfToken& family = TfToken(),
256 NdrVersionFilter filter =
257 NdrVersionFilterDefaultOnly);
258
270 NDR_API
271 NdrTokenVec GetAllNodeSourceTypes() const;
272
273protected:
274 NdrRegistry(const NdrRegistry&) = delete;
275 NdrRegistry& operator=(const NdrRegistry&) = delete;
276
277 NDR_API
278 NdrRegistry();
279
280 NDR_API
281 ~NdrRegistry();
282
283private:
284 class _DiscoveryContext;
285 friend class _DiscoveryContext;
286
287 using _TypeToParserPluginMap =
288 std::unordered_map<TfToken, NdrParserPlugin*, TfToken::HashFunctor>;
289
290 // Node cache data structure, stored NdrNodes keyed by identifier and source
291 // type.
292 using _NodeMapKey = std::pair<NdrIdentifier, TfToken>;
293 using _NodeMap = std::unordered_map<_NodeMapKey, NdrNodeUniquePtr, TfHash>;
294
295 // Discovery results data structure, NdrNodeDiscoveryResults multimap keyed
296 // by identifier
297 using _DiscoveryResultsByIdentifier = std::unordered_multimap<
299 using _DiscoveryResultsByIdentifierRange =
300 std::pair<_DiscoveryResultsByIdentifier::const_iterator,
301 _DiscoveryResultsByIdentifier::const_iterator>;
302
303 // Discovery results data structure: a multimap of raw pointers to
304 // NdrNodeDiscoveryResults (i.e. pointers to the discovery results stored
305 // in a _DiscoveryResultsByIdentifier) keyed by name.
306 using _DiscoveryResultPtrsByName = std::unordered_multimap<
307 std::string, const NdrNodeDiscoveryResult *, TfHash>;
308 using _DiscoveryResultPtrsByNameRange =
309 std::pair<_DiscoveryResultPtrsByName::const_iterator,
310 _DiscoveryResultPtrsByName::const_iterator>;
311
312 // The discovery result data structures are not concurrent and must be kept
313 // in sync, thus they need some locking infrastructure.
314 mutable std::mutex _discoveryResultMutex;
315
316 // The node map is not a concurrent data structure, thus it needs some
317 // locking infrastructure.
318 mutable std::mutex _nodeMapMutex;
319
320 // Runs each discovery plugin provided and adds the results to the
321 // internal discovery result maps
322 void _RunDiscoveryPlugins(const DiscoveryPluginRefPtrVec& discoveryPlugins);
323
324 // Takes the discovery and puts in the maps that hold the discovery results,
325 // keeping them in sync.
326 void _AddDiscoveryResultNoLock(NdrNodeDiscoveryResult&& dr);
327
328 // Finds and instantiates the discovery plugins
329 void _FindAndInstantiateDiscoveryPlugins();
330
331 // Finds and instantiates the parser plugins
332 void _FindAndInstantiateParserPlugins();
333
334 // Instantiates the specified parser plugins and adds them to
335 // the registry.
336 void _InstantiateParserPlugins(const std::set<TfType>& parserPluginTypes);
337
338 // Parses the node for the discovery result if adding it to the node map if
339 // able and adds the discovery result to the discovery result maps. Intended
340 // for the GetNodeFromAsset and GetNodeFromSourceCode APIs which can add
341 // nodes that don't already appear in the discovery results.
342 NdrNodeConstPtr _ParseNodeFromAssetOrSourceCode(
344
345 // Implementation helper for getting the first node of the given sourceType
346 // in the range of node discovery results for a paricular identifier.
347 NdrNodeConstPtr _GetNodeInIdentifierRangeWithSourceType(
348 _DiscoveryResultsByIdentifierRange range, const TfToken& sourceType);
349
350 // Implementation helper for getting the first node of the given sourceType
351 // and matching the given version filter in the range of node discovery
352 // results for a paricular name.
353 NdrNodeConstPtr _GetNodeInNameRangeWithSourceType(
354 _DiscoveryResultPtrsByNameRange range, const TfToken& sourceType,
355 NdrVersionFilter filter);
356
357 // Thread-safe find of a node in the cache by key.
358 NdrNodeConstPtr _FindNodeInCache(const _NodeMapKey &key) const;
359
360 // Thread-safe insertion of a node into the cache with a given key. If a
361 // node with the same key already exists in the cache, the pointer to the
362 // existing node will be returned, otherwise the pointer to pointer to the
363 // inserted node is returned.
364 NdrNodeConstPtr _InsertNodeInCache(
365 _NodeMapKey &&key, NdrNodeUniquePtr &&node);
366
367 // Finds an existing node in the node cache for the discovery result if one
368 // exists. Otherwise it parses the new node, inserts it into the cache, and
369 // returns it. If there was an error parsing or validating the node,
370 // `nullptr` will be returned.
371 NdrNodeConstPtr _FindOrParseNodeInCache(const NdrNodeDiscoveryResult& dr);
372
373 // Return the parser plugin for a discovery type. Returns null if no parser
374 // plugin has that discovery type.
376 _GetParserForDiscoveryType(const TfToken& discoveryType) const;
377
378 // The discovery plugins that were found through libplug and/or provided by
379 // the client
380 DiscoveryPluginRefPtrVec _discoveryPlugins;
381
382 // The parser plugins that have been discovered via the plugin system. Maps
383 // a discovery result's "discovery type" to a specific parser.
384 _TypeToParserPluginMap _parserPluginMap;
385
386 // The parser plugins. This has ownership of the plugin objects.
387 std::vector<std::unique_ptr<NdrParserPlugin>> _parserPlugins;
388
389 // The preliminary discovery results prior to parsing. These are stored
390 // in a multimap by identifier and a multimap by name. If accessing or
391 // mutating, _discoveryResultMutex should be used.
392 _DiscoveryResultsByIdentifier _discoveryResultsByIdentifier;
393 _DiscoveryResultPtrsByName _discoveryResultPtrsByName;
394
395 // Set of all possible source types as determined by the existing discovery
396 // results. Populated along with the discovery result multimaps. If
397 // accessing or mutating, _discoveryResultMutex should be used.
398 TfToken::Set _allSourceTypes;
399
400 // Maps a node's identifier and source type to a node instance. If accessing
401 // or mutating, _nodeMapMutex should be used.
402 _NodeMap _nodeMap;
403};
404
405PXR_NAMESPACE_CLOSE_SCOPE
406
407#endif // PXR_USD_NDR_REGISTRY_H
Interface for parser plugins.
Definition: parserPlugin.h:109
The registry provides access to node information.
Definition: registry.h:48
NDR_API void AddDiscoveryResult(NdrNodeDiscoveryResult &&discoveryResult)
Allows the client to explicitly set additional discovery results that would otherwise NOT be found th...
NDR_API void SetExtraDiscoveryPlugins(const std::vector< TfType > &pluginTypes)
Allows the client to set any additional discovery plugins that would otherwise NOT be found through t...
NDR_API NdrNodeConstPtr GetNodeByName(const std::string &name, const NdrTokenVec &sourceTypePriority=NdrTokenVec(), NdrVersionFilter filter=NdrVersionFilterDefaultOnly)
Get the node with the specified name.
NDR_API NdrNodeConstPtr GetNodeByIdentifierAndType(const NdrIdentifier &identifier, const TfToken &sourceType)
Get the node with the specified identifier and sourceType.
NDR_API void AddDiscoveryResult(const NdrNodeDiscoveryResult &discoveryResult)
Copy version of the method above.
NDR_API NdrNodeConstPtr GetNodeFromSourceCode(const std::string &sourceCode, const TfToken &sourceType, const NdrTokenMap &metadata)
Parses the given sourceCode string, constructs a NdrNode from it and adds it to the registry.
NDR_API NdrNodeConstPtrVec GetNodesByIdentifier(const NdrIdentifier &identifier)
Get all nodes matching the specified identifier (multiple nodes of the same identifier,...
NDR_API NdrStringVec GetSearchURIs() const
Get the locations where the registry is searching for nodes.
NDR_API NdrNodeConstPtr GetNodeByIdentifier(const NdrIdentifier &identifier, const NdrTokenVec &sourceTypePriority=NdrTokenVec())
Get the node with the specified identifier, and an optional sourceTypePriority list specifying the se...
NDR_API NdrNodeConstPtrVec GetNodesByName(const std::string &name, NdrVersionFilter filter=NdrVersionFilterDefaultOnly)
Get all nodes matching the specified name.
NDR_API NdrNodeConstPtrVec GetNodesByFamily(const TfToken &family=TfToken(), NdrVersionFilter filter=NdrVersionFilterDefaultOnly)
Get all nodes from the registry, optionally restricted to the nodes that fall under a specified famil...
NDR_API NdrTokenVec GetAllNodeSourceTypes() const
Get a sorted list of all node source types that may be present on the nodes in the registry.
NDR_API NdrNodeConstPtr GetNodeByNameAndType(const std::string &name, const TfToken &sourceType, NdrVersionFilter filter=NdrVersionFilterDefaultOnly)
A convenience wrapper around GetNodeByName().
NDR_API NdrNodeConstPtr GetNodeFromAsset(const SdfAssetPath &asset, const NdrTokenMap &metadata, const TfToken &subIdentifier=TfToken(), const TfToken &sourceType=TfToken())
Parses the given asset, constructs a NdrNode from it and adds it to the registry.
NDR_API NdrStringVec GetNodeNames(const TfToken &family=TfToken()) const
Get the names of all the nodes that the registry is aware of.
NDR_API void SetExtraDiscoveryPlugins(DiscoveryPluginRefPtrVec plugins)
Allows the client to set any additional discovery plugins that would otherwise NOT be found through t...
NDR_API void SetExtraParserPlugins(const std::vector< TfType > &pluginTypes)
Allows the client to set any additional parser plugins that would otherwise NOT be found through the ...
NDR_API NdrIdentifierVec GetNodeIdentifiers(const TfToken &family=TfToken(), NdrVersionFilter filter=NdrVersionFilterDefaultOnly) const
Get the identifiers of all the nodes that the registry is aware of.
Contains an asset path and an optional resolved path.
Definition: assetPath.h:30
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:460
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
std::set< TfToken, TfTokenFastArbitraryLessThan > Set
Predefined type for set of tokens, for when faster lookup is desired, without paying the memory or in...
Definition: token.h:166
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:124
Represents the raw data of a node, and some other bits of metadata, that were determined via a NdrDis...
Common typedefs that are used throughout the NDR library.
NdrVersionFilter
Enumeration used to select nodes by version.
Definition: declare.h:185