cppfig 0.1.0
Modern C++20 compile-time type-safe configuration library
Loading...
Searching...
No Matches
setting.h
Go to the documentation of this file.
1#pragma once
2
3#include <string_view>
4
5#include "cppfig/traits.h"
6#include "cppfig/validator.h"
7
8namespace cppfig {
9
34template <typename S>
35concept IsSetting = requires {
36 { S::path } -> std::convertible_to<std::string_view>;
37 typename S::value_type;
38 { S::default_value() } -> std::same_as<typename S::value_type>;
40
42template <typename S>
43concept HasEnvOverride = IsSetting<S> && requires {
44 { S::env_override } -> std::convertible_to<std::string_view>;
45};
46
48template <typename S>
49concept HasValidator = IsSetting<S> && requires {
50 { S::validator() } -> std::same_as<Validator<typename S::value_type>>;
51};
52
54template <IsSetting S>
55constexpr auto GetEnvOverride() -> std::string_view
56{
57 if constexpr (HasEnvOverride<S>) {
58 return S::env_override;
59 }
60 else {
61 return "";
62 }
63}
64
66template <IsSetting S>
68{
69 if constexpr (HasValidator<S>) {
70 return S::validator();
71 }
72 else {
73 return AlwaysValid<typename S::value_type>();
74 }
75}
76
77} // namespace cppfig
Type-erased validator that can hold any validation function.
Definition validator.h:33
Concept constraining types that can be used as configuration values.
Definition traits.h:48
Concept for settings with environment variable override.
Definition setting.h:43
Concept for settings with custom validator.
Definition setting.h:49
Concept for valid setting types.
Definition setting.h:35
C++20 compile-time type-safe configuration library.
Definition conf.h:13
auto GetSettingValidator() -> Validator< typename S::value_type >
Helper to get validator for a setting (always-valid if not defined).
Definition setting.h:67
constexpr auto GetEnvOverride() -> std::string_view
Helper to get environment override for a setting (empty if not defined).
Definition setting.h:55