diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-07-01 17:07:04 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-07-01 17:07:04 +0100 |
| commit | 2e2d1bd3b6c7776b77c33b94f30ead89367a71e6 (patch) | |
| tree | 54d37ffadf8e677938d9b7a28e4e9b71be1e75c1 /nihil.ucl/type.ccm | |
| parent | 36427c0966faa7aecd586b397ed9b845f18172f5 (diff) | |
| download | nihil-2e2d1bd3b6c7776b77c33b94f30ead89367a71e6.tar.gz nihil-2e2d1bd3b6c7776b77c33b94f30ead89367a71e6.tar.bz2 | |
add nihil.std
Diffstat (limited to 'nihil.ucl/type.ccm')
| -rw-r--r-- | nihil.ucl/type.ccm | 87 |
1 files changed, 61 insertions, 26 deletions
diff --git a/nihil.ucl/type.ccm b/nihil.ucl/type.ccm index f3b3aef..476546a 100644 --- a/nihil.ucl/type.ccm +++ b/nihil.ucl/type.ccm @@ -1,40 +1,61 @@ -/* - * This source code is released into the public domain. - */ - +// This source code is released into the public domain. module; -#include <concepts> -#include <format> -#include <stdexcept> -#include <string> - #include <ucl.h> export module nihil.ucl:type; +import nihil.std; import nihil.error; namespace nihil::ucl { // Our strongly-typed version of ::ucl_type. -export enum struct object_type { - object = UCL_OBJECT, - array = UCL_ARRAY, - integer = UCL_INT, - real = UCL_FLOAT, - string = UCL_STRING, - boolean = UCL_BOOLEAN, - time = UCL_TIME, - userdata = UCL_USERDATA, - null = UCL_NULL, +export enum struct object_type : std::uint8_t { + object = UCL_OBJECT, + array = UCL_ARRAY, + integer = UCL_INT, + real = UCL_FLOAT, + string = UCL_STRING, + boolean = UCL_BOOLEAN, + time = UCL_TIME, + userdata = UCL_USERDATA, + null = UCL_NULL, }; // Get the name of a type. -export auto str(object_type type) -> std::string_view; +export auto str(object_type type) -> std::string_view +{ + using namespace std::literals; + + switch (type) { + case object_type::object: + return "object"sv; + case object_type::array: + return "array"sv; + case object_type::integer: + return "integer"sv; + case object_type::real: + return "real"sv; + case object_type::string: + return "string"sv; + case object_type::boolean: + return "boolean"sv; + case object_type::time: + return "time"sv; + case object_type::userdata: + return "userdata"sv; + case object_type::null: + return "null"sv; + default: + // Don't fail here, since UCL might add more types that we + // don't know about. + return "unknown"sv; + } +} // Concept of a UCL data type. -export template<typename T> +export template <typename T> concept datatype = requires(T o) { { o.get_ucl_object() } -> std::convertible_to<::ucl_object_t const *>; { o.type() } -> std::same_as<object_type>; @@ -42,14 +63,28 @@ concept datatype = requires(T o) { }; // Exception thrown when a type assertion fails. -export struct type_mismatch : error { - type_mismatch(object_type expected_type, object_type actual_type); +export struct type_mismatch : error +{ + type_mismatch(object_type expected_type, object_type actual_type) + : error(std::format("expected type '{}' != actual type '{}'", + ucl::str(expected_type), ucl::str(actual_type))) + , m_expected_type(expected_type) + , m_actual_type(actual_type) + { + } // The type we expected. - auto expected_type(this type_mismatch const &self) -> object_type; + auto expected_type(this type_mismatch const &self) -> object_type + { + return self.m_expected_type; + } + // The type we got. - auto actual_type(this type_mismatch const &self) -> object_type; - + auto actual_type(this type_mismatch const &self) -> object_type + { + return self.m_actual_type; + } + private: object_type m_expected_type; object_type m_actual_type; |
