Skip to content

Commit ad92562

Browse files
andy31415andreilitvinrestyled-commits
authored
Use a reference to the context on ServerClusterInterface::Startup (#37929)
* Use a reference to the context on ServerClusterInterface::Startup This is to make it clear that the input value is never null. * Restyled by clang-format --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent bdc569b commit ad92562

6 files changed

+16
-10
lines changed

src/app/server-cluster/DefaultServerCluster.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ CHIP_ERROR DefaultServerCluster::Attributes(const ConcreteClusterPath & path, Da
8585
return builder.ReferenceExisting(GlobalAttributes());
8686
}
8787

88-
CHIP_ERROR DefaultServerCluster::Startup(ServerClusterContext * context)
88+
CHIP_ERROR DefaultServerCluster::Startup(ServerClusterContext & context)
8989
{
9090
VerifyOrReturnError(mContext == nullptr, CHIP_ERROR_ALREADY_INITIALIZED);
91-
mContext = context;
91+
mContext = &context;
9292
return CHIP_NO_ERROR;
9393
}
9494

src/app/server-cluster/DefaultServerCluster.h

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

1919
#include <app/server-cluster/ServerClusterInterface.h>
20+
#include <optional>
2021

2122
namespace chip {
2223
namespace app {
@@ -45,7 +46,7 @@ class DefaultServerCluster : public ServerClusterInterface
4546
/// been initialized.
4647
///
4748
/// Call Shutdown to de-initialize the object.
48-
CHIP_ERROR Startup(ServerClusterContext * context) override;
49+
CHIP_ERROR Startup(ServerClusterContext & context) override;
4950
void Shutdown() override;
5051

5152
[[nodiscard]] DataVersion GetDataVersion() const override { return mDataVersion; }

src/app/server-cluster/ServerClusterInterface.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ class ServerClusterInterface
4646
/// Starts up the server cluster interface.
4747
///
4848
/// The `context` lifetime must be guaranteed to last
49-
/// until `Shutdown` is called.
50-
virtual CHIP_ERROR Startup(ServerClusterContext * context) = 0;
49+
/// until `Shutdown` is called:
50+
///
51+
/// - You are allowed to take and use a pointer to it until
52+
/// shutdown is called.
53+
/// - If context is needed, you SHOULD store a pointer rather
54+
/// than a copy to save RAM usage.
55+
virtual CHIP_ERROR Startup(ServerClusterContext & context) = 0;
5156

5257
/// A shutdown will always be paired with a corresponding Startup.
5358
virtual void Shutdown() = 0;

src/app/server-cluster/testing/TestServerClusterContext.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717
#pragma once
1818

19-
#include "app/data-model-provider/Context.h"
2019
#include <app/data-model-provider/ActionContext.h>
20+
#include <app/data-model-provider/Context.h>
2121
#include <app/data-model-provider/Provider.h>
2222
#include <app/server-cluster/ServerClusterContext.h>
2323
#include <app/server-cluster/testing/EmptyProvider.h>
@@ -59,7 +59,7 @@ class TestServerClusterContext
5959
}
6060

6161
/// Get a stable pointer to the underlying context
62-
app::ServerClusterContext * Get() { return &mContext; }
62+
app::ServerClusterContext & Get() { return mContext; }
6363

6464
/// Create a new context bound to this test context
6565
app::ServerClusterContext Create()

src/data-model-providers/codegen/ServerClusterInterfaceRegistry.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ CHIP_ERROR ServerClusterInterfaceRegistry::Register(ServerClusterRegistration &
5858

5959
if (mContext.has_value())
6060
{
61-
ReturnErrorOnFailure(entry.serverClusterInterface->Startup(&*mContext));
61+
ReturnErrorOnFailure(entry.serverClusterInterface->Startup(*mContext));
6262
}
6363

6464
entry.next = mRegistrations;
@@ -193,7 +193,7 @@ CHIP_ERROR ServerClusterInterfaceRegistry::SetContext(ServerClusterContext && co
193193

194194
for (ServerClusterRegistration * registration = mRegistrations; registration != nullptr; registration = registration->next)
195195
{
196-
CHIP_ERROR err = registration->serverClusterInterface->Startup(&*mContext);
196+
CHIP_ERROR err = registration->serverClusterInterface->Startup(*mContext);
197197
if (err != CHIP_NO_ERROR)
198198
{
199199
#if CHIP_ERROR_LOGGING

src/data-model-providers/codegen/tests/TestServerClusterInterfaceRegistry.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class CannotStartUpCluster : public FakeServerClusterInterface
7676
{
7777
public:
7878
CannotStartUpCluster(EndpointId endpoint, ClusterId id) : FakeServerClusterInterface(endpoint, id) {}
79-
CHIP_ERROR Startup(ServerClusterContext * context) override { return CHIP_ERROR_BUSY; }
79+
CHIP_ERROR Startup(ServerClusterContext & context) override { return CHIP_ERROR_BUSY; }
8080
};
8181

8282
struct TestServerClusterInterfaceRegistry : public ::testing::Test

0 commit comments

Comments
 (0)