add sqare shape to example

This commit is contained in:
ado 2021-01-03 03:18:33 +01:00
parent a657670920
commit cc7e6f7806

View File

@ -250,12 +250,13 @@ Custom types can be used when converting values. An override of the **ss::extrac
function needs to be made and you are good to go. Custom conversion for an enum function needs to be made and you are good to go. Custom conversion for an enum
would look like this: would look like this:
```cpp ```cpp
enum class shape { circle, rectangle, triangle }; enum class shape { circle, square, rectangle, triangle };
template <> template <>
inline bool ss::extract(const char* begin, const char* end, shape& dst) { inline bool ss::extract(const char* begin, const char* end, shape& dst) {
const static std::unordered_map<std::string, shape> const static std::unordered_map<std::string, shape>
shapes{{"circle", shape::circle}, shapes{{"circle", shape::circle},
{"square", shape::square},
{"rectangle", shape::rectangle}, {"rectangle", shape::rectangle},
{"triangle", shape::triangle}}; {"triangle", shape::triangle}};
@ -298,15 +299,16 @@ std::vector<std::pair<shape, double>> shapes;
while (!p.eof()) { while (!p.eof()) {
using ss::nx; using ss::nx;
auto [circle, rectangle, triangle] = auto [circle_or_square, rectangle, triangle] =
p.try_next<nx<shape, shape::circle>, double>() p.try_next<nx<shape, shape::circle, shape::square>, double>()
.or_else<nx<shape, shape::rectangle>, double, double>() .or_else<nx<shape, shape::rectangle>, double, double>()
.or_else<nx<shape, shape::triangle>, double, double, double>() .or_else<nx<shape, shape::triangle>, double, double, double>()
.values(); .values();
if (circle) { if (circle_or_square) {
auto& [s, r] = circle.value(); auto& [s, x] = circle_or_square.value();
shapes.emplace_back(s, r * r * M_PI); double area = (s == shape::circle) ? x * x * M_PI : x * x;
shapes.emplace_back(s, area);
} }
if (rectangle) { if (rectangle) {