From a0d44fa97cafd52613819cc825854c951e75003a Mon Sep 17 00:00:00 2001 From: erwinpan1 Date: Wed, 10 Jan 2024 23:45:56 +0800 Subject: [PATCH] Use std::unique_ptr and ValueOr in AirPurifier sample Use std::unique_ptr to manage the pointer in chef-resource-monitoring-delegates.cpp Add ValueOr to chip::Nullable and simplify the code based on it --- .../chef/common/chef-fan-control-manager.cpp | 2 +- .../chef-resource-monitoring-delegates.cpp | 42 ++++++------------- src/app/data-model/Nullable.h | 4 ++ 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/examples/chef/common/chef-fan-control-manager.cpp b/examples/chef/common/chef-fan-control-manager.cpp index 3ed69d52ec090a..4e301c1ad511ce 100644 --- a/examples/chef/common/chef-fan-control-manager.cpp +++ b/examples/chef/common/chef-fan-control-manager.cpp @@ -93,7 +93,7 @@ Status ChefFanControlManager::HandleStep(StepDirectionEnum aDirection, bool aWra DataModel::Nullable speedSetting; SpeedSetting::Get(mEndpoint, speedSetting); - uint8_t newSpeedSetting = speedSetting.IsNull() ? 0 : speedSetting.Value(); + uint8_t newSpeedSetting = speedSetting.ValueOr(0); if (aDirection == StepDirectionEnum::kIncrease) { diff --git a/examples/chef/common/chef-resource-monitoring-delegates.cpp b/examples/chef/common/chef-resource-monitoring-delegates.cpp index 6fe11f1ade4e03..1fc5f4f9ce7953 100644 --- a/examples/chef/common/chef-resource-monitoring-delegates.cpp +++ b/examples/chef/common/chef-resource-monitoring-delegates.cpp @@ -36,11 +36,11 @@ constexpr std::bitset<4> gActivatedCarbonFeatureMap{ static_cast(Resou static_cast(ResourceMonitoring::Feature::kWarning) | static_cast(ResourceMonitoring::Feature::kReplacementProductList) }; -static ActivatedCarbonFilterMonitoringDelegate * gActivatedCarbonFilterDelegate = nullptr; -static ResourceMonitoring::Instance * gActivatedCarbonFilterInstance = nullptr; +static std::unique_ptr gActivatedCarbonFilterDelegate = nullptr; +static std::unique_ptr gActivatedCarbonFilterInstance = nullptr; -static HepaFilterMonitoringDelegate * gHepaFilterDelegate = nullptr; -static ResourceMonitoring::Instance * gHepaFilterInstance = nullptr; +static std::unique_ptr gHepaFilterDelegate = nullptr; +static std::unique_ptr gHepaFilterInstance = nullptr; static ImmutableReplacementProductListManager sReplacementProductListManager; @@ -66,16 +66,8 @@ Status ActivatedCarbonFilterMonitoringDelegate::PostResetCondition() void ActivatedCarbonFilterMonitoring::Shutdown() { - if (gActivatedCarbonFilterInstance != nullptr) - { - delete gActivatedCarbonFilterInstance; - gActivatedCarbonFilterInstance = nullptr; - } - if (gActivatedCarbonFilterDelegate != nullptr) - { - delete gActivatedCarbonFilterDelegate; - gActivatedCarbonFilterDelegate = nullptr; - } + gActivatedCarbonFilterInstance = nullptr; + gActivatedCarbonFilterDelegate = nullptr; } //-- Hepa Filter Monitoring delegate methods @@ -100,24 +92,16 @@ Status HepaFilterMonitoringDelegate::PostResetCondition() void HepaFilterMonitoring::Shutdown() { - if (gHepaFilterInstance != nullptr) - { - delete gHepaFilterInstance; - gHepaFilterInstance = nullptr; - } - if (gHepaFilterDelegate != nullptr) - { - delete gHepaFilterDelegate; - gHepaFilterDelegate = nullptr; - } + gHepaFilterInstance = nullptr; + gHepaFilterDelegate = nullptr; } void emberAfActivatedCarbonFilterMonitoringClusterInitCallback(chip::EndpointId endpoint) { VerifyOrDie(gActivatedCarbonFilterInstance == nullptr && gActivatedCarbonFilterDelegate == nullptr); - gActivatedCarbonFilterDelegate = new ActivatedCarbonFilterMonitoringDelegate; - gActivatedCarbonFilterInstance = new ResourceMonitoring::Instance( - gActivatedCarbonFilterDelegate, endpoint, ActivatedCarbonFilterMonitoring::Id, + gActivatedCarbonFilterDelegate = std::make_unique(); + gActivatedCarbonFilterInstance = std::make_unique( + gActivatedCarbonFilterDelegate.get(), endpoint, ActivatedCarbonFilterMonitoring::Id, static_cast(gActivatedCarbonFeatureMap.to_ulong()), ResourceMonitoring::DegradationDirectionEnum::kDown, true); gActivatedCarbonFilterInstance->Init(); } @@ -126,8 +110,8 @@ void emberAfHepaFilterMonitoringClusterInitCallback(chip::EndpointId endpoint) { VerifyOrDie(gHepaFilterInstance == nullptr && gHepaFilterDelegate == nullptr); - gHepaFilterDelegate = new HepaFilterMonitoringDelegate; - gHepaFilterInstance = new ResourceMonitoring::Instance(gHepaFilterDelegate, endpoint, HepaFilterMonitoring::Id, + gHepaFilterDelegate = std::make_unique(); + gHepaFilterInstance = std::make_unique(gHepaFilterDelegate.get(), endpoint, HepaFilterMonitoring::Id, static_cast(gHepaFilterFeatureMap.to_ulong()), ResourceMonitoring::DegradationDirectionEnum::kDown, true); gHepaFilterInstance->Init(); diff --git a/src/app/data-model/Nullable.h b/src/app/data-model/Nullable.h index dec7fc5eb9ef55..9941a347515181 100644 --- a/src/app/data-model/Nullable.h +++ b/src/app/data-model/Nullable.h @@ -49,6 +49,10 @@ struct Nullable : protected Optional // Optional. using Optional::Value; + // Pull in APIs that make sense on Nullable with the same names as on + // Optional. + using Optional::ValueOr; + // Some consumers need an easy way to determine our underlying type. using UnderlyingType = T;