Skip to content

Commit 229e83d

Browse files
committed
[DL] store extra functions in exported wrapper
1 parent 0e4ee1f commit 229e83d

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

interfaces/python/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ target_include_directories(_alpaqa PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
6767
target_link_libraries(_alpaqa PRIVATE pybind11::pybind11 alpaqa::alpaqa)
6868
target_link_libraries(_alpaqa PRIVATE alpaqa::warnings)
6969
target_compile_definitions(_alpaqa PRIVATE
70-
MODULE_NAME=$<TARGET_FILE_BASE_NAME:_alpaqa>)
70+
MODULE_NAME=$<TARGET_FILE_BASE_NAME:_alpaqa>
71+
PYBIND11_NAMESPACE=pybind11 # prevent visibility hidden in pybind11 common.h
72+
)
7173
set_target_properties(_alpaqa PROPERTIES
7274
CXX_VISIBILITY_PRESET "hidden"
7375
VISIBILITY_INLINES_HIDDEN true

interfaces/python/src/problem/problems.py.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ using namespace py::literals;
3232
#if ALPAQA_WITH_DL
3333
template class ALPAQA_PYTHON_EXPORT std::span<std::string_view>;
3434
template class ALPAQA_PYTHON_EXPORT std::tuple<py::args, py::kwargs>;
35+
template class ALPAQA_PYTHON_EXPORT
36+
alpaqa::detail::function_wrapper_t<py::object(void *, py::args, py::kwargs)>;
3537
#endif
3638

3739
#include <util/copy.hpp>

src/interop/dl-api/include/alpaqa/dl/dl-problem.h

+21-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
#define ALPAQA_DL_ABI_VERSION 0xA1A000000004
99

10+
#ifdef _WIN32
11+
#ifdef ALPAQA_DL_PROBLEM_EXPORTS // TODO: what's the right approach on Windows?
12+
#define ALPAQA_DL_PROBLEM_EXPORT __declspec(dllexport)
13+
#else
14+
#define ALPAQA_DL_PROBLEM_EXPORT __declspec(dllimport)
15+
#endif
16+
#elif defined(__GNUC__)
17+
#define ALPAQA_DL_PROBLEM_EXPORT __attribute__((visibility("default")))
18+
#else
19+
#define ALPAQA_DL_PROBLEM_EXPORT
20+
#endif
21+
1022
#ifdef __cplusplus
1123
extern "C" {
1224
#define ALPAQA_BEGIN_STRUCT(name) struct name
@@ -654,6 +666,13 @@ using control_problem_register_t = alpaqa_control_problem_register_t;
654666
using problem_functions_t = alpaqa_problem_functions_t;
655667
using control_problem_functions_t = alpaqa_control_problem_functions_t;
656668

669+
namespace detail {
670+
template <class Signature>
671+
struct ALPAQA_DL_PROBLEM_EXPORT function_wrapper_t {
672+
std::function<Signature> function;
673+
};
674+
} // namespace detail
675+
657676
/// Make the given function available to alpaqa.
658677
/// @see @ref alpaqa::dl::DLProblem::call_extra_func
659678
/// @see @ref alpaqa::dl::DLControlProblem::call_extra_func
@@ -663,7 +682,8 @@ void register_function(function_dict_t *&extra_functions, std::string name,
663682
if (extra_functions == nullptr)
664683
extra_functions = new function_dict_t{};
665684
extra_functions->dict.insert_or_assign(
666-
std::move(name), std::function{std::forward<Func>(func)});
685+
std::move(name),
686+
detail::function_wrapper_t{std::function{std::forward<Func>(func)}});
667687
}
668688

669689
template <class Func>

src/interop/dl/include/alpaqa/dl/dl-problem.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class ExtraFuncs {
3838
throw std::out_of_range("DLProblem: no extra function named \"" +
3939
name + '"');
4040
try {
41-
return std::any_cast<const std::function<Signature> &>(it->second);
41+
using func_t = detail::function_wrapper_t<Signature>;
42+
return std::any_cast<const func_t &>(it->second).function;
4243
} catch (const std::bad_any_cast &) {
4344
throw std::logic_error(
4445
"DLProblem: incorrect type for extra function \"" + name +

0 commit comments

Comments
 (0)