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/weakBase.h"
33 #include "pxr/usd/ndr/declare.h"
34 #include "pxr/usd/ndr/discoveryPlugin.h"
35 #include "pxr/usd/ndr/node.h"
36 #include "pxr/usd/ndr/nodeDiscoveryResult.h"
38 #include "pxr/usd/sdf/assetPath.h"
39 #include <mutex>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
63 class NdrRegistry : public TfWeakBase
64 {
65 public:
66  using DiscoveryPluginRefPtrVec = NdrDiscoveryPluginRefPtrVector;
67 
75  NDR_API
76  void SetExtraDiscoveryPlugins(DiscoveryPluginRefPtrVec plugins);
77 
85  NDR_API
86  void SetExtraDiscoveryPlugins(const std::vector<TfType>& pluginTypes);
87 
94  NDR_API
95  void SetExtraParserPlugins(const std::vector<TfType>& pluginTypes);
96 
119  NDR_API
120  NdrNodeConstPtr GetNodeFromAsset(const SdfAssetPath &asset,
121  const NdrTokenMap &metadata,
122  const TfToken &subIdentifier=TfToken(),
123  const TfToken &sourceType=TfToken());
124 
140  NDR_API
141  NdrNodeConstPtr GetNodeFromSourceCode(const std::string &sourceCode,
142  const TfToken &sourceType,
143  const NdrTokenMap &metadata);
144 
149  NDR_API
150  NdrStringVec GetSearchURIs() const;
151 
159  NDR_API
160  NdrIdentifierVec
161  GetNodeIdentifiers(const TfToken& family = TfToken(),
162  NdrVersionFilter filter =
163  NdrVersionFilterDefaultOnly) const;
164 
171  NDR_API
172  NdrStringVec GetNodeNames(const TfToken& family = TfToken()) const;
173 
199  NDR_API
200  NdrNodeConstPtr GetNodeByIdentifier(const NdrIdentifier& identifier,
201  const NdrTokenVec& sourceTypePriority = NdrTokenVec());
202 
210  NDR_API
211  NdrNodeConstPtr GetNodeByIdentifierAndType(const NdrIdentifier& identifier,
212  const TfToken& sourceType);
213 
223  NDR_API
224  NdrNodeConstPtr GetNodeByName(const std::string& name,
225  const NdrTokenVec& sourceTypePriority = NdrTokenVec(),
226  NdrVersionFilter filter = NdrVersionFilterDefaultOnly);
227 
236  NDR_API
237  NdrNodeConstPtr GetNodeByNameAndType(const std::string& name,
238  const TfToken& sourceType,
239  NdrVersionFilter filter =
240  NdrVersionFilterDefaultOnly);
241 
248  NDR_API
249  NdrNodeConstPtrVec GetNodesByIdentifier(const NdrIdentifier& identifier);
250 
255  NDR_API
256  NdrNodeConstPtrVec GetNodesByName(const std::string& name,
257  NdrVersionFilter filter =
258  NdrVersionFilterDefaultOnly);
259 
266  NDR_API
267  NdrNodeConstPtrVec GetNodesByFamily(const TfToken& family = TfToken(),
268  NdrVersionFilter filter =
269  NdrVersionFilterDefaultOnly);
270 
282  NDR_API
283  NdrTokenVec GetAllNodeSourceTypes() const;
284 
285 protected:
286  NdrRegistry(const NdrRegistry&) = delete;
287  NdrRegistry& operator=(const NdrRegistry&) = delete;
288 
289  NDR_API
290  NdrRegistry();
291 
292  NDR_API
293  ~NdrRegistry();
294 
295 private:
296  class _DiscoveryContext;
297  friend class _DiscoveryContext;
298 
299  typedef std::unordered_map<TfToken, NdrParserPlugin*,
300  TfToken::HashFunctor> TypeToParserPluginMap;
301  typedef std::pair<NdrIdentifier, TfToken> NodeMapKey;
302  struct NodeMapKeyHashFunctor {
303  size_t operator()(const NodeMapKey& x) const {
304  return NdrIdentifierHashFunctor()(x.first) ^
305  TfToken::HashFunctor()(x.second);
306  }
307  };
308  typedef std::unordered_multimap<NodeMapKey, NdrNodeUniquePtr,
309  NodeMapKeyHashFunctor> NodeMap;
310 
311  // The discovery result vec is not a concurrent data structure, thus it
312  // needs some locking infrastructure.
313  mutable std::mutex _discoveryResultMutex;
314 
315  // The node map is not a concurrent data structure, thus it needs some
316  // locking infrastructure.
317  mutable std::mutex _nodeMapMutex;
318 
319  // Runs each discovery plugin provided and appends the results to the
320  // internal discovery results vector
321  void _RunDiscoveryPlugins(const DiscoveryPluginRefPtrVec& discoveryPlugins);
322 
323  // Finds and instantiates the discovery plugins
324  void _FindAndInstantiateDiscoveryPlugins();
325 
326  // Finds and instantiates the parser plugins
327  void _FindAndInstantiateParserPlugins();
328 
329  // Instantiates the specified parser plugins and adds them to
330  // the registry.
331  void _InstantiateParserPlugins(const std::set<TfType>& parserPluginTypes);
332 
333  // Returns the cached or newly parsed node for the discovery result if its
334  // identifier matches the given identifier.
335  NdrNodeConstPtr _ParseNodeMatchingIdentifier(
336  const NdrNodeDiscoveryResult& dr, const NdrIdentifier& identifier);
337 
338  // Returns the cached or newly parsed node for the discovery result if it
339  // has an alias that matches the given identifier.
340  NdrNodeConstPtr _ParseNodeMatchingAlias(
341  const NdrNodeDiscoveryResult& dr, const NdrIdentifier& identifier);
342 
343  // Returns the cached or newly parsed node for the discovery result if its
344  // name and version match the given name and version filter.
345  NdrNodeConstPtr _ParseNodeMatchingNameAndFilter(
346  const NdrNodeDiscoveryResult& dr, const std::string& name,
347  NdrVersionFilter filter);
348 
349  // Implementation helper for getting the first node of the given sourceType
350  // that matches the given indentifier. This includes node that match the
351  // identifier through an alias.
352  NdrNodeConstPtr _GetNodeByIdentifierAndTypeImpl(
353  const NdrIdentifier& identifier, const TfToken& sourceType);
354 
355  // Implementation helper for getting the first node of the given sourceType
356  // that matches the given name and version filter.
357  NdrNodeConstPtr _GetNodeByNameAndTypeImpl(
358  const std::string& name, const TfToken& sourceType,
359  NdrVersionFilter filter);
360 
361  // Inserts a new node into the node cache. If a node with the
362  // same name and type already exists in the cache, the pointer to the
363  // existing node will be returned. If there was an error inserting the node,
364  // `nullptr` will be returned.
365  NdrNodeConstPtr _InsertNodeIntoCache(const NdrNodeDiscoveryResult& dr);
366 
367  // Get a vector of all of the node unique_ptrs in the node map as raw ptrs
368  NdrNodeConstPtrVec _GetNodeMapAsNodePtrVec(const TfToken& family,
369  NdrVersionFilter filter) const;
370 
371  // Return the parser plugin for a discovery type. Returns null if no parser
372  // plugin has that discovery type.
374  _GetParserForDiscoveryType(const TfToken& discoveryType) const;
375 
376  // The discovery plugins that were found through libplug and/or provided by
377  // the client
378  DiscoveryPluginRefPtrVec _discoveryPlugins;
379 
380  // The parser plugins that have been discovered via the plugin system. Maps
381  // a discovery result's "discovery type" to a specific parser.
382  TypeToParserPluginMap _parserPluginMap;
383 
384  // The parser plugins. This has ownership of the plugin objects.
385  std::vector<std::unique_ptr<NdrParserPlugin>> _parserPlugins;
386 
387  // The preliminary discovery results prior to parsing. If accessing or
388  // mutating, _discoveryResultMutex should be used.
389  NdrNodeDiscoveryResultVec _discoveryResults;
390 
391  // Additional mapping of discovery results by grouped source type to aid in
392  // getting nodes by type priority. Stored as indices into the
393  // _disoveryResults vector.
394  std::map<TfToken, std::vector<size_t>> _discoveryResultIndicesBySourceType;
395 
396  // Maps a node's name to a node instance. If accessing or mutating,
397  // _nodeMapMutex should be used.
398  NodeMap _nodeMap;
399 };
400 
401 PXR_NAMESPACE_CLOSE_SCOPE
402 
403 #endif // PXR_USD_NDR_REGISTRY_H
NdrVersionFilter
Enumeration used to select nodes by version.
Definition: declare.h:203
NDR_API NdrNodeConstPtr GetNodeByName(const std::string &name, const NdrTokenVec &sourceTypePriority=NdrTokenVec(), NdrVersionFilter filter=NdrVersionFilterDefaultOnly)
Get the node with the specified name.
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:166
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 &sourceType)
Get the node with the specified identifier and sourceType.
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 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:87
NDR_API NdrStringVec GetSearchURIs() const
Get the locations where the registry is searching for nodes.
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) as well as any nodes which have an alias that matches the identifier.
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 GetNodeByNameAndType(const std::string &name, const TfToken &sourceType, NdrVersionFilter filter=NdrVersionFilterDefaultOnly)
A convenience wrapper around GetNodeByName().
Contains an asset path and an optional resolved path.
Definition: assetPath.h:47
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 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:141