3 ms·
Sorry for the noob question: can you explain how it helps with serialization or database access? I've read simliar statements before but I don't think I unders
by flumpcakes 6y ago
Sorry for the noob question: can you explain how it helps with serialization or database access?
I've read simliar statements before but I don't think I understand how it helps.
- jcranmer 6y agoThe standard serialization case is basically, I have this definition: struct foo { int a; int b; const char *c; }; and I want to automatically generate this function: void serialize_foo(foo &obj, ostream &out) { out.serialize_int(obj.a); out.serialize_int(obj.b); out.serialize_string(obj.c); } With some sort of reflection, you can automatically build that method with something like this: void emit_serialize_method(class_definition &clazz) { emit("void serialize_foo(" + clazz.name + "&obj, ostream &out) {"); for (auto &field : clazz.fields) { emit(" out.serialize_" + field.type + "(obj." + field.name + ");"); } emit("}"); } (syntax of course varies).