summaryrefslogtreecommitdiff
path: root/ddl/templates/data.cpp.ede
blob: 39bf39d42a912c9ffd1490db6f9e811f057bbafa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// generated file, do not modify!
// {{ dateTime }}

#include "{{ moduleName }}.hpp"
{% for t in definitions %}
template<> json toJSON<std::shared_ptr<{{ t.value.dataName }}>>(std::shared_ptr<{{ t.value.dataName }}> &v) {
  json obj({});
  switch (v->tag) { {% for c in t.value.constructors %}
    case ::{{ t.value.dataName }}::tag::{{ c.value.name }}:
      obj["tag"] = "{{ c.value.name }}";{% if !(c.value.fields | empty) %}
      {
        std::shared_ptr<data::{{ c.value.name }}> tv = std::static_pointer_cast<data::{{ c.value.name }}>(v);{% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %}
        obj["{{ f.value.fieldName }}"] = toJSON(tv->{{ f.value.fieldName }});{% else %}
        obj["arg{{ f.index0 }}"] = toJSON(tv->_{{ f.index0 }});{% endif %}{% endfor %}
      }{% endif %}
      break;{% endfor %}
  }
  return obj;
}

template<> std::shared_ptr<{{ t.value.dataName }}> fromJSON<std::shared_ptr<{{ t.value.dataName }}>>(W<std::shared_ptr<{{ t.value.dataName }}>> v, json &obj) {
  enum ::{{ t.value.dataName }}::tag tagType;
  std::string tag = obj["tag"];
  {% for c in t.value.constructors %}
  {% if !c.first %}else {% endif %}if (tag == "{{ c.value.name }}") {
    tagType = ::{{ t.value.dataName }}::tag::{{ c.value.name }};
    {% if !(c.value.fields | empty) %}
    std::shared_ptr<data::{{ c.value.name }}> tv(new data::{{ c.value.name }}());{% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %}
    tv->{{ f.value.fieldName }} = fromJSON(W<{{ f.value.fieldType | cppType }}>(), obj["{{ f.value.fieldName }}"]);{% else %}
    tv->_{{ f.index0 }} = fromJSON(W<{{ f.value.fieldType | cppType }}>(), obj["arg{{ f.index0 }}"]);{% endif %}{% endfor %}
    return tv;
    {% endif %}
  }
{% endfor %}
  else throw "unknown constructor: " + tag;
  std::shared_ptr<::{{ t.value.dataName }}> o(new ::{{ t.value.dataName }}());
  o->tag = tagType;
  return o;
}

{% endfor %}