diff options
author | Jacques Lucke <jacques@blender.org> | 2021-04-01 13:33:41 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-04-01 13:38:14 +0300 |
commit | 328b39335ef82248eb4a8af16251247763be5a2f (patch) | |
tree | bf4b3afb267f47593255a7c5727d599cf11d43fd /source/blender/blenlib/BLI_function_ref.hh | |
parent | fa50edc999c1ef2aa76e5c3a74fd5d8789fcb8bf (diff) |
BLI: add call_safe method for FunctionRef
This is useful to avoid nullity checks in some places.
Diffstat (limited to 'source/blender/blenlib/BLI_function_ref.hh')
-rw-r--r-- | source/blender/blenlib/BLI_function_ref.hh | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_function_ref.hh b/source/blender/blenlib/BLI_function_ref.hh index 57fffdc09b4..38e1ba593c5 100644 --- a/source/blender/blenlib/BLI_function_ref.hh +++ b/source/blender/blenlib/BLI_function_ref.hh @@ -16,6 +16,7 @@ #pragma once +#include <optional> #include <type_traits> #include <utility> @@ -139,6 +140,29 @@ template<typename Ret, typename... Params> class FunctionRef<Ret(Params...)> { return callback_(callable_, std::forward<Params>(params)...); } + using OptionalReturnValue = std::conditional_t<std::is_void_v<Ret>, void, std::optional<Ret>>; + + /** + * Calls the referenced function if it is available. + * The return value is of type `std::optional<Ret>` if `Ret` is not `void`. + * Otherwise the return type is `void`. + */ + OptionalReturnValue call_safe(Params... params) const + { + if constexpr (std::is_void_v<Ret>) { + if (callback_ == nullptr) { + return; + } + callback_(callable_, std::forward<Params>(params)...); + } + else { + if (callback_ == nullptr) { + return {}; + } + return callback_(callable_, std::forward<Params>(params)...); + } + } + /** * Returns true, when the `FunctionRef` references a function currently. * If this returns false, the `FunctionRef` must not be called. |