2023-07-06 09:45:50 +08:00
|
|
|
#pragma once
|
|
|
|
|
2023-12-24 12:18:48 +08:00
|
|
|
#include <future>
|
2023-07-06 09:45:50 +08:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2023-12-24 12:18:48 +08:00
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
2023-07-06 09:45:50 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2023-12-24 12:18:48 +08:00
|
|
|
#include "common/queue.h"
|
|
|
|
|
2023-07-06 09:45:50 +08:00
|
|
|
enum ParamKeyType {
|
|
|
|
PERSISTENT = 0x02,
|
|
|
|
CLEAR_ON_MANAGER_START = 0x04,
|
|
|
|
CLEAR_ON_ONROAD_TRANSITION = 0x08,
|
|
|
|
CLEAR_ON_OFFROAD_TRANSITION = 0x10,
|
|
|
|
DONT_LOG = 0x20,
|
2023-12-24 12:18:48 +08:00
|
|
|
DEVELOPMENT_ONLY = 0x40,
|
2023-07-06 09:45:50 +08:00
|
|
|
ALL = 0xFFFFFFFF
|
|
|
|
};
|
|
|
|
|
|
|
|
class Params {
|
|
|
|
public:
|
2023-10-10 01:54:45 +08:00
|
|
|
explicit Params(const std::string &path = {});
|
2023-12-24 12:18:48 +08:00
|
|
|
~Params();
|
2023-10-10 01:54:45 +08:00
|
|
|
// Not copyable.
|
|
|
|
Params(const Params&) = delete;
|
|
|
|
Params& operator=(const Params&) = delete;
|
|
|
|
|
2023-07-06 09:45:50 +08:00
|
|
|
std::vector<std::string> allKeys() const;
|
|
|
|
bool checkKey(const std::string &key);
|
|
|
|
ParamKeyType getKeyType(const std::string &key);
|
|
|
|
inline std::string getParamPath(const std::string &key = {}) {
|
2023-12-24 12:18:48 +08:00
|
|
|
return params_path + params_prefix + (key.empty() ? "" : "/" + key);
|
2023-07-06 09:45:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a value
|
|
|
|
int remove(const std::string &key);
|
|
|
|
void clearAll(ParamKeyType type);
|
|
|
|
|
|
|
|
// helpers for reading values
|
|
|
|
std::string get(const std::string &key, bool block = false);
|
|
|
|
inline bool getBool(const std::string &key, bool block = false) {
|
|
|
|
return get(key, block) == "1";
|
|
|
|
}
|
|
|
|
std::map<std::string, std::string> readAll();
|
|
|
|
|
|
|
|
// helpers for writing values
|
|
|
|
int put(const char *key, const char *val, size_t value_size);
|
|
|
|
inline int put(const std::string &key, const std::string &val) {
|
|
|
|
return put(key.c_str(), val.data(), val.size());
|
|
|
|
}
|
|
|
|
inline int putBool(const std::string &key, bool val) {
|
|
|
|
return put(key.c_str(), val ? "1" : "0", 1);
|
|
|
|
}
|
2023-12-24 12:18:48 +08:00
|
|
|
void putNonBlocking(const std::string &key, const std::string &val);
|
|
|
|
inline void putBoolNonBlocking(const std::string &key, bool val) {
|
|
|
|
putNonBlocking(key, val ? "1" : "0");
|
|
|
|
}
|
2023-07-06 09:45:50 +08:00
|
|
|
|
|
|
|
private:
|
2023-12-24 12:18:48 +08:00
|
|
|
void asyncWriteThread();
|
|
|
|
|
2023-07-06 09:45:50 +08:00
|
|
|
std::string params_path;
|
2023-12-24 12:18:48 +08:00
|
|
|
std::string params_prefix;
|
|
|
|
|
|
|
|
// for nonblocking write
|
|
|
|
std::future<void> future;
|
|
|
|
SafeQueue<std::pair<std::string, std::string>> queue;
|
2023-07-06 09:45:50 +08:00
|
|
|
};
|