|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <cstddef> |
| 10 | +#include <cstring> |
| 11 | +#include <executorch/runtime/core/error.h> |
| 12 | + |
| 13 | +namespace executorch { |
| 14 | +namespace runtime { |
| 15 | + |
| 16 | +// Strongly-typed option key template |
| 17 | +// Wraps a string key with type information for type-safe option access |
| 18 | +template <typename T> |
| 19 | +struct OptionKey { |
| 20 | + const char* key; // String identifier for the option |
| 21 | + constexpr explicit OptionKey(const char* k) : key(k) {} |
| 22 | +}; |
| 23 | + |
| 24 | +// Supported option data types |
| 25 | +enum class OptionType { BOOL, INT, STRING }; |
| 26 | + |
| 27 | +// Union-like container for option values (only one member is valid per option) |
| 28 | +struct OptionValue { |
| 29 | + bool bool_value; // Storage for boolean values |
| 30 | + int int_value; // Storage for integer values |
| 31 | + const char* string_value; // Storage for string values |
| 32 | +}; |
| 33 | + |
| 34 | +// Represents a single backend configuration option |
| 35 | +struct BackendOption { |
| 36 | + const char* key; // Name of the option |
| 37 | + OptionType type; // Data type of the option |
| 38 | + OptionValue value; // Current value of the option |
| 39 | +}; |
| 40 | + |
| 41 | +// Fixed-capacity container for backend options with type-safe access |
| 42 | +// MaxCapacity: Maximum number of options this container can hold |
| 43 | +template <size_t MaxCapacity> |
| 44 | +class BackendOptions { |
| 45 | + public: |
| 46 | + // Initialize with zero options |
| 47 | + BackendOptions() : size(0) {} |
| 48 | + |
| 49 | + // Type-safe setters --------------------------------------------------- |
| 50 | + |
| 51 | + /// Sets or updates a boolean option |
| 52 | + /// @param key: Typed option key |
| 53 | + /// @param value: Boolean value to set |
| 54 | + void set_option(OptionKey<bool> key, bool value) { |
| 55 | + set_option_internal(key.key, OptionType::BOOL, {.bool_value = value}); |
| 56 | + } |
| 57 | + |
| 58 | + /// Sets or updates an integer option |
| 59 | + /// @param key: Typed option key |
| 60 | + /// @param value: Integer value to set |
| 61 | + void set_option(OptionKey<int> key, int value) { |
| 62 | + set_option_internal(key.key, OptionType::INT, {.int_value = value}); |
| 63 | + } |
| 64 | + |
| 65 | + /// Sets or updates a string option |
| 66 | + /// @param key: Typed option key |
| 67 | + /// @param value: Null-terminated string value to set |
| 68 | + void set_option(OptionKey<const char*> key, const char* value) { |
| 69 | + set_option_internal(key.key, OptionType::STRING, {.string_value = value}); |
| 70 | + } |
| 71 | + |
| 72 | + // Type-safe getters --------------------------------------------------- |
| 73 | + |
| 74 | + /// Retrieves a boolean option value |
| 75 | + /// @param key: Typed option key |
| 76 | + /// @param out_value: Reference to store retrieved value |
| 77 | + /// @return: Error code (Ok on success) |
| 78 | + executorch::runtime::Error get_option(OptionKey<bool> key, bool& out_value) |
| 79 | + const { |
| 80 | + OptionValue val{}; |
| 81 | + executorch::runtime::Error err = |
| 82 | + get_option_internal(key.key, OptionType::BOOL, val); |
| 83 | + if (err == executorch::runtime::Error::Ok) { |
| 84 | + out_value = val.bool_value; |
| 85 | + } |
| 86 | + return err; |
| 87 | + } |
| 88 | + |
| 89 | + /// Retrieves an integer option value |
| 90 | + /// @param key: Typed option key |
| 91 | + /// @param out_value: Reference to store retrieved value |
| 92 | + /// @return: Error code (Ok on success) |
| 93 | + executorch::runtime::Error get_option(OptionKey<int> key, int& out_value) |
| 94 | + const { |
| 95 | + OptionValue val{}; |
| 96 | + executorch::runtime::Error err = |
| 97 | + get_option_internal(key.key, OptionType::INT, val); |
| 98 | + if (err == executorch::runtime::Error::Ok) { |
| 99 | + out_value = val.int_value; |
| 100 | + } |
| 101 | + return err; |
| 102 | + } |
| 103 | + |
| 104 | + /// Retrieves a string option value |
| 105 | + /// @param key: Typed option key |
| 106 | + /// @param out_value: Reference to store retrieved pointer |
| 107 | + /// @return: Error code (Ok on success) |
| 108 | + executorch::runtime::Error get_option( |
| 109 | + OptionKey<const char*> key, |
| 110 | + const char*& out_value) const { |
| 111 | + OptionValue val{}; |
| 112 | + executorch::runtime::Error err = |
| 113 | + get_option_internal(key.key, OptionType::STRING, val); |
| 114 | + if (err == executorch::runtime::Error::Ok) { |
| 115 | + out_value = val.string_value; |
| 116 | + } |
| 117 | + return err; |
| 118 | + } |
| 119 | + |
| 120 | + private: |
| 121 | + BackendOption options[MaxCapacity]{}; // Storage for options |
| 122 | + size_t size; // Current number of options |
| 123 | + |
| 124 | + // Internal helper to set/update an option |
| 125 | + void |
| 126 | + set_option_internal(const char* key, OptionType type, OptionValue value) { |
| 127 | + // Update existing key if found |
| 128 | + for (size_t i = 0; i < size; ++i) { |
| 129 | + if (strcmp(options[i].key, key) == 0) { |
| 130 | + options[i].type = type; |
| 131 | + options[i].value = value; |
| 132 | + return; |
| 133 | + } |
| 134 | + } |
| 135 | + // Add new option if capacity allows |
| 136 | + if (size < MaxCapacity) { |
| 137 | + options[size++] = {key, type, value}; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // Internal helper to get an option value with type checking |
| 142 | + executorch::runtime::Error get_option_internal( |
| 143 | + const char* key, |
| 144 | + OptionType expected_type, |
| 145 | + OptionValue& out) const { |
| 146 | + for (size_t i = 0; i < size; ++i) { |
| 147 | + if (strcmp(options[i].key, key) == 0) { |
| 148 | + // Verify type matches expectation |
| 149 | + if (options[i].type != expected_type) { |
| 150 | + return executorch::runtime::Error::InvalidArgument; |
| 151 | + } |
| 152 | + out = options[i].value; |
| 153 | + return executorch::runtime::Error::Ok; |
| 154 | + } |
| 155 | + } |
| 156 | + return executorch::runtime::Error::NotFound; // Key not found |
| 157 | + } |
| 158 | +}; |
| 159 | + |
| 160 | +// Helper functions for creating typed option keys -------------------------- |
| 161 | + |
| 162 | +/// Creates a boolean option key |
| 163 | +constexpr OptionKey<bool> BoolKey(const char* k) { |
| 164 | + return OptionKey<bool>(k); |
| 165 | +} |
| 166 | + |
| 167 | +/// Creates an integer option key |
| 168 | +constexpr OptionKey<int> IntKey(const char* k) { |
| 169 | + return OptionKey<int>(k); |
| 170 | +} |
| 171 | + |
| 172 | +/// Creates a string option key |
| 173 | +constexpr OptionKey<const char*> StrKey(const char* k) { |
| 174 | + return OptionKey<const char*>(k); |
| 175 | +} |
| 176 | +} // namespace runtime |
| 177 | +} // namespace executorch |
0 commit comments