Skip to content

Commit 614827d

Browse files
committed
Update on "[2/N] Add option context"
For future needs without breacking API BC, in case we need to pass more information to the update API Differential Revision: [D75919212](https://our.internmc.facebook.com/intern/diff/D75919212/) Differential Revision: [D75919212](https://our.internmc.facebook.com/intern/diff/D75919212) [ghstack-poisoned]
2 parents a0beade + 94b6d8e commit 614827d

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

runtime/backend/options.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,12 @@ class BackendOptions {
7474
*/
7575
BackendOptions() : size_(0) {}
7676

77-
/**
78-
* Returns a const view of all stored options as a Span.
79-
*
80-
* @return A const Span containing all BackendOption entries
81-
*/
82-
executorch::runtime::Span<const BackendOption> view() const {
83-
return executorch::runtime::Span<const BackendOption>(options_, size_);
84-
}
85-
8677
/**
8778
* Returns a mutable view of all stored options as a Span.
8879
*
8980
* @return A mutable Span containing all BackendOption entries
9081
*/
91-
executorch::runtime::Span<BackendOption> mutable_view() {
82+
executorch::runtime::Span<BackendOption> view() {
9283
return executorch::runtime::Span<BackendOption>(options_, size_);
9384
}
9485

@@ -101,9 +92,13 @@ class BackendOptions {
10192
* @param value The boolean value to set
10293
* @return Error::Ok on success, Error::InvalidArgument if storage is full
10394
*/
104-
template <size_t N>
105-
Error set_option(const char (&key)[N], bool value) noexcept {
106-
static_assert(N <= kMaxOptionKeyLength, "Option key is too long");
95+
Error set_option(const char* key, bool value) noexcept {
96+
ET_CHECK_MSG(
97+
strlen(key) <= kMaxOptionKeyLength,
98+
"Option key %s (%zu) is too long (max %zu)",
99+
key,
100+
strlen(key),
101+
kMaxOptionKeyLength);
107102
return set_option_impl(key, value);
108103
}
109104

@@ -116,9 +111,13 @@ class BackendOptions {
116111
* @param value The integer value to set
117112
* @return Error::Ok on success, Error::InvalidArgument if storage is full
118113
*/
119-
template <size_t N>
120-
Error set_option(const char (&key)[N], int value) noexcept {
121-
static_assert(N <= kMaxOptionKeyLength, "Option key is too long");
114+
Error set_option(const char* key, int value) noexcept {
115+
ET_CHECK_MSG(
116+
strlen(key) <= kMaxOptionKeyLength,
117+
"Option key %s (%zu) is too long (max %zu)",
118+
key,
119+
strlen(key),
120+
kMaxOptionKeyLength);
122121
return set_option_impl(key, value);
123122
}
124123

@@ -134,9 +133,13 @@ class BackendOptions {
134133
* @param value The string value to set (must have static storage duration)
135134
* @return Error::Ok on success, Error::InvalidArgument if storage is full
136135
*/
137-
template <size_t N>
138-
Error set_option(const char (&key)[N], const char* value) noexcept {
139-
static_assert(N <= kMaxOptionKeyLength, "Option key is too long");
136+
Error set_option(const char* key, const char* value) noexcept {
137+
ET_CHECK_MSG(
138+
strlen(key) <= kMaxOptionKeyLength,
139+
"Option key %s (%zu) is too long (max %zu)",
140+
key,
141+
strlen(key),
142+
kMaxOptionKeyLength);
140143
// Create a fixed-size array and copy the string
141144
std::array<char, kMaxOptionValueLength> arr;
142145
strncpy(arr.data(), value, kMaxOptionValueLength - 1);

runtime/backend/test/backend_options_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ TEST_F(BackendOptionsTest, MutableOption) {
101101
EXPECT_EQ(options.get_option("flag", ival), Error::Ok);
102102
EXPECT_EQ(ival, 0);
103103

104-
options.mutable_view()[0].value = 123; // Overwrites the entry
104+
options.view()[0].value = 123; // Overwrites the entry
105105

106106
// Integer get should succeed with the updated value
107107
EXPECT_EQ(options.get_option("flag", ival), Error::Ok);

0 commit comments

Comments
 (0)