From 2c8294b069f5df30d281e0f4f580f6f313a50eb2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 4 Dec 2017 12:39:33 +0200 Subject: Add cast_empty() for value casting --- build2/variable.cxx | 6 ++++++ build2/variable.hxx | 11 +++++++++++ build2/variable.ixx | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 5 deletions(-) (limited to 'build2') diff --git a/build2/variable.cxx b/build2/variable.cxx index ce88712..9ce9fe8 100644 --- a/build2/variable.cxx +++ b/build2/variable.cxx @@ -533,6 +533,8 @@ namespace build2 return s; } + const string& value_traits::empty_instance = empty_string; + const char* const value_traits::type_name = "string"; const value_type value_traits::value_type @@ -585,6 +587,8 @@ namespace build2 throw_invalid_argument (n, r, "path"); } + const path& value_traits::empty_instance = empty_path; + const char* const value_traits::type_name = "path"; const value_type value_traits::value_type @@ -630,6 +634,8 @@ namespace build2 throw_invalid_argument (n, r, "dir_path"); } + const dir_path& value_traits::empty_instance = empty_dir_path; + const char* const value_traits::type_name = "dir_path"; const value_type value_traits::value_type diff --git a/build2/variable.hxx b/build2/variable.hxx index c5e1a13..9a58584 100644 --- a/build2/variable.hxx +++ b/build2/variable.hxx @@ -289,6 +289,12 @@ namespace build2 template const T* cast_null (const value&); template const T* cast_null (const lookup&); + // As above but returns empty value if the value is NULL (or not defined, in + // case of lookup). + // + template const T& cast_empty (const value&); + template const T& cast_empty (const lookup&); + // As above but returns false/true if the value is NULL (or not defined, // in case of lookup). Note that the template argument is only for // documentation and should be bool (or semantically compatible). @@ -433,6 +439,8 @@ namespace build2 // // // static const bool empty_value = true; // + // static const T empty_instance; + // // // For simple types (those that can be used as elements of containers), // // type_name must be constexpr in order to sidestep the static init // // order issue (in fact, that's the only reason we have it both here @@ -579,6 +587,7 @@ namespace build2 static bool empty (const string& x) {return x.empty ();} static const bool empty_value = true; + static const string& empty_instance; static const char* const type_name; static const build2::value_type value_type; }; @@ -608,6 +617,7 @@ namespace build2 static bool empty (const path& x) {return x.empty ();} static const bool empty_value = true; + static const path& empty_instance; static const char* const type_name; static const build2::value_type value_type; }; @@ -628,6 +638,7 @@ namespace build2 static bool empty (const dir_path& x) {return x.empty ();} static const bool empty_value = true; + static const dir_path& empty_instance; static const char* const type_name; static const build2::value_type value_type; }; diff --git a/build2/variable.ixx b/build2/variable.ixx index 6126a4e..634e3dc 100644 --- a/build2/variable.ixx +++ b/build2/variable.ixx @@ -187,21 +187,46 @@ namespace build2 } template - inline T - cast_false (const value& v) {return v && cast (v);} + inline const T& + cast_empty (const value& v) + { + return v ? cast (v) : value_traits::empty_instance; + } + + template + inline const T& + cast_empty (const lookup& l) + { + return l ? cast (l) : value_traits::empty_instance; + } template inline T - cast_false (const lookup& l) {return l && cast (l);} + cast_false (const value& v) + { + return v && cast (v); + } template inline T - cast_true (const value& v) {return !v || cast (v);} + cast_false (const lookup& l) + { + return l && cast (l); + } template inline T - cast_true (const lookup& l) {return !l || cast (l);} + cast_true (const value& v) + { + return !v || cast (v); + } + template + inline T + cast_true (const lookup& l) + { + return !l || cast (l); + } template inline void -- cgit v1.1