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