From 0efae7db7b5870246f1e294a5fedaa69e9c90331 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 20 Feb 2024 15:40:02 +0200 Subject: Add json_map and json_set buildfile value types These expose the std::map and std::set types to buildfiles. New functions: $size() $size() $keys() Note that the $keys() function returns the list of map key as a json array. For example: m = [json_map] 2@([json] a@1 b@2) 1@([json] 1 2) s = [json_set] ([json] x@1 y@2) ([json] a@1 b@2) print ($m[2][b]) # 2 print ($s[([json] y@2 x@1)]) # true --- libbuild2/variable.ixx | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'libbuild2/variable.ixx') diff --git a/libbuild2/variable.ixx b/libbuild2/variable.ixx index a448cd8..ca84a33 100644 --- a/libbuild2/variable.ixx +++ b/libbuild2/variable.ixx @@ -362,13 +362,53 @@ namespace build2 // This one will be SFINAE'd out unless T is a container. // + // If T is both (e.g., json_value), then make this version preferable. + // template inline auto - convert (names&& ns) -> decltype (value_traits::convert (move (ns))) + convert_impl (names&& ns, int) + -> decltype (value_traits::convert (move (ns))) { return value_traits::convert (move (ns)); } + // This one will be SFINAE'd out unless T is a simple value. + // + // If T is both (e.g., json_value), then make this version less preferable. + // + template + auto // NOTE: not inline! + convert_impl (names&& ns, ...) -> + decltype (value_traits::convert (move (ns[0]), nullptr)) + { + size_t n (ns.size ()); + + if (n == 0) + { + if (value_traits::empty_value) + return T (); + } + else if (n == 1) + { + return convert (move (ns[0])); + } + else if (n == 2 && ns[0].pair != '\0') + { + return convert (move (ns[0]), move (ns[1])); + } + + throw invalid_argument ( + string ("invalid ") + value_traits::type_name + + (n == 0 ? " value: empty" : " value: multiple names")); + } + + template + inline T + convert (names&& ns) + { + return convert_impl (move (ns), 0); + } + // bool value // inline void value_traits:: -- cgit v1.1