Xo Wang | 069db2f | 2017-08-09 15:31:33 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <cstdint> |
| 4 | #include <functional> |
| 5 | #include <map> |
| 6 | #include <string> |
| 7 | #include <tuple> |
| 8 | |
| 9 | /** |
| 10 | * Key-value store for string-type system info parameters. |
| 11 | */ |
| 12 | class SysInfoParamStoreIntf |
| 13 | { |
| 14 | public: |
| 15 | virtual ~SysInfoParamStoreIntf() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Returns true if parameter is found. If and only if s is non-null, |
| 21 | * invokes the parameter's callback and writes the value. |
| 22 | * |
| 23 | * @param[in] paramSelector - the key to lookup. |
| 24 | * @return tuple of bool and string, true if parameter is found and |
| 25 | * string set accordingly. |
| 26 | */ |
| 27 | virtual std::tuple<bool, std::string> |
| 28 | lookup(uint8_t paramSelector) const = 0; |
| 29 | |
| 30 | /** |
| 31 | * Update a parameter by its code with a string value. |
| 32 | * |
| 33 | * @param[in] paramSelector - the key to update. |
| 34 | * @param[in] s - the value to set. |
| 35 | */ |
| 36 | virtual void update(uint8_t paramSelector, const std::string& s) = 0; |
| 37 | |
| 38 | /** |
| 39 | * Update a parameter by its code with a callback that is called to retrieve |
| 40 | * its value whenever called. Callback must be idempotent, as it may be |
| 41 | * called multiple times by the host to retrieve the parameter by chunks. |
| 42 | * |
| 43 | * @param[in] paramSelector - the key to update. |
| 44 | * @param[in] callback - the callback to use for parameter retrieval. |
| 45 | */ |
| 46 | virtual void update(uint8_t paramSelector, |
| 47 | const std::function<std::string()>& callback) = 0; |
| 48 | |
| 49 | // TODO: Store "read-only" flag for each parameter. |
| 50 | // TODO: Function to erase a parameter? |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * Implement the system info parameters store as a map of callbacks. |
| 55 | */ |
| 56 | class SysInfoParamStore : public SysInfoParamStoreIntf |
| 57 | { |
| 58 | public: |
| 59 | std::tuple<bool, std::string> lookup(uint8_t paramSelector) const override; |
| 60 | void update(uint8_t paramSelector, const std::string& s) override; |
| 61 | void update(uint8_t paramSelector, |
| 62 | const std::function<std::string()>& callback) override; |
| 63 | |
| 64 | private: |
| 65 | std::map<uint8_t, std::function<std::string()>> params; |
| 66 | }; |