From f211934d6ff53388a806c5cc06483e9d843056e4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 27 Jan 2021 06:14:57 +0200 Subject: Make std::map prepend (=+) overriding (like insert_or_assign()) --- libbuild2/variable.ixx | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'libbuild2/variable.ixx') diff --git a/libbuild2/variable.ixx b/libbuild2/variable.ixx index 9b24b9d..fd78842 100644 --- a/libbuild2/variable.ixx +++ b/libbuild2/variable.ixx @@ -114,6 +114,22 @@ namespace build2 return *this; } + template + inline value& value:: + prepend (T v) + { + assert (type == &value_traits::value_type || (type == nullptr && null)); + + // Prepare the receiving value. + // + if (type == nullptr) + type = &value_traits::value_type; + + value_traits::prepend (*this, move (v)); + null = false; + return *this; + } + inline value& value:: operator= (names v) { @@ -867,6 +883,26 @@ namespace build2 new (&v.data_) map (move (x)); } + template + inline void value_traits>:: + prepend (value& v, map&& x) + { + if (v) + { + map& m (v.as> ()); + + m.swap (x); + + // Note that this will only move values. Keys (being const) are still + // copied. + // + m.insert (make_move_iterator (x.begin ()), + make_move_iterator (x.end ())); + } + else + new (&v.data_) map (move (x)); + } + // variable_pool // inline const variable& variable_pool:: -- cgit v1.1