alpaqa pantr
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiLoader-util.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <casadi/core/casadi_types.hpp>
4#include <casadi/core/external.hpp>
5#include <array>
6#include <optional>
7#include <stdexcept>
8#include <string>
9#include <utility>
10
11namespace alpaqa::casadi_loader {
12
13template <class F>
14auto wrap_load(const std::string &so_name, const char *name, F f) {
15 try {
16 return f();
17 } catch (const std::invalid_argument &e) {
18 throw std::invalid_argument("Unable to load function '" + so_name +
19 ":" + name + "': " + e.what());
20 }
21}
22
23template <class T, class... Args>
24auto wrapped_load(const std::string &so_name, const char *name,
25 Args &&...args) {
26 return wrap_load(so_name, name, [&] {
27 return T(casadi::external(name, so_name), std::forward<Args>(args)...);
28 });
29}
30
31template <class T, class... Args>
32std::optional<T> try_load(const std::string &so_name, const char *name,
33 Args &&...args) {
34 try {
35 return std::make_optional(
36 wrapped_load<T>(so_name, name, std::forward<Args>(args)...));
37 } catch (casadi::CasadiException &e) {
38 return std::nullopt;
39 }
40}
41
42using dim = std::pair<casadi_int, casadi_int>;
43inline constexpr auto dims(auto... a) {
44 if constexpr ((... && std::is_constructible_v<dim, decltype(a)>))
45 return std::array{a...};
46 else
47 return std::array{dim{a, 1}...};
48}
49
50} // namespace alpaqa::casadi_loader
constexpr auto dims(auto... a)
std::optional< T > try_load(const std::string &so_name, const char *name, Args &&...args)
std::pair< casadi_int, casadi_int > dim
auto wrap_load(const std::string &so_name, const char *name, F f)
auto wrapped_load(const std::string &so_name, const char *name, Args &&...args)