Skip to content

Instantly share code, notes, and snippets.

@ecatmur
Created June 26, 2022 21:51
Show Gist options
  • Save ecatmur/39336596fe10e5d66ab4aa0fe102b02c to your computer and use it in GitHub Desktop.
Save ecatmur/39336596fe10e5d66ab4aa0fe102b02c to your computer and use it in GitHub Desktop.
#include <boost/assert/source_location.hpp>
#include <algorithm>
#include <source_location>
#if not _MSC_VER
template<unsigned M, unsigned N>
#endif
struct sloc_literal {
#if _MSC_VER
char const* file = nullptr;
char const* function = nullptr;
#else
char file[M] = {};
char function[N] = {};
#endif
unsigned line;
unsigned column;
constexpr sloc_literal(std::source_location loc) {
#if _MSC_VER
file = loc.file_name();
function = loc.function_name();
#else
std::copy_n(loc.file_name(), M, file);
std::copy_n(loc.function_name(), N, function);
#endif
line = loc.line();
column = loc.column();
}
};
#if _MSC_VER
template<sloc_literal loc>
static boost::source_location sloc(loc.file, loc.line, loc.function, loc.column);
#define PCURRENT_LOCATION &::sloc< \
std::source_location::current()>
#elif 0
#define PCURRENT_LOCATION ([](auto l) { \
static boost::source_location const loc = l; \
return &loc; \
}(std::source_location::current()))
#elif 1 and __GNUC__
#define PCURRENT_LOCATION reinterpret_cast<boost::source_location const*>( \
__builtin_source_location())
#else
template<unsigned M, unsigned N, sloc_literal<M, N> loc>
static boost::source_location sloc(loc.file, loc.line, loc.function, loc.column);
#define PCURRENT_LOCATION &::sloc< \
std::strlen(std::source_location::current().file_name()), \
std::strlen(std::source_location::current().function_name()), \
std::source_location::current()>
#endif
auto f() { return PCURRENT_LOCATION; }
#if 0
#include <boost/system/error_code.hpp>
#include <iostream>
int main() {
auto const ec = boost::system::error_code(
make_error_code(boost::system::errc::io_error,
PCURRENT_LOCATION));
std::cout << ec.what();
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment