From 32b4443ba2ec5c3f7c09221ab9b21911a3126ef9 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Mon, 23 Jun 2025 18:34:18 +0100 Subject: add separate module implementation files --- nihil.config/CMakeLists.txt | 10 +- nihil.config/option.cc | 67 ++++++++++++ nihil.config/option.ccm | 50 ++------- nihil.config/store.cc | 89 ++++++++++++++++ nihil.config/store.ccm | 68 ++---------- nihil.config/string.cc | 57 ++++++++++ nihil.config/string.ccm | 42 ++------ nihil.ucl/CMakeLists.txt | 17 ++- nihil.ucl/boolean.cc | 77 ++++++++++++++ nihil.ucl/boolean.ccm | 60 ++--------- nihil.ucl/emit.cc | 21 ++++ nihil.ucl/emit.ccm | 15 +-- nihil.ucl/error.cc | 19 ++++ nihil.ucl/error.ccm | 5 +- nihil.ucl/integer.cc | 76 ++++++++++++++ nihil.ucl/integer.ccm | 65 ++---------- nihil.ucl/object.cc | 123 ++++++++++++++++++++++ nihil.ucl/object.ccm | 198 ++++------------------------------- nihil.ucl/parser.cc | 75 +++++++++++++ nihil.ucl/parser.ccm | 58 +++------- nihil.ucl/real.cc | 79 ++++++++++++++ nihil.ucl/real.ccm | 65 ++---------- nihil.ucl/string.cc | 139 ++++++++++++++++++++++++ nihil.ucl/string.ccm | 122 ++++----------------- nihil.ucl/type.cc | 62 +++++++++++ nihil.ucl/type.ccm | 48 +-------- nihil/CMakeLists.txt | 12 ++- nihil/exec.cc | 75 +++++++++++++ nihil/exec.ccm | 63 ++--------- nihil/fd.cc | 250 ++++++++++++++++++++++++++++++++++++++++++++ nihil/fd.ccm | 234 ++++++++--------------------------------- nihil/find_in_path.cc | 52 +++++++++ nihil/find_in_path.ccm | 49 ++------- nihil/getenv.cc | 44 ++++++++ nihil/getenv.ccm | 27 +---- nihil/open_file.cc | 28 +++++ nihil/open_file.ccm | 15 +-- nihil/process.cc | 111 ++++++++++++++++++++ nihil/process.ccm | 90 +++------------- 39 files changed, 1668 insertions(+), 1089 deletions(-) create mode 100644 nihil.config/option.cc create mode 100644 nihil.config/store.cc create mode 100644 nihil.config/string.cc create mode 100644 nihil.ucl/boolean.cc create mode 100644 nihil.ucl/emit.cc create mode 100644 nihil.ucl/error.cc create mode 100644 nihil.ucl/integer.cc create mode 100644 nihil.ucl/object.cc create mode 100644 nihil.ucl/parser.cc create mode 100644 nihil.ucl/real.cc create mode 100644 nihil.ucl/string.cc create mode 100644 nihil.ucl/type.cc create mode 100644 nihil/exec.cc create mode 100644 nihil/fd.cc create mode 100644 nihil/find_in_path.cc create mode 100644 nihil/getenv.cc create mode 100644 nihil/open_file.cc create mode 100644 nihil/process.cc diff --git a/nihil.config/CMakeLists.txt b/nihil.config/CMakeLists.txt index ed2bba3..6b1073d 100644 --- a/nihil.config/CMakeLists.txt +++ b/nihil.config/CMakeLists.txt @@ -2,9 +2,8 @@ add_library(nihil.config STATIC) target_link_libraries(nihil.config PUBLIC nihil nihil.ucl) -target_sources(nihil.config PUBLIC - FILE_SET modules TYPE CXX_MODULES FILES - +target_sources(nihil.config + PUBLIC FILE_SET modules TYPE CXX_MODULES FILES nihil.config.ccm error.ccm read.ccm @@ -13,6 +12,11 @@ target_sources(nihil.config PUBLIC option.ccm string.ccm + + PRIVATE + option.cc + store.cc + string.cc ) if(NIHIL_TESTS) diff --git a/nihil.config/option.cc b/nihil.config/option.cc new file mode 100644 index 0000000..9bf77c9 --- /dev/null +++ b/nihil.config/option.cc @@ -0,0 +1,67 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include +#include + +module nihil.config; + +namespace nihil::config { + +auto option::name(this option const &self) noexcept +-> std::string_view +{ + return self._name; +} + +// Human-readable description of this option. +auto option::description(this option const &self) noexcept +-> std::string_view +{ + return self._description; +} + +// If true, this option is set to its default value. +auto option::is_default(this option const &self) noexcept +-> bool +{ + return self._is_default; +} + +// Get or set this option as a string. +auto option::string(this option const &self) +-> std::string +{ + return self.get_string(); +} + +auto option::string(this option &self, std::string_view value) +-> void +{ + self.set_string(value); + self._is_default = false; +} + +option::option(std::string_view name, + std::string_view description) + : _name(name) + , _description(description) +{ +} + +auto option::is_default(bool b) +-> void +{ + _is_default = b; +} + +auto operator<<(std::ostream &strm, option const &opt) +-> std::ostream & +{ + return strm << "<" << opt.name() << "=" << opt.string() << ">"; +} + +} // namespace nihil diff --git a/nihil.config/option.ccm b/nihil.config/option.ccm index 1be542e..c6a8329 100644 --- a/nihil.config/option.ccm +++ b/nihil.config/option.ccm @@ -4,11 +4,9 @@ module; -#include +#include #include -#include - export module nihil.config:option; import nihil.ucl; @@ -23,34 +21,17 @@ namespace nihil::config { export struct option { // Short name of this option. - auto name(this option const &self) noexcept -> std::string_view - { - return self._name; - } + auto name(this option const &self) noexcept -> std::string_view; // Human-readable description of this option. - auto description(this option const &self) noexcept -> std::string_view - { - return self._description; - } + auto description(this option const &self) noexcept -> std::string_view; // If true, this option is set to its default value. - auto is_default(this option const &self) noexcept -> bool - { - return self._is_default; - } + auto is_default(this option const &self) noexcept -> bool; // Get or set this option as a string. - auto string(this option const &self) -> std::string - { - return self.get_string(); - } - - void string(this option &self, std::string_view value) - { - self.set_string(value); - self._is_default = false; - } + auto string(this option const &self) -> std::string; + auto string(this option &self, std::string_view value) -> void; /* * Return this object as a UCL object. This is used when writing the @@ -69,17 +50,9 @@ export struct option auto operator=(option const &) -> option& = delete; protected: - option(std::string_view name, - std::string_view description) - : _name(name) - , _description(description) - { - } - - auto is_default(bool b) -> void - { - _is_default = b; - } + option(std::string_view name, std::string_view description); + + auto is_default(bool b) -> void; /* * Get or set this option as a string. @@ -96,9 +69,6 @@ private: /* * Make options printable. This is mostly useful for testing. */ -export auto operator<<(std::ostream &strm, option const &opt) -> std::ostream & -{ - return strm << "<" << opt.name() << "=" << opt.string() << ">"; -} +export auto operator<<(std::ostream &strm, option const &opt) -> std::ostream &; } // namespace nihil diff --git a/nihil.config/store.cc b/nihil.config/store.cc new file mode 100644 index 0000000..2ec8ade --- /dev/null +++ b/nihil.config/store.cc @@ -0,0 +1,89 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include +#include +#include +#include + +module nihil.config; + +import nihil; + +namespace nihil::config { + +unknown_option::unknown_option(std::string_view option_name) + : error(std::format("unknown configuration variable '{}'", + option_name)) + , _option_name(option_name) +{ +} + +auto unknown_option::option_name(this unknown_option const &self) +-> std::string_view +{ + return self._option_name; +} + +auto store::get() +-> store& +{ + if (instance == nullptr) + instance = new store; + + return *instance; +} + + +auto store::register_option(this store &self, option *object) +-> void +{ + auto [it, okay] = self.options.insert( + std::pair{object->name(), object}); + + if (!okay) + throw error(std::format( + "INTERNAL ERROR: attempt to register " + "duplicate config value '{0}'", + object->name())); +} + +auto store::unregister_option(this store &self, option *object) +-> void +{ + auto it = self.options.find(object->name()); + if (it == self.options.end()) + throw error(std::format( + "INTERNAL ERROR: attempt to unregister " + "non-existent config value '{}'", + object->name())); + + self.options.erase(it); +} + +auto store::fetch(this store const &self, std::string_view name) +-> option & +{ + if (auto it = self.options.find(name); it != self.options.end()) + return *it->second; + + throw unknown_option(name); +} + +auto store::all(this store const &self) +-> nihil::generator