All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
valueTypeRegistry.h
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 SDF_VALUETYPEREGISTRY_H
25 #define SDF_VALUETYPEREGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/sdf/valueTypeName.h"
29 #include "pxr/base/tf/enum.h"
30 #include "pxr/base/vt/array.h"
31 #include "pxr/base/vt/value.h"
32 #include <boost/noncopyable.hpp>
33 #include <memory>
34 #include <vector>
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
38 class TfType;
39 
44 class Sdf_ValueTypeRegistry : boost::noncopyable {
45 public:
46  Sdf_ValueTypeRegistry();
47  ~Sdf_ValueTypeRegistry();
48 
50  std::vector<SdfValueTypeName> GetAllTypes() const;
51 
53  SdfValueTypeName FindType(const std::string& name) const;
54 
59  SdfValueTypeName FindType(const TfType& type,
60  const TfToken& role = TfToken()) const;
61 
66  SdfValueTypeName FindType(const VtValue& value,
67  const TfToken& role = TfToken()) const;
68 
76  SdfValueTypeName FindOrCreateTypeName(const std::string& name) const;
77 
81  class Type
82  {
83  public:
84  // Specify a type with the given name, default value, and default
85  // array value.
86  Type(const std::string& name,
87  const VtValue& defaultValue,
88  const VtValue& defaultArrayValue)
89  : _name(name)
90  , _defaultValue(defaultValue)
91  , _defaultArrayValue(defaultArrayValue)
92  { }
93 
94  // Specify a type with the given name, default value, and default
95  // array value of VtArray<T>.
96  template <class T>
97  Type(const std::string& name, const T& defaultValue)
98  : Type(name, VtValue(defaultValue), VtValue(VtArray<T>()))
99  { }
100 
101  // Specify a type with the given name and underlying C++ type.
102  // No default value or array value will be registered.
103  Type(const std::string& name, const TfType& type)
104  : _name(name)
105  , _type(type)
106  { }
107 
108  // Set C++ type name string for this type. Defaults to type name
109  // from TfType.
110  Type& CPPTypeName(const std::string& cppTypeName)
111  {
112  _cppTypeName = cppTypeName;
113  if (!_defaultArrayValue.IsEmpty()) {
114  _arrayCppTypeName = "VtArray<" + cppTypeName + ">";
115  }
116  return *this;
117  }
118 
119  // Set shape for this type. Defaults to shapeless.
120  Type& Dimensions(const SdfTupleDimensions& dims)
121  { _dimensions = dims; return *this; }
122 
123  // Set default unit for this type. Defaults to dimensionless unit.
124  Type& DefaultUnit(TfEnum unit) { _unit = unit; return *this; }
125 
126  // Set role for this type. Defaults to no role.
127  Type& Role(const TfToken& role) { _role = role; return *this; }
128 
129  // Indicate that arrays of this type are not supported.
130  Type& NoArrays()
131  {
132  _defaultArrayValue = VtValue();
133  _arrayCppTypeName = std::string();
134  return *this;
135  }
136 
137  private:
138  friend class Sdf_ValueTypeRegistry;
139 
140  std::string _name;
141  TfType _type;
142  VtValue _defaultValue, _defaultArrayValue;
143  std::string _cppTypeName, _arrayCppTypeName;
144  TfEnum _unit;
145  TfToken _role;
146  SdfTupleDimensions _dimensions;
147  };
148 
151  void AddType(const Type& type);
152 
154  void AddType(const std::string& name,
155  const VtValue& defaultValue,
156  const VtValue& defaultArrayValue,
157  const std::string& cppName, const std::string& cppArrayName,
158  TfEnum defaultUnit, const TfToken& role,
159  const SdfTupleDimensions& dimensions);
160 
165  void AddType(const std::string& name,
166  const TfType& type, const TfType& arrayType,
167  const std::string& cppName, const std::string& cppArrayName,
168  TfEnum defaultUnit, const TfToken& role,
169  const SdfTupleDimensions& dimensions);
170 
173  void Clear();
174 
175 private:
176  class _Impl;
177  std::unique_ptr<_Impl> _impl;
178 };
179 
180 PXR_NAMESPACE_CLOSE_SCOPE
181 
182 #endif // SDF_VALUETYPEREGISTRY_H
An enum class that records both enum type and enum value.
Definition: enum.h:140
Represents a value type name, i.e.
Definition: valueTypeName.h:83
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Represents an arbitrary dimensional rectangular container class.
Definition: array.h:193
TfType represents a dynamic runtime type.
Definition: type.h:70
Represents the shape of a value type (or that of an element in an array).
Definition: valueTypeName.h:46
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182