Skip to content

Commit c9024ed

Browse files
committed
Make the copy from a pointer logic cleaner
1 parent 4a71023 commit c9024ed

File tree

6 files changed

+36
-37
lines changed

6 files changed

+36
-37
lines changed

src/app/AttributeEncodeState.h

+27-3
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,38 @@
2121
namespace chip {
2222
namespace app {
2323

24-
/**
25-
* Maintains the internal state of list encoding
26-
*/
24+
/// Maintains the internal state of list encoding
25+
///
26+
/// List encoding is generally assumed incremental and chunkable (i.e.
27+
/// partial encoding is ok.). For this purpose the class maintains two
28+
/// pieces of data:
29+
/// - AllowPartialData tracks if partial encoding is acceptable in the
30+
/// current encoding state (to be used for atomic/non-atomic list item writes)
31+
/// - CurrentEncodingListIndex representing the list index that is next
32+
/// to be encoded in the output. kInvalidListIndex means that a new list
33+
/// encoding has been started.
2734
class AttributeEncodeState
2835
{
2936
public:
3037
AttributeEncodeState() = default;
3138

39+
/// Allows the encode state to be initialized from an OPTIONAL
40+
/// other encoding state
41+
///
42+
/// if other is nullptr, this is the same as the default initializer.
43+
AttributeEncodeState(AttributeEncodeState * other)
44+
{
45+
if (other != nullptr)
46+
{
47+
*this = *other;
48+
}
49+
else
50+
{
51+
mCurrentEncodingListIndex = kInvalidListIndex;
52+
mAllowPartialData = false;
53+
}
54+
}
55+
3256
bool AllowPartialData() const { return mAllowPartialData; }
3357
ListIndex CurrentEncodingListIndex() const { return mCurrentEncodingListIndex; }
3458

src/app/AttributeValueEncoder.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#pragma once
1818

1919
#include <access/SubjectDescriptor.h>
20-
#include <app/AttributeReportBuilder.h>
2120
#include <app/AttributeEncodeState.h>
21+
#include <app/AttributeReportBuilder.h>
2222
#include <app/ConcreteAttributePath.h>
2323
#include <app/MessageDef/AttributeReportIBs.h>
2424
#include <app/data-model/FabricScoped.h>
@@ -141,7 +141,7 @@ class AttributeValueEncoder
141141
if (err == CHIP_NO_ERROR)
142142
{
143143
// The Encode procedure finished without any error, clear the state.
144-
mEncodeState = AttributeEncodeState();
144+
mEncodeState.Reset();
145145
}
146146
return err;
147147
}

src/app/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ source_set("events") {
277277

278278
static_library("attribute-access") {
279279
sources = [
280-
"AttributeAccessInterfaceCache.h",
281280
"AttributeAccessInterface.h",
281+
"AttributeAccessInterfaceCache.h",
282282
"AttributeAccessInterfaceRegistry.cpp",
283283
"AttributeAccessInterfaceRegistry.h",
284284
"AttributeEncodeState.h",

src/app/util/ember-compatibility-functions.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,7 @@ CHIP_ERROR ReadViaAccessInterface(SubjectDescriptor subjectDescriptor, bool aIsF
430430
AttributeEncodeState * aEncoderState, AttributeAccessInterface * aAccessInterface,
431431
bool * aTriedEncode)
432432
{
433-
AttributeEncodeState state;
434-
if (aEncoderState != nullptr)
435-
{
436-
state = *aEncoderState;
437-
}
438-
433+
AttributeEncodeState state(aEncoderState);
439434
DataVersion version = 0;
440435
ReturnErrorOnFailure(ReadClusterDataVersion(aPath, version));
441436
AttributeValueEncoder valueEncoder(aAttributeReports, subjectDescriptor, aPath, version, aIsFabricFiltered, state);

src/app/util/mock/attribute-storage.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,7 @@ CHIP_ERROR ReadSingleMockClusterData(FabricIndex aAccessingFabricIndex, const Co
350350
// Attribute 4 acts as a large attribute to trigger chunking.
351351
if (aPath.mAttributeId == MockAttributeId(4))
352352
{
353-
AttributeEncodeState state;
354-
if (apEncoderState != nullptr)
355-
{
356-
state = *apEncoderState;
357-
}
353+
AttributeEncodeState state(apEncoderState);
358354
Access::SubjectDescriptor subject;
359355
subject.fabricIndex = aAccessingFabricIndex;
360356

src/controller/tests/data_model/TestRead.cpp

+4-20
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ CHIP_ERROR ReadSingleClusterData(const Access::SubjectDescriptor & aSubjectDescr
109109
// Use an incorrect attribute id for some of the responses.
110110
path.mAttributeId =
111111
static_cast<AttributeId>(path.mAttributeId + (i / 2) + (responseDirective == kSendManyDataResponsesWrongPath));
112-
AttributeEncodeState state;
113-
if (apEncoderState != nullptr)
114-
{
115-
state = *apEncoderState;
116-
}
112+
AttributeEncodeState state(apEncoderState);
117113
AttributeValueEncoder valueEncoder(aAttributeReports, aSubjectDescriptor, path, kDataVersion, aIsFabricFiltered, state);
118114
ReturnErrorOnFailure(valueEncoder.Encode(true));
119115
}
@@ -126,11 +122,7 @@ CHIP_ERROR ReadSingleClusterData(const Access::SubjectDescriptor & aSubjectDescr
126122
if (aPath.mClusterId == app::Clusters::UnitTesting::Id &&
127123
aPath.mAttributeId == app::Clusters::UnitTesting::Attributes::ListFabricScoped::Id)
128124
{
129-
AttributeEncodeState state;
130-
if (apEncoderState != nullptr)
131-
{
132-
state = *apEncoderState;
133-
}
125+
AttributeEncodeState state(apEncoderState);
134126
AttributeValueEncoder valueEncoder(aAttributeReports, aSubjectDescriptor, aPath, kDataVersion, aIsFabricFiltered,
135127
state);
136128

@@ -146,11 +138,7 @@ CHIP_ERROR ReadSingleClusterData(const Access::SubjectDescriptor & aSubjectDescr
146138
if (aPath.mClusterId == app::Clusters::UnitTesting::Id &&
147139
aPath.mAttributeId == app::Clusters::UnitTesting::Attributes::Int16u::Id)
148140
{
149-
AttributeEncodeState state;
150-
if (apEncoderState != nullptr)
151-
{
152-
state = *apEncoderState;
153-
}
141+
AttributeEncodeState state(apEncoderState);
154142
AttributeValueEncoder valueEncoder(aAttributeReports, aSubjectDescriptor, aPath, kDataVersion, aIsFabricFiltered,
155143
state);
156144

@@ -182,11 +170,7 @@ CHIP_ERROR ReadSingleClusterData(const Access::SubjectDescriptor & aSubjectDescr
182170
if (aPath.mClusterId == app::Clusters::IcdManagement::Id &&
183171
aPath.mAttributeId == app::Clusters::IcdManagement::Attributes::OperatingMode::Id)
184172
{
185-
AttributeEncodeState state;
186-
if (apEncoderState != nullptr)
187-
{
188-
state = *apEncoderState;
189-
}
173+
AttributeEncodeState state(apEncoderState);
190174
AttributeValueEncoder valueEncoder(aAttributeReports, aSubjectDescriptor, aPath, kDataVersion, aIsFabricFiltered,
191175
state);
192176

0 commit comments

Comments
 (0)