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 NDR_REGISTRY_H
26 #define 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 
106  NDR_API
107  NdrNodeConstPtr GetNodeFromAsset(const SdfAssetPath &asset,
108  const NdrTokenMap &metadata);
109 
125  NDR_API
126  NdrNodeConstPtr GetNodeFromSourceCode(const std::string &sourceCode,
127  const TfToken &sourceType,
128  const NdrTokenMap &metadata);
129 
134  NDR_API
135  NdrStringVec GetSearchURIs() const;
136 
144  NDR_API
145  NdrIdentifierVec
146  GetNodeIdentifiers(const TfToken& family = TfToken(),
147  NdrVersionFilter filter =
148  NdrVersionFilterDefaultOnly) const;
149 
156  NDR_API
157  NdrStringVec GetNodeNames(const TfToken& family = TfToken()) const;
158 
175  NDR_API
176  NdrNodeConstPtr GetNodeByIdentifier(const NdrIdentifier& identifier,
177  const NdrTokenVec& typePriority = NdrTokenVec());
178 
183  NDR_API
184  NdrNodeConstPtr GetNodeByIdentifierAndType(const NdrIdentifier& identifier,
185  const TfToken& nodeType);
186 
196  NDR_API
197  NdrNodeConstPtr GetNodeByName(const std::string& name,
198  const NdrTokenVec& typePriority = NdrTokenVec(),
199  NdrVersionFilter filter =
200  NdrVersionFilterDefaultOnly);
201 
210  NDR_API
211  NdrNodeConstPtr GetNodeByNameAndType(const std::string& name,
212  const TfToken& nodeType,
213  NdrVersionFilter filter =
214  NdrVersionFilterDefaultOnly);
215 
221  NDR_API
222  NdrNodeConstPtr GetNodeByURI(const std::string& uri);
223 
228  NDR_API
229  NdrNodeConstPtrVec GetNodesByIdentifier(const NdrIdentifier& identifier);
230 
235  NDR_API
236  NdrNodeConstPtrVec GetNodesByName(const std::string& name,
237  NdrVersionFilter filter =
238  NdrVersionFilterDefaultOnly);
239 
246  NDR_API
247  NdrNodeConstPtrVec GetNodesByFamily(const TfToken& family = TfToken(),
248  NdrVersionFilter filter =
249  NdrVersionFilterDefaultOnly);
250 
262  NDR_API
263  NdrTokenVec GetAllNodeSourceTypes() const;
264 
265 protected:
266  NdrRegistry(const NdrRegistry&) = delete;
267  NdrRegistry& operator=(const NdrRegistry&) = delete;
268 
269  // Allow TF to construct the class
270  friend class TfSingleton<NdrRegistry>;
271 
272  NDR_API
273  NdrRegistry();
274 
275  NDR_API
276  virtual ~NdrRegistry();
277 
278 private:
279  class _DiscoveryContext;
280  friend class _DiscoveryContext;
281 
282  typedef std::unordered_map<TfToken, NdrParserPlugin*,
283  TfToken::HashFunctor> TypeToParserPluginMap;
284  typedef std::pair<NdrIdentifier, TfToken> NodeMapKey;
285  struct NodeMapKeyHashFunctor {
286  size_t operator()(const NodeMapKey& x) const {
287  return NdrIdentifierHashFunctor()(x.first) ^
288  TfToken::HashFunctor()(x.second);
289  }
290  };
291  typedef std::unordered_multimap<NodeMapKey, NdrNodeUniquePtr,
292  NodeMapKeyHashFunctor> NodeMap;
293 
294  // The discovery result vec is not a concurrent data structure, thus it
295  // needs some locking infrastructure.
296  mutable std::mutex _discoveryResultMutex;
297 
298  // The node map is not a concurrent data structure, thus it needs some
299  // locking infrastructure.
300  mutable std::mutex _nodeMapMutex;
301 
302  // Runs each discovery plugin provided and appends the results to the
303  // internal discovery results vector
304  void _RunDiscoveryPlugins(const DiscoveryPluginRefPtrVec& discoveryPlugins);
305 
306  // Finds and instantiates the discovery plugins via Tf
307  void _FindAndInstantiateDiscoveryPlugins();
308 
309  // Finds and instantiates the parser plugins via Tf
310  void _FindAndInstantiateParserPlugins();
311 
312  // Parses all nodes that match the specified predicate, optionally only
313  // parsing the first node that matches (good to use when the predicate will
314  // only ever match one node). This is a lightweight, single-threaded version
315  // of the parsing routine found in `GetNodes()`. Note that if a node matches
316  // the predicate and it has already been parsed, the already-parsed version
317  // will be returned, and a new node will not be inserted into the map.
318  NdrNodeConstPtrVec _ParseNodesMatchingPredicate(
319  std::function<bool(const NdrNodeDiscoveryResult&)> shouldParsePredicate,
320  bool onlyParseFirstMatch);
321 
322  // Inserts a new node into the node cache. If a node with the
323  // same name and type already exists in the cache, the pointer to the
324  // existing node will be returned. If there was an error inserting the node,
325  // `nullptr` will be returned.
326  NdrNodeConstPtr _InsertNodeIntoCache(const NdrNodeDiscoveryResult& dr);
327 
328  // Get a vector of all of the node unique_ptrs in the node map as raw ptrs
329  NdrNodeConstPtrVec _GetNodeMapAsNodePtrVec(const TfToken& family,
330  NdrVersionFilter filter) const;
331 
332  // Return the source type for a discovery type or the empty token if
333  // no parser plugin has that discovery type.
335  _GetParserForDiscoveryType(const TfToken& discoveryType) const;
336 
337  // Return the first node matching the strongest possible source type.
338  // That is, for each source type from beginning to end, check every
339  // node in nodes beginning-to-end and return the first node that
340  // has the source type.
341  static NdrNodeConstPtr
342  _GetNodeByTypePriority(const NdrNodeConstPtrVec& nodes,
343  const NdrTokenVec& typePriority);
344 
345  // The discovery plugins that were found through libplug and/or provided by
346  // the client
347  DiscoveryPluginRefPtrVec _discoveryPlugins;
348 
349  // The parser plugins that have been discovered via the plugin system. Maps
350  // a discovery result's "discovery type" to a specific parser.
351  TypeToParserPluginMap _parserPluginMap;
352 
353  // The parser plugins. This has ownership of the plugin objects.
354  std::vector<std::unique_ptr<NdrParserPlugin>> _parserPlugins;
355 
356  // The preliminary discovery results prior to parsing. If accessing or
357  // mutating, _discoveryResultMutex should be used.
358  NdrNodeDiscoveryResultVec _discoveryResults;
359 
360  // Maps a node's name to a node instance. If accessing or mutating,
361  // _nodeMapMutex should be used.
362  NodeMap _nodeMap;
363 
364  // The source types that have been made available via parser plugins
365  NdrTokenVec _availableSourceTypes;
366 };
367 
368 PXR_NAMESPACE_CLOSE_SCOPE
369 
370 #endif // 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.
Functor to use for hash maps from tokens to other things.
Definition: token.h:168
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 GetNodeFromAsset(const SdfAssetPath &asset, const NdrTokenMap &metadata)
Parses the given asset, constucts a NdrNode from it and adds it to the registry.
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