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