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