2019-07-01 02:09:31 +00:00
|
|
|
#pragma once
|
2018-04-28 12:22:42 +00:00
|
|
|
|
2019-01-20 04:59:37 +00:00
|
|
|
#include <string>
|
2020-05-07 13:08:30 +00:00
|
|
|
#include <map>
|
2019-02-16 01:45:05 +00:00
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include <system_properties.h>
|
2018-04-28 12:22:42 +00:00
|
|
|
|
2020-05-07 13:08:30 +00:00
|
|
|
#define PERSISTENT_PROPERTY_DIR "/data/property"
|
|
|
|
|
|
|
|
struct prop_cb {
|
|
|
|
virtual void exec(const char *name, const char *value) = 0;
|
2018-04-28 12:22:42 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 13:08:30 +00:00
|
|
|
template<class T>
|
|
|
|
struct prop_cb_impl : public prop_cb {
|
|
|
|
typedef void (*callback_type)(const char *, const char *, T&);
|
|
|
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wdangling-field" // Dangling field is expected
|
|
|
|
prop_cb_impl(T &arg, callback_type fn) : arg(arg), fn(fn) {}
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
void exec(const char *name, const char *value) override {
|
|
|
|
fn(name, value, arg);
|
2018-11-03 03:56:15 +00:00
|
|
|
}
|
2020-05-07 13:08:30 +00:00
|
|
|
private:
|
|
|
|
T &arg;
|
|
|
|
callback_type fn;
|
2018-04-28 12:22:42 +00:00
|
|
|
};
|
|
|
|
|
2018-11-03 03:56:15 +00:00
|
|
|
extern bool use_pb;
|
|
|
|
|
2020-05-07 13:08:30 +00:00
|
|
|
using prop_list = std::map<std::string_view, std::string>;
|
|
|
|
|
|
|
|
struct prop_collector : prop_cb_impl<prop_list> {
|
|
|
|
prop_collector(prop_list &list) :
|
|
|
|
prop_cb_impl<prop_list>(list, [](auto name, auto val, auto list){ list.emplace(name, val); }) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T, class Func>
|
|
|
|
prop_cb_impl<T> make_prop_cb(T &arg, Func f) {
|
|
|
|
return prop_cb_impl<T>(arg, f);
|
|
|
|
}
|
|
|
|
|
2019-01-20 04:59:37 +00:00
|
|
|
std::string persist_getprop(const char *name);
|
2020-05-07 13:08:30 +00:00
|
|
|
void persist_getprops(prop_cb *prop_cb);
|
2018-04-28 12:22:42 +00:00
|
|
|
bool persist_deleteprop(const char *name);
|
2020-05-07 13:08:30 +00:00
|
|
|
void persist_cleanup();
|