diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2021-01-27 06:14:57 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2021-01-28 08:01:04 +0200 |
commit | f211934d6ff53388a806c5cc06483e9d843056e4 (patch) | |
tree | 5085f616fe013fac6274e760b8e6c4c893fab4c1 /libbuild2/variable.txx | |
parent | ac03ae75f46196c09cec0e3e45f401dc59faee73 (diff) |
Make std::map prepend (=+) overriding (like insert_or_assign())
Diffstat (limited to 'libbuild2/variable.txx')
-rw-r--r-- | libbuild2/variable.txx | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/libbuild2/variable.txx b/libbuild2/variable.txx index 9c9c867..e1808fc 100644 --- a/libbuild2/variable.txx +++ b/libbuild2/variable.txx @@ -838,6 +838,35 @@ namespace build2 template <typename K, typename V> void + map_prepend (value& v, names&& ns, const variable* var) + { + using std::map; + + map<K, V>& p (v + ? v.as<map<K, V>> () + : *new (&v.data_) map<K, V> ()); + + // Verify we have a sequence of pairs and convert each lhs/rhs to K/V. + // + for (auto i (ns.begin ()); i != ns.end (); ++i) + { + name& l (*i); + name* r (l.pair ? &*++i : nullptr); + + pair<K, V> v (value_traits<pair<K, V>>::convert ( + move (l), r, + value_traits<map<K, V>>::value_type.name, + "element", + var)); + + // Poor man's emplace_or_assign(). + // + p.emplace (move (v.first), V ()).first->second = move (v.second); + } + } + + template <typename K, typename V> + void map_assign (value& v, names&& ns, const variable* var) { using std::map; @@ -961,7 +990,7 @@ namespace build2 &default_copy_assign<map<K, V>>, &map_assign<K, V>, &map_append<K, V>, - &map_append<K, V>, // Prepend is the same as append. + &map_prepend<K, V>, &map_reverse<K, V>, nullptr, // No cast (cast data_ directly). &map_compare<K, V>, |