/* Copyright 2023 The JAX Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #ifndef JAXLIB_ABSL_STATUS_CASTERS_H_ #define JAXLIB_ABSL_STATUS_CASTERS_H_ #include #include "absl/status/status.h" #include "absl/status/statusor.h" namespace jax { // C++ -> Python caster helpers. // // Failing statuses become Python exceptions; OK Status() becomes None. // // For example: // // - Functions without arguments: // m.def("my_func", []() { ThrowIfError(MyFunc()); } // - Classes with a single argument: // py_class.def("delete", [](Buffer& self) { // ThrowIfError(self.Delete()); // } // // For functions with more arguments, you can either inline the arguments, // or use the `ThrowIfErrorWrapper` wrapper defined below: // // m.def("my_func", ThrowIfErrorWrapper(MyFunc)); // // Nonstatic member functions can be wrapped by passing a // pointer-to-member-function: // ThrowIfErrorWrapper(&MyClass::MyMethod) inline void ThrowIfError(absl::Status src) { if (!src.ok()) { throw std::runtime_error(src.ToString()); } } // If one does not want to have to define a lambda specifying the inputs // arguments, on can use the `ThrowIfErrorWrapper` wrapper. // // There are three specializations: // - For free functions, `Sig` is the function type and `F` is `Sig&`. // - For callable types, `Sig` is the pointer to member function type // and `F` is the type of the callable. // - For a nonstatic member function of a class `C`, `Sig` is the function type // and `F` is Sig C::*. // // In the first two cases, the wrapper returns a callable with signature `Sig`; // in the third case, the wrapper returns callable with a modified signature // that takes a C instance as the first argument. template struct ThrowIfErrorWrapper; // C++17 "deduction guide" that guides class template argument deduction (CTAD) // For free functions. template ThrowIfErrorWrapper(F) -> ThrowIfErrorWrapper; // For callable types (with operator()). template ThrowIfErrorWrapper(absl::Status (&)(Args...)) -> ThrowIfErrorWrapper; // For unbound nonstatic member functions. template ThrowIfErrorWrapper(absl::Status (C::*)(Args...)) -> ThrowIfErrorWrapper; // Template specializations. // For free functions. template struct ThrowIfErrorWrapper { explicit ThrowIfErrorWrapper(absl::Status (&f)(Args...)) : func(f) {} void operator()(Args... args) { ThrowIfError(func(std::forward(args)...)); } absl::Status (&func)(Args...); }; // For callable types (with operator()), non-const and const versions. template struct ThrowIfErrorWrapper { explicit ThrowIfErrorWrapper(F&& f) : func(std::move(f)) {} void operator()(Args... args) { ThrowIfError(func(std::forward(args)...)); } F func; }; template struct ThrowIfErrorWrapper { explicit ThrowIfErrorWrapper(F&& f) : func(std::move(f)) {} void operator()(Args... args) const { ThrowIfError(func(std::forward(args)...)); } F func; }; // For unbound nonstatic member functions, non-const and const versions. // `ptmf` stands for "pointer to member function". template struct ThrowIfErrorWrapper { explicit ThrowIfErrorWrapper(absl::Status (C::*ptmf)(Args...)) : ptmf(ptmf) {} void operator()(C& instance, Args... args) { ThrowIfError((instance.*ptmf)(std::forward(args)...)); } absl::Status (C::*ptmf)(Args...); }; template struct ThrowIfErrorWrapper { explicit ThrowIfErrorWrapper(absl::Status (C::*ptmf)(Args...) const) : ptmf(ptmf) {} void operator()(const C& instance, Args... args) const { ThrowIfError((instance.*ptmf)(std::forward(args)...)); } absl::Status (C::*ptmf)(Args...) const; }; // Utilities for `StatusOr`. template T ValueOrThrow(absl::StatusOr v) { if (!v.ok()) { throw std::runtime_error(v.status().ToString()); } return std::move(v).value(); } template struct ValueOrThrowWrapper; template ValueOrThrowWrapper(F) -> ValueOrThrowWrapper; template ValueOrThrowWrapper(absl::StatusOr (&)(Args...)) -> ValueOrThrowWrapper(Args...), absl::StatusOr (&)(Args...)>; template ValueOrThrowWrapper(absl::StatusOr (C::*)(Args...)) -> ValueOrThrowWrapper(Args...), C>; // Deduction guide for const methods. template ValueOrThrowWrapper(absl::StatusOr (C::*)(Args...) const) -> ValueOrThrowWrapper(Args...) const, C>; template struct ValueOrThrowWrapper(Args...), absl::StatusOr (&)(Args...)> { explicit ValueOrThrowWrapper(absl::StatusOr (&f)(Args...)) : func(f) {} R operator()(Args... args) const { return ValueOrThrow(func(std::forward(args)...)); } absl::StatusOr (&func)(Args...); }; template struct ValueOrThrowWrapper (C::*)(Args...), F> { explicit ValueOrThrowWrapper(F&& f) : func(std::move(f)) {} R operator()(Args... args) const { return ValueOrThrow(func(std::forward(args)...)); } F func; }; template struct ValueOrThrowWrapper (C::*)(Args...) const, F> { explicit ValueOrThrowWrapper(F&& f) : func(std::move(f)) {} R operator()(Args... args) const { return ValueOrThrow(func(std::forward(args)...)); } F func; }; // For unbound nonstatic member functions, non-const and const versions. // `ptmf` stands for "pointer to member function". template struct ValueOrThrowWrapper(Args...), C> { explicit ValueOrThrowWrapper(absl::StatusOr (C::*ptmf)(Args...)) : ptmf(ptmf) {} R operator()(C& instance, Args... args) { return ValueOrThrow((instance.*ptmf)(std::forward(args)...)); } absl::StatusOr (C::*ptmf)(Args...); }; template struct ValueOrThrowWrapper(Args...) const, C> { explicit ValueOrThrowWrapper(absl::StatusOr (C::*ptmf)(Args...) const) : ptmf(ptmf) {} R operator()(const C& instance, Args... args) const { return ValueOrThrow((instance.*ptmf)(std::forward(args)...)); } absl::StatusOr (C::*ptmf)(Args...) const; }; } // namespace jax #endif // JAXLIB_ABSL_STATUS_CASTERS_H_