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 private:
101  friend class TfSingleton<KindRegistry>;
102 
103  KindRegistry();
104  virtual ~KindRegistry();
105 
106  bool _HasKind(const TfToken& kind) const;
107 
108  TfToken _GetBaseKind(const TfToken &kind) const;
109 
110  bool _IsA(const TfToken& derivedKind, const TfToken &baseKind) const;
111 
112  std::vector<TfToken> _GetAllKinds() const;
113 
114  /// Register the given \a kind with the given \a baseKind.
115  /// It is valid for \a baseKind to be empty (the default),
116  /// in which case \a kind represents a root of the kind hierarchy.
117  void _Register(const TfToken& kind,
118  const TfToken& baseKind = TfToken());
119 
120  void _RegisterDefaults();
121 
122  struct _KindData {
123  TfToken baseKind;
124  };
125 
126  typedef std::unordered_map<TfToken, _KindData, TfToken::HashFunctor>
127  _KindMap;
128 
129 private:
130  _KindMap _kindMap;
131 };
132 
134 
136 
137 #endif // PXR_USD_KIND_REGISTRY_H
static KIND_API TfToken GetBaseKind(const TfToken &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)
Definition: token.h:87
#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:1441
#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.
KIND_API_TEMPLATE_CLASS(TfSingleton< KindRegistry >)
static KIND_API bool IsA(const TfToken &derivedKind, const TfToken &baseKind)