HDK
 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 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_KIND_REGISTRY_H
8 #define PXR_USD_KIND_REGISTRY_H
9 
10 /// \file kind/registry.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/kind/api.h"
14 #include "pxr/base/tf/weakBase.h"
15 #include "pxr/base/tf/singleton.h"
17 #include "pxr/base/tf/token.h"
18 
19 #include <unordered_map>
20 #include <vector>
21 
23 
24 /// \hideinitializer
25 #define KIND_TOKENS \
26  (model) \
27  (component) \
28  (group) \
29  (assembly) \
30  (subcomponent)
31 
32 /// \anchor KindTokens
33 /// Provides static, efficient TfToken's for built-in Kinds
34 ///
35 /// See \ref kind_coreKinds for description of the builtin kinds.
37 
38 /// \class KindRegistry
39 ///
40 /// A singleton that holds known kinds and information about them. See
41 /// \ref mainpage_kind "Kind Overview" for a description of why kind exists,
42 /// what the builtin registered kinds are, and how to extend the core kinds.
43 ///
44 /// \section kind_threadsafety KindRegistry Threadsafty
45 ///
46 /// KindRegistry serves performance-critical clients that operate under the
47 /// stl threading model, and therefore itself follows that model in order
48 /// to avoid locking during HasKind() and IsA() queries.
49 ///
50 /// To make this robust, KindRegistry exposes no means to mutate the registry.
51 /// All extensions must be accomplished via plugInfo.json files, which are
52 /// consumed once during the registry initialization (See \ref kind_extensions )
53 class KindRegistry : public TfWeakBase
54 {
55  KindRegistry(const KindRegistry&) = delete;
56  KindRegistry& operator=(const KindRegistry&) = delete;
57 public:
58  /// Return the single \c KindRegistry instance.
60 
61  /// Test whether \a kind is known to the registry.
62  KIND_API static bool HasKind(const TfToken& kind);
63 
64  /// Return the base kind of the given kind.
65  /// If there is no base, the result will be an empty token.
66  /// Issues a coding error if \a kind is unknown to the registry.
67  KIND_API static TfToken GetBaseKind(const TfToken &kind);
68 
69  /// Test whether \a derivedKind is the same as \a baseKind or
70  /// has it as a base kind (either directly or indirectly).
71  ///
72  /// It is \em not required that \a derivedKind or \a baseKind
73  /// be known to the registry: if they are unknown but equal,
74  /// IsA will return \c true; otherwise if either is unknown, we
75  /// will simply return false.
76  ///
77  /// Therefore this method will not raise any errors.
78  KIND_API static bool IsA(const TfToken& derivedKind, const TfToken &baseKind);
79 
80  /// Return an unordered vector of all kinds known to the registry.
81  KIND_API static std::vector<TfToken> GetAllKinds();
82 
83  /// Returns true if \p kind IsA model kind
84  KIND_API static bool IsModel(const TfToken& kind);
85 
86  /// Returns true if \p kind IsA group kind
87  KIND_API static bool IsGroup(const TfToken& kind);
88 
89  /// Return true if \p kind IsA assembly kind
90  KIND_API static bool IsAssembly(const TfToken& kind);
91 
92  /// Returns true if \p kind IsA component kind
93  KIND_API static bool IsComponent(const TfToken& kind);
94 
95  /// Returns true if \p kind IsA subcomponent kind
96  KIND_API static bool IsSubComponent(const TfToken& kind);
97 
98 private:
99  friend class TfSingleton<KindRegistry>;
100 
101  KindRegistry();
102  virtual ~KindRegistry();
103 
104  bool _HasKind(const TfToken& kind) const;
105 
106  TfToken _GetBaseKind(const TfToken &kind) const;
107 
108  bool _IsA(const TfToken& derivedKind, const TfToken &baseKind) const;
109 
110  std::vector<TfToken> _GetAllKinds() const;
111 
112  /// Register the given \a kind with the given \a baseKind.
113  /// It is valid for \a baseKind to be empty (the default),
114  /// in which case \a kind represents a root of the kind hierarchy.
115  void _Register(const TfToken& kind,
116  const TfToken& baseKind = TfToken());
117 
118  void _RegisterDefaults();
119 
120  struct _KindData {
121  TfToken baseKind;
122  };
123 
124  typedef std::unordered_map<TfToken, _KindData, TfToken::HashFunctor>
125  _KindMap;
126 
127 private:
128  _KindMap _kindMap;
129 };
130 
132 
134 
135 #endif // PXR_USD_KIND_REGISTRY_H
static KIND_API TfToken GetBaseKind(const TfToken &kind)
static KIND_API bool IsGroup(const TfToken &kind)
Returns true if kind IsA group kind.
static KIND_API bool HasKind(const TfToken &kind)
Test whether kind is known to the registry.
TF_DECLARE_PUBLIC_TOKENS(KindTokens, KIND_API, KIND_TOKENS)
static KIND_API bool IsAssembly(const TfToken &kind)
Return true if kind IsA assembly kind.
Definition: token.h:70
static KIND_API bool IsSubComponent(const TfToken &kind)
Returns true if kind IsA subcomponent kind.
#define KIND_API
Definition: api.h:23
static KIND_API std::vector< TfToken > GetAllKinds()
Return an unordered vector of all kinds known to the registry.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
static KIND_API bool IsComponent(const TfToken &kind)
Returns true if kind IsA component kind.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
#define KIND_TOKENS
Definition: registry.h:25
TfRefPtr< Tf_Remnant > _Register() const
Definition: weakBase.h:169
static KIND_API KindRegistry & GetInstance()
Return the single KindRegistry instance.
static KIND_API bool IsModel(const TfToken &kind)
Returns true if kind IsA model kind.
KIND_API_TEMPLATE_CLASS(TfSingleton< KindRegistry >)
static KIND_API bool IsA(const TfToken &derivedKind, const TfToken &baseKind)