Browse Source

Added bitfields

master
Ludovic 'Archivist' Lagouardette 5 years ago
parent
commit
0e341392e6
6 changed files with 78 additions and 13 deletions
  1. +4
    -2
      src/andrew.cr
  2. +4
    -4
      src/views/attribute.ecr
  3. +4
    -4
      src/views/attribute_after.ecr
  4. +25
    -0
      src/views/bitfield_generator.ecr
  5. +6
    -1
      src/views/class.ecr
  6. +35
    -2
      tests/record.yml

+ 4
- 2
src/andrew.cr View File

@ -70,12 +70,14 @@ class BitfieldGenerator
name: String,
out_type: String,
start: Int32,
size: Int32
mask: String
)
def initialize(@name : String, @out_type : String, @start : Int32, @size : Int32)
def initialize(@name : String, @out_type : String, @start : Int32, @mask : String)
@class_name = "no_class"
end
ECR.def_to_s "src/views/bitfield_generator.ecr"
end
class AttributeGenerator

+ 4
- 4
src/views/attribute.ecr View File

@ -1,19 +1,19 @@
<%= @out_type %> get_<%= @name %>(const <%= class_name %>& alter) {
static <%= @out_type %> get_<%= @name %>(const <%= class_name %>& alter) {
auto ptr = (char*)&alter;
ptr+=<%= @start %>;
return *(<%= @out_type %>*)ptr;
}
void set_<%= @name %>(<%= class_name %>& alter, const <%= @out_type %>& param) {
static void set_<%= @name %>(<%= class_name %>& alter, const <%= @out_type %>& param) {
auto ptr = (char*)&alter;
ptr+=<%= @start %>;
*(<%= @out_type %>*)ptr = param;
}
constexpr size_t <%= @name %>_position() {
static constexpr size_t <%= @name %>_position() {
return <%= @start %>;
}
constexpr size_t <%= @name %>_end_position() {
static constexpr size_t <%= @name %>_end_position() {
return <%= @start %>+sizeof(<%= @out_type %>);
}

+ 4
- 4
src/views/attribute_after.ecr View File

@ -1,18 +1,18 @@
size_t <%= @name %>_position() {
static size_t <%= @name %>_position() {
return <%= @name %>_end_position();
}
size_t <%= @name %>_end_position() {
static size_t <%= @name %>_end_position() {
return <%= @name %>_position()+sizeof(<%= @out_type %>);
}
<%= @out_type %> get_<%= @name %>(const <%= class_name %>& alter) {
static <%= @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) {
static void set_<%= @name %>(<%= class_name %>& alter, const <%= @out_type %>& param) {
auto ptr = (char*)&alter;
ptr+=<%= @name %>_position();
*(<%= @out_type %>*)ptr = param;

+ 25
- 0
src/views/bitfield_generator.ecr View File

@ -0,0 +1,25 @@
static <%= @out_type %> get_<%= @name %>(const <%= class_name %>& alter) {
auto ptr = (char*)&alter;
ptr+=<%= @start %>;
return <%= @mask %> & *(<%= @out_type %>*)ptr;
}
static void set_<%= @name %>(<%= class_name %>& alter) {
auto ptr = (char*)&alter;
ptr+=<%= @start %>;
*(<%= @out_type %>*)ptr = <%= @mask %> | *(<%= @out_type %>*)ptr;
}
static void unset_<%= @name %>(<%= class_name %>& alter) {
auto ptr = (char*)&alter;
ptr+=<%= @start %>;
*(<%= @out_type %>*)ptr = (<%= @mask %> | *(<%= @out_type %>*)ptr) ^ <%= @mask %>;
}
static constexpr size_t <%= @name %>_position() {
return <%= @start %>;
}
static constexpr size_t <%= @name %>_end_position() {
return <%= @start %>+sizeof(<%= @out_type %>);
}

+ 6
- 1
src/views/class.ecr View File

@ -5,7 +5,6 @@ virtual ~<%= @name %>() = 0;
<%- @attributes.not_nil!.each do |attr| -%>
<%- attr.class_name = @name -%>
<%= attr.to_s %>
<%- end -%>
<% end %>
@ -13,7 +12,13 @@ virtual ~<%= @name %>() = 0;
<%- @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 -%>
<% end %>
};

+ 35
- 2
tests/record.yml View File

@ -32,7 +32,40 @@ classes:
name: offset
out_type: uint64_t
after_name: timestamp
bitfields:
-
name: flags
out_type: uint32_t
after_name: offset
out_type: uint16_t
start: 40
mask: 0b0000000000000111
-
name: v_flag
out_type: uint16_t
after_name: offset
start: 40
mask: 0b0000000000000100
-
name: r_flags
out_type: uint16_t
start: 40
mask: 0b0000000000000010
-
name: c_flags
out_type: uint16_t
start: 40
mask: 0b0000000000000001
-
name: confirmed
out_type: uint16_t
start: 40
mask: 0b0000000000000101
-
name: validated
out_type: uint16_t
start: 40
mask: 0b0000000000000101
-
name: removed
out_type: uint16_t
start: 40
mask: 0b0000000000000010

Loading…
Cancel
Save