Loading...
Searching...
No Matches
mapEditor.h
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_MAP_EDITOR_H
8#define PXR_USD_SDF_MAP_EDITOR_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/sdf/allowed.h"
12#include "pxr/usd/sdf/spec.h"
13
14#include "pxr/base/vt/value.h"
16#include "pxr/base/tf/token.h"
17
18#include <memory>
19#include <string>
20#include <utility>
21
22PXR_NAMESPACE_OPEN_SCOPE
23
24class VtValueRef;
25
31class Sdf_MapEditorBase {
32public:
35 SDF_API std::string GetLocation() const;
36
38 SDF_API SdfSpecHandle GetOwner() const;
39
41 SDF_API bool IsExpired() const;
42
43protected:
44 SDF_API Sdf_MapEditorBase(const SdfSpecHandle& owner, const TfToken& field);
45
46 SDF_API SdfAllowed _IsValidKey(const VtValueRef& key) const;
47 SDF_API SdfAllowed _IsValidValue(const VtValueRef& value) const;
48 SDF_API void _WriteToSpec(const VtValueRef& value);
49
50 SdfSpecHandle _owner;
51 TfToken _field;
52};
53
58template <class MapType>
59class Sdf_MapEditor : public Sdf_MapEditorBase {
60public:
61 typedef typename MapType::key_type key_type;
62 typedef typename MapType::mapped_type mapped_type;
63 typedef typename MapType::value_type value_type;
64 typedef typename MapType::iterator iterator;
65
66 Sdf_MapEditor(const SdfSpecHandle& owner, const TfToken& field)
67 : Sdf_MapEditorBase(owner, field)
68 {
69 const VtValue& dataVal = _owner->GetField(_field);
70 if (!dataVal.IsEmpty()) {
71 if (dataVal.IsHolding<MapType>()) {
72 _data = dataVal.Get<MapType>();
73 }
74 else {
75 TF_CODING_ERROR("%s does not hold value of expected type.",
76 GetLocation().c_str());
77 }
78 }
79 }
80
81 const MapType* GetData() const { return &_data; }
82 MapType* GetData() { return &_data; }
83
86
87 void Copy(const MapType& other)
88 {
89 _data = other;
90 _UpdateDataInSpec();
91 }
92
93 void Set(const key_type& key, const mapped_type& other)
94 {
95 _data[key] = other;
96 _UpdateDataInSpec();
97 }
98
99 std::pair<iterator, bool> Insert(const value_type& value)
100 {
101 const std::pair<iterator, bool> insertStatus = _data.insert(value);
102 if (insertStatus.second) {
103 _UpdateDataInSpec();
104 }
105 return insertStatus;
106 }
107
108 bool Erase(const key_type& key)
109 {
110 const bool didErase = (_data.erase(key) != 0);
111 if (didErase) {
112 _UpdateDataInSpec();
113 }
114 return didErase;
115 }
116
117 SdfAllowed IsValidKey(const key_type& key) const
118 {
119 return _IsValidKey(VtValueRef(key));
120 }
121
122 SdfAllowed IsValidValue(const mapped_type& value) const
123 {
124 return _IsValidValue(VtValueRef(value));
125 }
126
128
129private:
130 void _UpdateDataInSpec()
131 {
132 _WriteToSpec(_data.empty() ? VtValueRef() : VtValueRef(_data));
133 }
134
135 MapType _data;
136};
137
138template <class T>
139std::unique_ptr<Sdf_MapEditor<T>>
140Sdf_CreateMapEditor(const SdfSpecHandle& owner, const TfToken& field)
141{
142 return std::make_unique<Sdf_MapEditor<T>>(owner, field);
143}
144
145PXR_NAMESPACE_CLOSE_SCOPE
146
147#endif // PXR_USD_SDF_MAP_EDITOR_H
Indicates if an operation is allowed and, if not, why not.
Definition allowed.h:29
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:81
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition value.h:90
bool IsEmpty() const
Returns true iff this value is empty.
Definition value.h:1286
A non-owning type-erased view of a value, interoperating with VtValue.
Definition valueRef.h:65
Stripped down version of diagnostic.h that doesn't define std::string.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...