Skip to content

Commit

Permalink
Use std::unique_ptr and ValueOr in AirPurifier sample
Browse files Browse the repository at this point in the history
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
  • Loading branch information
erwinpan1 committed Jan 10, 2024
1 parent 8994e7a commit a0d44fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
2 changes: 1 addition & 1 deletion examples/chef/common/chef-fan-control-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Status ChefFanControlManager::HandleStep(StepDirectionEnum aDirection, bool aWra
DataModel::Nullable<uint8_t> speedSetting;
SpeedSetting::Get(mEndpoint, speedSetting);

uint8_t newSpeedSetting = speedSetting.IsNull() ? 0 : speedSetting.Value();
uint8_t newSpeedSetting = speedSetting.ValueOr(0);

if (aDirection == StepDirectionEnum::kIncrease)
{
Expand Down
42 changes: 13 additions & 29 deletions examples/chef/common/chef-resource-monitoring-delegates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ constexpr std::bitset<4> gActivatedCarbonFeatureMap{ static_cast<uint32_t>(Resou
static_cast<uint32_t>(ResourceMonitoring::Feature::kWarning) |
static_cast<uint32_t>(ResourceMonitoring::Feature::kReplacementProductList) };

static ActivatedCarbonFilterMonitoringDelegate * gActivatedCarbonFilterDelegate = nullptr;
static ResourceMonitoring::Instance * gActivatedCarbonFilterInstance = nullptr;
static std::unique_ptr<ActivatedCarbonFilterMonitoringDelegate> gActivatedCarbonFilterDelegate = nullptr;
static std::unique_ptr<ResourceMonitoring::Instance> gActivatedCarbonFilterInstance = nullptr;

static HepaFilterMonitoringDelegate * gHepaFilterDelegate = nullptr;
static ResourceMonitoring::Instance * gHepaFilterInstance = nullptr;
static std::unique_ptr<HepaFilterMonitoringDelegate> gHepaFilterDelegate = nullptr;
static std::unique_ptr<ResourceMonitoring::Instance> gHepaFilterInstance = nullptr;

static ImmutableReplacementProductListManager sReplacementProductListManager;

Expand All @@ -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
Expand All @@ -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<ActivatedCarbonFilterMonitoringDelegate>();
gActivatedCarbonFilterInstance = std::make_unique<ResourceMonitoring::Instance>(
gActivatedCarbonFilterDelegate.get(), endpoint, ActivatedCarbonFilterMonitoring::Id,
static_cast<uint32_t>(gActivatedCarbonFeatureMap.to_ulong()), ResourceMonitoring::DegradationDirectionEnum::kDown, true);
gActivatedCarbonFilterInstance->Init();
}
Expand All @@ -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<HepaFilterMonitoringDelegate>();
gHepaFilterInstance = std::make_unique<ResourceMonitoring::Instance>(gHepaFilterDelegate.get(), endpoint, HepaFilterMonitoring::Id,
static_cast<uint32_t>(gHepaFilterFeatureMap.to_ulong()),
ResourceMonitoring::DegradationDirectionEnum::kDown, true);
gHepaFilterInstance->Init();
Expand Down
4 changes: 4 additions & 0 deletions src/app/data-model/Nullable.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ struct Nullable : protected Optional<T>
// Optional.
using Optional<T>::Value;

// Pull in APIs that make sense on Nullable with the same names as on
// Optional.
using Optional<T>::ValueOr;

// Some consumers need an easy way to determine our underlying type.
using UnderlyingType = T;

Expand Down

0 comments on commit a0d44fa

Please sign in to comment.