|
| 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 | +#pragma once |
| 10 | +#include <executorch/runtime/core/error.h> |
| 11 | +#include <executorch/runtime/core/span.h> |
| 12 | +#include <array> |
| 13 | +#include <cstddef> |
| 14 | +#include <cstring> |
| 15 | +#include <variant> |
| 16 | + |
| 17 | +namespace executorch { |
| 18 | +namespace runtime { |
| 19 | + |
| 20 | +static constexpr size_t kMaxOptionKeyLength = 64; |
| 21 | +static constexpr size_t kMaxOptionValueLength = 256; |
| 22 | + |
| 23 | +// All string keys and values must have static storage duration (string |
| 24 | +// literals, static const char arrays, or global constants). The BackendOptions |
| 25 | +// class does NOT take ownership of strings. |
| 26 | +using OptionValue = |
| 27 | + std::variant<bool, int, std::array<char, kMaxOptionValueLength>>; |
| 28 | + |
| 29 | +struct BackendOption { |
| 30 | + // key is the name of the backend option, like num_threads, enable_profiling, |
| 31 | + // etc |
| 32 | + char key[kMaxOptionKeyLength]{}; |
| 33 | + // value is the value of the backend option, like 4, true, etc |
| 34 | + OptionValue value; |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * A template class for storing and managing backend-specific configuration |
| 39 | + * options. |
| 40 | + * |
| 41 | + * This class provides a type-safe way to store key-value pairs for backend |
| 42 | + * configuration, with compile-time capacity limits and runtime type checking. |
| 43 | + * It supports bool, int, and const char* value types. |
| 44 | + * |
| 45 | + * @tparam MaxCapacity The maximum number of options that can be stored |
| 46 | + */ |
| 47 | +template <size_t MaxCapacity> |
| 48 | +class BackendOptions { |
| 49 | + public: |
| 50 | + /** |
| 51 | + * Copy constructor |
| 52 | + */ |
| 53 | + BackendOptions(const BackendOptions& other) : size_(other.size_) { |
| 54 | + for (size_t i = 0; i < size_; ++i) { |
| 55 | + options_[i] = other.options_[i]; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Copy assignment operator |
| 61 | + */ |
| 62 | + BackendOptions& operator=(const BackendOptions& other) { |
| 63 | + if (this != &other) { |
| 64 | + size_ = other.size_; |
| 65 | + for (size_t i = 0; i < size_; ++i) { |
| 66 | + options_[i] = other.options_[i]; |
| 67 | + } |
| 68 | + } |
| 69 | + return *this; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Default constructor - initializes with zero options. |
| 74 | + */ |
| 75 | + BackendOptions() : size_(0) {} |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns a mutable view of all stored options as a Span. |
| 79 | + * |
| 80 | + * @return A mutable Span containing all BackendOption entries |
| 81 | + */ |
| 82 | + executorch::runtime::Span<BackendOption> view() { |
| 83 | + return executorch::runtime::Span<BackendOption>(options_, size_); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Sets a boolean option value for the given key. |
| 88 | + * If the key already exists, updates its value. Otherwise, adds a new option. |
| 89 | + * |
| 90 | + * @tparam N The length of the key string (automatically deduced) |
| 91 | + * @param key The option key (must be a string literal or array) |
| 92 | + * @param value The boolean value to set |
| 93 | + * @return Error::Ok on success, Error::InvalidArgument if storage is full |
| 94 | + */ |
| 95 | + template <size_t N> |
| 96 | + Error set_option(const char (&key)[N], bool value) noexcept { |
| 97 | + static_assert(N <= kMaxOptionKeyLength, "Option key is too long"); |
| 98 | + return set_option_impl(key, value); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Sets an integer option value for the given key. |
| 103 | + * If the key already exists, updates its value. Otherwise, adds a new option. |
| 104 | + * |
| 105 | + * @tparam N The length of the key string (automatically deduced) |
| 106 | + * @param key The option key (must be a string literal or array) |
| 107 | + * @param value The integer value to set |
| 108 | + * @return Error::Ok on success, Error::InvalidArgument if storage is full |
| 109 | + */ |
| 110 | + template <size_t N> |
| 111 | + Error set_option(const char (&key)[N], int value) noexcept { |
| 112 | + static_assert(N <= kMaxOptionKeyLength, "Option key is too long"); |
| 113 | + return set_option_impl(key, value); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sets a string option value for the given key. |
| 118 | + * If the key already exists, updates its value. Otherwise, adds a new option. |
| 119 | + * |
| 120 | + * Note: The string value must have static storage duration. This class does |
| 121 | + * NOT take ownership of the string - it only stores the pointer. |
| 122 | + * |
| 123 | + * @tparam N The length of the key string (automatically deduced) |
| 124 | + * @param key The option key (must be a string literal or array) |
| 125 | + * @param value The string value to set (must have static storage duration) |
| 126 | + * @return Error::Ok on success, Error::InvalidArgument if storage is full |
| 127 | + */ |
| 128 | + template <size_t N> |
| 129 | + Error set_option(const char (&key)[N], const char* value) noexcept { |
| 130 | + static_assert(N <= kMaxOptionKeyLength, "Option key is too long"); |
| 131 | + // Create a fixed-size array and copy the string |
| 132 | + std::array<char, kMaxOptionValueLength> arr{}; |
| 133 | + strncpy(arr.data(), value, kMaxOptionValueLength - 1); |
| 134 | + arr[kMaxOptionValueLength - 1] = '\0'; // Ensure null termination |
| 135 | + return set_option_impl(key, arr); |
| 136 | + } |
| 137 | + /** |
| 138 | + * Retrieves an option value by key and type. |
| 139 | + * |
| 140 | + * @tparam T The expected type of the option value (bool, int, or const char*) |
| 141 | + * @tparam KeyLen The length of the key string (automatically deduced) |
| 142 | + * @param key The option key to look up |
| 143 | + * @param out Reference to store the retrieved value |
| 144 | + * @return Error::Ok if found and type matches, Error::NotFound if key doesn't |
| 145 | + * exist, Error::InvalidArgument if type doesn't match |
| 146 | + */ |
| 147 | + template <typename T, size_t KeyLen> |
| 148 | + Error get_option(const char (&key)[KeyLen], T& out) const { |
| 149 | + static_assert(KeyLen <= kMaxOptionKeyLength, "Option key is too long"); |
| 150 | + for (size_t i = 0; i < size_; ++i) { |
| 151 | + if (std::strcmp(options_[i].key, key) == 0) { |
| 152 | + // Special handling for string (convert array to const char*) |
| 153 | + if constexpr (std::is_same_v<T, const char*>) { |
| 154 | + if (auto* arr = std::get_if<std::array<char, kMaxOptionValueLength>>( |
| 155 | + &options_[i].value)) { |
| 156 | + out = arr->data(); // Return pointer to stored array |
| 157 | + return Error::Ok; |
| 158 | + } |
| 159 | + } |
| 160 | + // Default handling for bool/int |
| 161 | + else if (auto* val = std::get_if<T>(&options_[i].value)) { |
| 162 | + out = *val; |
| 163 | + return Error::Ok; |
| 164 | + } |
| 165 | + return Error::InvalidArgument; |
| 166 | + } |
| 167 | + } |
| 168 | + return Error::NotFound; |
| 169 | + } |
| 170 | + |
| 171 | + private: |
| 172 | + BackendOption options_[MaxCapacity]{}; // Storage for backend options |
| 173 | + size_t size_; // Current number of options |
| 174 | + |
| 175 | + /** |
| 176 | + * Internal implementation for setting option values. |
| 177 | + * Handles both updating existing options and adding new ones. |
| 178 | + * |
| 179 | + * @tparam T The type of the value (bool, int, or const char*) |
| 180 | + * @param key The option key |
| 181 | + * @param value The value to set |
| 182 | + * @return Error::Ok on success, Error::InvalidArgument if storage is full |
| 183 | + */ |
| 184 | + template <typename T> |
| 185 | + Error set_option_impl(const char* key, T value) { |
| 186 | + // Update existing if found |
| 187 | + for (size_t i = 0; i < size_; ++i) { |
| 188 | + if (strcmp(options_[i].key, key) == 0) { |
| 189 | + options_[i].value = value; |
| 190 | + return Error::Ok; |
| 191 | + } |
| 192 | + } |
| 193 | + if (size_ < MaxCapacity) { |
| 194 | + BackendOption new_option; |
| 195 | + const size_t key_len = std::strlen(key); |
| 196 | + const size_t copy_len = std::min(key_len, kMaxOptionKeyLength - 1); |
| 197 | + std::memcpy(new_option.key, key, copy_len); |
| 198 | + new_option.key[copy_len] = '\0'; |
| 199 | + new_option.value = value; // Restored value assignment |
| 200 | + options_[size_++] = new_option; // Store option and increment size |
| 201 | + return Error::Ok; |
| 202 | + } |
| 203 | + return Error::InvalidArgument; |
| 204 | + } |
| 205 | +}; |
| 206 | + |
| 207 | +} // namespace runtime |
| 208 | +} // namespace executorch |
0 commit comments