mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-06-12 21:27:41 +02:00
Modernize resetprop with fancy C++
This commit is contained in:
@ -1,57 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <logging.hpp>
|
||||
|
||||
#include <system_properties.h>
|
||||
|
||||
struct prop_t {
|
||||
char *name;
|
||||
char value[PROP_VALUE_MAX];
|
||||
prop_t() : name(nullptr) {}
|
||||
explicit prop_t(const char *name) {
|
||||
this->name = strdup(name);
|
||||
value[0] = '\0';
|
||||
}
|
||||
prop_t(const char *name, const char *value) {
|
||||
this->name = strdup(name);
|
||||
strcpy(this->value, value);
|
||||
}
|
||||
prop_t(prop_t &&prop): name(nullptr) {
|
||||
operator=(std::move(prop));
|
||||
}
|
||||
bool operator<(const prop_t &prop) const {
|
||||
return strcmp(name, prop.name) < 0;
|
||||
}
|
||||
prop_t& operator=(prop_t &&prop) {
|
||||
if (this != &prop) {
|
||||
free(name);
|
||||
name = prop.name;
|
||||
strcpy(value, prop.value);
|
||||
prop.name = nullptr;
|
||||
}
|
||||
return *this;
|
||||
};
|
||||
~prop_t() {
|
||||
free(name);
|
||||
}
|
||||
};
|
||||
|
||||
struct read_cb_t {
|
||||
void (*cb)(const char *, const char *, void *);
|
||||
void *arg;
|
||||
explicit read_cb_t(void (*cb)(const char *, const char *, void *) = nullptr, void *arg = nullptr)
|
||||
: cb(cb), arg(arg) {}
|
||||
void exec(const char *name, const char *value) {
|
||||
cb(name, value, arg);
|
||||
}
|
||||
};
|
||||
|
||||
#define PERSISTENT_PROPERTY_DIR "/data/property"
|
||||
|
||||
struct prop_cb {
|
||||
virtual void exec(const char *name, const char *value) = 0;
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
private:
|
||||
T &arg;
|
||||
callback_type fn;
|
||||
};
|
||||
|
||||
extern bool use_pb;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
std::string persist_getprop(const char *name);
|
||||
void persist_getprop(read_cb_t *read_cb);
|
||||
void persist_getprops(prop_cb *prop_cb);
|
||||
bool persist_deleteprop(const char *name);
|
||||
void collect_props(const char *name, const char *value, void *v_plist);
|
||||
void persist_cleanup();
|
||||
|
Reference in New Issue
Block a user