pimgen: generate serialization code

Subsequent commits will introduce serialization of inventory properties.
Generate the save()/load() functions required by Cereal.

Change-Id: I2ce16d205cad9684711c49c32ddae9f69cd8632d
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 78418a1..769592d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,4 +1,4 @@
-BUILT_SOURCES=generated.cpp extra_ifaces.cpp
+BUILT_SOURCES=generated.cpp extra_ifaces.cpp gen_serialization.hpp
 
 CLEANFILES=$(BUILT_SOURCES)
 
@@ -41,7 +41,12 @@
 endif
 
 generated.cpp:
-	$(AM_V_GEN)$(PYTHON) $(top_srcdir)/pimgen.py $(PIMGEN_ARGS) -d $(base_yamldir) -o $(builddir) generate-cpp
+	$(AM_V_GEN)$(PYTHON) $(top_srcdir)/pimgen.py $(PIMGEN_ARGS) -d \
+		$(base_yamldir) -o $(builddir) generate-cpp
+
+gen_serialization.hpp:
+	$(AM_V_GEN)$(PYTHON) $(top_srcdir)/pimgen.py $(PIMGEN_ARGS) -d \
+		$(base_yamldir) -o $(builddir) generate-serialization
 
 SUBDIRS = . test
 
diff --git a/gen_serialization.mako.hpp b/gen_serialization.mako.hpp
new file mode 100644
index 0000000..f417917
--- /dev/null
+++ b/gen_serialization.mako.hpp
@@ -0,0 +1,47 @@
+## This file is a template.  The comment below is emitted
+## into the rendered file; feel free to edit this file.
+// This file was auto generated.  Do not edit.
+
+#include <cereal/types/string.hpp>
+#include <cereal/types/vector.hpp>
+% for iface in interfaces:
+#include <${iface.header()}>
+% endfor
+
+namespace cereal
+{
+
+% for iface in interfaces:
+<% properties = interface_composite.names(str(iface)) %>\
+template<class Archive>
+void save(Archive& a,
+          const ${iface.namespace()}& object)
+{
+<%
+    props = ["object." + p[:1].lower() + p[1:] + "()" for p in properties]
+    props = ', '.join(props)
+%>\
+    a(${props});
+}
+
+
+template<class Archive>
+void load(Archive& a,
+          ${iface.namespace()}& object)
+{
+% for p in properties:
+<% t = "object." + p[:1].lower() + p[1:] + "()" %>\
+    decltype(${t}) ${p}{};
+% endfor
+<%
+    props = ', '.join(properties)
+%>\
+    a(${props});
+% for p in properties:
+<% t = "object." + p[:1].lower() + p[1:] + "(" + p + ")" %>\
+    ${t};
+% endfor
+}
+
+% endfor
+} // namespace cereal
diff --git a/pimgen.py b/pimgen.py
index 4f0a566..73d1868 100755
--- a/pimgen.py
+++ b/pimgen.py
@@ -570,11 +570,23 @@
                     interfaces=self.interfaces,
                     indent=Indent()))
 
+    def generate_serialization(self, loader):
+        with open(os.path.join(
+                args.outputdir,
+                'gen_serialization.hpp'), 'w') as fd:
+            fd.write(
+                self.render(
+                    loader,
+                    'gen_serialization.mako.hpp',
+                    interfaces=self.interfaces,
+                    interface_composite=self.interface_composite))
+
 
 if __name__ == '__main__':
     script_dir = os.path.dirname(os.path.realpath(__file__))
     valid_commands = {
         'generate-cpp': 'generate_cpp',
+        'generate-serialization': 'generate_serialization',
     }
 
     parser = argparse.ArgumentParser(
diff --git a/test/Makefile.am b/test/Makefile.am
index deda4ff..98b521a 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1,4 +1,4 @@
-BUILT_SOURCES=generated.cpp extra_ifaces.cpp
+BUILT_SOURCES=generated.cpp extra_ifaces.cpp gen_serialization.hpp
 CLEANFILES=$(BUILT_SOURCES)
 
 AM_CPPFLAGS = -iquote$(top_srcdir)
@@ -31,4 +31,7 @@
 generated.cpp:
 	$(AM_V_GEN)$(PYTHON) $(top_srcdir)/pimgen.py -d $(extra_yamldir)/.. -o $(builddir) generate-cpp
 
+gen_serialization.hpp:
+	$(AM_V_GEN)$(PYTHON) $(top_srcdir)/pimgen.py -d $(extra_yamldir)/.. -o $(builddir) generate-serialization
+
 -include Makefile.extra