Skip to content

Commit d51a00c

Browse files
committed
Strong const correctness into the contexts
1 parent 29a1502 commit d51a00c

8 files changed

+63
-49
lines changed

src/app/server-cluster/ServerClusterContext.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ namespace app {
3434
/// for every cluster maintaining such a context.
3535
struct ServerClusterContext
3636
{
37-
DataModel::Provider * provider = nullptr; /// underlying provider that the cluster operates in
38-
PersistentStorageDelegate * storage = nullptr; /// read/write persistent storage
39-
DataModel::InteractionModelContext * interactionContext = nullptr; /// outside-world communication
37+
DataModel::Provider * const provider = nullptr; /// underlying provider that the cluster operates in
38+
PersistentStorageDelegate * const storage = nullptr; /// read/write persistent storage
39+
DataModel::InteractionModelContext * const interactionContext = nullptr; /// outside-world communication
40+
41+
ServerClusterContext(ServerClusterContext &&) = default;
4042

4143
bool operator!=(const ServerClusterContext & other) const
4244
{

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

+23-6
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,35 @@ class NullActionContext : public app::DataModel::ActionContext
4242
/// At thist time, `interactionContext::actionContext::CurrentExchange` WILL return nullptr
4343
/// in the existing implementation as the exchange is too heavy of an object
4444
/// to create for testing
45-
class TestServerClusterContext : public app::ServerClusterContext
45+
class TestServerClusterContext
4646
{
4747
public:
48-
TestServerClusterContext()
48+
TestServerClusterContext() :
49+
mContext{
50+
.provider = &mTestProvider,
51+
.storage = &mTestStorage,
52+
.interactionContext = &mTestContext,
53+
}
4954
{
5055
mTestContext.eventsGenerator = &mTestEventsGenerator;
5156
mTestContext.dataModelChangeListener = &mTestDataModelChangeListener;
5257
mTestContext.actionContext = &mNullActionContext;
53-
54-
provider = &mTestProvider;
55-
storage = &mTestStorage;
56-
interactionContext = &mTestContext;
5758
}
5859

60+
/// Get a stable pointer to the underlying context
61+
app::ServerClusterContext * Get() { return &mContext; }
62+
63+
/// Create a new context bound to this test context
64+
app::ServerClusterContext Create()
65+
{
66+
return {
67+
.provider = &mTestProvider,
68+
.storage = &mTestStorage,
69+
.interactionContext = &mTestContext,
70+
71+
};
72+
};
73+
5974
LogOnlyEvents & EventsGenerator() { return mTestEventsGenerator; }
6075
TestProviderChangeListener & ChangeListener() { return mTestDataModelChangeListener; }
6176
TestPersistentStorageDelegate & StorageDelegate() { return mTestStorage; }
@@ -68,6 +83,8 @@ class TestServerClusterContext : public app::ServerClusterContext
6883
TestPersistentStorageDelegate mTestStorage;
6984

7085
app::DataModel::InteractionModelContext mTestContext;
86+
87+
app::ServerClusterContext mContext;
7188
};
7289

7390
} // namespace Test

src/app/server-cluster/tests/TestDefaultServerCluster.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
#include "app/server-cluster/ServerClusterContext.h"
1718
#include <pw_unit_test/framework.h>
1819

1920
#include <access/Privilege.h>
@@ -190,7 +191,7 @@ TEST(TestDefaultServerCluster, NotifyAttributeChanged)
190191

191192
// Create a ServerClusterContext and verify that attribute change notifications are processed.
192193
TestServerClusterContext context;
193-
ASSERT_EQ(cluster.Startup(&context), CHIP_NO_ERROR);
194+
ASSERT_EQ(cluster.Startup(context.Get()), CHIP_NO_ERROR);
194195

195196
oldVersion = cluster.GetDataVersion();
196197
cluster.TestNotifyAttributeChanged(234);
@@ -214,7 +215,7 @@ TEST(TestDefaultServerCluster, NotifyAllAttributesChanged)
214215

215216
// Create a ServerClusterContext and verify that attribute change notifications are processed.
216217
TestServerClusterContext context;
217-
ASSERT_EQ(cluster.Startup(&context), CHIP_NO_ERROR);
218+
ASSERT_EQ(cluster.Startup(context.Get()), CHIP_NO_ERROR);
218219

219220
oldVersion = cluster.GetDataVersion();
220221
cluster.TestNotifyAllAttributesChanged();

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

+5-10
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,11 @@ CHIP_ERROR CodegenDataModelProvider::Startup(DataModel::InteractionModelContext
164164

165165
InitDataModelForTesting();
166166

167-
{
168-
ServerClusterContext clusterContext;
169-
clusterContext.provider = this;
170-
clusterContext.storage = mPersistentStorageDelegate;
171-
clusterContext.interactionContext = &mContext;
172-
173-
ReturnErrorOnFailure(mRegistry.SetContext(clusterContext));
174-
}
175-
176-
return CHIP_NO_ERROR;
167+
return mRegistry.SetContext(ServerClusterContext{
168+
.provider = this,
169+
.storage = mPersistentStorageDelegate,
170+
.interactionContext = &mContext,
171+
});
177172
}
178173

179174
std::optional<DataModel::ActionReturnStatus> CodegenDataModelProvider::InvokeCommand(const DataModel::InvokeRequest & request,

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

+13-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <lib/core/DataModelTypes.h>
2323
#include <lib/support/CHIPMem.h>
2424
#include <lib/support/CodeUtils.h>
25+
#include <optional>
2526

2627
namespace chip {
2728
namespace app {
@@ -31,7 +32,7 @@ ServerClusterInterfaceRegistry::~ServerClusterInterfaceRegistry()
3132
while (mRegistrations != nullptr)
3233
{
3334
ServerClusterRegistration * next = mRegistrations->next;
34-
if (mContextIsValid)
35+
if (mContext.has_value())
3536
{
3637
mRegistrations->serverClusterInterface->Shutdown();
3738
}
@@ -55,9 +56,9 @@ CHIP_ERROR ServerClusterInterfaceRegistry::Register(ServerClusterRegistration &
5556
// items. We preserve this however we may want to make this optional at some point in time.
5657
VerifyOrReturnError(Get(path) == nullptr, CHIP_ERROR_DUPLICATE_KEY_ID);
5758

58-
if (mContextIsValid)
59+
if (mContext.has_value())
5960
{
60-
ReturnErrorOnFailure(entry.serverClusterInterface->Startup(&mContext));
61+
ReturnErrorOnFailure(entry.serverClusterInterface->Startup(&*mContext));
6162
}
6263

6364
entry.next = mRegistrations;
@@ -93,7 +94,7 @@ ServerClusterInterface * ServerClusterInterfaceRegistry::Unregister(const Concre
9394
}
9495

9596
current->next = nullptr; // Make sure current does not look like part of a list.
96-
if (mContextIsValid)
97+
if (mContext.has_value())
9798
{
9899
current->serverClusterInterface->Shutdown();
99100
}
@@ -137,7 +138,7 @@ void ServerClusterInterfaceRegistry::UnregisterAllFromEndpoint(EndpointId endpoi
137138
ServerClusterRegistration * actual_next = current->next;
138139

139140
current->next = nullptr; // Make sure current does not look like part of a list.
140-
if (mContextIsValid)
141+
if (mContext.has_value())
141142
{
142143
current->serverClusterInterface->Shutdown();
143144
}
@@ -178,22 +179,21 @@ ServerClusterInterface * ServerClusterInterfaceRegistry::Get(const ConcreteClust
178179
return nullptr;
179180
}
180181

181-
CHIP_ERROR ServerClusterInterfaceRegistry::SetContext(const ServerClusterContext & context)
182+
CHIP_ERROR ServerClusterInterfaceRegistry::SetContext(ServerClusterContext && context)
182183
{
183-
if (mContextIsValid)
184+
if (mContext.has_value())
184185
{
185186
// if there is no difference, do not re-initialize.
186-
VerifyOrReturnError(mContext != context, CHIP_NO_ERROR);
187+
VerifyOrReturnError(*mContext != context, CHIP_NO_ERROR);
187188
ClearContext();
188189
}
189190

190-
mContext = context;
191-
mContextIsValid = true;
191+
mContext.emplace(std::move(context));
192192
bool had_failure = false;
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
@@ -219,7 +219,7 @@ CHIP_ERROR ServerClusterInterfaceRegistry::SetContext(const ServerClusterContext
219219

220220
void ServerClusterInterfaceRegistry::ClearContext()
221221
{
222-
if (!mContextIsValid)
222+
if (!mContext.has_value())
223223
{
224224
return;
225225
}
@@ -228,8 +228,7 @@ void ServerClusterInterfaceRegistry::ClearContext()
228228
registration->serverClusterInterface->Shutdown();
229229
}
230230

231-
mContext = ServerClusterContext{};
232-
mContextIsValid = false;
231+
mContext.reset();
233232
}
234233

235234
} // namespace app

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class ServerClusterInterfaceRegistry
127127
// Set up the underlying context for all clusters that are managed by this registry.
128128
//
129129
// The values within context will be copied and used.
130-
CHIP_ERROR SetContext(const ServerClusterContext & context);
130+
CHIP_ERROR SetContext(ServerClusterContext && context);
131131

132132
// Invalidates current context.
133133
void ClearContext();
@@ -140,8 +140,7 @@ class ServerClusterInterfaceRegistry
140140
ServerClusterInterface * mCachedInterface = nullptr;
141141

142142
// Managing context for this registry
143-
bool mContextIsValid = false;
144-
ServerClusterContext mContext;
143+
std::optional<ServerClusterContext> mContext;
145144
};
146145

147146
} // namespace app

src/data-model-providers/codegen/tests/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ chip_test_suite("tests") {
6262
public_deps = [
6363
":mock_model",
6464
"${chip_root}/src/app/data-model-provider:string-builder-adapters",
65+
"${chip_root}/src/app/server-cluster/testing",
6566
]
6667
}

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <app-common/zap-generated/ids/Attributes.h>
2020
#include <app/ConcreteClusterPath.h>
2121
#include <app/server-cluster/DefaultServerCluster.h>
22+
#include <app/server-cluster/ServerClusterContext.h>
23+
#include <app/server-cluster/testing/TestServerClusterContext.h>
2224
#include <data-model-providers/codegen/ServerClusterInterfaceRegistry.h>
2325
#include <lib/core/CHIPError.h>
2426
#include <lib/core/DataModelTypes.h>
@@ -29,6 +31,7 @@
2931
#include <random>
3032

3133
using namespace chip;
34+
using namespace chip::Test;
3235
using namespace chip::app;
3336
using namespace chip::app::DataModel;
3437
using namespace chip::app::Clusters;
@@ -348,8 +351,8 @@ TEST_F(TestServerClusterInterfaceRegistry, Context)
348351
EXPECT_FALSE(cluster1.HasContext());
349352

350353
// set up the registry
351-
ServerClusterContext nullContext; // not valid, however we do not care
352-
EXPECT_EQ(registry.SetContext(nullContext), CHIP_NO_ERROR);
354+
TestServerClusterContext context;
355+
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_NO_ERROR);
353356

354357
EXPECT_TRUE(cluster1.HasContext());
355358
EXPECT_FALSE(cluster2.HasContext());
@@ -365,7 +368,7 @@ TEST_F(TestServerClusterInterfaceRegistry, Context)
365368
EXPECT_FALSE(cluster2.HasContext());
366369
EXPECT_FALSE(cluster3.HasContext());
367370

368-
EXPECT_EQ(registry.SetContext(nullContext), CHIP_NO_ERROR);
371+
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_NO_ERROR);
369372
EXPECT_TRUE(cluster1.HasContext());
370373
EXPECT_TRUE(cluster2.HasContext());
371374
EXPECT_FALSE(cluster3.HasContext());
@@ -380,17 +383,15 @@ TEST_F(TestServerClusterInterfaceRegistry, Context)
380383
EXPECT_TRUE(cluster3.HasContext());
381384

382385
// re-setting context works
383-
EXPECT_EQ(registry.SetContext(nullContext), CHIP_NO_ERROR);
386+
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_NO_ERROR);
384387
EXPECT_TRUE(cluster1.HasContext());
385388
EXPECT_FALSE(cluster2.HasContext());
386389
EXPECT_TRUE(cluster3.HasContext());
387390

388391
// also not valid, but different
389-
ServerClusterContext otherContext;
390-
InteractionModelContext modelContext;
391-
otherContext.interactionContext = &modelContext;
392+
TestServerClusterContext otherContext;
392393

393-
EXPECT_EQ(registry.SetContext(otherContext), CHIP_NO_ERROR);
394+
EXPECT_EQ(registry.SetContext(otherContext.Create()), CHIP_NO_ERROR);
394395
EXPECT_TRUE(cluster1.HasContext());
395396
EXPECT_FALSE(cluster2.HasContext());
396397
EXPECT_TRUE(cluster3.HasContext());
@@ -412,7 +413,6 @@ TEST_F(TestServerClusterInterfaceRegistry, StartupErrors)
412413

413414
{
414415
ServerClusterInterfaceRegistry registry;
415-
416416
EXPECT_FALSE(cluster1.HasContext());
417417
EXPECT_FALSE(cluster2.HasContext());
418418

@@ -423,8 +423,8 @@ TEST_F(TestServerClusterInterfaceRegistry, StartupErrors)
423423
EXPECT_FALSE(cluster1.HasContext());
424424
EXPECT_FALSE(cluster2.HasContext());
425425

426-
ServerClusterContext nullContext; // not valid, however we do not care
427-
EXPECT_EQ(registry.SetContext(nullContext), CHIP_ERROR_HAD_FAILURES);
426+
TestServerClusterContext context;
427+
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_ERROR_HAD_FAILURES);
428428
EXPECT_TRUE(cluster1.HasContext());
429429
EXPECT_FALSE(cluster2.HasContext());
430430

0 commit comments

Comments
 (0)