Skip to content

Commit

Permalink
Make the copy from a pointer logic cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 committed Apr 30, 2024
1 parent 0a5c35e commit 834e8db
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 37 deletions.
30 changes: 27 additions & 3 deletions src/app/AttributeEncodeState.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,38 @@
namespace chip {
namespace app {

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

/// Allows the encode state to be initialized from an OPTIONAL
/// other encoding state
///
/// if other is nullptr, this is the same as the default initializer.
AttributeEncodeState(AttributeEncodeState * other)
{
if (other != nullptr)
{
*this = *other;
}
else
{
mCurrentEncodingListIndex = kInvalidListIndex;
mAllowPartialData = false;
}
}

bool AllowPartialData() const { return mAllowPartialData; }
ListIndex CurrentEncodingListIndex() const { return mCurrentEncodingListIndex; }

Expand Down
4 changes: 2 additions & 2 deletions src/app/AttributeValueEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#pragma once

#include <access/SubjectDescriptor.h>
#include <app/AttributeReportBuilder.h>
#include <app/AttributeEncodeState.h>
#include <app/AttributeReportBuilder.h>
#include <app/ConcreteAttributePath.h>
#include <app/MessageDef/AttributeReportIBs.h>
#include <app/data-model/FabricScoped.h>
Expand Down Expand Up @@ -141,7 +141,7 @@ class AttributeValueEncoder
if (err == CHIP_NO_ERROR)
{
// The Encode procedure finished without any error, clear the state.
mEncodeState = AttributeEncodeState();
mEncodeState.Reset();
}
return err;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ source_set("events") {

static_library("attribute-access") {
sources = [
"AttributeAccessInterfaceCache.h",
"AttributeAccessInterface.h",
"AttributeAccessInterfaceCache.h",
"AttributeAccessInterfaceRegistry.cpp",
"AttributeAccessInterfaceRegistry.h",
"AttributeEncodeState.h",
Expand Down
7 changes: 1 addition & 6 deletions src/app/util/ember-compatibility-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,7 @@ CHIP_ERROR ReadViaAccessInterface(SubjectDescriptor subjectDescriptor, bool aIsF
AttributeEncodeState * aEncoderState, AttributeAccessInterface * aAccessInterface,
bool * aTriedEncode)
{
AttributeEncodeState state;
if (aEncoderState != nullptr)
{
state = *aEncoderState;
}

AttributeEncodeState state(aEncoderState);
DataVersion version = 0;
ReturnErrorOnFailure(ReadClusterDataVersion(aPath, version));
AttributeValueEncoder valueEncoder(aAttributeReports, subjectDescriptor, aPath, version, aIsFabricFiltered, state);
Expand Down
6 changes: 1 addition & 5 deletions src/app/util/mock/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,7 @@ CHIP_ERROR ReadSingleMockClusterData(FabricIndex aAccessingFabricIndex, const Co
// Attribute 4 acts as a large attribute to trigger chunking.
if (aPath.mAttributeId == MockAttributeId(4))
{
AttributeEncodeState state;
if (apEncoderState != nullptr)
{
state = *apEncoderState;
}
AttributeEncodeState state(apEncoderState);
Access::SubjectDescriptor subject;
subject.fabricIndex = aAccessingFabricIndex;

Expand Down
24 changes: 4 additions & 20 deletions src/controller/tests/data_model/TestRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ CHIP_ERROR ReadSingleClusterData(const Access::SubjectDescriptor & aSubjectDescr
// Use an incorrect attribute id for some of the responses.
path.mAttributeId =
static_cast<AttributeId>(path.mAttributeId + (i / 2) + (responseDirective == kSendManyDataResponsesWrongPath));
AttributeEncodeState state;
if (apEncoderState != nullptr)
{
state = *apEncoderState;
}
AttributeEncodeState state(apEncoderState);
AttributeValueEncoder valueEncoder(aAttributeReports, aSubjectDescriptor, path, kDataVersion, aIsFabricFiltered, state);
ReturnErrorOnFailure(valueEncoder.Encode(true));
}
Expand All @@ -126,11 +122,7 @@ CHIP_ERROR ReadSingleClusterData(const Access::SubjectDescriptor & aSubjectDescr
if (aPath.mClusterId == app::Clusters::UnitTesting::Id &&
aPath.mAttributeId == app::Clusters::UnitTesting::Attributes::ListFabricScoped::Id)
{
AttributeEncodeState state;
if (apEncoderState != nullptr)
{
state = *apEncoderState;
}
AttributeEncodeState state(apEncoderState);
AttributeValueEncoder valueEncoder(aAttributeReports, aSubjectDescriptor, aPath, kDataVersion, aIsFabricFiltered,
state);

Expand All @@ -146,11 +138,7 @@ CHIP_ERROR ReadSingleClusterData(const Access::SubjectDescriptor & aSubjectDescr
if (aPath.mClusterId == app::Clusters::UnitTesting::Id &&
aPath.mAttributeId == app::Clusters::UnitTesting::Attributes::Int16u::Id)
{
AttributeEncodeState state;
if (apEncoderState != nullptr)
{
state = *apEncoderState;
}
AttributeEncodeState state(apEncoderState);
AttributeValueEncoder valueEncoder(aAttributeReports, aSubjectDescriptor, aPath, kDataVersion, aIsFabricFiltered,
state);

Expand Down Expand Up @@ -182,11 +170,7 @@ CHIP_ERROR ReadSingleClusterData(const Access::SubjectDescriptor & aSubjectDescr
if (aPath.mClusterId == app::Clusters::IcdManagement::Id &&
aPath.mAttributeId == app::Clusters::IcdManagement::Attributes::OperatingMode::Id)
{
AttributeEncodeState state;
if (apEncoderState != nullptr)
{
state = *apEncoderState;
}
AttributeEncodeState state(apEncoderState);
AttributeValueEncoder valueEncoder(aAttributeReports, aSubjectDescriptor, aPath, kDataVersion, aIsFabricFiltered,
state);

Expand Down

0 comments on commit 834e8db

Please sign in to comment.