diff options
Diffstat (limited to 'libbuild2')
-rw-r--r-- | libbuild2/functions-string.cxx | 25 | ||||
-rw-r--r-- | libbuild2/parser.cxx | 10 | ||||
-rw-r--r-- | libbuild2/variable.cxx | 4 | ||||
-rw-r--r-- | libbuild2/variable.hxx | 17 | ||||
-rw-r--r-- | libbuild2/variable.txx | 151 |
5 files changed, 161 insertions, 46 deletions
diff --git a/libbuild2/functions-string.cxx b/libbuild2/functions-string.cxx index 8e5a315..b332efa 100644 --- a/libbuild2/functions-string.cxx +++ b/libbuild2/functions-string.cxx @@ -119,14 +119,16 @@ namespace build2 }; // $size(<strings>) + // $size(<string-map>) // $size(<string>) // - // First form: return the number of elements in the sequence. + // First two forms: return the number of elements in the sequence. // - // Second form: return the number of characters (bytes) in the string. + // Third form: return the number of characters (bytes) in the string. // - f["size"] += [] (strings v) {return v.size ();}; - f["size"] += [] (string v) {return v.size ();}; + f["size"] += [] (strings v) {return v.size ();}; + f["size"] += [] (map<string, string> v) {return v.size ();}; + f["size"] += [] (string v) {return v.size ();}; // $sort(<strings> [, <flags>]) // @@ -204,6 +206,21 @@ namespace build2 return find_index (vs, move (v), move (fs)); }; + // $keys(<string-map>) + // + // Return the list of keys in a string map. + // + // Note that the result is sorted in ascending order. + // + f["keys"] += [](map<string, string> v) + { + strings r; + r.reserve (v.size ()); + for (pair<const string, string>& p: v) + r.push_back (p.first); // @@ PERF: use C++17 map::extract() to steal. + return r; + }; + // String-specific overloads from builtins. // function_family b (m, "builtin"); diff --git a/libbuild2/parser.cxx b/libbuild2/parser.cxx index 6ccae8a..b68f009 100644 --- a/libbuild2/parser.cxx +++ b/libbuild2/parser.cxx @@ -6090,7 +6090,8 @@ namespace build2 if (n[4] == 's' && n[5] == '\0') return &value_traits<paths>::value_type; } - else if (n == "project_name") return &value_traits<project_name>::value_type; + else if (n == "project_name") + return &value_traits<project_name>::value_type; break; } case 's': @@ -6100,12 +6101,15 @@ namespace build2 if (n[6] == '\0') return &value_traits<string>::value_type; if (n[6] == 's' && n[7] == '\0') return &value_traits<strings>::value_type; + if (n == "string_map") + return &value_traits<map<string,string>>::value_type; } break; } case 't': { - if (n == "target_triplet") return &value_traits<target_triplet>::value_type; + if (n == "target_triplet") + return &value_traits<target_triplet>::value_type; break; } case 'u': @@ -8972,7 +8976,7 @@ namespace build2 result = &result_data; } - // See if we have another subscript. + // See if we have chained subscript. // enable_subscript (); tt = peek (); diff --git a/libbuild2/variable.cxx b/libbuild2/variable.cxx index 6cdf3ee..40eeccd 100644 --- a/libbuild2/variable.cxx +++ b/libbuild2/variable.cxx @@ -1994,7 +1994,7 @@ namespace build2 { // Seeing that we are reversing for consumption, it feels natural to // reverse JSON null to our [null] rather than empty. This, in - // particular, helps nested subscript. + // particular, helps chained subscript. // #if 0 case json_type::null: r = value (names {}); break; @@ -2085,7 +2085,7 @@ namespace build2 ? json_subscript_impl (val, val_data, i, n, index).first : value ()); - // Typify null values so that we get called for nested subscripts. + // Typify null values so that we get called for chained subscripts. // if (r.null) r.type = &value_traits<json_value>::value_type; diff --git a/libbuild2/variable.hxx b/libbuild2/variable.hxx index 6b0b30e..d67098e 100644 --- a/libbuild2/variable.hxx +++ b/libbuild2/variable.hxx @@ -111,11 +111,11 @@ namespace build2 // // Note: should normally be consistent with iterate. // - value (*const subscript) (const value& val, - value* val_data, - value&& subscript, - const location& sloc, - const location& bloc); + value (*/*const*/ subscript) (const value& val, + value* val_data, + value&& subscript, + const location& sloc, + const location& bloc); // Custom iteration function. It should invoked the specified function for // each element in order. If NULL, then the generic implementation is @@ -1181,8 +1181,11 @@ namespace build2 // // Either K or V can be optional<T> making the key or value optional. // - // Note that append/+= is non-overriding (like insert()) while prepend/=+ - // is (like insert_or_assign()). + // Note that append/+= is overriding (like insert_or_assign()) while + // prepend/=+ is not (like 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 representation (e.g., + // a@0 a@1). // template <typename K, typename V> struct map_value_type; 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 <typename T> - static names_view + names_view vector_reverse (const value& v, names& s, bool) { auto& vv (v.as<vector<T>> ()); @@ -604,7 +604,7 @@ namespace build2 } template <typename T> - static int + int vector_compare (const value& l, const value& r) { auto& lv (l.as<vector<T>> ()); @@ -637,6 +637,8 @@ namespace build2 vector_value_type (value_type&& v) : value_type (move (v)) { + // Note: vector<T> always has a convenience alias. + // type_name = value_traits<T>::type_name; type_name += 's'; name = type_name.c_str (); @@ -706,7 +708,7 @@ namespace build2 } template <typename K, typename V> - static names_view + names_view pair_vector_reverse (const value& v, names& s, bool) { auto& vv (v.as<vector<pair<K, V>>> ()); @@ -719,7 +721,7 @@ namespace build2 } template <typename K, typename V> - static int + int pair_vector_compare (const value& l, const value& r) { auto& lv (l.as<vector<pair<K, V>>> ()); @@ -754,10 +756,13 @@ namespace build2 pair_vector_value_type (value_type&& v) : value_type (move (v)) { - type_name = value_traits<K>::type_name; - type_name += '_'; + // vector<pair<K,V>> + // + type_name = "vector<pair<"; + type_name += value_traits<K>::type_name; + type_name += ','; type_name += value_traits<V>::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<K>::type_name; - type_name += "_optional_"; + // vector<pair<K,optional<V>>> + // + type_name = "vector<pair<"; + type_name += value_traits<K>::type_name; + type_name += ",optional<"; type_name += value_traits<V>::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<pair<optional<K>,V>> + // + type_name = "vector<pair<optional<"; type_name += value_traits<K>::type_name; - type_name += '_'; + type_name += ">,"; type_name += value_traits<V>::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 <typename K, typename V> - static names_view + names_view map_reverse (const value& v, names& s, bool) { auto& vm (v.as<map<K, V>> ()); @@ -902,7 +912,7 @@ namespace build2 } template <typename K, typename V> - static int + int map_compare (const value& l, const value& r) { auto& lm (l.as<map<K, V>> ()); @@ -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<uint64_t,...>. + // Secondly, even something like map<string,...> 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 <typename K, typename V> + 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<K> (move (sub)); + } + catch (const invalid_argument& e) + { + fail (sloc) << "invalid " << value_traits<map<K, V>>::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<map<K, V>> ()); + auto i (m.find (k)); + if (i != m.end ()) + { + // Steal the value if possible. + // + r = (&val == val_data + ? V (move (const_cast<V&> (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<V>::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<K>::type_name; - type_name += '_'; + // map<K,V> + // + type_name = "map<"; + type_name += value_traits<K>::type_name; + type_name += ','; type_name += value_traits<V>::type_name; - type_name += "_map"; + type_name += '>'; name = type_name.c_str (); + subscript = &map_subscript<K, V>; } }; @@ -956,11 +1023,15 @@ namespace build2 map_value_type (value_type&& v) : value_type (move (v)) { - type_name = value_traits<K>::type_name; - type_name += "_optional_"; + // map<K,optional<V>> + // + type_name = "map<"; + type_name += value_traits<K>::type_name; + type_name += ",optional<"; type_name += value_traits<V>::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<optional<K>,V> + // + type_name = "map<optional<"; type_name += value_traits<K>::type_name; - type_name += '_'; + type_name += ">,"; type_name += value_traits<V>::type_name; - type_name += "_map"; + type_name += '>'; name = type_name.c_str (); + // @@ TODO: subscript + } + }; + + // Convenience aliases for certain map<T,T> cases. + // + template <> + struct map_value_type<string, string>: value_type + { + map_value_type (value_type&& v) + : value_type (move (v)) + { + name = "string_map"; + subscript = &map_subscript<string, string>; } }; template <typename K, typename V> const map<K, V> value_traits<map<K, V>>::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 <typename K, typename V> const map_value_type<K, V> value_traits<map<K, V>>::value_type = build2::value_type // VC14 wants = @@ -992,7 +1083,7 @@ namespace build2 sizeof (map<K, V>), nullptr, // No base. true, // Container. - nullptr, // No element (not named). + nullptr, // No element (pair<> not a value type yet). &default_dtor<map<K, V>>, &default_copy_ctor<map<K, V>>, &default_copy_assign<map<K, V>>, @@ -1003,7 +1094,7 @@ namespace build2 nullptr, // No cast (cast data_ directly). &map_compare<K, V>, &default_empty<map<K, V>>, - nullptr, // Subscript. + nullptr, // Subscript (patched in by map_value_type above). nullptr // Iterate. }; |