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 41 /// \class Sdf_ValueTypeRegistry 42 /// 43 /// A registry of value type names used by a schema. 44 /// 45 class Sdf_ValueTypeRegistry : boost::noncopyable { 46 public: 47 Sdf_ValueTypeRegistry(); 48 ~Sdf_ValueTypeRegistry(); 49 50 /// Returns all registered value type names. 51 std::vector<SdfValueTypeName> GetAllTypes() const; 52 53 /// Returns a value type name by name. 54 SdfValueTypeName FindType(const TfToken& name) const; 55 SdfValueTypeName FindType(const char *name) const; 56 SdfValueTypeName FindType(const std::string &name) const; 57 58 /// Returns the value type name for the type and role if any, otherwise 59 /// returns the invalid value type name. This returns the first 60 /// registered value type name for a given type/role pair if there are 61 /// aliases 62 SdfValueTypeName FindType(const TfType& type, 63 const TfToken& role = TfToken()) const; 64 65 /// Returns the value type name for the held value and given role if 66 /// any, otherwise returns the invalid value type. This returns the 67 /// first registered name for a given type/role pair if there are 68 /// aliases. 69 SdfValueTypeName FindType(const VtValue& value, 70 const TfToken& role = TfToken()) const; 71 72 /// Returns a value type name by name. If a type with that name is 73 /// registered it returns the object for that name. Otherwise a 74 /// temporary type name is created and returned. This name will match 75 /// other temporary value type names that use the exact same name. Use 76 /// this function when you need to ensure that the name isn't lost even 77 /// if the type isn't registered, typically when writing the name to a 78 /// file or log. 79 SdfValueTypeName FindOrCreateTypeName(const TfToken& name) const; 80 81 /// \class Type 82 /// Named parameter object for specifying an SdfValueTypeName to 83 /// be added to the registry. 84 class Type 85 { 86 public: 87 // Specify a type with the given name, default value, and default 88 // array value. Type(const TfToken & name,const VtValue & defaultValue,const VtValue & defaultArrayValue)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> Type(char const * name,const T & defaultValue)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. Type(const TfToken & name,const TfType & type)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. CPPTypeName(const std::string & cppTypeName)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. Dimensions(const SdfTupleDimensions & dims)123 Type& Dimensions(const SdfTupleDimensions& dims) 124 { _dimensions = dims; return *this; } 125 126 // Set default unit for this type. Defaults to dimensionless unit. DefaultUnit(TfEnum unit)127 Type& DefaultUnit(TfEnum unit) { _unit = unit; return *this; } 128 129 // Set role for this type. Defaults to no role. Role(const TfToken & role)130 Type& Role(const TfToken& role) { _role = role; return *this; } 131 132 // Indicate that arrays of this type are not supported. NoArrays()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 152 /// Register the value type specified by \p type. 153 /// \see Type 154 void AddType(const Type& type); 155 156 /// Register a value type and it's corresponding array value type. 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 164 /// Register a value type and it's corresponding array value type. 165 /// In this case the default values are empty. This is useful for types 166 /// provided by plugins; you don't need to load the plugin just to 167 /// register the type. However, there is no default value. 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 174 /// Empties out the registry. Any existing types, roles or their names 175 /// become invalid and must not be used. 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 186