Simplify prop_cb

This commit is contained in:
topjohnwu 2023-05-16 02:09:24 -07:00
parent 1be647a279
commit fe1ca52f6d
2 changed files with 8 additions and 16 deletions

View File

@ -75,7 +75,7 @@ PB_BIND(PersistentProperties_PersistentPropertyRecord, PersistentProperties_Pers
* End of auto generated code * End of auto generated code
* ***************************/ * ***************************/
static bool name_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) { static bool name_decode(pb_istream_t *stream, const pb_field_t *, void **arg) {
string &name = *static_cast<string *>(*arg); string &name = *static_cast<string *>(*arg);
name.resize(stream->bytes_left); name.resize(stream->bytes_left);
return pb_read(stream, (pb_byte_t *)(name.data()), stream->bytes_left); return pb_read(stream, (pb_byte_t *)(name.data()), stream->bytes_left);
@ -86,7 +86,7 @@ static bool name_encode(pb_ostream_t *stream, const pb_field_t *field, void * co
pb_encode_string(stream, (const pb_byte_t *) *arg, strlen((const char *) *arg)); pb_encode_string(stream, (const pb_byte_t *) *arg, strlen((const char *) *arg));
} }
static bool prop_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) { static bool prop_decode(pb_istream_t *stream, const pb_field_t *, void **arg) {
PersistentProperties_PersistentPropertyRecord prop{}; PersistentProperties_PersistentPropertyRecord prop{};
string name; string name;
prop.name.funcs.decode = name_decode; prop.name.funcs.decode = name_decode;
@ -94,7 +94,7 @@ static bool prop_decode(pb_istream_t *stream, const pb_field_t *field, void **ar
if (!pb_decode(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop)) if (!pb_decode(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
return false; return false;
auto cb = static_cast<prop_cb*>(*arg); auto cb = static_cast<prop_cb*>(*arg);
cb->exec(std::move(name), prop.value); cb->exec(name.data(), prop.value);
return true; return true;
} }
@ -107,7 +107,7 @@ static bool prop_encode(pb_ostream_t *stream, const pb_field_t *field, void * co
if (!pb_encode_tag_for_field(stream, field)) if (!pb_encode_tag_for_field(stream, field))
return false; return false;
prop.name.arg = (void *) p.first.data(); prop.name.arg = (void *) p.first.data();
strcpy(prop.value, p.second.data()); strscpy(prop.value, p.second.data(), sizeof(prop.value));
if (!pb_encode_submessage(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop)) if (!pb_encode_submessage(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
return false; return false;
} }
@ -173,9 +173,9 @@ void persist_getprops(prop_cb *prop_cb) {
struct match_prop_name : prop_cb { struct match_prop_name : prop_cb {
explicit match_prop_name(const char *name) : _name(name) { value[0] = '\0'; } explicit match_prop_name(const char *name) : _name(name) { value[0] = '\0'; }
void exec(string &&name, const char *val) override { void exec(const char *name, const char *val) override {
if (name == _name) if (std::strcmp(name, _name) == 0)
strcpy(value, val); strscpy(value, val, sizeof(value));
} }
char value[PROP_VALUE_MAX]; char value[PROP_VALUE_MAX];
private: private:

View File

@ -8,12 +8,7 @@
#define PERSISTENT_PROPERTY_DIR "/data/property" #define PERSISTENT_PROPERTY_DIR "/data/property"
struct prop_cb { struct prop_cb {
virtual void exec(const char *name, const char *value) { virtual void exec(const char *name, const char *value) = 0;
exec(std::string(name), value);
}
virtual void exec(std::string &&name, const char *value) {
exec(name.data(), value);
}
}; };
using prop_list = std::map<std::string, std::string>; using prop_list = std::map<std::string, std::string>;
@ -23,9 +18,6 @@ struct prop_collector : prop_cb {
void exec(const char *name, const char *value) override { void exec(const char *name, const char *value) override {
list.insert_or_assign(name, value); list.insert_or_assign(name, value);
} }
void exec(std::string &&name, const char *value) override {
list.insert_or_assign(std::move(name), value);
}
private: private:
prop_list &list; prop_list &list;
}; };