|
| 1 | +/* Copyright 2023 The JAX Authors. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#ifndef JAXLIB_ABSL_STATUS_CASTERS_H_ |
| 17 | +#define JAXLIB_ABSL_STATUS_CASTERS_H_ |
| 18 | + |
| 19 | +#include <stdexcept> |
| 20 | + |
| 21 | +#include "absl/status/status.h" |
| 22 | +#include "absl/status/statusor.h" |
| 23 | + |
| 24 | +namespace jax { |
| 25 | + |
| 26 | +// C++ -> Python caster helpers. |
| 27 | +// |
| 28 | +// Failing statuses become Python exceptions; OK Status() becomes None. |
| 29 | +// |
| 30 | +// For example: |
| 31 | +// |
| 32 | +// - Functions without arguments: |
| 33 | +// m.def("my_func", []() { ThrowIfError(MyFunc()); } |
| 34 | +// - Classes with a single argument: |
| 35 | +// py_class.def("delete", [](Buffer& self) { |
| 36 | +// ThrowIfError(self.Delete()); |
| 37 | +// } |
| 38 | +// |
| 39 | +// For functions with more arguments, you can either inline the arguments, |
| 40 | +// or use the `ThrowIfErrorWrapper` wrapper defined below: |
| 41 | +// |
| 42 | +// m.def("my_func", ThrowIfErrorWrapper(MyFunc)); |
| 43 | +// |
| 44 | +// Nonstatic member functions can be wrapped by passing a |
| 45 | +// pointer-to-member-function: |
| 46 | +// ThrowIfErrorWrapper(&MyClass::MyMethod) |
| 47 | + |
| 48 | +inline void ThrowIfError(absl::Status src) { |
| 49 | + if (!src.ok()) { |
| 50 | + throw std::runtime_error(src.ToString()); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// If one does not want to have to define a lambda specifying the inputs |
| 55 | +// arguments, on can use the `ThrowIfErrorWrapper` wrapper. |
| 56 | +// |
| 57 | +// There are three specializations: |
| 58 | +// - For free functions, `Sig` is the function type and `F` is `Sig&`. |
| 59 | +// - For callable types, `Sig` is the pointer to member function type |
| 60 | +// and `F` is the type of the callable. |
| 61 | +// - For a nonstatic member function of a class `C`, `Sig` is the function type |
| 62 | +// and `F` is Sig C::*. |
| 63 | +// |
| 64 | +// In the first two cases, the wrapper returns a callable with signature `Sig`; |
| 65 | +// in the third case, the wrapper returns callable with a modified signature |
| 66 | +// that takes a C instance as the first argument. |
| 67 | +template <typename Sig, typename F> |
| 68 | +struct ThrowIfErrorWrapper; |
| 69 | + |
| 70 | +// C++17 "deduction guide" that guides class template argument deduction (CTAD) |
| 71 | +// For free functions. |
| 72 | +template <typename F> |
| 73 | +ThrowIfErrorWrapper(F) -> ThrowIfErrorWrapper<decltype(&F::operator()), F>; |
| 74 | + |
| 75 | +// For callable types (with operator()). |
| 76 | +template <typename... Args> |
| 77 | +ThrowIfErrorWrapper(absl::Status (&)(Args...)) |
| 78 | + -> ThrowIfErrorWrapper<absl::Status(Args...), absl::Status (&)(Args...)>; |
| 79 | + |
| 80 | +// For unbound nonstatic member functions. |
| 81 | +template <typename C, typename... Args> |
| 82 | +ThrowIfErrorWrapper(absl::Status (C::*)(Args...)) |
| 83 | + -> ThrowIfErrorWrapper<absl::Status(Args...), C>; |
| 84 | + |
| 85 | +// Template specializations. |
| 86 | + |
| 87 | +// For free functions. |
| 88 | +template <typename... Args> |
| 89 | +struct ThrowIfErrorWrapper<absl::Status(Args...), absl::Status (&)(Args...)> { |
| 90 | + explicit ThrowIfErrorWrapper(absl::Status (&f)(Args...)) : func(f) {} |
| 91 | + void operator()(Args... args) { |
| 92 | + ThrowIfError(func(std::forward<Args>(args)...)); |
| 93 | + } |
| 94 | + absl::Status (&func)(Args...); |
| 95 | +}; |
| 96 | + |
| 97 | +// For callable types (with operator()), non-const and const versions. |
| 98 | +template <typename C, typename... Args, typename F> |
| 99 | +struct ThrowIfErrorWrapper<absl::Status (C::*)(Args...), F> { |
| 100 | + explicit ThrowIfErrorWrapper(F &&f) : func(std::move(f)) {} |
| 101 | + void operator()(Args... args) { |
| 102 | + ThrowIfError(func(std::forward<Args>(args)...)); |
| 103 | + } |
| 104 | + F func; |
| 105 | +}; |
| 106 | +template <typename C, typename... Args, typename F> |
| 107 | +struct ThrowIfErrorWrapper<absl::Status (C::*)(Args...) const, F> { |
| 108 | + explicit ThrowIfErrorWrapper(F &&f) : func(std::move(f)) {} |
| 109 | + void operator()(Args... args) const { |
| 110 | + ThrowIfError(func(std::forward<Args>(args)...)); |
| 111 | + } |
| 112 | + F func; |
| 113 | +}; |
| 114 | + |
| 115 | +// For unbound nonstatic member functions, non-const and const versions. |
| 116 | +// `ptmf` stands for "pointer to member function". |
| 117 | +template <typename C, typename... Args> |
| 118 | +struct ThrowIfErrorWrapper<absl::Status(Args...), C> { |
| 119 | + explicit ThrowIfErrorWrapper(absl::Status (C::*ptmf)(Args...)) : ptmf(ptmf) {} |
| 120 | + void operator()(C &instance, Args... args) { |
| 121 | + ThrowIfError((instance.*ptmf)(std::forward<Args>(args)...)); |
| 122 | + } |
| 123 | + absl::Status (C::*ptmf)(Args...); |
| 124 | +}; |
| 125 | +template <typename C, typename... Args> |
| 126 | +struct ThrowIfErrorWrapper<absl::Status(Args...) const, C> { |
| 127 | + explicit ThrowIfErrorWrapper(absl::Status (C::*ptmf)(Args...) const) |
| 128 | + : ptmf(ptmf) {} |
| 129 | + void operator()(const C &instance, Args... args) const { |
| 130 | + ThrowIfError((instance.*ptmf)(std::forward<Args>(args)...)); |
| 131 | + } |
| 132 | + absl::Status (C::*ptmf)(Args...) const; |
| 133 | +}; |
| 134 | + |
| 135 | +// Utilities for `StatusOr`. |
| 136 | +template <typename T> |
| 137 | +T ValueOrThrow(absl::StatusOr<T> v) { |
| 138 | + if (!v.ok()) { |
| 139 | + throw std::runtime_error(v.status().ToString()); |
| 140 | + } |
| 141 | + return std::move(v).value(); |
| 142 | +} |
| 143 | + |
| 144 | +template <typename Sig, typename F> |
| 145 | +struct ValueOrThrowWrapper; |
| 146 | + |
| 147 | +template <typename F> |
| 148 | +ValueOrThrowWrapper(F) -> ValueOrThrowWrapper<decltype(&F::operator()), F>; |
| 149 | + |
| 150 | +template <typename R, typename... Args> |
| 151 | +ValueOrThrowWrapper(absl::StatusOr<R> (&)(Args...)) |
| 152 | + -> ValueOrThrowWrapper<absl::StatusOr<R>(Args...), |
| 153 | + absl::StatusOr<R> (&)(Args...)>; |
| 154 | + |
| 155 | +template <typename C, typename R, typename... Args> |
| 156 | +ValueOrThrowWrapper(absl::StatusOr<R> (C::*)(Args...)) |
| 157 | + -> ValueOrThrowWrapper<absl::StatusOr<R>(Args...), C>; |
| 158 | + |
| 159 | +// Deduction guide for const methods. |
| 160 | +template <typename C, typename R, typename... Args> |
| 161 | +ValueOrThrowWrapper(absl::StatusOr<R> (C::*)(Args...) const) |
| 162 | + -> ValueOrThrowWrapper<absl::StatusOr<R>(Args...) const, C>; |
| 163 | + |
| 164 | +template <typename R, typename... Args> |
| 165 | +struct ValueOrThrowWrapper<absl::StatusOr<R>(Args...), |
| 166 | + absl::StatusOr<R> (&)(Args...)> { |
| 167 | + explicit ValueOrThrowWrapper(absl::StatusOr<R> (&f)(Args...)) : func(f) {} |
| 168 | + R operator()(Args... args) const { |
| 169 | + return ValueOrThrow(func(std::forward<Args>(args)...)); |
| 170 | + } |
| 171 | + absl::StatusOr<R> (&func)(Args...); |
| 172 | +}; |
| 173 | +template <typename R, typename C, typename... Args, typename F> |
| 174 | +struct ValueOrThrowWrapper<absl::StatusOr<R> (C::*)(Args...), F> { |
| 175 | + explicit ValueOrThrowWrapper(F &&f) : func(std::move(f)) {} |
| 176 | + R operator()(Args... args) const { |
| 177 | + return ValueOrThrow(func(std::forward<Args>(args)...)); |
| 178 | + } |
| 179 | + F func; |
| 180 | +}; |
| 181 | +template <typename R, typename C, typename... Args, typename F> |
| 182 | +struct ValueOrThrowWrapper<absl::StatusOr<R> (C::*)(Args...) const, F> { |
| 183 | + explicit ValueOrThrowWrapper(F &&f) : func(std::move(f)) {} |
| 184 | + R operator()(Args... args) const { |
| 185 | + return ValueOrThrow(func(std::forward<Args>(args)...)); |
| 186 | + } |
| 187 | + F func; |
| 188 | +}; |
| 189 | + |
| 190 | +// For unbound nonstatic member functions, non-const and const versions. |
| 191 | +// `ptmf` stands for "pointer to member function". |
| 192 | +template <typename R, typename C, typename... Args> |
| 193 | +struct ValueOrThrowWrapper<absl::StatusOr<R>(Args...), C> { |
| 194 | + explicit ValueOrThrowWrapper(absl::StatusOr<R> (C::*ptmf)(Args...)) |
| 195 | + : ptmf(ptmf) {} |
| 196 | + R operator()(C &instance, Args... args) { |
| 197 | + return ValueOrThrow((instance.*ptmf)(std::forward<Args>(args)...)); |
| 198 | + } |
| 199 | + absl::StatusOr<R> (C::*ptmf)(Args...); |
| 200 | +}; |
| 201 | +template <typename R, typename C, typename... Args> |
| 202 | +struct ValueOrThrowWrapper<absl::StatusOr<R>(Args...) const, C> { |
| 203 | + explicit ValueOrThrowWrapper(absl::StatusOr<R> (C::*ptmf)(Args...) const) |
| 204 | + : ptmf(ptmf) {} |
| 205 | + R operator()(const C &instance, Args... args) const { |
| 206 | + return ValueOrThrow((instance.*ptmf)(std::forward<Args>(args)...)); |
| 207 | + } |
| 208 | + absl::StatusOr<R> (C::*ptmf)(Args...) const; |
| 209 | +}; |
| 210 | + |
| 211 | +} // namespace jax |
| 212 | + |
| 213 | +#endif // JAXLIB_ABSL_STATUS_CASTERS_H_ |
0 commit comments