From 32d1895f0b9030de308cb3f881fd2e6a288df3bc Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Wed, 30 Oct 2019 20:01:37 +0100 Subject: [PATCH] fixed stuff --- src/andrew.cr | 20 +++++++++------- src/views/class.ecr | 57 ++++++++++++++++++++++++++++++++++---------- src/views/file.ecr | 7 ++++++ src/views/repeat.ecr | 19 +++++++++++++++ tests/db_file.yml | 24 +++++++++++++++++++ tests/record.yml | 22 +++++++++++------ 6 files changed, 121 insertions(+), 28 deletions(-) create mode 100644 src/views/repeat.ecr create mode 100644 tests/db_file.yml diff --git a/src/andrew.cr b/src/andrew.cr index 6ae1526..9460296 100644 --- a/src/andrew.cr +++ b/src/andrew.cr @@ -11,7 +11,11 @@ end class FileGenerator YAML.mapping( classes: Array(ClassGenerator), - includes: Array(String) + includes: Array(String), + namespace: { + type: String, + nilable: true + } ) def initialize @@ -40,6 +44,10 @@ class ClassGenerator attributeafters: { type: Array(AttributeAfterGenerator), nilable: true + }, + size: { + type: Int32, + nilable: true } ) @@ -50,14 +58,6 @@ class ClassGenerator @attributeafters = Array(AttributeAfterGenerator).new end - def push(attr : AttributeGenerator) - attr.class_name = @name - @attributes.push(attr) - end - - def push(bitf : BitfieldGenerator) - end - ECR.def_to_s "src/views/class.ecr" end @@ -112,6 +112,8 @@ class RepeatGenerator def initialize(@name : String, @out_type : String, @start : Int32, @count_name : String) @class_name = "no_class" end + + ECR.def_to_s "src/views/repeat.ecr" end class AttributeAfterGenerator diff --git a/src/views/class.ecr b/src/views/class.ecr index 6a3d14a..ca40a51 100644 --- a/src/views/class.ecr +++ b/src/views/class.ecr @@ -1,24 +1,57 @@ class <%= @name %> { virtual ~<%= @name %>() = 0; +public: <% if !(@attributes.nil?) %> -<%- @attributes.not_nil!.each do |attr| -%> -<%- attr.class_name = @name -%> -<%= attr.to_s %> -<%- end -%> + <%- @attributes.not_nil!.each do |attr| -%> + <%- 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 -%> + <%- @attributeafters.not_nil!.each do |attr| -%> + <%- attr.class_name = @name -%> + <%-= attr.to_s %> + <%- end -%> <% end %> <% if !(@bitfields.nil?) %> -<%- @bitfields.not_nil!.each do |attr| -%> -<%- attr.class_name = @name -%> -<%= attr.to_s %> -<%- end -%> + <%- @bitfields.not_nil!.each do |attr| -%> + <%- attr.class_name = @name -%> + <%-= attr.to_s %> + <%- end -%> <% end %> + +<% if !(@repeats.nil?) %> + <%- @repeats.not_nil!.each do |attr| -%> + <%- attr.class_name = @name -%> + <%-= attr.to_s %> + <%- end -%> +<% end %> + +private: +void dummy_function() +{ +<% if !@size.nil? %> + <%- sz = @size.not_nil! -%> + <% if !(@attributes.nil?) %> + <%- @attributes.not_nil!.each do |attr|-%> + <%- if attr.start > sz -%> + <%- raise "attribute starts after end: "+attr.name+" in class "+@name -%> + <%- end -%> + static_assert(<%= attr.name %>_end_position()<=<%= sz%>, "attribute past the end"); + <%- end -%> + <%- end -%> + <% if !(@bitfields.nil?) %> + <%- @bitfields.not_nil!.each do |attr|-%> + <%- if attr.start > sz -%> + <%- raise "bitfield starts after end: "+attr.name+" in class "+@name -%> + <%- end -%> + static_assert(<%= attr.name %>_end_position()<=<%= sz%>, "attribute past the end"); + <%- end -%> + <%- end -%> +<% end %> +} + }; diff --git a/src/views/file.ecr b/src/views/file.ecr index 4b18309..8d7458c 100644 --- a/src/views/file.ecr +++ b/src/views/file.ecr @@ -3,7 +3,14 @@ #include <%= v%> <%- end -%> +<%- if !(@namespace.nil?) -%> +namespace <%= @namespace.to_s %> { +<%- end -%> <%- @classes.each do |v| -%> <%= v.to_s %> +<%- end -%> + +<%- if !(@namespace.nil?) -%> +} <%- end -%> \ No newline at end of file diff --git a/src/views/repeat.ecr b/src/views/repeat.ecr new file mode 100644 index 0000000..7c71591 --- /dev/null +++ b/src/views/repeat.ecr @@ -0,0 +1,19 @@ + static std::span<<%= @out_type %>> get_<%= @name %>(const <%= class_name %>& alter) { + auto ptr = (char*)&alter; + ptr+=<%= @start %>; + return std::span<<%= @out_type %>>((<%= @out_type %>*)ptr, get_<%= @count_name %>(alter)); + } + + static void set_<%= @name %>(<%= class_name %>& alter, size_t index, const <%= @out_type %>& param) { + auto ptr = (char*)&alter; + ptr+=<%= @start %>; + *(<%= @out_type %>*)ptr = param; + } + + static constexpr size_t <%= @name %>_position() { + return <%= @start %>; + } + + static size_t <%= @name %>_end_position(<%= class_name %>& alter) { + return <%= @start %>+get_<%= @count_name %>(alter)*sizeof(<%= @out_type %>); + } \ No newline at end of file diff --git a/tests/db_file.yml b/tests/db_file.yml new file mode 100644 index 0000000..0186132 --- /dev/null +++ b/tests/db_file.yml @@ -0,0 +1,24 @@ +includes: + - + - +namespace: db +classes: + - + name: database + attributes: + - + name: metadata + start: 0 + out_type: std::array + - + name: record_cnt + start: 0 + out_type: uint64_t + repeats: + - + name: record_list + start: 4096 + out_type: std::array + count_name: record_cnt + + diff --git a/tests/record.yml b/tests/record.yml index 43e046e..e59dab6 100644 --- a/tests/record.yml +++ b/tests/record.yml @@ -1,8 +1,10 @@ includes: - +namespace: db classes: - name: record_identifier + size: 24 attributes: - name: uuid @@ -18,6 +20,7 @@ classes: start: 20 - name: record + size: 64 attributes: - name: record_id @@ -37,35 +40,40 @@ classes: name: flags out_type: uint16_t start: 40 - mask: 0b0000000000000111 + mask: 0b1110000000000000 - name: v_flag out_type: uint16_t after_name: offset start: 40 - mask: 0b0000000000000100 + mask: 0b1000000000000000 - name: r_flags out_type: uint16_t start: 40 - mask: 0b0000000000000010 + mask: 0b0100000000000000 - name: c_flags out_type: uint16_t start: 40 - mask: 0b0000000000000001 + mask: 0b0010000000000000 - name: confirmed out_type: uint16_t start: 40 - mask: 0b0000000000000001 + mask: 0b0010000000000000 - name: validated out_type: uint16_t start: 40 - mask: 0b0000000000000101 + mask: 0b1010000000000000 - name: removed out_type: uint16_t start: 40 - mask: 0b0000000000000010 \ No newline at end of file + mask: 0b0100000000000000 + - + name: next_jump + out_type: uint16_t + start: 40 + mask: 0b0000111111111111 \ No newline at end of file