Remove std::function from SCOPE_INFO_FMT implementation

This commit is contained in:
Jonathan G Rennison 2024-09-02 00:10:31 +01:00
parent 515f33b84d
commit e2fbd8aa0e
2 changed files with 17 additions and 10 deletions

View File

@ -24,7 +24,7 @@
#ifdef USE_SCOPE_INFO
std::vector<std::function<int(char *, const char *)>> _scope_stack;
std::vector<ScopeStackRecord> _scope_stack;
int WriteScopeLog(char *buf, const char *last)
{
@ -34,7 +34,7 @@ int WriteScopeLog(char *buf, const char *last)
int depth = 0;
for (auto it = _scope_stack.rbegin(); it != _scope_stack.rend(); ++it, depth++) {
b += seprintf(b, last, "\n %2d: ", depth);
b += (*it)(b, last);
b += it->functor(it->target, b, last);
}
b += seprintf(b, last, "\n\n");
}

View File

@ -12,7 +12,6 @@
#include "tile_type.h"
#include <functional>
#include <vector>
struct Vehicle;
@ -21,12 +20,19 @@ struct Window;
#ifdef USE_SCOPE_INFO
extern std::vector<std::function<int(char *, const char *)>> _scope_stack;
struct ScopeStackRecord {
using ScopeStackFunctor = int (*)(void *, char *, const char *);
ScopeStackFunctor functor;
void *target;
};
extern std::vector<ScopeStackRecord> _scope_stack;
struct scope_info_func_obj {
scope_info_func_obj(std::function<int(char *, const char *)> func)
scope_info_func_obj(ScopeStackRecord record)
{
_scope_stack.emplace_back(std::move(func));
_scope_stack.emplace_back(record);
}
scope_info_func_obj(const scope_info_func_obj &copysrc) = delete;
@ -43,16 +49,17 @@ int WriteScopeLog(char *buf, const char *last);
/**
* This creates a lambda in the current scope with the specified capture which outputs the given args as a format string.
* This lambda is then captured by reference in a std::function which is pushed onto the scope stack
* This lambda is then captured by pointer in a ScopeStackRecord which is pushed onto the scope stack
* The scope stack is popped at the end of the scope
*/
#define SCOPE_INFO_FMT(capture, ...) \
auto SCOPE_INFO_PASTE(_sc_lm_, __LINE__) = capture (char *buf, const char *last) { \
return seprintf(buf, last, __VA_ARGS__); \
}; \
scope_info_func_obj SCOPE_INFO_PASTE(_sc_obj_, __LINE__) ([&](char *buf, const char *last) -> int { \
return SCOPE_INFO_PASTE(_sc_lm_, __LINE__) (buf, last); \
});
scope_info_func_obj SCOPE_INFO_PASTE(_sc_obj_, __LINE__) (ScopeStackRecord{ [](void *target, char *buf, const char *last) -> int { \
auto targ = static_cast<decltype(& SCOPE_INFO_PASTE(_sc_lm_, __LINE__))>(target); \
return (*targ)(buf, last); \
}, static_cast<void *>(& SCOPE_INFO_PASTE(_sc_lm_, __LINE__)) });
#else /* USE_SCOPE_INFO */