HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
childrenUtils.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_SDF_CHILDREN_UTILS_H
25 #define PXR_USD_SDF_CHILDREN_UTILS_H
26 
27 /// \file sdf/childrenUtils.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/sdf/api.h"
31 #include "pxr/usd/sdf/allowed.h"
32 #include "pxr/usd/sdf/types.h"
33 
35 
36 /// \class Sdf_ChildrenUtils
37 ///
38 /// Helper functions for creating and manipulating the children
39 /// of a spec. A ChildPolicy must be provided that specifies which type
40 /// of children to edit. (See childrenPolicies.h for details).
41 ///
42 template<class ChildPolicy>
44 {
45 public:
46  /// The type of the key that identifies a child. This is usually
47  /// a std::string or an SdfPath.
48  typedef typename ChildPolicy::KeyType KeyType;
49 
50  /// The type of the child identifier as it's stored in the layer's data.
51  /// This is usually a TfToken.
52  typedef typename ChildPolicy::FieldType FieldType;
53 
54  /// Create a new spec in \a layer at \childPath and add it to its parent's
55  /// field named \childrenKey. Emit an error and return false if the new spec
56  /// couldn't be created.
57  static bool CreateSpec(
58  const SdfLayerHandle &layer,
59  const SdfPath &childPath,
60  SdfSpecType specType,
61  bool inert=true) {
62  return CreateSpec(get_pointer(layer), childPath, specType, inert);
63  }
64 
65  // This overload is intended primarily for internal use.
66  SDF_API
67  static bool CreateSpec(
68  SdfLayer *layer,
69  const SdfPath &childPath,
70  SdfSpecType specType,
71  bool inert=true);
72 
73  /// \name Rename API
74  /// @{
75 
76  /// Return whether \a newName is a valid name for a child.
77  SDF_API
78  static bool IsValidName(const FieldType &newName);
79 
80  /// Return whether \a newName is a valid name for a child.
81  SDF_API
82  static bool IsValidName(const std::string &newName);
83 
84  /// Return whether \a spec can be renamed to \a newName.
85  static SdfAllowed CanRename(
86  const SdfSpec &spec,
87  const FieldType &newName);
88 
89  /// Rename \a spec to \a newName. If \a fixPrimListEdits is true,
90  /// then also fix up the name children order. It's an error for
91  /// \a fixPrimListEdits to be true if spec is not a PrimSpec.
92  SDF_API
93  static bool Rename(
94  const SdfSpec &spec,
95  const FieldType &newName);
96 
97  /// @}
98 
99  /// \name Children List API
100  /// @{
101 
102  /// Replace the children of the spec at \a path with the specs in \a
103  /// values. This will delete existing children that aren't in \a values and
104  /// reparent children from other locations in the layer.
105  SDF_API
106  static bool SetChildren(
107  const SdfLayerHandle &layer,
108  const SdfPath &path,
109  const std::vector<typename ChildPolicy::ValueType> &values);
110 
111  /// Insert \a value as a child of \a path at the specified index.
112  SDF_API
113  static bool InsertChild(
114  const SdfLayerHandle &layer,
115  const SdfPath &path,
116  const typename ChildPolicy::ValueType& value,
117  int index);
118 
119  /// Remove the child identified by \a key.
120  SDF_API
121  static bool RemoveChild(
122  const SdfLayerHandle &layer,
123  const SdfPath &path,
124  const typename ChildPolicy::KeyType& key);
125 
126  /// @}
127  /// \name Batch editing API
128  /// @{
129 
130 
131  /// Insert \a value as a child of \a path at the specified index with
132  /// the new name \p newName.
133  SDF_API
134  static bool MoveChildForBatchNamespaceEdit(
135  const SdfLayerHandle &layer,
136  const SdfPath &path,
137  const typename ChildPolicy::ValueType& value,
138  const typename ChildPolicy::FieldType& newName,
139  int index);
140 
141  /// Remove the child identified by \a key.
142  SDF_API
144  const SdfLayerHandle &layer,
145  const SdfPath &path,
146  const typename ChildPolicy::KeyType& key)
147  {
148  return RemoveChild(layer, path, key);
149  }
150 
151  /// Returns \c true if \p value can be inserted as a child of \p path
152  /// with the new name \p newName at the index \p index, otherwise
153  /// returns \c false and sets \p whyNot.
154  SDF_API
156  const SdfLayerHandle &layer,
157  const SdfPath &path,
158  const typename ChildPolicy::ValueType& value,
159  const typename ChildPolicy::FieldType& newName,
160  int index,
161  std::string* whyNot);
162 
163  /// Returns \c true if the child of \p path identified by \p key can
164  /// be removed, otherwise returns \c false and sets \p whyNot.
165  SDF_API
167  const SdfLayerHandle &layer,
168  const SdfPath &path,
169  const typename ChildPolicy::FieldType& key,
170  std::string* whyNot);
171 
172  /// @}
173 };
174 
176 
177 #endif // PXR_USD_SDF_CHILDREN_UTILS_H
Definition: layer.h:97
static SDF_API bool MoveChildForBatchNamespaceEdit(const SdfLayerHandle &layer, const SdfPath &path, const typename ChildPolicy::ValueType &value, const typename ChildPolicy::FieldType &newName, int index)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
Definition: spec.h:49
static SDF_API bool Rename(const SdfSpec &spec, const FieldType &newName)
Y * get_pointer(TfWeakPtrFacade< X, Y > const &p)
Definition: weakPtrFacade.h:83
static SDF_API bool CanRemoveChildForBatchNamespaceEdit(const SdfLayerHandle &layer, const SdfPath &path, const typename ChildPolicy::FieldType &key, std::string *whyNot)
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
ChildPolicy::KeyType KeyType
Definition: childrenUtils.h:48
static bool CreateSpec(const SdfLayerHandle &layer, const SdfPath &childPath, SdfSpecType specType, bool inert=true)
Definition: childrenUtils.h:57
static SDF_API bool CanMoveChildForBatchNamespaceEdit(const SdfLayerHandle &layer, const SdfPath &path, const typename ChildPolicy::ValueType &value, const typename ChildPolicy::FieldType &newName, int index, std::string *whyNot)
Definition: path.h:290
static SDF_API bool RemoveChildForBatchNamespaceEdit(const SdfLayerHandle &layer, const SdfPath &path, const typename ChildPolicy::KeyType &key)
Remove the child identified by key.
#define SDF_API
Definition: api.h:40
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
SdfSpecType
Definition: types.h:85
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
static SDF_API bool InsertChild(const SdfLayerHandle &layer, const SdfPath &path, const typename ChildPolicy::ValueType &value, int index)
Insert value as a child of path at the specified index.
Definition: core.h:1131
static SdfAllowed CanRename(const SdfSpec &spec, const FieldType &newName)
Return whether spec can be renamed to newName.
static SDF_API bool RemoveChild(const SdfLayerHandle &layer, const SdfPath &path, const typename ChildPolicy::KeyType &key)
Remove the child identified by key.
static SDF_API bool IsValidName(const FieldType &newName)
Return whether newName is a valid name for a child.
bool ValueType
Definition: NanoVDB.h:5729
static SDF_API bool SetChildren(const SdfLayerHandle &layer, const SdfPath &path, const std::vector< typename ChildPolicy::ValueType > &values)
ChildPolicy::FieldType FieldType
Definition: childrenUtils.h:52