forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttributePathExpandIterator.h
213 lines (185 loc) · 8.32 KB
/
AttributePathExpandIterator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <app/AttributePathParams.h>
#include <app/ConcreteAttributePath.h>
#include <app/data-model-provider/MetadataList.h>
#include <app/data-model-provider/MetadataTypes.h>
#include <app/data-model-provider/Provider.h>
#include <lib/core/DataModelTypes.h>
#include <lib/support/LinkedList.h>
#include <lib/support/Span.h>
#include <limits>
namespace chip {
namespace app {
/// Handles attribute path expansions
/// Usage:
///
/// - Start iterating by creating an iteration state
///
/// AttributePathExpandIterator::Position position = AttributePathExpandIterator::Position::StartIterating(path);
///
/// - Use the iteration state in a for loop:
///
/// ConcreteAttributePath path;
/// for (AttributePathExpandIterator iterator(position); iterator->Next(path);) {
/// // use `path` here`
/// }
///
/// OR:
///
/// ConcreteAttributePath path;
/// AttributePathExpandIterator iterator(position);
///
/// while (iterator.Next(path)) {
/// // use `path` here`
/// }
///
/// Usage requirements and assumptions:
///
/// - An ` AttributePathExpandIterator::Position` can only be used by a single AttributePathExpandIterator at a time.
///
/// - `position` is automatically updated by the AttributePathExpandIterator, so
/// calling `Next` on the iterator will update the position cursor variable.
///
class AttributePathExpandIterator
{
public:
class Position
{
public:
// Position is treated as a direct member access by the AttributePathExpandIterator, however it is opaque (except copying)
// for external code. We allow friendship here to not have specific get/set for methods (clearer interface and less
// likelihood of extra code usage).
friend class AttributePathExpandIterator;
/// External callers can only ever start iterating on a new path from the beginning
static Position StartIterating(SingleLinkedListNode<AttributePathParams> * path) { return Position(path); }
/// Copies are allowed
Position(const Position &) = default;
Position & operator=(const Position &) = default;
Position() : mAttributePath(nullptr) {}
/// Reset the iterator to the beginning of current cluster if we are in the middle of expanding a wildcard attribute id for
/// some cluster.
///
/// When attributes are changed in the middle of expanding a wildcard attribute, we need to reset the iterator, to provide
/// the client with a consistent state of the cluster.
void IterateFromTheStartOfTheCurrentClusterIfAttributeWildcard()
{
VerifyOrReturn(mAttributePath != nullptr && mAttributePath->mValue.HasWildcardAttributeId());
mOutputPath.mAttributeId = kInvalidAttributeId;
}
protected:
Position(SingleLinkedListNode<AttributePathParams> * path) :
mAttributePath(path), mOutputPath(kInvalidEndpointId, kInvalidClusterId, kInvalidAttributeId)
{}
SingleLinkedListNode<AttributePathParams> * mAttributePath;
ConcreteAttributePath mOutputPath;
};
AttributePathExpandIterator(DataModel::Provider * dataModel, Position & position);
// This class may not be copied. A new one should be created when needed and they
// should not overlap.
AttributePathExpandIterator(const AttributePathExpandIterator &) = delete;
AttributePathExpandIterator & operator=(const AttributePathExpandIterator &) = delete;
/// Get the next path of the expansion (if one exists).
///
/// On success, true is returned and `path` is filled with the next path in the
/// expansion.
/// On iteration completion, false is returned and the content of path IS NOT DEFINED.
bool Next(ConcreteAttributePath & path);
private:
static constexpr size_t kInvalidIndex = std::numeric_limits<size_t>::max();
DataModel::Provider * mDataModelProvider;
Position & mPosition;
DataModel::MetadataList<DataModel::EndpointEntry> mEndpoints; // all endpoints
size_t mEndpointIndex = kInvalidIndex;
DataModel::MetadataList<DataModel::ServerClusterEntry> mClusters; // all clusters ON THE CURRENT endpoint
size_t mClusterIndex = kInvalidIndex;
DataModel::MetadataList<DataModel::AttributeEntry> mAttributes; // all attributes ON THE CURRENT cluster
size_t mAttributeIndex = kInvalidIndex;
/// Move to the next endpoint/cluster/attribute triplet that is valid given
/// the current mOutputPath and mpAttributePath.
///
/// returns true if such a next value was found.
bool AdvanceOutputPath();
/// Get the next attribute ID in mOutputPath(endpoint/cluster) if one is available.
/// Will start from the beginning if current mOutputPath.mAttributeId is kInvalidAttributeId
///
/// Respects path expansion/values in mpAttributePath
///
/// Handles Global attributes (which are returned at the end)
std::optional<AttributeId> NextAttributeId();
/// Get the next cluster ID in mOutputPath(endpoint) if one is available.
/// Will start from the beginning if current mOutputPath.mClusterId is kInvalidClusterId
///
/// Respects path expansion/values in mpAttributePath
std::optional<ClusterId> NextClusterId();
/// Get the next endpoint ID in mOutputPath if one is available.
/// Will start from the beginning if current mOutputPath.mEndpointId is kInvalidEndpointId
///
/// Respects path expansion/values in mpAttributePath
std::optional<EndpointId> NextEndpointId();
/// Checks if the given attributeId is valid for the current mOutputPath(endpoint/cluster)
///
/// Meaning that it is known to the data model OR it is a always-there global attribute.
bool IsValidAttributeId(AttributeId attributeId);
};
/// RollbackAttributePathExpandIterator is an AttributePathExpandIterator wrapper that rolls back the Next()
/// call whenever a new `MarkCompleted()` method is not called.
///
/// Example use cases:
///
/// - Iterate over all attributes and process one-by-one, however when the iteration fails, resume at
/// the last failure point:
///
/// RollbackAttributePathExpandIterator iterator(....);
/// ConcreteAttributePath path;
///
/// for ( ; iterator.Next(path); iterator.MarkCompleted()) {
/// if (!CanProcess(path)) {
/// // iterator state IS PRESERVED so that Next() will return the SAME path on the next call.
/// return CHIP_ERROR_TRY_AGAIN_LATER;
/// }
/// }
///
/// - Grab what the next output path would be WITHOUT advancing a state;
///
/// {
/// RollbackAttributePathExpandIterator iterator(...., state);
/// if (iterator.Next(...)) { ... }
/// }
/// // state here is ROLLED BACK (i.e. initializing a new iterator with it will start at the same place as the previous
/// iteration attempt).
///
///
class RollbackAttributePathExpandIterator
{
public:
RollbackAttributePathExpandIterator(DataModel::Provider * dataModel, AttributePathExpandIterator::Position & position) :
mAttributePathExpandIterator(dataModel, position), mPositionTarget(position), mCompletedPosition(position)
{}
~RollbackAttributePathExpandIterator() { mPositionTarget = mCompletedPosition; }
bool Next(ConcreteAttributePath & path) { return mAttributePathExpandIterator.Next(path); }
/// Marks the current iteration completed (so peek does not actually roll back)
void MarkCompleted() { mCompletedPosition = mPositionTarget; }
private:
AttributePathExpandIterator mAttributePathExpandIterator;
AttributePathExpandIterator::Position & mPositionTarget;
AttributePathExpandIterator::Position mCompletedPosition;
};
} // namespace app
} // namespace chip