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