love lambdas

v1
Brett 2023-12-11 16:58:20 -05:00
parent 4e526f1e8e
commit c1c989d416
2 changed files with 22 additions and 28 deletions

View File

@ -209,6 +209,19 @@ namespace blt
} }
}; };
template<class... TLambdas>
struct lambda_visitor : TLambdas... {
using TLambdas::operator()...;
};
#if __cplusplus < 202002L
// explicit deduction guide (not needed as of C++20)
template<class... TLambdas>
lambda_visitor(TLambdas...) -> lambda_visitor<TLambdas...>;
#endif
#if defined(__GNUC__) || defined(__llvm__) #if defined(__GNUC__) || defined(__llvm__)
#define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline)) #define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline))

View File

@ -8,6 +8,7 @@
#include <cmath> #include <cmath>
#include "blt/std/logging.h" #include "blt/std/logging.h"
#include "blt/std/assert.h" #include "blt/std/assert.h"
#include "blt/std/utility.h"
#include <stack> #include <stack>
#include <queue> #include <queue>
#include <algorithm> #include <algorithm>
@ -422,32 +423,6 @@ std::string blt::string::createPadding(size_t length, char spacing)
namespace blt 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) void constructVerticalSeparator(blt::string::ascii_data& data, size_t offset, size_t height)
{ {
for (size_t i = 0; i < height; i++) 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) blt::string::ascii_data blt::string::constructBox(const blt::string::box_type& box)
{ {
auto width = std::visit(getWidth(), box); auto width = std::visit(blt::lambda_visitor{
auto height = std::visit(getHeight(), box); [](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); string::ascii_data data(width, height);