diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2017-12-04 12:39:33 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2017-12-04 12:39:33 +0200 |
commit | 2c8294b069f5df30d281e0f4f580f6f313a50eb2 (patch) | |
tree | fe398543c4fa8565039651643fa2f51bbbe9999c | |
parent | 6d58efb0a5ca051bb205812edd130573bf3c39ce (diff) |
Add cast_empty() for value casting
-rw-r--r-- | build2/variable.cxx | 6 | ||||
-rw-r--r-- | build2/variable.hxx | 11 | ||||
-rw-r--r-- | build2/variable.ixx | 35 |
3 files changed, 47 insertions, 5 deletions
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<string>::empty_instance = empty_string; + const char* const value_traits<string>::type_name = "string"; const value_type value_traits<string>::value_type @@ -585,6 +587,8 @@ namespace build2 throw_invalid_argument (n, r, "path"); } + const path& value_traits<path>::empty_instance = empty_path; + const char* const value_traits<path>::type_name = "path"; const value_type value_traits<path>::value_type @@ -630,6 +634,8 @@ namespace build2 throw_invalid_argument (n, r, "dir_path"); } + const dir_path& value_traits<dir_path>::empty_instance = empty_dir_path; + const char* const value_traits<dir_path>::type_name = "dir_path"; const value_type value_traits<dir_path>::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 <typename T> const T* cast_null (const value&); template <typename T> 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 <typename T> const T& cast_empty (const value&); + template <typename T> 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 <typename T> - inline T - cast_false (const value& v) {return v && cast<T> (v);} + inline const T& + cast_empty (const value& v) + { + return v ? cast<T> (v) : value_traits<T>::empty_instance; + } + + template <typename T> + inline const T& + cast_empty (const lookup& l) + { + return l ? cast<T> (l) : value_traits<T>::empty_instance; + } template <typename T> inline T - cast_false (const lookup& l) {return l && cast<T> (l);} + cast_false (const value& v) + { + return v && cast<T> (v); + } template <typename T> inline T - cast_true (const value& v) {return !v || cast<T> (v);} + cast_false (const lookup& l) + { + return l && cast<T> (l); + } template <typename T> inline T - cast_true (const lookup& l) {return !l || cast<T> (l);} + cast_true (const value& v) + { + return !v || cast<T> (v); + } + template <typename T> + inline T + cast_true (const lookup& l) + { + return !l || cast<T> (l); + } template <typename T> inline void |