From b9d0839c6e9bc586e2862ff9457ac582354b8347 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 27 Apr 2020 09:46:34 +0200 Subject: Require explicit variable type in scope::{assign,append}() --- libbuild2/scope.hxx | 65 +++++++++++++++++++++++++++------------------- libbuild2/version/init.cxx | 9 ++++--- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/libbuild2/scope.hxx b/libbuild2/scope.hxx index 39be143..e1cdf78 100644 --- a/libbuild2/scope.hxx +++ b/libbuild2/scope.hxx @@ -187,6 +187,22 @@ namespace build2 value& assign (const variable* var) {return vars.assign (var);} // For cached. + template + T& + assign (const variable& var, T&& val) + { + value& v (assign (var) = forward (val)); + return v.as (); + } + + template + T& + assign (const variable* var, T&& val) + { + value& v (assign (var) = forward (val)); + return v.as (); + } + // Assign an untyped non-overridable variable with project visibility. // value& @@ -195,47 +211,44 @@ namespace build2 return assign (var_pool ().insert (move (name))); } - // As above, but assign a typed variable. + // As above, but assign a typed variable (note: variable type must be + // specified explicitly). // - template + template value& assign (string name) { - return vars.assign (var_pool ().insert (move (name))); + return vars.assign (var_pool ().insert (move (name))); } - template - T& + template + V& assign (string name, T&& val) { - value& v (assign (move (name)) = forward (val)); - return v.as (); + value& v (assign (move (name)) = forward (val)); + return v.as (); } - template - T& - assign (const variable& var, T&& val) - { - value& v (assign (var) = forward (val)); - return v.as (); - } + // Return a value suitable for appending. If the variable does not exist + // in this scope's map, then outer scopes are searched for the same + // variable. If found then a new variable with the found value is added to + // this scope and returned. Otherwise this function proceeds as assign(). + // + value& + append (const variable&); - template - T& - assign (const variable* var, T&& val) + value& + append (string name) { - value& v (assign (var) = forward (val)); - return v.as (); + return append (var_pool ().insert (move (name))); } - // Return a value suitable for appending. If the variable does not - // exist in this scope's map, then outer scopes are searched for - // the same variable. If found then a new variable with the found - // value is added to this scope and returned. Otherwise this - // function proceeds as assign(). - // + template value& - append (const variable&); + append (string name) + { + return append (var_pool ().insert (move (name))); + } // Target type/pattern-specific variables. // diff --git a/libbuild2/version/init.cxx b/libbuild2/version/init.cxx index d30dc24..7c5d589 100644 --- a/libbuild2/version/init.cxx +++ b/libbuild2/version/init.cxx @@ -226,13 +226,14 @@ namespace build2 // Note also that we have "gifted" the config.version variable name to // the config module. // - auto set = [&rs] (auto var, auto val) + auto set = [&rs] (const char* var, auto val) { - rs.assign (var, move (val)); + using T = decltype (val); + rs.assign (var, move (val)); }; - if (!sum.empty ()) set (ctx.var_project_summary, move (sum)); - if (!url.empty ()) set (ctx.var_project_url, move (url)); + if (!sum.empty ()) rs.assign (ctx.var_project_summary, move (sum)); + if (!url.empty ()) rs.assign (ctx.var_project_url, move (url)); set ("version", v.string ()); // Project version (var_version). -- cgit v1.1