From f396f57bd65fc8d490af9705f94ae249471b0951 Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 18 Feb 2025 11:41:26 -0600 Subject: [PATCH 1/3] Generic supported concepts --- include/glaze/beve/read.hpp | 18 +++++--- include/glaze/beve/write.hpp | 21 ++++++--- include/glaze/core/opts.hpp | 80 +---------------------------------- include/glaze/csv/read.hpp | 9 ++-- include/glaze/csv/write.hpp | 9 ++-- include/glaze/json/json_t.hpp | 6 ++- include/glaze/json/ndjson.hpp | 18 +++++--- include/glaze/json/read.hpp | 18 +++++--- include/glaze/json/write.hpp | 24 +++++++---- include/glaze/toml/write.hpp | 12 ++++-- tests/beve_test/beve_test.cpp | 4 +- tests/json_test/json_test.cpp | 4 +- 12 files changed, 96 insertions(+), 127 deletions(-) diff --git a/include/glaze/beve/read.hpp b/include/glaze/beve/read.hpp index dc7d6c5cd0..7db98d5dab 100644 --- a/include/glaze/beve/read.hpp +++ b/include/glaze/beve/read.hpp @@ -1546,13 +1546,15 @@ namespace glz }; } - template + template + requires (read_supported) [[nodiscard]] inline error_ctx read_beve(T&& value, Buffer&& buffer) { return read(value, std::forward(buffer)); } - template + template + requires (read_supported) [[nodiscard]] inline expected read_beve(Buffer&& buffer) { T value{}; @@ -1563,7 +1565,8 @@ namespace glz return value; } - template + template + requires (read_supported) [[nodiscard]] inline error_ctx read_file_beve(T& value, const sv file_name, auto&& buffer) { context ctx{}; @@ -1578,14 +1581,16 @@ namespace glz return read()>(value, buffer, ctx); } - template + template + requires (read_supported) [[nodiscard]] inline error_ctx read_binary_untagged(T&& value, Buffer&& buffer) { return read(std::forward(value), std::forward(buffer)); } - template + template + requires (read_supported) [[nodiscard]] inline expected read_binary_untagged(Buffer&& buffer) { T value{}; @@ -1596,7 +1601,8 @@ namespace glz return value; } - template + template + requires (read_supported) [[nodiscard]] inline error_ctx read_file_beve_untagged(T& value, const std::string& file_name, auto&& buffer) { return read_file_beve>(value, file_name, buffer); diff --git a/include/glaze/beve/write.hpp b/include/glaze/beve/write.hpp index 666f4780ab..8c4f6b2370 100644 --- a/include/glaze/beve/write.hpp +++ b/include/glaze/beve/write.hpp @@ -910,26 +910,30 @@ namespace glz }; } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_beve(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] glz::expected write_beve(T&& value) { return write()>(std::forward(value)); } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_beve(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } // requires file_name to be null terminated - template + template + requires (write_supported) [[nodiscard]] error_ctx write_file_beve(T&& value, const sv file_name, auto&& buffer) { static_assert(sizeof(decltype(*buffer.data())) == 1); @@ -951,20 +955,23 @@ namespace glz return {}; } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_beve_untagged(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_beve_untagged(T&& value) { return write(std::forward(value)); } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_file_beve_untagged(T&& value, const std::string& file_name, auto&& buffer) { return write_file_beve>(std::forward(value), file_name, buffer); diff --git a/include/glaze/core/opts.hpp b/include/glaze/core/opts.hpp index 22e85e75e7..2303387cc8 100644 --- a/include/glaze/core/opts.hpp +++ b/include/glaze/core/opts.hpp @@ -341,85 +341,9 @@ namespace glz struct skip_value; } - template - concept write_beve_supported = requires { detail::to>{}; }; - - template - concept read_beve_supported = requires { detail::from>{}; }; - - template - concept write_json_supported = requires { detail::to>{}; }; - - template - concept read_json_supported = requires { detail::from>{}; }; - - template - concept write_ndjson_supported = requires { detail::to>{}; }; - - template - concept read_ndjson_supported = requires { detail::from>{}; }; - - template - concept write_toml_supported = requires { detail::to>{}; }; - - template - concept read_toml_supported = requires { detail::from>{}; }; - - template - concept write_csv_supported = requires { detail::to>{}; }; - - template - concept read_csv_supported = requires { detail::from>{}; }; - - template - consteval bool write_format_supported() - { - if constexpr (Format == BEVE) { - return write_beve_supported; - } - else if constexpr (Format == JSON) { - return write_json_supported; - } - else if constexpr (Format == NDJSON) { - return write_ndjson_supported; - } - else if constexpr (Format == TOML) { - return write_toml_supported; - } - else if constexpr (Format == CSV) { - return write_csv_supported; - } - else { - static_assert(false_v, "Glaze metadata is probably needed for your type"); - } - } - - template - consteval bool read_format_supported() - { - if constexpr (Format == BEVE) { - return read_beve_supported; - } - else if constexpr (Format == JSON) { - return read_json_supported; - } - else if constexpr (Format == NDJSON) { - return read_ndjson_supported; - } - else if constexpr (Format == TOML) { - return read_toml_supported; - } - else if constexpr (Format == CSV) { - return read_csv_supported; - } - else { - static_assert(false_v, "Glaze metadata is probably needed for your type"); - } - } - template - concept write_supported = write_format_supported(); + concept write_supported = requires { detail::to>{}; }; template - concept read_supported = read_format_supported(); + concept read_supported = requires { detail::from>{}; }; } diff --git a/include/glaze/csv/read.hpp b/include/glaze/csv/read.hpp index ceb175511a..0bc4f6123c 100644 --- a/include/glaze/csv/read.hpp +++ b/include/glaze/csv/read.hpp @@ -649,13 +649,15 @@ namespace glz }; } - template + template + requires (read_supported) [[nodiscard]] inline auto read_csv(T&& value, Buffer&& buffer) { return read(value, std::forward(buffer)); } - template + template + requires (read_supported) [[nodiscard]] inline auto read_csv(Buffer&& buffer) { T value{}; @@ -663,7 +665,8 @@ namespace glz return value; } - template + template + requires (read_supported) [[nodiscard]] inline error_ctx read_file_csv(T& value, const sv file_name, Buffer&& buffer) { context ctx{}; diff --git a/include/glaze/csv/write.hpp b/include/glaze/csv/write.hpp index 2dc8850393..badbf7b339 100644 --- a/include/glaze/csv/write.hpp +++ b/include/glaze/csv/write.hpp @@ -331,19 +331,22 @@ namespace glz }; } - template + template + requires (write_supported) [[nodiscard]] auto write_csv(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] expected write_csv(T&& value) { return write(std::forward(value)); } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_file_csv(T&& value, const std::string& file_name, auto&& buffer) { const auto ec = write(std::forward(value), buffer); diff --git a/include/glaze/json/json_t.hpp b/include/glaze/json/json_t.hpp index 21e8a87465..8df845af52 100644 --- a/include/glaze/json/json_t.hpp +++ b/include/glaze/json/json_t.hpp @@ -348,7 +348,8 @@ namespace glz } } - template + template + requires (read_supported) [[nodiscard]] error_ctx read_json(T& value, const json_t& source) { auto buffer = source.dump(); @@ -360,7 +361,8 @@ namespace glz } } - template + template + requires (read_supported) [[nodiscard]] expected read_json(const json_t& source) { auto buffer = source.dump(); diff --git a/include/glaze/json/ndjson.hpp b/include/glaze/json/ndjson.hpp index 28f648c611..1cc08bef9b 100644 --- a/include/glaze/json/ndjson.hpp +++ b/include/glaze/json/ndjson.hpp @@ -257,14 +257,16 @@ namespace glz }; } // namespace detail - template + template + requires (read_supported) [[nodiscard]] auto read_ndjson(T& value, Buffer&& buffer) { context ctx{}; return read(value, std::forward(buffer), ctx); } - template + template + requires (read_supported) [[nodiscard]] expected read_ndjson(Buffer&& buffer) { T value{}; @@ -276,7 +278,8 @@ namespace glz return unexpected(ec); } - template + template + requires (read_supported) [[nodiscard]] error_ctx read_file_ndjson(T& value, const sv file_name) { context ctx{}; @@ -293,19 +296,22 @@ namespace glz return read(value, buffer, ctx); } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_ndjson(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] expected write_ndjson(T&& value) { return write(std::forward(value)); } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_file_ndjson(T&& value, const std::string& file_name, auto&& buffer) { write(std::forward(value), buffer); diff --git a/include/glaze/json/read.hpp b/include/glaze/json/read.hpp index cf83312d94..539f3491fa 100644 --- a/include/glaze/json/read.hpp +++ b/include/glaze/json/read.hpp @@ -3245,14 +3245,16 @@ namespace glz skip_value, std::forward(buffer), ctx); } - template + template + requires (read_supported) [[nodiscard]] error_ctx read_json(T& value, Buffer&& buffer) { context ctx{}; return read(value, std::forward(buffer), ctx); } - template + template + requires (read_supported) [[nodiscard]] expected read_json(Buffer&& buffer) { T value{}; @@ -3264,14 +3266,16 @@ namespace glz return value; } - template + template + requires (read_supported) [[nodiscard]] error_ctx read_jsonc(T& value, Buffer&& buffer) { context ctx{}; return read(value, std::forward(buffer), ctx); } - template + template + requires (read_supported) [[nodiscard]] expected read_jsonc(Buffer&& buffer) { T value{}; @@ -3283,7 +3287,8 @@ namespace glz return value; } - template + template + requires (read_supported) [[nodiscard]] error_ctx read_file_json(T& value, const sv file_name, Buffer&& buffer) { context ctx{}; @@ -3298,7 +3303,8 @@ namespace glz return read()>(value, buffer, ctx); } - template + template + requires (read_supported) [[nodiscard]] error_ctx read_file_jsonc(T& value, const sv file_name, Buffer&& buffer) { context ctx{}; diff --git a/include/glaze/json/write.hpp b/include/glaze/json/write.hpp index 475ff640a4..e5b9d718c6 100644 --- a/include/glaze/json/write.hpp +++ b/include/glaze/json/write.hpp @@ -1965,49 +1965,57 @@ namespace glz }; } // namespace detail - template + template + requires (write_supported) [[nodiscard]] error_ctx write_json(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] glz::expected write_json(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] glz::expected write_json(T&& value) { return write(std::forward(value)); } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_json(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] glz::expected write_json(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_jsonc(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] glz::expected write_jsonc(T&& value) { return write(std::forward(value)); } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_file_json(T&& value, const sv file_name, auto&& buffer) { const auto ec = write()>(std::forward(value), buffer); diff --git a/include/glaze/toml/write.hpp b/include/glaze/toml/write.hpp index 3a876ec729..fbebae82e2 100644 --- a/include/glaze/toml/write.hpp +++ b/include/glaze/toml/write.hpp @@ -555,25 +555,29 @@ namespace glz }; } // namespace detail - template + template + requires (write_supported) [[nodiscard]] error_ctx write_toml(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] glz::expected write_toml(T&& value, Buffer&& buffer) { return write(std::forward(value), std::forward(buffer)); } - template + template + requires (write_supported) [[nodiscard]] glz::expected write_toml(T&& value) { return write(std::forward(value)); } - template + template + requires (write_supported) [[nodiscard]] error_ctx write_file_toml(T&& value, const sv file_name, auto&& buffer) { const auto ec = write()>(std::forward(value), buffer); diff --git a/tests/beve_test/beve_test.cpp b/tests/beve_test/beve_test.cpp index 8f35ba0fd1..784e5b1bdb 100644 --- a/tests/beve_test/beve_test.cpp +++ b/tests/beve_test/beve_test.cpp @@ -46,8 +46,8 @@ struct glz::meta ); }; -static_assert(glz::write_beve_supported); -static_assert(glz::read_beve_supported); +static_assert(glz::write_supported); +static_assert(glz::read_supported); struct sub_thing { diff --git a/tests/json_test/json_test.cpp b/tests/json_test/json_test.cpp index 7e5c5ee9f8..842c40558a 100644 --- a/tests/json_test/json_test.cpp +++ b/tests/json_test/json_test.cpp @@ -61,8 +61,8 @@ struct glz::meta ); }; -static_assert(glz::write_json_supported); -static_assert(glz::read_json_supported); +static_assert(glz::write_supported); +static_assert(glz::read_supported); suite starter = [] { "example"_test = [] { From 95cc63a7dd4848996cf0ecab107773104cbabaa5 Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Tue, 18 Feb 2025 11:58:22 -0600 Subject: [PATCH 2/3] Feature test for generic supported concepts --- include/glaze/core/feature_test.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/glaze/core/feature_test.hpp b/include/glaze/core/feature_test.hpp index 0b39373d69..ce1a678c1c 100644 --- a/include/glaze/core/feature_test.hpp +++ b/include/glaze/core/feature_test.hpp @@ -5,6 +5,10 @@ // Glaze Feature Test Macros +// v5.0.0 moves to more generic read_supported and write_supported concepts +// removes concepts like `read_json_supported` and uses `read_supported` +#define glaze_v5_0_0_generic_supported + // v4.3.0 removed global glz::trace #define glaze_v4_3_0_trace From 08b60604cf06a55d24c4189c43db60a640839343 Mon Sep 17 00:00:00 2001 From: Stephen Berry Date: Wed, 19 Feb 2025 08:37:51 -0600 Subject: [PATCH 3/3] Comment on built in format range --- include/glaze/core/opts.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/glaze/core/opts.hpp b/include/glaze/core/opts.hpp index 2303387cc8..82d52e31b4 100644 --- a/include/glaze/core/opts.hpp +++ b/include/glaze/core/opts.hpp @@ -10,6 +10,8 @@ namespace glz { // format + // Built in formats must be less than 65536 + // User defined formats can be 65536 to 4294967296 inline constexpr uint32_t INVALID = 0; inline constexpr uint32_t BEVE = 1; inline constexpr uint32_t JSON = 10;