Loading...
Searching...
No Matches
tryInvoke.h
Go to the documentation of this file.
1//
2// Copyright 2026 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_TRY_INVOKE_H
8#define PXR_BASE_TF_TRY_INVOKE_H
9
11
12#include "pxr/pxr.h"
13
14#include <functional>
15#include <optional>
16#include <type_traits>
17
24template <class Ret>
25constexpr auto
27{
28 if constexpr (std::is_void_v<Ret>) {
29 return false;
30 } else {
31 return std::optional<Ret>{};
32 }
33}
34
66template <class Ret, class Fn, class... Args>
67auto
68TfTryInvoke(Fn &&fn, Args &&...args)
69{
70 if constexpr (std::is_invocable_v<Fn, Args...>) {
71 if constexpr (std::is_void_v<Ret>) {
72 std::invoke(std::forward<Fn>(fn), std::forward<Args>(args)...);
73 return true;
74 } else {
75 return std::optional<Ret>(
76 std::invoke(std::forward<Fn>(fn), std::forward<Args>(args)...));
77 }
78 } else {
79 return TfNotInvoked<Ret>();
80 }
81}
82
83#endif // PXR_BASE_TF_TRY_INVOKE_H
constexpr auto TfNotInvoked()
Return the result type of TfTryInvoke<Ret> in the case where the function was not invoked – false if ...
Definition: tryInvoke.h:26
auto TfTryInvoke(Fn &&fn, Args &&...args)
Invoke fn with args if fn is invocable with those arguments and return a result that indicates both w...
Definition: tryInvoke.h:68