|
cppfig 0.1.0
Modern C++20 compile-time type-safe configuration library
|
A modern, header-only C++20 configuration library with compile-time type safety, zero macros, IDE autocompletion, and pluggable serialization.
config.Get<settings::ServerPort>().conf default, JSON opt-in, extensible to YAML/TOMLMultiThreadedPolicy with reader-writer locking (zero overhead by default)Enable with -DCPPFIG_ENABLE_JSON=ON or vcpkg feature "json". See Serializers for details.
bash cp -r cppfig/src/cppfig your_project/include/
cmake add_subdirectory(cppfig) target_link_libraries(your_target PRIVATE cppfig)
set(CPPFIG_ENABLE_JSON ON)
bash
json { "dependencies": [ { "name": "cppfig", "features": ["json"] } ] }
cpp // Thread-safe configuration: cppfig::Configuration<MySchema, cppfig::ConfSerializer, cppfig::MultiThreadedPolicy> config("config.conf");
cpp struct MySetting { // Required static constexpr std::string_view path = "path.to.setting"; using value_type = int; static auto default_value() -> int { return 42; }
// Optional static constexpr std::string_view env_override = "MY_SETTING"; static auto validator() -> cppfig::Validator<int> { return cppfig::Min(0); } };
cpp // Numeric cppfig::Min(0) cppfig::Max(100) cppfig::Range(1, 65535) cppfig::Positive<int>() cppfig::NonNegative<int>()
// String cppfig::NotEmpty() cppfig::MinLength(8) cppfig::MaxLength(255)
// Generic cppfig::OneOf<std::string>({"a", "b", "c"}) cppfig::Predicate<int>([](int x) { return x % 2 == 0; }, "must be even")
// Combine cppfig::Min(1).And(cppfig::Max(100))
cpp struct Point { int x, y; };
// Register with cppfig — works with any serializer template <> struct cppfig::ConfigTraits<Point> { static auto Serialize(const Point& p) -> cppfig::Value { auto obj = cppfig::Value::Object(); obj["x"] = cppfig::Value(p.x); obj["y"] = cppfig::Value(p.y); return obj; } static auto Deserialize(const cppfig::Value& v) -> std::optional<Point> { try { return Point{static_cast<int>(v["x"].Get<int64_t>()), static_cast<int>(v["y"].Get<int64_t>())}; } catch (...) { return std::nullopt; } } static auto ToString(const Point& p) -> std::string { return "(" + std::to_string(p.x) + "," + std::to_string(p.y) + ")"; } static auto FromString(std::string_view) -> std::optional<Point> { return std::nullopt; } };
// Use in settings struct Origin { static constexpr std::string_view path = "origin"; using value_type = Point; static auto default_value() -> Point { return {0, 0}; } };
cpp #include <cppfig/json.h>
template <> struct cppfig::ConfigTraits<Point> : cppfig::ConfigTraitsFromJsonAdl<Point> {};
cpp #include <cppfig/testing/mock.h>
TEST(MyTest, UsesConfiguration) { cppfig::testing::MockConfiguration<MySchema> config; config.SetValue<settings::ServerPort>(9000);
MyService service(config.Get<settings::ServerHost>(), config.Get<settings::ServerPort>());
EXPECT_EQ(service.GetPort(), 9000); }
cpp cppfig::Configuration<Schema, Serializer = ConfSerializer, ThreadPolicy = SingleThreadedPolicy>
// Methods auto Load() -> cppfig::Status; auto Save() const -> cppfig::Status; auto Get<Setting>() const -> typename Setting::value_type; auto Set<Setting>(value) -> cppfig::Status; auto Diff() const -> ConfigDiff; auto ValidateAll() const -> cppfig::Status; auto GetFilePath() const -> std::string_view;
// Thread policies cppfig::SingleThreadedPolicy // Zero-overhead (default) cppfig::MultiThreadedPolicy // std::shared_mutex reader-writer locking
// Serializers cppfig::ConfSerializer // Built-in flat .conf (default) cppfig::JsonSerializer // Requires CPPFIG_ENABLE_JSON
cpp cppfig::ConfigSchema<Settings...>
// Compile-time checks static constexpr bool has_setting; static constexpr std::size_t size;
// Runtime utilities static auto GetPaths() -> std::array<std::string_view, size>; static void ForEachSetting(auto&& fn);
bash
cmake –workflow –preset=debug-clang
cmake –workflow –preset=debug-gcc
cmake –workflow –preset=release-clang
ctest –test-dir build/debug/clang –output-on-failure
./build/debug/clang/examples/cppfig_example
./build/release/clang/benchmark/cppfig_benchmark
bash
cmake –workflow –preset=coverage
ninja -C build/coverage coverage
ninja -C build/coverage coverage-text
ninja -C build/coverage coverage-xml
cppfig/ ├── src/cppfig/ # Library headers │ ├── cppfig.h # Main include │ ├── configuration.h # Configuration class │ ├── schema.h # ConfigSchema template │ ├── setting.h # Setting concepts │ ├── traits.h # Type traits (Serialize/Deserialize) │ ├── value.h # Core Value type │ ├── validator.h # Validators │ ├── serializer.h # Serializer concept │ ├── conf.h # Built-in .conf serializer │ ├── json.h # Optional JSON serializer │ ├── interface.h # Mockable interfaces │ ├── diff.h # Configuration diff │ ├── logging.h # Logging utilities │ ├── thread_policy.h # Thread safety policies │ └── testing/mock.h # Testing helpers ├── examples/ # Example code ├── test/ # Unit & integration tests ├── benchmark/ # Performance benchmarks └── docs/ # Documentation ```
clang-format)clang-tidy)MIT License - see [LICENSE](LICENSE) for details.