-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Replace chip::Optional with std::optional in StatusIB and StatusCode #39474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace chip::Optional with std::optional in StatusIB and StatusCode #39474
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request effectively replaces chip::Optional with the standard C++ std::optional across various files, notably in StatusIB and StatusCode components. The changes are consistent and correctly apply the std::optional API, including has_value(), dereferencing with * (safely guarded by checks), emplace(), value_or(), and std::nullopt.
The migration appears to be a clean search-and-replace as described, leading to code that leverages a standard library feature, which generally improves maintainability and correctness. The use of value_or in door-lock-server.cpp is a good example of idiomatic std::optional usage.
No specific style guide was provided, but the changes are consistent with common C++ practices and the existing codebase's style. All modifications in the diffs seem correct and beneficial. I did not identify any issues of medium, high, or critical severity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR replaces chip::Optional with std::optional for handling cluster-specific status codes in StatusIB and StatusCode, thereby standardizing the optional handling and saving flash.
- Updated all API usage from HasValue()/Value() to has_value()/dereference where applicable.
- Adjusted tests, error formatting, and logging to use std::optional.
- Ensured consistent handling in both application and controller layers.
Reviewed Changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/protocols/interaction_model/tests/TestStatusCode.cpp | Updated assertions to use std::nullopt instead of chip::NullOptional |
| src/protocols/interaction_model/StatusCode.h | Replaced chip::Optional with std::optional and updated related methods |
| src/protocols/interaction_model/StatusCode.cpp | Updated assignment of cluster-specific codes |
| src/darwin/Framework/CHIP/MTRError.mm | Adjusted error dictionary handling for cluster-specific code |
| src/controller/tests/data_model/TestWrite.cpp | Updated assertions to use has_value()/value() |
| src/controller/python/chip/clusters/command.cpp | Replaced chip::Optional API calls with std::optional equivalents |
| src/controller/java/AndroidCallbacks.cpp | Updated optional usage in JNI calls |
| src/app/tests/TestStatusIB.cpp | Updated test expectations using std::optional methods |
| src/app/tests/TestMessageDef.cpp | Replaced HasValue() with has_value() in status decoding |
| src/app/data-model-provider/ActionReturnStatus.cpp | Updated conversion and dereferencing of std::optional values |
| src/app/clusters/door-lock-server/door-lock-server.cpp | Replaced conditional handling with value_or and dereference usage |
| src/app/MessageDef/StatusIB.h | Transitioned to std::optional for mClusterStatus member |
| src/app/MessageDef/StatusIB.cpp | Adjusted encoding and error conversion to use std::optional |
| src/app/CommandHandlerImpl.cpp | Updated logging of cluster-specific failures with std::optional |
| src/app/ClusterStateCache.cpp | Replaced HasValue() with has_value() in size calculation |
| examples/fabric-admin/commands/common/RemoteDataModelLogger.cpp | Updated error logging to use std::optional dereference |
| examples/darwin-framework-tool/commands/common/RemoteDataModelLogger.mm | Adjusted handling of cluster-specific error codes for Darwin framework |
| examples/chip-tool/commands/common/RemoteDataModelLogger.cpp | Updated logging to use std::optional |
| examples/camera-controller/commands/common/RemoteDataModelLogger.cpp | Updated logging to use std::optional |
|
PR #39474: Size comparison from 151d2ac to 49e1f49 Increases above 0.2%:
Full report (72 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, linux, nrfconnect, nxp, psoc6, qpg, stm32, telink, tizen)
|
|
PR #39474: Size comparison from 11878a2 to 19fd82c Full report (29 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, nrfconnect, qpg, stm32, telink, tizen)
|
Co-authored-by: Sergio Soares <[email protected]>
|
PR #39474: Size comparison from 11878a2 to 13cb91c Full report (44 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, nrfconnect, psoc6, qpg, stm32, telink, tizen)
|
Co-authored-by: Sergio Soares <[email protected]>
Co-authored-by: Sergio Soares <[email protected]>
Co-authored-by: Sergio Soares <[email protected]>
Co-authored-by: Sergio Soares <[email protected]>
|
PR #39474: Size comparison from 11878a2 to 230e899 Full report (72 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, linux, nrfconnect, nxp, psoc6, qpg, stm32, telink, tizen)
|
|
PR #39474: Size comparison from 11878a2 to 415b821 Full report (18 builds for bl602, bl702, bl702l, stm32, telink, tizen)
|
|
PR #39474: Size comparison from 11878a2 to 991e1f1 Full report (49 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, nrfconnect, psoc6, qpg, stm32, telink, tizen)
|
|
PR #39474: Size comparison from 11878a2 to b072da0 Full report (72 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, linux, nrfconnect, nxp, psoc6, qpg, stm32, telink, tizen)
|
Summary
std::optional is generally more correct and this update seems to save some flash.
Testing
This is essentially a search&replace, so existing tests are remaining. CI is expected to still pass.