ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
resolver.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdlib>
4#include <optional>
5#include <string_view>
6#include <string>
7
8#define STRINGIFY_IMPL(x) #x
9#define STRINGIFY(x) STRINGIFY_IMPL(x)
10
11namespace arkanjo::config {
12 template<typename F>
13 auto resolve_config_impl(std::string_view name, F&& fallback) {
14 std::string name_cstr{name};
15 if (const char* env = std::getenv(name_cstr.c_str())) {
16 return fallback(std::optional<std::string_view>(env));
17 }
18 return fallback(std::optional<std::string_view>{std::nullopt});
19 }
20}
21
22#define CONFIG_VALUE(name, fallback_lambda) \
23 ([](auto&& f) { \
24 if constexpr (std::string_view(#name) != std::string_view(STRINGIFY(name))) { \
25 return f(std::optional<std::string_view>{STRINGIFY(name)}); \
26 } else { \
27 return arkanjo::config::resolve_config_impl(#name, f); \
28 } \
29 })(fallback_lambda)
auto resolve_config_impl(std::string_view name, F &&fallback)
Definition: resolver.hpp:13