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 PXR_USD_SDF_VALUE_TYPE_REGISTRY_H
25 #define PXR_USD_SDF_VALUE_TYPE_REGISTRY_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/tf/token.h"
31 #include "pxr/base/vt/array.h"
32 #include "pxr/base/vt/value.h"
33 #include <boost/noncopyable.hpp>
34 #include <memory>
35 #include <vector>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 class TfType;
40 
45 class Sdf_ValueTypeRegistry : boost::noncopyable {
46 public:
47  Sdf_ValueTypeRegistry();
48  ~Sdf_ValueTypeRegistry();
49 
51  std::vector<SdfValueTypeName> GetAllTypes() const;
52 
54  SdfValueTypeName FindType(const TfToken& name) const;
55  SdfValueTypeName FindType(const char *name) const;
56  SdfValueTypeName FindType(const std::string &name) const;
57 
62  SdfValueTypeName FindType(const TfType& type,
63  const TfToken& role = TfToken()) const;
64 
69  SdfValueTypeName FindType(const VtValue& value,
70  const TfToken& role = TfToken()) const;
71 
79  SdfValueTypeName FindOrCreateTypeName(const TfToken& name) const;
80 
84  class Type
85  {
86  public:
87  // Specify a type with the given name, default value, and default
88  // array value.
89  Type(const TfToken& name,
90  const VtValue& defaultValue,
91  const VtValue& defaultArrayValue)
92  : _name(name)
93  , _defaultValue(defaultValue)
94  , _defaultArrayValue(defaultArrayValue)
95  { }
96 
97  // Specify a type with the given name, default value, and default
98  // array value of VtArray<T>.
99  template <class T>
100  Type(char const *name, const T& defaultValue)
101  : Type(TfToken(name), VtValue(defaultValue), VtValue(VtArray<T>()))
102  { }
103 
104  // Specify a type with the given name and underlying C++ type.
105  // No default value or array value will be registered.
106  Type(const TfToken& name, const TfType& type)
107  : _name(name)
108  , _type(type)
109  { }
110 
111  // Set C++ type name string for this type. Defaults to type name
112  // from TfType.
113  Type& CPPTypeName(const std::string& cppTypeName)
114  {
115  _cppTypeName = cppTypeName;
116  if (!_defaultArrayValue.IsEmpty()) {
117  _arrayCppTypeName = "VtArray<" + cppTypeName + ">";
118  }
119  return *this;
120  }
121 
122  // Set shape for this type. Defaults to shapeless.
123  Type& Dimensions(const SdfTupleDimensions& dims)
124  { _dimensions = dims; return *this; }
125 
126  // Set default unit for this type. Defaults to dimensionless unit.
127  Type& DefaultUnit(TfEnum unit) { _unit = unit; return *this; }
128 
129  // Set role for this type. Defaults to no role.
130  Type& Role(const TfToken& role) { _role = role; return *this; }
131 
132  // Indicate that arrays of this type are not supported.
133  Type& NoArrays()
134  {
135  _defaultArrayValue = VtValue();
136  _arrayCppTypeName = std::string();
137  return *this;
138  }
139 
140  private:
141  friend class Sdf_ValueTypeRegistry;
142 
143  TfToken _name;
144  TfType _type;
145  VtValue _defaultValue, _defaultArrayValue;
146  std::string _cppTypeName, _arrayCppTypeName;
147  TfEnum _unit;
148  TfToken _role;
149  SdfTupleDimensions _dimensions;
150  };
151 
154  void AddType(const Type& type);
155 
157  void AddType(const TfToken& name,
158  const VtValue& defaultValue,
159  const VtValue& defaultArrayValue,
160  const std::string& cppName, const std::string& cppArrayName,
161  TfEnum defaultUnit, const TfToken& role,
162  const SdfTupleDimensions& dimensions);
163 
168  void AddType(const TfToken& name,
169  const TfType& type, const TfType& arrayType,
170  const std::string& cppName, const std::string& cppArrayName,
171  TfEnum defaultUnit, const TfToken& role,
172  const SdfTupleDimensions& dimensions);
173 
176  void Clear();
177 
178 private:
179  class _Impl;
180  std::unique_ptr<_Impl> _impl;
181 };
182 
183 PXR_NAMESPACE_CLOSE_SCOPE
184 
185 #endif // PXR_USD_SDF_VALUE_TYPE_REGISTRY_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:194
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