Loading...
Searching...
No Matches
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
16
17#include "pxr/pxr.h"
18#include "pxr/usd/ndr/api.h"
20#include "pxr/usd/ndr/declare.h"
22#include "pxr/usd/ndr/node.h"
23#include "pxr/usd/ndr/nodeDiscoveryResult.h"
26#include <map>
27#include <mutex>
28
29PXR_NAMESPACE_OPEN_SCOPE
30
53class NdrRegistry : public TfWeakBase
54{
55public:
56 using DiscoveryPluginRefPtrVec = NdrDiscoveryPluginRefPtrVector;
57
65 NDR_API
66 void SetExtraDiscoveryPlugins(DiscoveryPluginRefPtrVec plugins);
67
75 NDR_API
76 void SetExtraDiscoveryPlugins(const std::vector<TfType>& pluginTypes);
77
89 NDR_API
91
100 NDR_API
101 void AddDiscoveryResult(const NdrNodeDiscoveryResult& discoveryResult);
102
109 NDR_API
110 void SetExtraParserPlugins(const std::vector<TfType>& pluginTypes);
111
137 NDR_API
138 NdrNodeConstPtr GetNodeFromAsset(const SdfAssetPath &asset,
139 const NdrTokenMap &metadata,
140 const TfToken &subIdentifier=TfToken(),
141 const TfToken &sourceType=TfToken());
142
161 NDR_API
162 NdrNodeConstPtr GetNodeFromSourceCode(const std::string &sourceCode,
163 const TfToken &sourceType,
164 const NdrTokenMap &metadata);
165
170 NDR_API
171 NdrStringVec GetSearchURIs() const;
172
183 NDR_API
184 NdrIdentifierVec
186 NdrVersionFilter filter =
187 NdrVersionFilterDefaultOnly) const;
188
198 NDR_API
199 NdrStringVec GetNodeNames(const TfToken& family = TfToken()) const;
200
224 NDR_API
225 NdrNodeConstPtr GetNodeByIdentifier(const NdrIdentifier& identifier,
226 const NdrTokenVec& sourceTypePriority = NdrTokenVec());
227
233 NDR_API
234 NdrNodeConstPtr GetNodeByIdentifierAndType(const NdrIdentifier& identifier,
235 const TfToken& sourceType);
236
249 NDR_API
250 NdrNodeConstPtr GetNodeByName(const std::string& name,
251 const NdrTokenVec& sourceTypePriority = NdrTokenVec(),
252 NdrVersionFilter filter = NdrVersionFilterDefaultOnly);
253
265 NDR_API
266 NdrNodeConstPtr GetNodeByNameAndType(const std::string& name,
267 const TfToken& sourceType,
268 NdrVersionFilter filter =
269 NdrVersionFilterDefaultOnly);
270
277 NDR_API
278 NdrNodeConstPtrVec GetNodesByIdentifier(const NdrIdentifier& identifier);
279
287 NDR_API
288 NdrNodeConstPtrVec GetNodesByName(const std::string& name,
289 NdrVersionFilter filter =
290 NdrVersionFilterDefaultOnly);
291
301 NDR_API
302 NdrNodeConstPtrVec GetNodesByFamily(const TfToken& family = TfToken(),
303 NdrVersionFilter filter =
304 NdrVersionFilterDefaultOnly);
305
320 NDR_API
321 NdrTokenVec GetAllNodeSourceTypes() const;
322
323protected:
324 NdrRegistry(const NdrRegistry&) = delete;
325 NdrRegistry& operator=(const NdrRegistry&) = delete;
326
327 NDR_API
328 NdrRegistry();
329
330 NDR_API
331 ~NdrRegistry();
332
333private:
334 class _DiscoveryContext;
335 friend class _DiscoveryContext;
336
337 using _TypeToParserPluginMap =
338 std::unordered_map<TfToken, NdrParserPlugin*, TfToken::HashFunctor>;
339
340 // Node cache data structure, stored NdrNodes keyed by identifier and source
341 // type.
342 using _NodeMapKey = std::pair<NdrIdentifier, TfToken>;
343 using _NodeMap = std::unordered_map<_NodeMapKey, NdrNodeUniquePtr, TfHash>;
344
345 // Discovery results data structure, NdrNodeDiscoveryResults multimap keyed
346 // by identifier
347 using _DiscoveryResultsByIdentifier = std::unordered_multimap<
349 using _DiscoveryResultsByIdentifierRange =
350 std::pair<_DiscoveryResultsByIdentifier::const_iterator,
351 _DiscoveryResultsByIdentifier::const_iterator>;
352
353 // Discovery results data structure: a multimap of raw pointers to
354 // NdrNodeDiscoveryResults (i.e. pointers to the discovery results stored
355 // in a _DiscoveryResultsByIdentifier) keyed by name.
356 using _DiscoveryResultPtrsByName = std::unordered_multimap<
357 std::string, const NdrNodeDiscoveryResult *, TfHash>;
358 using _DiscoveryResultPtrsByNameRange =
359 std::pair<_DiscoveryResultPtrsByName::const_iterator,
360 _DiscoveryResultPtrsByName::const_iterator>;
361
362 // The discovery result data structures are not concurrent and must be kept
363 // in sync, thus they need some locking infrastructure.
364 mutable std::mutex _discoveryResultMutex;
365
366 // The node map is not a concurrent data structure, thus it needs some
367 // locking infrastructure.
368 mutable std::mutex _nodeMapMutex;
369
370 // Runs each discovery plugin provided and adds the results to the
371 // internal discovery result maps
372 void _RunDiscoveryPlugins(const DiscoveryPluginRefPtrVec& discoveryPlugins);
373
374 // Takes the discovery and puts in the maps that hold the discovery results,
375 // keeping them in sync.
376 void _AddDiscoveryResultNoLock(NdrNodeDiscoveryResult&& dr);
377
378 // Finds and instantiates the discovery plugins
379 void _FindAndInstantiateDiscoveryPlugins();
380
381 // Finds and instantiates the parser plugins
382 void _FindAndInstantiateParserPlugins();
383
384 // Instantiates the specified parser plugins and adds them to
385 // the registry.
386 void _InstantiateParserPlugins(const std::set<TfType>& parserPluginTypes);
387
388 // Parses the node for the discovery result if adding it to the node map if
389 // able and adds the discovery result to the discovery result maps. Intended
390 // for the GetNodeFromAsset and GetNodeFromSourceCode APIs which can add
391 // nodes that don't already appear in the discovery results.
392 NdrNodeConstPtr _ParseNodeFromAssetOrSourceCode(
394
395 // Implementation helper for getting the first node of the given sourceType
396 // in the range of node discovery results for a paricular identifier.
397 NdrNodeConstPtr _GetNodeInIdentifierRangeWithSourceType(
398 _DiscoveryResultsByIdentifierRange range, const TfToken& sourceType);
399
400 // Implementation helper for getting the first node of the given sourceType
401 // and matching the given version filter in the range of node discovery
402 // results for a paricular name.
403 NdrNodeConstPtr _GetNodeInNameRangeWithSourceType(
404 _DiscoveryResultPtrsByNameRange range, const TfToken& sourceType,
405 NdrVersionFilter filter);
406
407 // Thread-safe find of a node in the cache by key.
408 NdrNodeConstPtr _FindNodeInCache(const _NodeMapKey &key) const;
409
410 // Thread-safe insertion of a node into the cache with a given key. If a
411 // node with the same key already exists in the cache, the pointer to the
412 // existing node will be returned, otherwise the pointer to pointer to the
413 // inserted node is returned.
414 NdrNodeConstPtr _InsertNodeInCache(
415 _NodeMapKey &&key, NdrNodeUniquePtr &&node);
416
417 // Finds an existing node in the node cache for the discovery result if one
418 // exists. Otherwise it parses the new node, inserts it into the cache, and
419 // returns it. If there was an error parsing or validating the node,
420 // `nullptr` will be returned.
421 NdrNodeConstPtr _FindOrParseNodeInCache(const NdrNodeDiscoveryResult& dr);
422
423 // Return the parser plugin for a discovery type. Returns null if no parser
424 // plugin has that discovery type.
426 _GetParserForDiscoveryType(const TfToken& discoveryType) const;
427
428 // The discovery plugins that were found through libplug and/or provided by
429 // the client
430 DiscoveryPluginRefPtrVec _discoveryPlugins;
431
432 // The parser plugins that have been discovered via the plugin system. Maps
433 // a discovery result's "discovery type" to a specific parser.
434 _TypeToParserPluginMap _parserPluginMap;
435
436 // The parser plugins. This has ownership of the plugin objects.
437 std::vector<std::unique_ptr<NdrParserPlugin>> _parserPlugins;
438
439 // The preliminary discovery results prior to parsing. These are stored
440 // in a multimap by identifier and a multimap by name. If accessing or
441 // mutating, _discoveryResultMutex should be used.
442 _DiscoveryResultsByIdentifier _discoveryResultsByIdentifier;
443 _DiscoveryResultPtrsByName _discoveryResultPtrsByName;
444
445 // Set of all possible source types as determined by the existing discovery
446 // results. Populated along with the discovery result multimaps. If
447 // accessing or mutating, _discoveryResultMutex should be used.
448 TfToken::Set _allSourceTypes;
449
450 // Maps a node's identifier and source type to a node instance. If accessing
451 // or mutating, _nodeMapMutex should be used.
452 _NodeMap _nodeMap;
453};
454
455PXR_NAMESPACE_CLOSE_SCOPE
456
457#endif // PXR_USD_NDR_REGISTRY_H
Interface for parser plugins.
Definition: parserPlugin.h:119
The registry provides access to node information.
Definition: registry.h:54
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 optional evaluated and resolved paths.
Definition: assetPath.h:73
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:472
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:198