From c1c989d416a6ed749293e02694933f5208e7c554 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Mon, 11 Dec 2023 16:58:20 -0500 Subject: [PATCH] love lambdas --- include/blt/std/utility.h | 13 +++++++++++++ src/blt/std/format.cpp | 37 +++++++++---------------------------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index fcdb818..d05f4f6 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -208,6 +208,19 @@ namespace blt return range_itr(_end - offset, offset == 0); } }; + + template + struct lambda_visitor : TLambdas... { + using TLambdas::operator()...; + }; + +#if __cplusplus < 202002L + + // explicit deduction guide (not needed as of C++20) + template + lambda_visitor(TLambdas...) -> lambda_visitor; + +#endif #if defined(__GNUC__) || defined(__llvm__) diff --git a/src/blt/std/format.cpp b/src/blt/std/format.cpp index baf1b34..82a59f6 100755 --- a/src/blt/std/format.cpp +++ b/src/blt/std/format.cpp @@ -8,6 +8,7 @@ #include #include "blt/std/logging.h" #include "blt/std/assert.h" +#include "blt/std/utility.h" #include #include #include @@ -422,32 +423,6 @@ std::string blt::string::createPadding(size_t length, char spacing) namespace blt { - struct getHeight - { - inline size_t operator()(const blt::string::ascii_box& box) - { - return box.height(); - } - - inline size_t operator()(const blt::string::ascii_boxes& boxes) - { - return boxes.height(); - } - }; - - struct getWidth - { - inline size_t operator()(const blt::string::ascii_box& box) - { - return box.width(); - } - - inline size_t operator()(const blt::string::ascii_boxes& boxes) - { - return boxes.width(); - } - }; - void constructVerticalSeparator(blt::string::ascii_data& data, size_t offset, size_t height) { for (size_t i = 0; i < height; i++) @@ -491,8 +466,14 @@ namespace blt blt::string::ascii_data blt::string::constructBox(const blt::string::box_type& box) { - auto width = std::visit(getWidth(), box); - auto height = std::visit(getHeight(), box); + auto width = std::visit(blt::lambda_visitor{ + [](const blt::string::ascii_box& box) -> size_t {return box.width();}, + [](const blt::string::ascii_boxes& boxes) -> size_t {return boxes.width();} + }, box); + auto height = std::visit(blt::lambda_visitor{ + [](const blt::string::ascii_box& box) -> size_t {return box.height();}, + [](const blt::string::ascii_boxes& boxes) -> size_t {return boxes.height();} + }, box); string::ascii_data data(width, height);