From dbd68011363b01f22ab85e11ad52a8e90da9611a Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 6 Nov 2021 11:05:45 +0100 Subject: Use std::source_location instead of __FILE__. --- src/libctor.cc | 2 +- src/libctor.h | 10 +++++++--- src/rebuild.cc | 33 +++++++++++++++++++++++++-------- src/rebuild.h | 2 +- 4 files changed, 34 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/libctor.cc b/src/libctor.cc index c752192..1fe1298 100644 --- a/src/libctor.cc +++ b/src/libctor.cc @@ -200,7 +200,7 @@ Options: no_default_build = true; for(const auto& add_file : add_files) { - reg(add_file.data(), [](){ return std::vector{};}); + reg(add_file.data()); } for(const auto& remove_file : remove_files) diff --git a/src/libctor.h b/src/libctor.h index 2a10c53..6d3becf 100644 --- a/src/libctor.h +++ b/src/libctor.h @@ -3,6 +3,7 @@ // See accompanying file LICENSE for details. #pragma once +#include #include #include #include @@ -61,7 +62,9 @@ struct BuildConfiguration using BuildConfigurations = std::vector; -int reg(const char* location, BuildConfigurations (*cb)()); +int reg(BuildConfigurations (*cb)(), + const std::source_location location = std::source_location::current()); + struct ExternalConfiguration { @@ -74,13 +77,14 @@ struct ExternalConfiguration using ExternalConfigurations = std::vector; -int reg(const char* location, ExternalConfigurations (*cb)()); +int reg(ExternalConfigurations (*cb)(), + const std::source_location location = std::source_location::current()); // Convenience macro - ugly but keeps things simple(r) #define CONCAT(a, b) CONCAT_INNER(a, b) #define CONCAT_INNER(a, b) a ## b #define UNIQUE_NAME(base) CONCAT(base, __LINE__) -#define REG(cb) namespace { int UNIQUE_NAME(unique) = reg(__FILE__, cb); } +#define REG(cb) namespace { int UNIQUE_NAME(unique) = reg(cb); } // Predefined configuration keys namespace cfg diff --git a/src/rebuild.cc b/src/rebuild.cc index a03f5e3..fc95b16 100644 --- a/src/rebuild.cc +++ b/src/rebuild.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include "configure.h" #include "settings.h" @@ -16,9 +17,8 @@ std::array configFiles; std::size_t numConfigFiles{0}; -// TODO: Use c++20 when ready, somehing like this: -//int reg(const std::source_location location = std::source_location::current()) -int reg(const char* location, std::vector (*cb)()) +int reg(std::vector (*cb)(), + const std::source_location location) { // NOTE: std::cout cannot be used here if(numConfigFiles >= configFiles.size()) @@ -28,13 +28,31 @@ int reg(const char* location, std::vector (*cb)()) exit(1); } - configFiles[numConfigFiles].file = location; + configFiles[numConfigFiles].file = location.file_name(); configFiles[numConfigFiles].cb = cb; ++numConfigFiles; return 0; } +int reg(const char* location) +{ + // NOTE: std::cout cannot be used here + if(numConfigFiles >= configFiles.size()) + { + fprintf(stderr, "Max %d build configurations currently supported.\n", + (int)configFiles.size()); + exit(1); + } + + configFiles[numConfigFiles].file = location; + configFiles[numConfigFiles].cb = + [](){ return std::vector{}; }; + ++numConfigFiles; + + return 0; +} + int unreg(const char* location) { std::size_t found{0}; @@ -78,9 +96,8 @@ int unreg(const char* location) std::array externalConfigFiles; std::size_t numExternalConfigFiles{0}; -// TODO: Use c++20 when ready, somehing like this: -//int reg(const std::source_location location = std::source_location::current()) -int reg(const char* location, std::vector (*cb)()) +int reg(std::vector (*cb)(), + const std::source_location location) { // NOTE: std::cout cannot be used here if(numExternalConfigFiles >= externalConfigFiles.size()) @@ -90,7 +107,7 @@ int reg(const char* location, std::vector (*cb)()) exit(1); } - externalConfigFiles[numExternalConfigFiles].file = location; + externalConfigFiles[numExternalConfigFiles].file = location.file_name(); externalConfigFiles[numExternalConfigFiles].cb = cb; ++numExternalConfigFiles; diff --git a/src/rebuild.h b/src/rebuild.h index 020df6b..f14236e 100644 --- a/src/rebuild.h +++ b/src/rebuild.h @@ -28,7 +28,7 @@ extern std::size_t numConfigFiles; extern std::array externalConfigFiles; extern std::size_t numExternalConfigFiles; -//int reg(const char* location, std::vector (*cb)()); +int reg(const char* location); int unreg(const char* location); void recompileCheck(const Settings& settings, int argc, char* argv[], -- cgit v1.2.3