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