Skip to content

Commit

Permalink
Merge pull request #9 from marc-christian-schulze/default_values
Browse files Browse the repository at this point in the history
Default values
  • Loading branch information
marc-christian-schulze authored Nov 28, 2024
2 parents 81fc09f + 115fb1f commit e47d002
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 16 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fileHeader.write(buffer);
* padding
* bit fields
* implement Java interfaces
* default values

## Unsupported
* Unions
Expand Down Expand Up @@ -373,7 +374,7 @@ struct AnotherBitset
{
// any other fields ...
bitfield uint8_t {
int32_t number : 4; // 2^7, 2^6, 2^5, 2^4; value range 0 .. 16
int32_t number : 4; // 2^7, 2^6, 2^5, 2^4; value range 0 .. 15
boolean flag : 1; // 2^3
SomeEnum myEnum : 3; // 2^2, 2^1, 2^0
}
Expand All @@ -397,14 +398,37 @@ This will automatically transform the endianess of all fields having types that
## Implementing Java Interfaces

Although `struct`s can not form any inheritance relationship you can let them implement interfaces from your Java code, e.g.:
```C++
```
import org.myproject.MyJavaInterface;
struct SomeStruct implements MyJavaInterface {
// ...
}
```

## Default Values

You can assign fields within structures a default value which is taken when the structure is default constructed:
```
struct SomeStruct {
uint8_t int8 = 1;
uint16_t int16 = 0x45;
uint32_t int32 = 7;
uint64_t int64 = 0x20;
float f = 7.534;
double d = 9.75142476
char str[] = "my default";
SomeEnum e = SomeEnum.B;
}
enum SomeEnum : uint8_t {
A = 0xCAFE,
B = 123,
C = 42
}
```
You can NOT define default values for fields within a bitfield, though.

## Plugin configuration

Below is a full example configuration, including the default values, of the plugin:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ Import:
QualifiedNameWithWildcard:
QualifiedName '.*'?
;

ComplexTypeDeclaration:
StructDeclaration | EnumDeclaration
;

StructDeclaration:
{StructDeclaration}
Expand All @@ -56,6 +52,11 @@ EnumDeclaration:
'}'
;

Item:
(comments += SL_COMMENT)*
name=ID '=' value=LONG ','?
;

BitfieldMember:
(comments += SL_COMMENT)*
'bitfield' typename=INTEGER_TYPE (array=ArrayDimension)? ('padding' '(' padding=LONG ( ',' 'using' '=' using=LONG )? ')')? '{'
Expand All @@ -69,9 +70,8 @@ BitfieldEntry:
| (comments += SL_COMMENT)* type=[EnumDeclaration] name=ID (array=ArrayDimension)? ':' bits=LONG ';'
;

Item:
(comments += SL_COMMENT)*
name=ID '=' value=LONG ','?
ComplexTypeDeclaration:
StructDeclaration | EnumDeclaration
;

ComplexTypeMember:
Expand All @@ -80,6 +80,13 @@ ComplexTypeMember:
(array=ArrayDimension)?
('padding' '(' padding=LONG ( ',' 'using' '=' using=LONG )? ')')?
';'
|
(comments += SL_COMMENT)*
type=[EnumDeclaration|QualifiedName] name=ID
(array=ArrayDimension)?
('padding' '(' padding=LONG ( ',' 'using' '=' using=LONG )? ')')?
'=' defaultValue=[Item|QualifiedName]
';'
;

IntegerMember:
Expand All @@ -92,6 +99,7 @@ IntegerMember:
('countof' '(' countof=[Member] ')')?
)
('padding' '(' padding=LONG ( ',' 'using' '=' using=LONG )? ')')?
('=' defaultValue=LONG)?
';'
;

Expand All @@ -100,6 +108,7 @@ FloatMember:
typename=FLOAT_TYPE name=ID
(array=ArrayDimension)?
('padding' '(' padding=LONG ( ',' 'using' '=' using=LONG )? ')')?
('=' defaultValue=FLOAT)?
';'
;

Expand All @@ -111,6 +120,7 @@ StringMember:
('padding' '(' padding=LONG ( ',' 'using' '=' using=LONG )? ')')?
('filler' '(' filler=LONG ')')?
(nullTerminated='null-terminated')?
('=' defaultValue=STRING)?
';'
;

Expand All @@ -122,6 +132,7 @@ QualifiedName:
ID ('.' ID)*
;

terminal FLOAT returns ecore::EFloat: ('0'..'9')+ '.' ('0'..'9')+;
terminal LONG returns ecore::ELong: ('0'..'9')+ | '0x' ('0'..'9' | 'A'..'F' | 'a'..'f')+;
terminal STRING :
'"' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'"') )* '"' |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1509,12 +1509,15 @@ class StructGenerator {
switch (m) {
ComplexTypeMember: {
if(m.type instanceof EnumDeclaration) {
return "null"
if(m.defaultValue == null) {
return "null"
}
return attributeJavaType(m) + "." + m.defaultValue.name
}
"new " + javaType(m.type) + "()"
}
IntegerMember: "0"
FloatMember: "0.0f"
IntegerMember: "" + m.defaultValue
FloatMember: m.defaultValue + "f"
default: throw new RuntimeException("Unsupported member type: " + m)
}
}
Expand All @@ -1527,7 +1530,7 @@ class StructGenerator {
}
if(m instanceof StringMember) {
return "\"\""
return "\"" + m.defaultValue + "\""
}
val nativeType = nativeTypeName(m)
Expand All @@ -1536,11 +1539,11 @@ class StructGenerator {
}
if(m instanceof IntegerMember) {
return "0"
return "" + m.defaultValue
}
if(m instanceof FloatMember) {
return "0.0f"
return m.defaultValue + "f"
}
if(isBooleanType(m)) {
Expand All @@ -1549,7 +1552,10 @@ class StructGenerator {
if(m instanceof ComplexTypeMember) {
if(m.type instanceof EnumDeclaration) {
return "null"
if(m.defaultValue == null) {
return "null"
}
return attributeJavaType(m) + "." + m.defaultValue.name
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.structs4java.example.tests.defaultvalues;

struct StructWithIntegerDefaultValue {
uint8_t int8 = 1;
uint16_t int16 = 2;
uint32_t int32 = 3;
uint64_t int64 = 4;
}

struct StructWithFloatDefaultValue {
float f = 1.0;
double d = 2.0;
}

struct StructWithStringDefaultValue {
char str[] = "default";
}

enum Int8Enum : uint8_t {
A = 0x01,
B = 0x02,
C = 0x03
}

struct StructWithBitfield {
bitfield uint16_t {
boolean b : 1;
uint8_t int8 : 2;
uint16_t int16 : 2;
uint32_t int32 : 2;
uint64_t int64 : 2;
Int8Enum int8Enum : 7;
}
}

struct StructWithEnum {
Int8Enum int8Enum = Int8Enum.B;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.structs4java.example.tests;

import org.junit.Test;
import org.structs4java.example.tests.defaultvalues.*;

import static org.junit.Assert.assertEquals;

public class DefaultValueTest {
@Test
public void testStructWithIntegerDefaultValue() {
StructWithIntegerDefaultValue struct = new StructWithIntegerDefaultValue();
assertEquals(1, struct.getInt8());
assertEquals(2, struct.getInt16());
assertEquals(3, struct.getInt32());
assertEquals(4, struct.getInt64());
}

@Test
public void testStructWithFloatDefaultValue() {
StructWithFloatDefaultValue struct = new StructWithFloatDefaultValue();
assertEquals(1.0, struct.getF(), 0.001);
assertEquals(2.0, struct.getD(), 0.001);
}

@Test
public void testStructWithStringDefaultValue() {
StructWithStringDefaultValue struct = new StructWithStringDefaultValue();
assertEquals("default", struct.getStr());
}

@Test
public void testStructWithEnumDefaultValue() {
StructWithEnum struct = new StructWithEnum();
assertEquals(Int8Enum.B, struct.getInt8Enum());
}
}

0 comments on commit e47d002

Please sign in to comment.