Hacker News new | past | comments | ask | show | jobs | submit login

The 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).



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: