Skip to content

Commit e7ed125

Browse files
committed
use static instance
1 parent 7db7377 commit e7ed125

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

examples/chef/common/chef-rvc-operational-state-delegate.cpp

+8-19
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ using namespace chip::app::Clusters::OperationalState;
2828
using namespace chip::app::Clusters::RvcOperationalState;
2929
using chip::Protocols::InteractionModel::Status;
3030

31-
static RvcOperationalState::Instance * gRvcOperationalStateInstance = nullptr;
32-
static RvcOperationalStateDelegate * gRvcOperationalStateDelegate = nullptr;
31+
static std::unique_ptr<RvcOperationalStateDelegate> gRvcOperationalStateDelegate;
32+
static std::unique_ptr<RvcOperationalState::Instance > gRvcOperationalStateInstance;
3333

3434
static void onOperationalStateTimerTick(System::Layer * systemLayer, void * data);
3535

@@ -141,7 +141,7 @@ static void onOperationalStateTimerTick(System::Layer * systemLayer, void * data
141141
{
142142
RvcOperationalStateDelegate * delegate = reinterpret_cast<RvcOperationalStateDelegate *>(data);
143143

144-
OperationalState::Instance * instance = gRvcOperationalStateInstance;
144+
OperationalState::Instance * instance = gRvcOperationalStateInstance.get();
145145
OperationalState::OperationalStateEnum state =
146146
static_cast<OperationalState::OperationalStateEnum>(instance->GetCurrentOperationalState());
147147

@@ -171,16 +171,8 @@ static void onOperationalStateTimerTick(System::Layer * systemLayer, void * data
171171

172172
void RvcOperationalState::Shutdown()
173173
{
174-
if (gRvcOperationalStateInstance != nullptr)
175-
{
176-
delete gRvcOperationalStateInstance;
177-
gRvcOperationalStateInstance = nullptr;
178-
}
179-
if (gRvcOperationalStateDelegate != nullptr)
180-
{
181-
delete gRvcOperationalStateDelegate;
182-
gRvcOperationalStateDelegate = nullptr;
183-
}
174+
gRvcOperationalStateInstance.reset();
175+
gRvcOperationalStateDelegate.reset();
184176
}
185177

186178
chip::Protocols::InteractionModel::Status chefRvcOperationalStateWriteCallback(chip::EndpointId endpointId,
@@ -261,13 +253,10 @@ chip::Protocols::InteractionModel::Status chefRvcOperationalStateReadCallback(ch
261253
void emberAfRvcOperationalStateClusterInitCallback(chip::EndpointId endpointId)
262254
{
263255
VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1.
264-
VerifyOrDie(gRvcOperationalStateInstance == nullptr && gRvcOperationalStateDelegate == nullptr);
265-
266-
gRvcOperationalStateDelegate = new chip::app::Clusters::RvcOperationalState::RvcOperationalStateDelegate();
267-
EndpointId operationalStateEndpoint = 0x01;
268-
gRvcOperationalStateInstance = new RvcOperationalState::Instance(gRvcOperationalStateDelegate, operationalStateEndpoint);
256+
VerifyOrDie(!gRvcOperationalStateDelegate && !gRvcOperationalStateInstance);
269257

270-
gRvcOperationalStateInstance->SetOperationalState(to_underlying(OperationalState::OperationalStateEnum::kStopped));
258+
gRvcOperationalStateDelegate = std::make_unique<RvcOperationalStateDelegate>();
259+
gRvcOperationalStateInstance = std::make_unique<RvcOperationalState::Instance>(gRvcOperationalStateDelegate.get(), endpointId);
271260
gRvcOperationalStateInstance->Init();
272261
}
273262
#endif // MATTER_DM_PLUGIN_RVC_OPERATIONAL_STATE_SERVER

0 commit comments

Comments
 (0)