]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add TRY_ASSIGN macro
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 3 Aug 2025 11:43:51 +0000 (13:43 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 3 Aug 2025 11:45:20 +0000 (13:45 +0200)
src/ccache/util/expected.hpp

index e8a553725db704ddcabca769c57bda3f11a675ca..9f79ff95176b15f287cba36d78828892ce8f34f2 100644 (file)
@@ -58,6 +58,13 @@ void throw_on_error(const T& value, std::string_view prefix);
     }                                                                          \
   } while (false)
 
+#define TRY_ASSIGN(var_, expression_)                                          \
+  auto result_##__LINE__##_ = (expression_);                                   \
+  if (!result_##__LINE__##_) {                                                 \
+    return tl::unexpected(std::move(result_##__LINE__##_.error()));            \
+  }                                                                            \
+  var_ = std::move(*result_##__LINE__##_)
+
 // --- Inline implementations ---
 
 template<typename E, typename T>