All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
registry.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 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 
25 #ifndef PXR_USD_NDR_REGISTRY_H
26 #define PXR_USD_NDR_REGISTRY_H
27 
29 
30 #include "pxr/pxr.h"
31 #include "pxr/usd/ndr/api.h"
32 #include "pxr/base/tf/singleton.h"
33 #include "pxr/base/tf/weakBase.h"
34 #include "pxr/usd/ndr/declare.h"
35 #include "pxr/usd/ndr/discoveryPlugin.h"
36 #include "pxr/usd/ndr/node.h"
37 #include "pxr/usd/ndr/nodeDiscoveryResult.h"
38 #include "pxr/usd/ndr/parserPlugin.h"
39 #include "pxr/usd/sdf/assetPath.h"
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
63 class NdrRegistry : public TfWeakBase
64 {
65 public:
66  using DiscoveryPluginRefPtrVec = NdrDiscoveryPluginRefPtrVector;
67 
69  NDR_API
70  static NdrRegistry& GetInstance();
71 
79  NDR_API
80  void SetExtraDiscoveryPlugins(DiscoveryPluginRefPtrVec plugins);
81 
89  NDR_API
90  void SetExtraDiscoveryPlugins(const std::vector<TfType>& pluginTypes);
91 
98  NDR_API
99  void SetExtraParserPlugins(const std::vector<TfType>& pluginTypes);
100 
123  NDR_API
124  NdrNodeConstPtr GetNodeFromAsset(const SdfAssetPath &asset,
125  const NdrTokenMap &metadata,
126  const TfToken &subIdentifier=TfToken(),
127  const TfToken &sourceType=TfToken());
128 
144  NDR_API
145  NdrNodeConstPtr GetNodeFromSourceCode(const std::string &sourceCode,
146  const TfToken &sourceType,
147  const NdrTokenMap &metadata);
148 
153  NDR_API
154  NdrStringVec GetSearchURIs() const;
155 
163  NDR_API
164  NdrIdentifierVec
165  GetNodeIdentifiers(const TfToken& family = TfToken(),
166  NdrVersionFilter filter =
167  NdrVersionFilterDefaultOnly) const;
168 
175  NDR_API
176  NdrStringVec GetNodeNames(const TfToken& family = TfToken()) const;
177 
194  NDR_API
195  NdrNodeConstPtr GetNodeByIdentifier(const NdrIdentifier& identifier,
196  const NdrTokenVec& typePriority = NdrTokenVec());
197 
202  NDR_API
203  NdrNodeConstPtr GetNodeByIdentifierAndType(const NdrIdentifier& identifier,
204  const TfToken& nodeType);
205 
215  NDR_API
216  NdrNodeConstPtr GetNodeByName(const std::string& name,
217  const NdrTokenVec& typePriority = NdrTokenVec(),
218  NdrVersionFilter filter =
219  NdrVersionFilterDefaultOnly);
220 
229  NDR_API
230  NdrNodeConstPtr GetNodeByNameAndType(const std::string& name,
231  const TfToken& nodeType,
232  NdrVersionFilter filter =
233  NdrVersionFilterDefaultOnly);
234 
240  NDR_API
241  NdrNodeConstPtr GetNodeByURI(const std::string& uri);
242 
247  NDR_API
248  NdrNodeConstPtrVec GetNodesByIdentifier(const NdrIdentifier& identifier);
249 
254  NDR_API
255  NdrNodeConstPtrVec GetNodesByName(const std::string& name,
256  NdrVersionFilter filter =
257  NdrVersionFilterDefaultOnly);
258 
265  NDR_API
266  NdrNodeConstPtrVec GetNodesByFamily(const TfToken& family = TfToken(),
267  NdrVersionFilter filter =
268  NdrVersionFilterDefaultOnly);
269 
281  NDR_API
282  NdrTokenVec GetAllNodeSourceTypes() const;
283 
284 protected:
285  NdrRegistry(const NdrRegistry&) = delete;
286  NdrRegistry& operator=(const NdrRegistry&) = delete;
287 
288  // Allow TF to construct the class
289  friend class TfSingleton<NdrRegistry>;
290 
291  NDR_API
292  NdrRegistry();
293 
294  NDR_API
295  virtual ~NdrRegistry();
296 
297 private:
298  class _DiscoveryContext;
299  friend class _DiscoveryContext;
300 
301  typedef std::unordered_map<TfToken, NdrParserPlugin*,
302  TfToken::HashFunctor> TypeToParserPluginMap;
303  typedef std::pair<NdrIdentifier, TfToken> NodeMapKey;
304  struct NodeMapKeyHashFunctor {
305  size_t operator()(const NodeMapKey& x) const {
306  return NdrIdentifierHashFunctor()(x.first) ^
307  TfToken::HashFunctor()(x.second);
308  }
309  };
310  typedef std::unordered_multimap<NodeMapKey, NdrNodeUniquePtr,
311  NodeMapKeyHashFunctor> NodeMap;
312 
313  // The discovery result vec is not a concurrent data structure, thus it
314  // needs some locking infrastructure.
315  mutable std::mutex _discoveryResultMutex;
316 
317  // The node map is not a concurrent data structure, thus it needs some
318  // locking infrastructure.
319  mutable std::mutex _nodeMapMutex;
320 
321  // Runs each discovery plugin provided and appends the results to the
322  // internal discovery results vector
323  void _RunDiscoveryPlugins(const DiscoveryPluginRefPtrVec& discoveryPlugins);
324 
325  // Finds and instantiates the discovery plugins
326  void _FindAndInstantiateDiscoveryPlugins();
327 
328  // Finds and instantiates the parser plugins
329  void _FindAndInstantiateParserPlugins();
330 
331  // Instantiates the specified parser plugins and adds them to
332  // the registry.
333  void _InstantiateParserPlugins(const std::set<TfType>& parserPluginTypes);
334 
335  // Parses all nodes that match the specified predicate, optionally only
336  // parsing the first node that matches (good to use when the predicate will
337  // only ever match one node). This is a lightweight, single-threaded version
338  // of the parsing routine found in `GetNodes()`. Note that if a node matches
339  // the predicate and it has already been parsed, the already-parsed version
340  // will be returned, and a new node will not be inserted into the map.
341  NdrNodeConstPtrVec _ParseNodesMatchingPredicate(
342  std::function<bool(const NdrNodeDiscoveryResult&)> shouldParsePredicate,
343  bool onlyParseFirstMatch);
344 
345  // Inserts a new node into the node cache. If a node with the
346  // same name and type already exists in the cache, the pointer to the
347  // existing node will be returned. If there was an error inserting the node,
348  // `nullptr` will be returned.
349  NdrNodeConstPtr _InsertNodeIntoCache(const NdrNodeDiscoveryResult& dr);
350 
351  // Get a vector of all of the node unique_ptrs in the node map as raw ptrs
352  NdrNodeConstPtrVec _GetNodeMapAsNodePtrVec(const TfToken& family,
353  NdrVersionFilter filter) const;
354 
355  // Return the source type for a discovery type or the empty token if
356  // no parser plugin has that discovery type.
358  _GetParserForDiscoveryType(const TfToken& discoveryType) const;
359 
360  // Return the first node matching the strongest possible source type.
361  // That is, for each source type from beginning to end, check every
362  // node in nodes beginning-to-end and return the first node that
363  // has the source type.
364  static NdrNodeConstPtr
365  _GetNodeByTypePriority(const NdrNodeConstPtrVec& nodes,
366  const NdrTokenVec& typePriority);
367 
368  // The discovery plugins that were found through libplug and/or provided by
369  // the client
370  DiscoveryPluginRefPtrVec _discoveryPlugins;
371 
372  // The parser plugins that have been discovered via the plugin system. Maps
373  // a discovery result's "discovery type" to a specific parser.
374  TypeToParserPluginMap _parserPluginMap;
375 
376  // The parser plugins. This has ownership of the plugin objects.
377  std::vector<std::unique_ptr<NdrParserPlugin>> _parserPlugins;
378 
379  // The preliminary discovery results prior to parsing. If accessing or
380  // mutating, _discoveryResultMutex should be used.
381  NdrNodeDiscoveryResultVec _discoveryResults;
382 
383  // Maps a node's name to a node instance. If accessing or mutating,
384  // _nodeMapMutex should be used.
385  NodeMap _nodeMap;
386 
387  // The source types that have been made available via parser plugins
388  NdrTokenVec _availableSourceTypes;
389 };
390 
391 PXR_NAMESPACE_CLOSE_SCOPE
392 
393 #endif // PXR_USD_NDR_REGISTRY_H
NDR_API NdrNodeConstPtr GetNodeByURI(const std::string &uri)
Gets the node matching the specified URI (eg, a filesystem path).
Manage a single instance of an object (see.
Definition: singleton.h:122
Interface for parser plugins.
Definition: parserPlugin.h:125
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 NdrStringVec GetNodeNames(const TfToken &family=TfToken()) const
Get the names of all the nodes that the registry is aware of.
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. ...
Functor to use for hash maps from tokens to other things.
Definition: token.h:168
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 NdrNodeConstPtr GetNodeByIdentifierAndType(const NdrIdentifier &identifier, const TfToken &nodeType)
A convenience wrapper around GetNodeByIdentifier().
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.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
NDR_API NdrNodeConstPtr GetNodeByName(const std::string &name, const NdrTokenVec &typePriority=NdrTokenVec(), NdrVersionFilter filter=NdrVersionFilterDefaultOnly)
Get the node with the specified name.
NDR_API NdrStringVec GetSearchURIs() const
Get the locations where the registry is searching for nodes.
static NDR_API NdrRegistry & GetInstance()
Get the single NdrRegistry instance.
NdrVersionFilter
Enumeration used to select nodes by version.
Definition: declare.h:203
The registry provides access to node information.
Definition: registry.h:63
NDR_API NdrNodeConstPtrVec GetNodesByIdentifier(const NdrIdentifier &identifier)
Get all nodes matching the specified identifier (multiple nodes of the same identifier, but different source types, may exist).
Represents the raw data of a node, and some other bits of metadata, that were determined via a NdrDis...
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 GetNodeByIdentifier(const NdrIdentifier &identifier, const NdrTokenVec &typePriority=NdrTokenVec())
Get the node with the specified identifier, and an optional priority list specifying the set of node ...
Contains an asset path and an optional resolved path.
Definition: assetPath.h:43
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 NdrNodeConstPtrVec GetNodesByName(const std::string &name, NdrVersionFilter filter=NdrVersionFilterDefaultOnly)
Get all nodes matching the specified name.
NDR_API NdrNodeConstPtr GetNodeByNameAndType(const std::string &name, const TfToken &nodeType, NdrVersionFilter filter=NdrVersionFilterDefaultOnly)
A convenience wrapper around GetNodeByName().
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...
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:142