cppfig 0.1.0
Modern C++20 compile-time type-safe configuration library
Loading...
Searching...
No Matches
schema.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <string_view>
5#include <type_traits>
6
7#include "cppfig/setting.h"
8
9namespace cppfig {
10
11namespace detail {
12
14 template <typename T, typename... Types>
15 struct IsOneOf : std::disjunction<std::is_same<T, Types>...> { };
16
18 template <typename... Settings>
19 consteval auto AllPathsUnique() -> bool
20 {
21 constexpr std::array<std::string_view, sizeof...(Settings)> paths = { Settings::path... };
22 for (std::size_t i = 0; i < paths.size(); ++i) {
23 for (std::size_t j = i + 1; j < paths.size(); ++j) {
24 if (paths[i] == paths[j]) {
25 return false;
26 }
27 }
28 }
29 return true;
30 }
31
32} // namespace detail
33
58template <IsSetting... Settings>
60public:
61 static constexpr std::size_t size = sizeof...(Settings);
62
63 static_assert(detail::AllPathsUnique<Settings...>(), "All paths in ConfigSchema must be unique");
64
66 template <typename S>
67 static constexpr bool has_setting = detail::IsOneOf<S, Settings...>::value;
68
70 [[nodiscard]] static constexpr auto GetPaths() -> std::array<std::string_view, size>
71 {
72 return { Settings::path... };
73 }
74
76 [[nodiscard]] static constexpr auto Size() -> std::size_t { return size; }
77
82 template <typename Fn>
83 static void ForEachSetting(Fn&& fn)
84 {
85 (fn.template operator()<Settings>(), ...);
86 }
87};
88
90template <IsSetting S>
91using setting_value_type = typename S::value_type;
92
93} // namespace cppfig
Configuration schema holding all setting types.
Definition schema.h:59
static constexpr bool has_setting
Checks if a setting type is in this schema.
Definition schema.h:67
static constexpr std::size_t size
Definition schema.h:61
static constexpr auto Size() -> std::size_t
Returns the number of settings in the schema.
Definition schema.h:76
static void ForEachSetting(Fn &&fn)
Iterates over all setting types with a callable.
Definition schema.h:83
static constexpr auto GetPaths() -> std::array< std::string_view, size >
Returns all paths as a compile-time array.
Definition schema.h:70
consteval auto AllPathsUnique() -> bool
Helper to check if all paths are unique at compile time.
Definition schema.h:19
C++20 compile-time type-safe configuration library.
Definition conf.h:13
typename S::value_type setting_value_type
Helper alias to get the value type for a setting.
Definition schema.h:91
Helper to check if a type is in a parameter pack.
Definition schema.h:15