Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping more complex C++ types #260

Closed
archit120 opened this issue Jul 21, 2020 · 2 comments
Closed

Mapping more complex C++ types #260

archit120 opened this issue Jul 21, 2020 · 2 comments

Comments

@archit120
Copy link

While trying to map the OpenCV Point type to a corresponding type on Julia a lot of boiler-plate code is required along with changes to array.hpp. I was hoping this could be streamlined and made a better-supported feature in CxxWrap.

template<typename T>
struct CxxPoint
{
  T x;
  T y;
};
namespace jlcxx
{
  template<typename T> struct IsMirroredType<cv::Point_<T>> : std::true_type {};

  template<typename T> struct static_type_mapping<cv::Point_<T>> { using type = CxxPoint<T>; };

  template<typename T>
  struct julia_type_factory<cv::Point_<T>>
  {
    static inline jl_datatype_t* julia_type()
    {
      return (jl_datatype_t*)apply_type((jl_value_t*)jlcxx::julia_type("Point"), jl_svec1(julia_base_type<T>()));
    }
  };

  template<typename T>
  struct ConvertToJulia<cv::Point_<T>, NoMappingTrait>
  {
    CxxPoint<T> operator()(const cv::Point_<T>& cpp_val) const
    {
      return *reinterpret_cast<const CxxPoint<T>*>(&cpp_val);
    }
  };
};

The changes in array.hpp is from

  ValueT& operator[](const std::size_t i)
  {
    if constexpr(std::is_same<julia_t, ValueT>::value)
    {
      return data()[i];
    }
    else
    {
      return *extract_pointer_nonull<ValueT>(data()[i]);
    }
  }

to

  ValueT& operator[](const std::size_t i) const
  {
    if constexpr(std::is_same<julia_t, ValueT>::value)
    {
      return data()[i];
    }
     else if constexpr(std::is_same<julia_t, static_julia_type<ValueT>>::value && !std::is_same<julia_t, WrappedCppPtr>::value) 
    {
      return *reinterpret_cast<ValueT*>(&data()[i]);
    }
    else
    {
      return *extract_pointer_nonull<ValueT>(data()[i]);
    }
  }
barche added a commit to JuliaInterop/libcxxwrap-julia that referenced this issue Nov 12, 2024
barche added a commit to JuliaInterop/libcxxwrap-julia that referenced this issue Nov 13, 2024
@barche
Copy link
Collaborator

barche commented Nov 13, 2024

Not sure you will still read this, but the array.hpp change is now merged into libcxxwrap-julia and will be in the next release.

@barche barche closed this as completed Nov 13, 2024
@archit120
Copy link
Author

archit120 commented Nov 13, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants