66 * LICENSE file in the root directory of this source tree.
77 */
88
9- #pragma once
10- #include < executorch/runtime/core/error.h>
11- #include < cstddef>
12- #include < cstring>
13- #include < variant>
14-
15- namespace executorch {
16- namespace runtime {
17-
18- // Strongly-typed option key template
19- template <typename T>
20- struct OptionKey {
21- const char * key;
22- constexpr explicit OptionKey (const char * k) : key(k) {}
23- };
24-
25- // Union replaced with std::variant
26- using OptionValue = std::variant<bool , int , const char *>;
27-
28- struct BackendOption {
29- const char * key; // key is the name of the backend option, like num_threads,
30- // enable_profiling, etc
31- OptionValue
32- value; // value is the value of the backend option, like 4, true, etc
33- };
34-
35- template <size_t MaxCapacity>
36- class BackendOptions {
37- public:
38- // Initialize with zero options
39- BackendOptions () : size_(0 ) {}
40-
41- // Type-safe setters
42- template <typename T>
43- void set_option (OptionKey<T> key, T value) {
44- const char * k = key.key ;
45- // Update existing if found
46- for (size_t i = 0 ; i < size_; ++i) {
47- if (strcmp (options_[i].key , k) == 0 ) {
48- options_[i].value = value;
49- return ;
50- }
51- }
52- // Add new option if space available
53- if (size_ < MaxCapacity) {
54- options_[size_++] = BackendOption{k, value};
55- }
56- }
57-
58- // Type-safe getters
59- template <typename T>
60- Error get_option (OptionKey<T> key, T& out) const {
61- const char * k = key.key ;
62- for (size_t i = 0 ; i < size_; ++i) {
63- if (strcmp (options_[i].key , k) == 0 ) {
64- if (auto * val = std::get_if<T>(&options_[i].value )) {
65- out = *val;
66- return Error::Ok;
67- }
68- return Error::InvalidArgument;
69- }
70- }
71- return Error::NotFound;
72- }
73-
74- private:
75- BackendOption options_[MaxCapacity]{}; // Storage for backend options
76- size_t size_; // Current number of options
77- };
78-
79- // Helper functions for creating typed option keys (unchanged)
80- constexpr OptionKey<bool > BoolKey (const char * k) {
81- return OptionKey<bool >(k);
82- }
83-
84- constexpr OptionKey<int > IntKey (const char * k) {
85- return OptionKey<int >(k);
86- }
87-
88- constexpr OptionKey<const char *> StrKey (const char * k) {
89- return OptionKey<const char *>(k);
90- }
91-
92- } // namespace runtime
93- } // namespace executorch
9+ #pragma once
10+ #include < executorch/runtime/core/error.h>
11+ #include < cstddef>
12+ #include < cstring>
13+ #include < executorch/runtime/core/array_ref.h>
14+ #include < executorch/runtime/core/error.h>
15+ #include < variant>
16+
17+ namespace executorch {
18+ namespace runtime {
19+
20+ // Strongly-typed option key template
21+ template <typename T>
22+ struct OptionKey {
23+ const char * key;
24+ constexpr explicit OptionKey (const char * k) : key(k) {}
25+ };
26+
27+ // Union replaced with std::variant
28+ using OptionValue = std::variant<bool , int , const char *>;
29+
30+ struct BackendOption {
31+ const char * key; // key is the name of the backend option, like num_threads,
32+ // enable_profiling, etc
33+ OptionValue
34+ value; // value is the value of the backend option, like 4, true, etc
35+ };
36+
37+ template <size_t MaxCapacity>
38+ class BackendOptions {
39+ public:
40+ // Initialize with zero options
41+ BackendOptions () : size_(0 ) {}
42+
43+ // Type-safe setters
44+ template <typename T>
45+ void set_option (OptionKey<T> key, T value) {
46+ const char * k = key.key ;
47+ // Update existing if found
48+ for (size_t i = 0 ; i < size_; ++i) {
49+ if (strcmp (options_[i].key , k) == 0 ) {
50+ options_[i].value = value;
51+ return ;
52+ }
53+ }
54+ // Add new option if space available
55+ if (size_ < MaxCapacity) {
56+ options_[size_++] = BackendOption{k, value};
57+ }
58+ }
59+
60+ // Type-safe getters
61+ template <typename T>
62+ Error get_option (OptionKey<T> key, T& out) const {
63+ const char * k = key.key ;
64+ for (size_t i = 0 ; i < size_; ++i) {
65+ if (strcmp (options_[i].key , k) == 0 ) {
66+ if (auto * val = std::get_if<T>(&options_[i].value )) {
67+ out = *val;
68+ return Error::Ok;
69+ }
70+ return Error::InvalidArgument;
71+ }
72+ }
73+ return Error::NotFound;
74+ }
75+ executorch::runtime::ArrayRef<BackendOption> view () const {
76+ return executorch::runtime::ArrayRef<BackendOption>(options_, size_);
77+ }
78+
79+ private:
80+ BackendOption options_[MaxCapacity]{}; // Storage for backend options
81+ size_t size_; // Current number of options
82+ };
83+
84+ // Helper functions for creating typed option keys (unchanged)
85+ constexpr OptionKey<bool > BoolKey (const char * k) {
86+ return OptionKey<bool >(k);
87+ }
88+
89+ constexpr OptionKey<int > IntKey (const char * k) {
90+ return OptionKey<int >(k);
91+ }
92+
93+ constexpr OptionKey<const char *> StrKey (const char * k) {
94+ return OptionKey<const char *>(k);
95+ }
96+
97+ } // namespace runtime
98+ } // namespace executorch
99+
0 commit comments