aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2021-06-10 21:43:56 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2021-06-10 21:58:08 +0300
commit095cffecbd6c34105468c42414cef445dc1c0998 (patch)
tree04a5eabf8f534c4ba2bb9c459bceb85177e8a799
parent15370d2f54f9a6a286897715d74ffbf3bdf3e02e (diff)
Add path::try_relative()
-rw-r--r--libbutl/path.mxx6
-rw-r--r--libbutl/path.txx16
2 files changed, 19 insertions, 3 deletions
diff --git a/libbutl/path.mxx b/libbutl/path.mxx
index 12479ce..5a41ddc 100644
--- a/libbutl/path.mxx
+++ b/libbutl/path.mxx
@@ -952,6 +952,12 @@ LIBBUTL_MODEXPORT namespace butl
basic_path
relative (basic_path) const;
+ // As above but return nullopt rather than throw if a relative path cannot
+ // be derived.
+ //
+ optional<basic_path>
+ try_relative (basic_path) const;
+
// Iteration over path components.
//
// Note that for an absolute POSIX path the first component is empty,
diff --git a/libbutl/path.txx b/libbutl/path.txx
index 45b62bd..84fc038 100644
--- a/libbutl/path.txx
+++ b/libbutl/path.txx
@@ -103,8 +103,8 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason.
#endif
template <typename C, typename K>
- basic_path<C, K> basic_path<C, K>::
- relative (basic_path<C, K> d) const
+ optional<basic_path<C, K>> basic_path<C, K>::
+ try_relative (basic_path<C, K> d) const
{
dir_type r;
@@ -118,12 +118,22 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason.
// Roots of the paths do not match.
//
if (d.root ())
- throw invalid_basic_path<C> (this->path_);
+ return nullopt;
}
return r / leaf (d);
}
+ template <typename C, typename K>
+ basic_path<C, K> basic_path<C, K>::
+ relative (basic_path<C, K> d) const
+ {
+ if (optional<basic_path<C, K>> r = try_relative (std::move (d)))
+ return std::move (*r);
+
+ throw invalid_basic_path<C> (this->path_);
+ }
+
#ifdef _WIN32
// Find the actual spelling of a name in the specified dir. If the name is
// found, append it to the result and return true. Otherwise, return false.