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 #include "pxr/usd/usdContrived/publicMultipleApplyAPI.h"
25 #include "pxr/usd/usd/schemaRegistry.h"
26 #include "pxr/usd/usd/typed.h"
27 #include "pxr/usd/usd/tokens.h"
28 
29 #include "pxr/usd/sdf/types.h"
30 #include "pxr/usd/sdf/assetPath.h"
31 
32 namespace foo { namespace bar { namespace baz {
33 
34 // Register the schema with the TfType system.
TF_REGISTRY_FUNCTION(TfType)35 TF_REGISTRY_FUNCTION(TfType)
36 {
37     TfType::Define<UsdContrivedPublicMultipleApplyAPI,
38         TfType::Bases< UsdAPISchemaBase > >();
39 
40 }
41 
42 TF_DEFINE_PRIVATE_TOKENS(
43     _schemaTokens,
44     (PublicMultipleApplyAPI)
45     (testo)
46 );
47 
48 /* virtual */
~UsdContrivedPublicMultipleApplyAPI()49 UsdContrivedPublicMultipleApplyAPI::~UsdContrivedPublicMultipleApplyAPI()
50 {
51 }
52 
53 /* static */
54 UsdContrivedPublicMultipleApplyAPI
Get(const UsdStagePtr & stage,const SdfPath & path)55 UsdContrivedPublicMultipleApplyAPI::Get(const UsdStagePtr &stage, const SdfPath &path)
56 {
57     if (!stage) {
58         TF_CODING_ERROR("Invalid stage");
59         return UsdContrivedPublicMultipleApplyAPI();
60     }
61     TfToken name;
62     if (!IsPublicMultipleApplyAPIPath(path, &name)) {
63         TF_CODING_ERROR("Invalid testo path <%s>.", path.GetText());
64         return UsdContrivedPublicMultipleApplyAPI();
65     }
66     return UsdContrivedPublicMultipleApplyAPI(stage->GetPrimAtPath(path.GetPrimPath()), name);
67 }
68 
69 UsdContrivedPublicMultipleApplyAPI
Get(const UsdPrim & prim,const TfToken & name)70 UsdContrivedPublicMultipleApplyAPI::Get(const UsdPrim &prim, const TfToken &name)
71 {
72     return UsdContrivedPublicMultipleApplyAPI(prim, name);
73 }
74 
75 
76 /* static */
77 bool
IsSchemaPropertyBaseName(const TfToken & baseName)78 UsdContrivedPublicMultipleApplyAPI::IsSchemaPropertyBaseName(const TfToken &baseName)
79 {
80     static TfTokenVector attrsAndRels = {
81         UsdContrivedTokens->testAttrOne,
82         UsdContrivedTokens->testAttrTwo,
83     };
84 
85     return find(attrsAndRels.begin(), attrsAndRels.end(), baseName)
86             != attrsAndRels.end();
87 }
88 
89 /* static */
90 bool
IsPublicMultipleApplyAPIPath(const SdfPath & path,TfToken * name)91 UsdContrivedPublicMultipleApplyAPI::IsPublicMultipleApplyAPIPath(
92     const SdfPath &path, TfToken *name)
93 {
94     if (!path.IsPropertyPath()) {
95         return false;
96     }
97 
98     std::string propertyName = path.GetName();
99     TfTokenVector tokens = SdfPath::TokenizeIdentifierAsTokens(propertyName);
100 
101     // The baseName of the  path can't be one of the
102     // schema properties. We should validate this in the creation (or apply)
103     // API.
104     TfToken baseName = *tokens.rbegin();
105     if (IsSchemaPropertyBaseName(baseName)) {
106         return false;
107     }
108 
109     if (tokens.size() >= 2
110         && tokens[0] == _schemaTokens->testo) {
111         *name = TfToken(propertyName.substr(
112             _schemaTokens->testo.GetString().size() + 1));
113         return true;
114     }
115 
116     return false;
117 }
118 
119 /* virtual */
_GetSchemaKind() const120 UsdSchemaKind UsdContrivedPublicMultipleApplyAPI::_GetSchemaKind() const
121 {
122     return UsdContrivedPublicMultipleApplyAPI::schemaKind;
123 }
124 
125 /* static */
126 bool
CanApply(const UsdPrim & prim,const TfToken & name,std::string * whyNot)127 UsdContrivedPublicMultipleApplyAPI::CanApply(
128     const UsdPrim &prim, const TfToken &name, std::string *whyNot)
129 {
130     return prim.CanApplyAPI<UsdContrivedPublicMultipleApplyAPI>(name, whyNot);
131 }
132 
133 /* static */
134 UsdContrivedPublicMultipleApplyAPI
Apply(const UsdPrim & prim,const TfToken & name)135 UsdContrivedPublicMultipleApplyAPI::Apply(const UsdPrim &prim, const TfToken &name)
136 {
137     if (prim.ApplyAPI<UsdContrivedPublicMultipleApplyAPI>(name)) {
138         return UsdContrivedPublicMultipleApplyAPI(prim, name);
139     }
140     return UsdContrivedPublicMultipleApplyAPI();
141 }
142 
143 /* static */
144 const TfType &
_GetStaticTfType()145 UsdContrivedPublicMultipleApplyAPI::_GetStaticTfType()
146 {
147     static TfType tfType = TfType::Find<UsdContrivedPublicMultipleApplyAPI>();
148     return tfType;
149 }
150 
151 /* static */
152 bool
_IsTypedSchema()153 UsdContrivedPublicMultipleApplyAPI::_IsTypedSchema()
154 {
155     static bool isTyped = _GetStaticTfType().IsA<UsdTyped>();
156     return isTyped;
157 }
158 
159 /* virtual */
160 const TfType &
_GetTfType() const161 UsdContrivedPublicMultipleApplyAPI::_GetTfType() const
162 {
163     return _GetStaticTfType();
164 }
165 
166 /// Returns the property name prefixed with the correct namespace prefix, which
167 /// is composed of the the API's propertyNamespacePrefix metadata and the
168 /// instance name of the API.
169 static inline
170 TfToken
_GetNamespacedPropertyName(const TfToken instanceName,const TfToken propName)171 _GetNamespacedPropertyName(const TfToken instanceName, const TfToken propName)
172 {
173     TfTokenVector identifiers =
174         {_schemaTokens->testo, instanceName, propName};
175     return TfToken(SdfPath::JoinIdentifier(identifiers));
176 }
177 
178 UsdAttribute
GetTestAttrOneAttr() const179 UsdContrivedPublicMultipleApplyAPI::GetTestAttrOneAttr() const
180 {
181     return GetPrim().GetAttribute(
182         _GetNamespacedPropertyName(
183             GetName(),
184             UsdContrivedTokens->testAttrOne));
185 }
186 
187 UsdAttribute
CreateTestAttrOneAttr(VtValue const & defaultValue,bool writeSparsely) const188 UsdContrivedPublicMultipleApplyAPI::CreateTestAttrOneAttr(VtValue const &defaultValue, bool writeSparsely) const
189 {
190     return UsdSchemaBase::_CreateAttr(
191                        _GetNamespacedPropertyName(
192                             GetName(),
193                            UsdContrivedTokens->testAttrOne),
194                        SdfValueTypeNames->Int,
195                        /* custom = */ false,
196                        SdfVariabilityVarying,
197                        defaultValue,
198                        writeSparsely);
199 }
200 
201 UsdAttribute
GetTestAttrTwoAttr() const202 UsdContrivedPublicMultipleApplyAPI::GetTestAttrTwoAttr() const
203 {
204     return GetPrim().GetAttribute(
205         _GetNamespacedPropertyName(
206             GetName(),
207             UsdContrivedTokens->testAttrTwo));
208 }
209 
210 UsdAttribute
CreateTestAttrTwoAttr(VtValue const & defaultValue,bool writeSparsely) const211 UsdContrivedPublicMultipleApplyAPI::CreateTestAttrTwoAttr(VtValue const &defaultValue, bool writeSparsely) const
212 {
213     return UsdSchemaBase::_CreateAttr(
214                        _GetNamespacedPropertyName(
215                             GetName(),
216                            UsdContrivedTokens->testAttrTwo),
217                        SdfValueTypeNames->Double,
218                        /* custom = */ false,
219                        SdfVariabilityVarying,
220                        defaultValue,
221                        writeSparsely);
222 }
223 
224 namespace {
225 static inline TfTokenVector
_ConcatenateAttributeNames(const TfToken instanceName,const TfTokenVector & left,const TfTokenVector & right)226 _ConcatenateAttributeNames(
227     const TfToken instanceName,
228     const TfTokenVector& left,
229     const TfTokenVector& right)
230 {
231     TfTokenVector result;
232     result.reserve(left.size() + right.size());
233     result.insert(result.end(), left.begin(), left.end());
234 
235     for (const TfToken attrName : right) {
236         result.push_back(
237             _GetNamespacedPropertyName(instanceName, attrName));
238     }
239     result.insert(result.end(), right.begin(), right.end());
240     return result;
241 }
242 }
243 
244 /*static*/
245 const TfTokenVector&
GetSchemaAttributeNames(bool includeInherited,const TfToken instanceName)246 UsdContrivedPublicMultipleApplyAPI::GetSchemaAttributeNames(
247     bool includeInherited, const TfToken instanceName)
248 {
249     static TfTokenVector localNames = {
250         UsdContrivedTokens->testAttrOne,
251         UsdContrivedTokens->testAttrTwo,
252     };
253     static TfTokenVector allNames =
254         _ConcatenateAttributeNames(
255             instanceName,
256             UsdAPISchemaBase::GetSchemaAttributeNames(true),
257             localNames);
258 
259     if (includeInherited)
260         return allNames;
261     else
262         return localNames;
263 }
264 
265 }}}
266 
267 // ===================================================================== //
268 // Feel free to add custom code below this line. It will be preserved by
269 // the code generator.
270 //
271 // Just remember to wrap code in the appropriate delimiters:
272 // 'namespace foo { namespace bar { namespace baz {', '}}}'.
273 // ===================================================================== //
274 // --(BEGIN CUSTOM CODE)--
275