alpaqa pi-pico
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiLoader-util.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <alpaqa/util/dl.hpp>
7
8#include <array>
9#include <optional>
10#include <stdexcept>
11#include <utility>
12
13#if ALPAQA_WITH_EXTERNAL_CASADI
14#include <casadi/core/casadi_types.hpp>
15#else
17#endif
18
19namespace alpaqa {
21namespace casadi_loader {
22
23template <class Loader, class F>
24auto wrap_load(Loader &&loader, const char *name, F f) {
25 try {
26 return f();
27 } catch (const invalid_argument_dimensions &e) {
28 throw std::invalid_argument(
29 "Unable to load function '" + loader.format_name(name) +
30 "': " + demangled_typename(typeid(e)) + ": " + e.what());
31 }
32}
33
34template <class T, class Loader, class... Args>
35auto wrapped_load(Loader &&loader, const char *name, Args &&...args) {
36 return wrap_load(loader, name, [&] {
37 return T(loader(name), std::forward<Args>(args)...);
38 });
39}
40
41template <class T, class Loader, class... Args>
42std::optional<T> try_load(Loader &&loader, const char *name, Args &&...args) {
43 try {
44 return std::make_optional(wrapped_load<T>(
45 std::forward<Loader>(loader), name, std::forward<Args>(args)...));
46#if ALPAQA_WITH_EXTERNAL_CASADI
47 } catch (casadi::CasadiException &) {
48 return std::nullopt;
49#else
50 } catch (util::dynamic_load_error &e) {
51 return std::nullopt;
52#endif
53 } catch (std::out_of_range &) {
54 // TODO: can be made more robust against false positives
55 return std::nullopt;
56 }
57}
58
59using dim = std::pair<casadi_int, casadi_int>;
60inline constexpr auto dims(auto... a) {
61 if constexpr ((... && std::is_constructible_v<dim, decltype(a)>))
62 return std::array{a...};
63 else
64 return std::array{dim{a, 1}...};
65}
66
67} // namespace casadi_loader
69} // namespace alpaqa
#define BEGIN_ALPAQA_CASADI_LOADER_NAMESPACE
#define END_ALPAQA_CASADI_LOADER_NAMESPACE
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:112
Failed to load a DLL or SO file, or failed to access a function in it.
Definition dl.hpp:12