alpaqa 1.0.0a13
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 <exception>
8#include <optional>
9#include <stdexcept>
10#include <string>
11#include <utility>
12
13namespace alpaqa::casadi_loader {
14
15template <class Loader, class F>
16auto wrap_load(Loader &&loader, const char *name, F f) {
17 try {
18 return f();
19 } catch (const invalid_argument_dimensions &e) {
20 throw std::invalid_argument(
21 "Unable to load function '" + loader.format_name(name) +
22 "': " + demangled_typename(typeid(e)) + ": " + e.what());
23 }
24}
25
26template <class T, class Loader, class... Args>
27auto wrapped_load(Loader &&loader, const char *name, Args &&...args) {
28 return wrap_load(loader, name, [&] {
29 return T(loader(name), std::forward<Args>(args)...);
30 });
31}
32
33template <class T, class Loader, class... Args>
34std::optional<T> try_load(Loader &&loader, const char *name, Args &&...args) {
35 try {
36 return std::make_optional(wrapped_load<T>(
37 std::forward<Loader>(loader), name, std::forward<Args>(args)...));
38 } catch (casadi::CasadiException &) {
39 return std::nullopt;
40 } catch (std::out_of_range &) {
41 // TODO: can be made more robust against false positives
42 return std::nullopt;
43 }
44}
45
46using dim = std::pair<casadi_int, casadi_int>;
47inline constexpr auto dims(auto... a) {
48 if constexpr ((... && std::is_constructible_v<dim, decltype(a)>))
49 return std::array{a...};
50 else
51 return std::array{dim{a, 1}...};
52}
53
54} // 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