From 8f5047274a378aa835617969b730286910e9d6e0 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Tue, 22 Oct 2019 20:03:11 +0200 Subject: [PATCH] Added attributeafters --- src/andrew.cr | 5 +++-- src/views/attribute.ecr | 8 ++++++++ src/views/attribute_after.ecr | 19 +++++++++++++++++++ src/views/class.ecr | 8 ++++++++ tests/record.yml | 5 +++-- 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/views/attribute_after.ecr diff --git a/src/andrew.cr b/src/andrew.cr index b145089..38d5064 100644 --- a/src/andrew.cr +++ b/src/andrew.cr @@ -120,12 +120,13 @@ class AttributeAfterGenerator }, name: String, out_type: String, - start: Int32, after_name: String ) - def initialize(@name : String, @out_type : String, @start : Int32, @after_name : String) + def initialize(@name : String, @out_type : String, @after_name : String) @class_name = "no_class" end + + ECR.def_to_s "src/views/attribute_after.ecr" end if(ARGV.size != 0) diff --git a/src/views/attribute.ecr b/src/views/attribute.ecr index ec263d4..f061afd 100644 --- a/src/views/attribute.ecr +++ b/src/views/attribute.ecr @@ -8,4 +8,12 @@ auto ptr = (char*)&alter; ptr+=<%= @start %>; *(<%= @out_type %>*)ptr = param; + } + + constexpr size_t <%= @name %>_position() { + return <%= @name %>_position; + } + + constexpr size_t <%= @name %>_end_position() { + return <%= @start %>+sizeof(<%= @out_type %>); } \ No newline at end of file diff --git a/src/views/attribute_after.ecr b/src/views/attribute_after.ecr new file mode 100644 index 0000000..3fcf366 --- /dev/null +++ b/src/views/attribute_after.ecr @@ -0,0 +1,19 @@ + size_t <%= @name %>_position() { + return <%= @name %>_end_position()); + } + + size_t <%= @name %>_end_position() { + return <%= @name %>_position()+sizeof(<%= @out_type %>); + } + + <%= @out_type %> get_<%= @name %>(const <%= class_name %>& alter) { + auto ptr = (char*)&alter; + ptr+=<%= @name %>_position(); + return *(<%= @out_type %>*)ptr; + } + + void set_<%= @name %>(<%= class_name %>& alter, const <%= @out_type %>& param) { + auto ptr = (char*)&alter; + ptr+=<%= @name %>_position(); + *(<%= @out_type %>*)ptr = param; + } \ No newline at end of file diff --git a/src/views/class.ecr b/src/views/class.ecr index 7d706a5..183f1a3 100644 --- a/src/views/class.ecr +++ b/src/views/class.ecr @@ -6,6 +6,14 @@ virtual <%= @name %>() = 0; <%- attr.class_name = @name -%> <%= attr.to_s %> +<%- end -%> +<% end %> + +<% if !(@attributeafters.nil?) %> +<%- @attributeafters.not_nil!.each do |attr| -%> +<%- attr.class_name = @name -%> +<%= attr.to_s %> + <%- end -%> <% end %> }; diff --git a/tests/record.yml b/tests/record.yml index 1494495..d68b9d6 100644 --- a/tests/record.yml +++ b/tests/record.yml @@ -28,11 +28,12 @@ classes: name: timestamp out_type: bitops::regulated start: 24 + attributeafters: - name: offset out_type: bitops::regulated - start: 32 + after_name: timestamp - name: flags out_type: bitops::regulated - start: 40 \ No newline at end of file + after_name: offset \ No newline at end of file