Bläddra i källkod

Added various template macros to support oneof's.

Bart Hertog 6 år sedan
förälder
incheckning
5abf4213bf
3 ändrade filer med 211 tillägg och 151 borttagningar
  1. 2 0
      build_test.sh
  2. 208 150
      generator/Header_Template.h
  3. 1 1
      generator/protoc-gen-eams.py

+ 2 - 0
build_test.sh

@@ -5,12 +5,14 @@ mkdir -p ./build/EAMS
 protoc --plugin=protoc-gen-eams=protoc-gen-eams -I./test/proto --eams_out=./build/EAMS ./test/proto/simple_types.proto
 protoc --plugin=protoc-gen-eams=protoc-gen-eams -I./test/proto --eams_out=./build/EAMS ./test/proto/simple_types.proto
 protoc --plugin=protoc-gen-eams=protoc-gen-eams -I./test/proto --eams_out=./build/EAMS ./test/proto/nested_message.proto
 protoc --plugin=protoc-gen-eams=protoc-gen-eams -I./test/proto --eams_out=./build/EAMS ./test/proto/nested_message.proto
 protoc --plugin=protoc-gen-eams=protoc-gen-eams -I./test/proto --eams_out=./build/EAMS ./test/proto/repeated_fields.proto
 protoc --plugin=protoc-gen-eams=protoc-gen-eams -I./test/proto --eams_out=./build/EAMS ./test/proto/repeated_fields.proto
+protoc --plugin=protoc-gen-eams=protoc-gen-eams -I./test/proto --eams_out=./build/EAMS ./test/proto/oneof_fields.proto
 
 
 # For validation and testing generate the same message using python
 # For validation and testing generate the same message using python
 mkdir -p ./build/python
 mkdir -p ./build/python
 protoc -I./test/proto --python_out=./build/python ./test/proto/simple_types.proto
 protoc -I./test/proto --python_out=./build/python ./test/proto/simple_types.proto
 protoc -I./test/proto --python_out=./build/python ./test/proto/nested_message.proto
 protoc -I./test/proto --python_out=./build/python ./test/proto/nested_message.proto
 protoc -I./test/proto --python_out=./build/python ./test/proto/repeated_fields.proto
 protoc -I./test/proto --python_out=./build/python ./test/proto/repeated_fields.proto
+protoc -I./test/proto --python_out=./build/python ./test/proto/oneof_fields.proto
 
 
 
 
 # Build the tests
 # Build the tests

+ 208 - 150
generator/Header_Template.h

@@ -7,132 +7,179 @@ enum {{ _enum.name }}
 };
 };
 
 
 {% endmacro %}
 {% endmacro %}
-
+{# #}
+{# ------------------------------------------------------------------------------------------------------------------ #}
+{# #}
 {% macro field_get_set_macro(_field) %}
 {% macro field_get_set_macro(_field) %}
 static const uint32_t {{_field.variable_id_name}} = {{_field.variable_id}};
 static const uint32_t {{_field.variable_id_name}} = {{_field.variable_id}};
 {% if _field.is_repeated_field %}
 {% if _field.is_repeated_field %}
-    inline const {{_field.type}}& {{_field.name}}(uint32_t index) const { return {{_field.variable_name}}[index]; }
-    inline void clear_{{_field.name}}() { {{_field.variable_name}}.clear(); }
-    {% if _field.which_oneof is defined %}
-    inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}& value)
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      {{_field.variable_name}}.set(index, value);
-    }
-    inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}&& value)
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      {{_field.variable_name}}.set(index, value);
-    }
-    inline void add_{{_field.name}}(const {{_field.type}}& value)
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      {{_field.variable_name}}.add(value);
-    }
-    inline {{_field.repeated_type}}& mutable_{{_field.name}}()
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      return {{_field.variable_name}};
-    }
-    {% else %}
-    inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}& value) { {{_field.variable_name}}.set(index, value); }
-    inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}&& value) { {{_field.variable_name}}.set(index, value); }
-    inline void add_{{_field.name}}(const {{_field.type}}& value) { {{_field.variable_name}}.add(value); }
-    inline {{_field.repeated_type}}& mutable_{{_field.name}}() { return {{_field.variable_name}}; }
-    {% endif %}
-    inline const {{_field.repeated_type}}& get_{{_field.name}}() const { return {{_field.variable_name}}; }
+inline const {{_field.type}}& {{_field.name}}(uint32_t index) const { return {{_field.variable_name}}[index]; }
+inline void clear_{{_field.name}}() { {{_field.variable_name}}.clear(); }
+{% if _field.which_oneof is defined %}
+inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}& value)
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  {{_field.variable_name}}.set(index, value);
+}
+inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}&& value)
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  {{_field.variable_name}}.set(index, value);
+}
+inline void add_{{_field.name}}(const {{_field.type}}& value)
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  {{_field.variable_name}}.add(value);
+}
+inline {{_field.repeated_type}}& mutable_{{_field.name}}()
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  return {{_field.variable_name}};
+}
+{% else %}
+inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}& value) { {{_field.variable_name}}.set(index, value); }
+inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}&& value) { {{_field.variable_name}}.set(index, value); }
+inline void add_{{_field.name}}(const {{_field.type}}& value) { {{_field.variable_name}}.add(value); }
+inline {{_field.repeated_type}}& mutable_{{_field.name}}() { return {{_field.variable_name}}; }
+{% endif %}
+inline const {{_field.repeated_type}}& get_{{_field.name}}() const { return {{_field.variable_name}}; }
 {% elif _field.of_type_message %}
 {% elif _field.of_type_message %}
-    inline const {{_field.type}}& {{_field.name}}() const { return {{_field.variable_name}}; }
-    inline void clear_{{_field.name}}() { {{_field.variable_name}}.clear(); }
-    {% if _field.which_oneof is defined %}
-    inline void set_{{_field.name}}(const {{_field.type}}& value)
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      {{_field.variable_name}} = value;
-    }
-    inline void set_{{_field.name}}(const {{_field.type}}&& value)
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      {{_field.variable_name}} = value;
-    }
-    inline {{_field.type}}& mutable_{{_field.name}}()
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      return {{_field.variable_name}};
-    }
-    {% else %}
-    inline void set_{{_field.name}}(const {{_field.type}}& value) { {{_field.variable_name}} = value; }
-    inline void set_{{_field.name}}(const {{_field.type}}&& value) { {{_field.variable_name}} = value; }
-    inline {{_field.type}}& mutable_{{_field.name}}() { return {{_field.variable_name}}; }
-    {% endif %}
-    inline const {{_field.type}}& get_{{_field.name}}() const { return {{_field.variable_name}}; }
+inline const {{_field.type}}& {{_field.name}}() const { return {{_field.variable_name}}; }
+inline void clear_{{_field.name}}() { {{_field.variable_name}}.clear(); }
+{% if _field.which_oneof is defined %}
+inline void set_{{_field.name}}(const {{_field.type}}& value)
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  {{_field.variable_name}} = value;
+}
+inline void set_{{_field.name}}(const {{_field.type}}&& value)
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  {{_field.variable_name}} = value;
+}
+inline {{_field.type}}& mutable_{{_field.name}}()
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  return {{_field.variable_name}};
+}
+{% else %}
+inline void set_{{_field.name}}(const {{_field.type}}& value) { {{_field.variable_name}} = value; }
+inline void set_{{_field.name}}(const {{_field.type}}&& value) { {{_field.variable_name}} = value; }
+inline {{_field.type}}& mutable_{{_field.name}}() { return {{_field.variable_name}}; }
+{% endif %}
+inline const {{_field.type}}& get_{{_field.name}}() const { return {{_field.variable_name}}; }
 {% elif _field.of_type_enum %}
 {% elif _field.of_type_enum %}
-    inline {{_field.type}} {{_field.name}}() const { return {{_field.variable_name}}; }
-    inline void clear_{{_field.name}}() { {{_field.variable_name}} = static_cast<{{_field.type}}>({{_field.default_value}}); }
-    {% if _field.which_oneof is defined %}
-    inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE& value)
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      {{_field.variable_name}} = value;
-    }
-    inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE&& value)
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      {{_field.variable_name}} = value;
-    }
-    {% else %}
-    inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE& value) { {{_field.variable_name}} = value; }
-    inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE&& value) { {{_field.variable_name}} = value; }
-    {% endif %}    inline {{_field.type}} get_{{_field.name}}() const { return {{_field.variable_name}}; }
+inline {{_field.type}} {{_field.name}}() const { return {{_field.variable_name}}; }
+inline void clear_{{_field.name}}() { {{_field.variable_name}} = static_cast<{{_field.type}}>({{_field.default_value}}); }
+{% if _field.which_oneof is defined %}
+inline void set_{{_field.name}}(const {{_field.type}}& value)
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  {{_field.variable_name}} = value;
+}
+inline void set_{{_field.name}}(const {{_field.type}}&& value)
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  {{_field.variable_name}} = value;
+}
 {% else %}
 {% else %}
-    inline {{_field.type}}::FIELD_TYPE {{_field.name}}() const { return {{_field.variable_name}}.get(); }
-    inline void clear_{{_field.name}}() { {{_field.variable_name}}.set({{_field.default_value}}); }
-    {% if _field.which_oneof is defined %}
-    inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE& value)
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      {{_field.variable_name}}.set(value);
-    }
-    inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE&& value)
-    {
-      {{_field.which_oneof}} = {{_field.variable_id_name}};
-      {{_field.variable_name}}.set(value);
-    }
-    {% else %}
-    inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE& value) { {{_field.variable_name}}.set(value); }
-    inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE&& value) { {{_field.variable_name}}.set(value); }
-    {% endif %}
-    inline {{_field.type}}::FIELD_TYPE get_{{_field.name}}() const { return {{_field.variable_name}}.get(); }
+inline void set_{{_field.name}}(const {{_field.type}}& value) { {{_field.variable_name}} = value; }
+inline void set_{{_field.name}}(const {{_field.type}}&& value) { {{_field.variable_name}} = value; }
+{% endif %}    inline {{_field.type}} get_{{_field.name}}() const { return {{_field.variable_name}}; }
+{% else %}
+inline {{_field.type}}::FIELD_TYPE {{_field.name}}() const { return {{_field.variable_name}}.get(); }
+inline void clear_{{_field.name}}() { {{_field.variable_name}}.set({{_field.default_value}}); }
+{% if _field.which_oneof is defined %}
+inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE& value)
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  {{_field.variable_name}}.set(value);
+}
+inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE&& value)
+{
+  {{_field.which_oneof}} = {{_field.variable_id_name}};
+  {{_field.variable_name}}.set(value);
+}
+{% else %}
+inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE& value) { {{_field.variable_name}}.set(value); }
+inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE&& value) { {{_field.variable_name}}.set(value); }
+{% endif %}
+inline {{_field.type}}::FIELD_TYPE get_{{_field.name}}() const { return {{_field.variable_name}}.get(); }
 {% endif %}
 {% endif %}
 {% endmacro %}
 {% endmacro %}
-
+{# #}
+{# ------------------------------------------------------------------------------------------------------------------ #}
+{# #}
 {% macro field_serialize_macro(_field) %}
 {% macro field_serialize_macro(_field) %}
-      {% if _field.is_repeated_field %}
-      if(result)
-      {
-        result = {{_field.variable_name}}.serialize({{_field.variable_id_name}}, buffer);
-      }
-      {% elif _field.of_type_message %}
-      if(result)
-      {
-        const ::EmbeddedProto::MessageInterface* x = &{{_field.variable_name}};
-        result = x->serialize({{_field.variable_id_name}}, buffer);
-      }
-      {% elif _field.of_type_enum %}
-      if(({{_field.default_value}} != {{_field.variable_name}}) && result)
-      {
-        EmbeddedProto::uint32 value;
-        value.set(static_cast<uint32_t>({{_field.variable_name}}));
-        result = value.serialize({{_field.variable_id_name}}, buffer);
-      }
-      {% else %}
-      if(({{_field.default_value}} != {{_field.variable_name}}.get()) && result)
-      {
-        result = {{_field.variable_name}}.serialize({{_field.variable_id_name}}, buffer);
-      }
-      {% endif %}
-{% endmacro %}
-
+{% if _field.is_repeated_field %}
+if(result)
+{
+  result = {{_field.variable_name}}.serialize({{_field.variable_id_name}}, buffer);
+}
+{% elif _field.of_type_message %}
+if(result)
+{
+  const ::EmbeddedProto::MessageInterface* x = &{{_field.variable_name}};
+  result = x->serialize({{_field.variable_id_name}}, buffer);
+}
+{% elif _field.of_type_enum %}
+if(({{_field.default_value}} != {{_field.variable_name}}) && result)
+{
+  EmbeddedProto::uint32 value;
+  value.set(static_cast<uint32_t>({{_field.variable_name}}));
+  result = value.serialize({{_field.variable_id_name}}, buffer);
+}
+{% else %}
+if(({{_field.default_value}} != {{_field.variable_name}}.get()) && result)
+{
+  result = {{_field.variable_name}}.serialize({{_field.variable_id_name}}, buffer);
+} {% endif %} {% endmacro %}
+{# #}
+{# ------------------------------------------------------------------------------------------------------------------ #}
+{# #}
+{% macro field_deserialize_macro(_field) %}
+{% if _field.is_repeated_field %}
+if(::EmbeddedProto::WireFormatter::WireType::LENGTH_DELIMITED == wire_type)
+{
+  result = {{_field.variable_name}}.deserialize(buffer);
+}
+{% else %}
+if(::EmbeddedProto::WireFormatter::WireType::{{_field.wire_type}} == wire_type)
+{
+  {% if _field.of_type_message %}
+  uint32_t size;
+  result = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, size);
+  ::EmbeddedProto::ReadBufferSection bufferSection(buffer, size);
+  result = result && {{_field.variable_name}}.deserialize(bufferSection);
+  {% elif _field.of_type_enum %}
+  uint32_t value;
+  result = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, value);
+  if(result)
+  {
+    {{_field.variable_name}} = static_cast<{{_field.type}}>(value);
+    {% if _field.which_oneof is defined %}
+    {{_field.which_oneof}} = {{_field.variable_id_name}};
+    {% endif %}
+  }
+  {% else %}
+  result = {{_field.variable_name}}.deserialize(buffer);
+  {% if _field.which_oneof is defined %}
+  if(result)
+  {
+    {{_field.which_oneof}} = {{_field.variable_id_name}};
+  }
+  {% endif %}
+  {% endif %}
+}
+{% endif %}
+else
+{
+  // TODO Error wire type does not match field.
+  result = false;
+} {% endmacro %}
+{# #}
+{# ------------------------------------------------------------------------------------------------------------------ #}
+{# #}
 {% macro msg_macro(msg) %}
 {% macro msg_macro(msg) %}
 {% if msg.templates is defined %}
 {% if msg.templates is defined %}
 {% for template in msg.templates %}
 {% for template in msg.templates %}
@@ -160,11 +207,11 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
 
 
     {% endfor %}
     {% endfor %}
     {% for field in msg.fields() %}
     {% for field in msg.fields() %}
-    {{ field_get_set_macro(field) }}
+    {{ field_get_set_macro(field)|indent(4) }}
     {% endfor %}
     {% endfor %}
     {% for oneof in msg.oneofs() %}
     {% for oneof in msg.oneofs() %}
     {% for field in oneof.fields() %}
     {% for field in oneof.fields() %}
-    {{ field_get_set_macro(field) }}
+    {{ field_get_set_macro(field)|indent(4) }}
     {% endfor %}
     {% endfor %}
     {% endfor %}
     {% endfor %}
     bool serialize(::EmbeddedProto::WriteBufferInterface& buffer) const final
     bool serialize(::EmbeddedProto::WriteBufferInterface& buffer) const final
@@ -172,7 +219,23 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
       bool result = true;
       bool result = true;
 
 
       {% for field in msg.fields() %}
       {% for field in msg.fields() %}
-      {{ field_serialize_macro(field)|indent(0) }}
+      {{ field_serialize_macro(field)|indent(6) }}
+      {% endfor %}
+      {% for oneof in msg.oneofs() %}
+      if((0 != {{oneof.which_oneof}}) && result)
+      {
+        switch({{oneof.which_oneof}})
+        {
+          {% for field in oneof.fields() %}
+          case {{field.variable_id_name}}:
+            {{ field_serialize_macro(field)|indent(12) }}
+            break;
+
+          {% endfor %}
+          default:
+            break;
+        }
+      }
       {% endfor %}
       {% endfor %}
       return result;
       return result;
     };
     };
@@ -190,39 +253,20 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
           {% for field in msg.fields() %}
           {% for field in msg.fields() %}
           case {{field.variable_id_name}}:
           case {{field.variable_id_name}}:
           {
           {
-            {% if field.is_repeated_field %}
-            if(::EmbeddedProto::WireFormatter::WireType::LENGTH_DELIMITED == wire_type)
-            {
-              result = {{field.variable_name}}.deserialize(buffer);
-            }
-            {% else %}
-            if(::EmbeddedProto::WireFormatter::WireType::{{field.wire_type}} == wire_type)
-            {
-              {% if field.of_type_message %}
-              uint32_t size;
-              result = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, size);
-              ::EmbeddedProto::ReadBufferSection bufferSection(buffer, size);
-              result = result && {{field.variable_name}}.deserialize(bufferSection);
-              {% elif field.of_type_enum %}
-              uint32_t value;
-              result = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, value);
-              if(result)
-              {
-                {{field.variable_name}} = static_cast<{{field.type}}>(value);
-              }
-              {% else %}
-              result = {{field.variable_name}}.deserialize(buffer);
-              {% endif %}
-            }
-            {% endif %}
-            else
-            {
-              // TODO Error wire type does not match field.
-              result = false;
-            }
+            {{ field_deserialize_macro(field)|indent(12) }}
+            break;
+          }
+
+          {% endfor %}
+          {% for oneof in msg.oneofs() %}
+          {% for field in oneof.fields() %}
+          case {{field.variable_id_name}}:
+          {
+            {{ field_deserialize_macro(field)|indent(12) }}
             break;
             break;
           }
           }
 
 
+          {% endfor %}
           {% endfor %}
           {% endfor %}
           default:
           default:
             break;
             break;
@@ -247,8 +291,22 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
     {{field.type}} {{field.variable_name}};
     {{field.type}} {{field.variable_name}};
     {% endif %}
     {% endif %}
     {% endfor %}
     {% endfor %}
+
+    {% for oneof in msg.oneofs() %}
+    uint32 {{oneof.which_oneof}};
+    {% for field in oneof.fields() %}
+    {% if field.is_repeated_field %}
+    {{field.repeated_type}} {{field.variable_name}};
+    {% else %}
+    {{field.type}} {{field.variable_name}};
+    {% endif %}
+    {% endfor %}
+    {% endfor %}
 };
 };
 {% endmacro %}
 {% endmacro %}
+{# #}
+{# ------------------------------------------------------------------------------------------------------------------ #}
+{# #}
 // This file is generated. Please do not edit!
 // This file is generated. Please do not edit!
 #ifndef _{{filename.upper()}}_H_
 #ifndef _{{filename.upper()}}_H_
 #define _{{filename.upper()}}_H_
 #define _{{filename.upper()}}_H_

+ 1 - 1
generator/protoc-gen-eams.py

@@ -115,7 +115,7 @@ class FieldTemplateParameters:
 class OneofTemplateParameters:
 class OneofTemplateParameters:
     def __init__(self, name, index, msg_proto):
     def __init__(self, name, index, msg_proto):
         self.name = name
         self.name = name
-        self.which_oneof = "which_" + name
+        self.which_oneof = "which_" + name + "_"
         self.index = index
         self.index = index
         self.msg_proto = msg_proto
         self.msg_proto = msg_proto