alpaqa cmake-targets
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiLoader-util.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <casadi/core/casadi_types.hpp>
6#include <array>
7#include <optional>
8#include <stdexcept>
9#include <utility>
10
11namespace alpaqa::casadi_loader {
12
13template <class Loader, class F>
14auto wrap_load(Loader &&loader, const char *name, F f) {
15 try {
16 return f();
17 } catch (const invalid_argument_dimensions &e) {
18 throw std::invalid_argument(
19 "Unable to load function '" + loader.format_name(name) +
20 "': " + demangled_typename(typeid(e)) + ": " + e.what());
21 }
22}
23
24template <class T, class Loader, class... Args>
25auto wrapped_load(Loader &&loader, const char *name, Args &&...args) {
26 return wrap_load(loader, name, [&] {
27 return T(loader(name), std::forward<Args>(args)...);
28 });
29}
30
31template <class T, class Loader, class... Args>
32std::optional<T> try_load(Loader &&loader, const char *name, Args &&...args) {
33 try {
34 return std::make_optional(wrapped_load<T>(
35 std::forward<Loader>(loader), name, std::forward<Args>(args)...));
36 } catch (casadi::CasadiException &) {
37 return std::nullopt;
38 } catch (std::out_of_range &) {
39 // TODO: can be made more robust against false positives
40 return std::nullopt;
41 }
42}
43
44using dim = std::pair<casadi_int, casadi_int>;
45inline constexpr auto dims(auto... a) {
46 if constexpr ((... && std::is_constructible_v<dim, decltype(a)>))
47 return std::array{a...};
48 else
49 return std::array{dim{a, 1}...};
50}
51
52} // namespace alpaqa::casadi_loader
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
auto wrapped_load(Loader &&loader, const char *name, Args &&...args)
std::optional< T > try_load(Loader &&loader, const char *name, Args &&...args)
constexpr auto dims(auto... a)
std::pair< casadi_int, casadi_int > dim
auto wrap_load(Loader &&loader, const char *name, F f)
constexpr const auto inf
Definition config.hpp:85