Skip to content

Commit

Permalink
Support for reflection with Eigen vector types (#1552)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenberry authored Jan 6, 2025
1 parent ac09e84 commit c0bc0d3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
22 changes: 19 additions & 3 deletions include/glaze/concepts/container_concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,36 @@ namespace glz
requires std::input_iterator<decltype(t.begin())>;
};

// Concept for a matrix type (not a vector, which is a range)
template <class T>
concept matrix_t = requires(T matrix) {
matrix.resize(2, 4);
matrix.data();
{
matrix.rows()
} -> std::convertible_to<size_t>;
} -> std::convertible_to<int>;
{
matrix.cols()
} -> std::convertible_to<size_t>;
} -> std::convertible_to<int>;
{
matrix.size()
} -> std::convertible_to<size_t>;
} -> std::convertible_to<int>;
} && !range<T>;

// concept for the Eigen library: matrices and vectors
template <class T>
concept eigen_t = requires(T matrix) {
matrix.data();
{
matrix.rows()
} -> std::convertible_to<int>;
{
matrix.cols()
} -> std::convertible_to<int>;
{
matrix.size()
} -> std::convertible_to<int>;
};

// range like
template <class T>
Expand Down
2 changes: 1 addition & 1 deletion include/glaze/core/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ namespace glz
else if constexpr (std::is_member_function_pointer_v<V>) {
return element;
}
else if constexpr (std::invocable<Element, Value> && not matrix_t<Element>) {
else if constexpr (std::invocable<Element, Value> && not eigen_t<Element>) {
// Eigen places a static_assert inside of the operator()(), so we must target
// matrix types and reject them from the invocable check
// Eigen ought to put the check in the `enable_if` for operator()()
Expand Down
8 changes: 2 additions & 6 deletions tests/eigen_test/eigen_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ struct complex_struct
Eigen::MatrixXcd mcd;
} complex_test_value;

template <>
struct glz::meta<complex_struct>
{
using T = complex_struct;
static constexpr auto value = object(&T::mf, &T::vi, &T::mcd);
};
static_assert(glz::eigen_t<Eigen::Matrix2f>);
static_assert(glz::eigen_t<Eigen::Vector4i>);

// Initialize complex_test_value
void initialize_complex_struct()
Expand Down

0 comments on commit c0bc0d3

Please sign in to comment.