From 5f0734078cbab4ebca5f54e25305cbcec2506769 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Wed, 20 Dec 2023 21:46:01 +0100 Subject: [PATCH] Added flag enums in the README --- README.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d8f36d12..96b936f9 100644 --- a/README.md +++ b/README.md @@ -165,22 +165,29 @@ Found 5 errors: reflect-cpp supports scoped enumerations: ```cpp -enum class Color { red, green, blue, yellow }; +enum class Shape { circle, square, rectangle }; -struct Circle { - float radius; +enum class Color { red = 256, green = 512, blue = 1024, yellow = 2048 }; + +struct Item { + float pos_x; + float pos_y; + Shape shape; Color color; }; -const auto circle = Circle{.radius = 2.0, .color = Color::green}; +const auto item = Item{.pos_x = 2.0, + .pos_y = 3.0, + .shape = Shape::square, + .color = Color::red | Color::blue}; -rfl::json::write(circle); +rfl::json::write(item); ``` This results in the following JSON string: ```json -{"radius":2.0,"color":"green"} +{"pos_x":2.0,"pos_y":3.0,"shape":"square","color":"red|blue"} ``` ## Algebraic data types