From bed6b6a9170253e010cbffd59202add4edfd1c2b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 19 Feb 2024 10:34:40 +0200 Subject: Add string_map buildfile value type This exposes the std::map type to buildfiles. New functions: $size() $keys() Subscript can be used to lookup a value by key. The result is [null] if there is no value associated with the specified key. For example: map = [string_map] a@1 b@2 c@3 b = ($map[b]) # 2 if ($map[z] == [null]) ... Note that append (+=) is overriding (like std::map::insert_or_assign()) while prepend (=+) is not (like std::map::insert()). In a sense, whatever appears last (from left to right) is kept, which is consistent with what we expect to happen when specifying the same key repeatedly in a literal representation. For example: map = [string_map] a@0 b@2 a@1 # a@1 b@2 map += b@0 c@3 # a@1 b@0 c@3 map =+ b@1 d@4 # a@1 b@0 c@3 d@4 Example of iteration: map = [string_map] a@1 b@2 c@3 for p: $map { k = $first($p) v = $second($p) } While the subscript is mapped to key lookup only, index-based access can be implemented (with a bit of overhead) using the $keys() function: map = [string_map] a@1 b@2 c@3 keys = $keys($m) for i: $integer_sequence(0, $size($keys)) { k = ($keys[$i]) v = ($map[$k]) } Also, this commit changes the naming of other template-based value types (not exposed as buildfile value types) to use C++ template id-like names (e.g., map>). --- libbuild2/variable.txx | 151 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 121 insertions(+), 30 deletions(-) (limited to 'libbuild2/variable.txx') diff --git a/libbuild2/variable.txx b/libbuild2/variable.txx index bc4132f..485f9dc 100644 --- a/libbuild2/variable.txx +++ b/libbuild2/variable.txx @@ -591,7 +591,7 @@ namespace build2 } template - static names_view + names_view vector_reverse (const value& v, names& s, bool) { auto& vv (v.as> ()); @@ -604,7 +604,7 @@ namespace build2 } template - static int + int vector_compare (const value& l, const value& r) { auto& lv (l.as> ()); @@ -637,6 +637,8 @@ namespace build2 vector_value_type (value_type&& v) : value_type (move (v)) { + // Note: vector always has a convenience alias. + // type_name = value_traits::type_name; type_name += 's'; name = type_name.c_str (); @@ -706,7 +708,7 @@ namespace build2 } template - static names_view + names_view pair_vector_reverse (const value& v, names& s, bool) { auto& vv (v.as>> ()); @@ -719,7 +721,7 @@ namespace build2 } template - static int + int pair_vector_compare (const value& l, const value& r) { auto& lv (l.as>> ()); @@ -754,10 +756,13 @@ namespace build2 pair_vector_value_type (value_type&& v) : value_type (move (v)) { - type_name = value_traits::type_name; - type_name += '_'; + // vector> + // + type_name = "vector::type_name; + type_name += ','; type_name += value_traits::type_name; - type_name += "_pair_vector"; + type_name += ">>"; name = type_name.c_str (); } }; @@ -773,10 +778,13 @@ namespace build2 pair_vector_value_type (value_type&& v) : value_type (move (v)) { - type_name = value_traits::type_name; - type_name += "_optional_"; + // vector>> + // + type_name = "vector::type_name; + type_name += ",optional<"; type_name += value_traits::type_name; - type_name += "_pair_vector"; + type_name += ">>>"; name = type_name.c_str (); } }; @@ -789,11 +797,13 @@ namespace build2 pair_vector_value_type (value_type&& v) : value_type (move (v)) { - type_name = "optional_"; + // vector,V>> + // + type_name = "vector::type_name; - type_name += '_'; + type_name += ">,"; type_name += value_traits::type_name; - type_name += "_pair_vector"; + type_name += ">>"; name = type_name.c_str (); } }; @@ -847,7 +857,9 @@ namespace build2 "element", var)); - p.emplace (move (v.first), move (v.second)); + // Poor man's emplace_or_assign(). + // + p.emplace (move (v.first), V ()).first->second = move (v.second); } } @@ -872,9 +884,7 @@ namespace build2 "element", var)); - // Poor man's emplace_or_assign(). - // - p.emplace (move (v.first), V ()).first->second = move (v.second); + p.emplace (move (v.first), move (v.second)); } } @@ -889,7 +899,7 @@ namespace build2 } template - static names_view + names_view map_reverse (const value& v, names& s, bool) { auto& vm (v.as> ()); @@ -902,7 +912,7 @@ namespace build2 } template - static int + int map_compare (const value& l, const value& r) { auto& lm (l.as> ()); @@ -926,6 +936,59 @@ namespace build2 return 0; } + // Note that unlike json_value, we don't provide index support for maps. + // There are two reasons for this: Firstly, consider map. + // Secondly, even something like map may contain integers as + // keys (in JSON, there is a strong convention for object member names not + // to be integers). Instead, we provide the $keys() function which allows + // one to implement an index-based access with a bit of overhead, if needed. + // + template + value + map_subscript (const value& val, value* val_data, + value&& sub, + const location& sloc, + const location& bloc) + { + // Process subscript even if the value is null to make sure it is valid. + // + K k; + try + { + k = convert (move (sub)); + } + catch (const invalid_argument& e) + { + fail (sloc) << "invalid " << value_traits>::value_type.name + << " value subscript: " << e << + info (bloc) << "use the '\\[' escape sequence if this is a " + << "wildcard pattern"; + } + + value r; + if (!val.null) + { + const auto& m (val.as> ()); + auto i (m.find (k)); + if (i != m.end ()) + { + // Steal the value if possible. + // + r = (&val == val_data + ? V (move (const_cast (i->second))) + : V (i->second)); + } + } + + // Typify null values so that type-specific subscript (e.g., for + // json_value) gets called for chained subscripts. + // + if (r.null) + r.type = &value_traits::value_type; + + return r; + } + // Make sure these are static-initialized together. Failed that VC will make // sure it's done in the wrong order. // @@ -937,11 +1000,15 @@ namespace build2 map_value_type (value_type&& v) : value_type (move (v)) { - type_name = value_traits::type_name; - type_name += '_'; + // map + // + type_name = "map<"; + type_name += value_traits::type_name; + type_name += ','; type_name += value_traits::type_name; - type_name += "_map"; + type_name += '>'; name = type_name.c_str (); + subscript = &map_subscript; } }; @@ -956,11 +1023,15 @@ namespace build2 map_value_type (value_type&& v) : value_type (move (v)) { - type_name = value_traits::type_name; - type_name += "_optional_"; + // map> + // + type_name = "map<"; + type_name += value_traits::type_name; + type_name += ",optional<"; type_name += value_traits::type_name; - type_name += "_map"; + type_name += ">>"; name = type_name.c_str (); + // @@ TODO: subscript } }; @@ -972,18 +1043,38 @@ namespace build2 map_value_type (value_type&& v) : value_type (move (v)) { - type_name = "optional_"; + // map,V> + // + type_name = "map::type_name; - type_name += '_'; + type_name += ">,"; type_name += value_traits::type_name; - type_name += "_map"; + type_name += '>'; name = type_name.c_str (); + // @@ TODO: subscript + } + }; + + // Convenience aliases for certain map cases. + // + template <> + struct map_value_type: value_type + { + map_value_type (value_type&& v) + : value_type (move (v)) + { + name = "string_map"; + subscript = &map_subscript; } }; template const map value_traits>::empty_instance; + // Note that custom iteration would be better (more efficient, return typed + // value), but we don't yet have pair<> as value type so we let the generic + // implementation return an untyped pair. + // template const map_value_type value_traits>::value_type = build2::value_type // VC14 wants = @@ -992,7 +1083,7 @@ namespace build2 sizeof (map), nullptr, // No base. true, // Container. - nullptr, // No element (not named). + nullptr, // No element (pair<> not a value type yet). &default_dtor>, &default_copy_ctor>, &default_copy_assign>, @@ -1003,7 +1094,7 @@ namespace build2 nullptr, // No cast (cast data_ directly). &map_compare, &default_empty>, - nullptr, // Subscript. + nullptr, // Subscript (patched in by map_value_type above). nullptr // Iterate. }; -- cgit v1.1