| 149 | template <typename TScalar, DifferentiabilityMode SourceMode, |
| 150 | DifferentiabilityMode TargetMode, int TDimension> |
| 151 | struct ModeDowngradeAdapter |
| 152 | : public FunctionInterface<TScalar, TargetMode, TDimension> { |
| 153 | static_assert(static_cast<int>(SourceMode) >= static_cast<int>(TargetMode), |
| 154 | "ModeDowngradeAdapter only lowers the differentiability mode " |
| 155 | "-- attempting to upgrade."); |
| 156 | |
| 157 | using VectorType = Eigen::Matrix<TScalar, TDimension, 1>; |
| 158 | using MatrixType = Eigen::Matrix<TScalar, TDimension, TDimension>; |
| 159 | |
| 160 | std::unique_ptr<FunctionInterface<TScalar, SourceMode, TDimension>> source; |
| 161 | |
| 162 | explicit ModeDowngradeAdapter( |
| 163 | std::unique_ptr<FunctionInterface<TScalar, SourceMode, TDimension>> s) |
| 164 | : source(std::move(s)) {} |
| 165 | |
| 166 | TScalar operator()(const VectorType& x, VectorType* grad = nullptr, |
| 167 | MatrixType* hess = nullptr) const override { |
| 168 | (void)hess; // Never forwarded -- the target mode does not expose it. |
| 169 | if constexpr (TargetMode == DifferentiabilityMode::None) { |
| 170 | (void)grad; |
| 171 | return (*source)(x, nullptr, nullptr); |
| 172 | } else if constexpr (TargetMode == DifferentiabilityMode::First) { |
| 173 | return (*source)(x, grad, nullptr); |
| 174 | } else { |
| 175 | // TargetMode == Second: SourceMode must also be Second by the |
| 176 | // static_assert above, in which case the adapter is a no-op and |
| 177 | // nothing should ever construct it at this branch. Keep the path |
| 178 | // defined in case some future code wires it up. |
| 179 | return (*source)(x, grad, hess); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | std::unique_ptr<FunctionInterface<TScalar, TargetMode, TDimension>> clone() |
| 184 | const override { |
| 185 | return std::make_unique< |
| 186 | ModeDowngradeAdapter<TScalar, SourceMode, TargetMode, TDimension>>( |
| 187 | source->clone()); |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | template <typename TScalar, |
| 192 | DifferentiabilityMode TMode = DifferentiabilityMode::First, |
nothing calls this directly
no outgoing calls
no test coverage detected