Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpilibj] Procedural structs for enums and records #7149

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

oh-yes-0-fps
Copy link
Contributor

@oh-yes-0-fps oh-yes-0-fps commented Oct 2, 2024

2024-10-02.16-01-39.mp4
import static edu.wpi.first.util.struct.ProceduralStructGenerator.genEnum;
import static edu.wpi.first.util.struct.ProceduralStructGenerator.genRecord;

// An example of using procedural structs
public class TestingGround {
    public record MySubRecord(int int64, boolean bool, double float64) implements StructSerializable {
        public static final Struct<MySubRecord> struct = genRecord(MySubRecord.class);
    }
    public enum MySubEnum implements StructSerializable {
        A(1), B(2), C(3);

        public final int value;

        MySubEnum(int value) {
            this.value = value;
        }

        public static final Struct<MySubEnum> struct = genEnum(MySubEnum.class);
    }

    public record MyRecord(MySubRecord sub, Translation2d pose, MySubEnum eenum) {}
    private static final Struct<MyRecord> myRecordStruct = genRecord(MyRecord.class);

    private static final StructPublisher<MyRecord> publisher = NetworkTableInstance.getDefault()
            .getStructTopic("structTestTopic", myRecordStruct)
            .publish();

    public static void periodic() {
        double rand = Math.random() * 100;
        MySubRecord subRec = new MySubRecord((int) (rand/2.0), rand > 50, rand);
        Translation2d translation = new Translation2d(-rand, rand);
        MySubEnum eenum = MySubEnum.values()[(int) (rand / 50.0)];
        MyRecord record = new MyRecord(subRec, translation, eenum);
        publisher.set(record);
    }
}

@oh-yes-0-fps oh-yes-0-fps marked this pull request as draft October 2, 2024 20:18
@oh-yes-0-fps
Copy link
Contributor Author

The enum variant field not working as intended is isolated to glass, in advantage scope it works as intended.
image

@oh-yes-0-fps oh-yes-0-fps marked this pull request as ready for review October 2, 2024 22:06
@oh-yes-0-fps
Copy link
Contributor Author

Hypothetically if a genObject was added to this you could do stuff like this. The issue with genObject is it will only work with statically sized POJO's. But that static size constraint is already there for enums and records so maybe adding a genObject method wouldn't be a bad idea.

2024-10-06.23-03-10.mp4

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants