Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DL] store extra functions in exported wrapper
Browse files Browse the repository at this point in the history
tttapa committed Jan 30, 2024
1 parent 0e4ee1f commit 229e83d
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 3 additions & 1 deletion interfaces/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -67,7 +67,9 @@ target_include_directories(_alpaqa PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(_alpaqa PRIVATE pybind11::pybind11 alpaqa::alpaqa)
target_link_libraries(_alpaqa PRIVATE alpaqa::warnings)
target_compile_definitions(_alpaqa PRIVATE
MODULE_NAME=$<TARGET_FILE_BASE_NAME:_alpaqa>)
MODULE_NAME=$<TARGET_FILE_BASE_NAME:_alpaqa>
PYBIND11_NAMESPACE=pybind11 # prevent visibility hidden in pybind11 common.h
)
set_target_properties(_alpaqa PROPERTIES
CXX_VISIBILITY_PRESET "hidden"
VISIBILITY_INLINES_HIDDEN true
2 changes: 2 additions & 0 deletions interfaces/python/src/problem/problems.py.cpp
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@ using namespace py::literals;
#if ALPAQA_WITH_DL
template class ALPAQA_PYTHON_EXPORT std::span<std::string_view>;
template class ALPAQA_PYTHON_EXPORT std::tuple<py::args, py::kwargs>;
template class ALPAQA_PYTHON_EXPORT
alpaqa::detail::function_wrapper_t<py::object(void *, py::args, py::kwargs)>;
#endif

#include <util/copy.hpp>
22 changes: 21 additions & 1 deletion src/interop/dl-api/include/alpaqa/dl/dl-problem.h
Original file line number Diff line number Diff line change
@@ -7,6 +7,18 @@

#define ALPAQA_DL_ABI_VERSION 0xA1A000000004

#ifdef _WIN32
#ifdef ALPAQA_DL_PROBLEM_EXPORTS // TODO: what's the right approach on Windows?
#define ALPAQA_DL_PROBLEM_EXPORT __declspec(dllexport)
#else
#define ALPAQA_DL_PROBLEM_EXPORT __declspec(dllimport)
#endif
#elif defined(__GNUC__)
#define ALPAQA_DL_PROBLEM_EXPORT __attribute__((visibility("default")))
#else
#define ALPAQA_DL_PROBLEM_EXPORT
#endif

#ifdef __cplusplus
extern "C" {
#define ALPAQA_BEGIN_STRUCT(name) struct name
@@ -654,6 +666,13 @@ using control_problem_register_t = alpaqa_control_problem_register_t;
using problem_functions_t = alpaqa_problem_functions_t;
using control_problem_functions_t = alpaqa_control_problem_functions_t;

namespace detail {
template <class Signature>
struct ALPAQA_DL_PROBLEM_EXPORT function_wrapper_t {
std::function<Signature> function;
};
} // namespace detail

/// Make the given function available to alpaqa.
/// @see @ref alpaqa::dl::DLProblem::call_extra_func
/// @see @ref alpaqa::dl::DLControlProblem::call_extra_func
@@ -663,7 +682,8 @@ void register_function(function_dict_t *&extra_functions, std::string name,
if (extra_functions == nullptr)
extra_functions = new function_dict_t{};
extra_functions->dict.insert_or_assign(
std::move(name), std::function{std::forward<Func>(func)});
std::move(name),
detail::function_wrapper_t{std::function{std::forward<Func>(func)}});
}

template <class Func>
3 changes: 2 additions & 1 deletion src/interop/dl/include/alpaqa/dl/dl-problem.hpp
Original file line number Diff line number Diff line change
@@ -38,7 +38,8 @@ class ExtraFuncs {
throw std::out_of_range("DLProblem: no extra function named \"" +
name + '"');
try {
return std::any_cast<const std::function<Signature> &>(it->second);
using func_t = detail::function_wrapper_t<Signature>;
return std::any_cast<const func_t &>(it->second).function;
} catch (const std::bad_any_cast &) {
throw std::logic_error(
"DLProblem: incorrect type for extra function \"" + name +

0 comments on commit 229e83d

Please sign in to comment.