From b86c1d8d2e0be140f6854d869e07139ff3c4221c Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 24 Aug 2020 10:51:26 +0200 Subject: Add copying version of convert(value) --- libbuild2/variable.txx | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'libbuild2/variable.txx') diff --git a/libbuild2/variable.txx b/libbuild2/variable.txx index 51176ae..0e10e87 100644 --- a/libbuild2/variable.txx +++ b/libbuild2/variable.txx @@ -55,6 +55,9 @@ namespace build2 (n == 0 ? " value: empty" : " value: multiple names")); } + [[noreturn]] void + convert_throw (const value_type* from, const value_type& to); + template T convert (value&& v) @@ -67,19 +70,22 @@ namespace build2 return move (v).as (); } - string m ("invalid "); - m += value_traits::value_type.name; - m += " value: "; + convert_throw (v ? v.type : nullptr, value_traits::value_type); + } + template + T + convert (const value& v) + { if (v) { - m += "conversion from "; - m += v.type->name; + if (v.type == nullptr) + return convert (names (v.as ())); + else if (v.type == &value_traits::value_type) + return v.as (); } - else - m += "null"; - throw invalid_argument (move (m)); + convert_throw (v ? v.type : nullptr, value_traits::value_type); } template -- cgit v1.1