Created
April 14, 2020 23:16
-
-
Save barche/a723667478a5701c4f48a11935519287 to your computer and use it in GitHub Desktop.
Example on mapping templated types that are not POD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module OpenCV | |
using CxxWrap | |
struct Point{T} | |
x::T | |
y::T | |
end | |
@wrapmodule "build/lib/libtestlib.so" | |
function __init__() | |
@initcxx | |
end | |
end | |
@show OpenCV.makepoint(1,2) | |
@show OpenCV.readpoint(OpenCV.Point(1,2)) | |
@show OpenCV.readpointcref(OpenCV.Point(1,2)) | |
@show OpenCV.readpoint(OpenCV.Point(1.0,2.0)) | |
@show OpenCV.readpointcref(OpenCV.Point(1.0,2.0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "jlcxx/jlcxx.hpp" | |
#include <opencv2/core/types.hpp> | |
template<typename T> | |
struct MyPoint | |
{ | |
T x; | |
T y; | |
}; | |
namespace jlcxx | |
{ | |
// Indicate that the type is not wrapped using add_type | |
template<typename T> struct IsMirroredType<cv::Point_<T>> : std::true_type {}; | |
// Set the equivalent "C-compatible" type | |
template<typename T> struct static_type_mapping<cv::Point_<T>> { using type = MyPoint<T>; }; | |
// This replaces the need to call map_type, useful for templated types | |
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>())); | |
} | |
}; | |
// Conversions need to be defined manually too: | |
template<typename T> | |
struct ConvertToJulia<cv::Point_<T>, NoMappingTrait> | |
{ | |
MyPoint<T> operator()(const cv::Point_<T>& cpp_val) const | |
{ | |
return *reinterpret_cast<const MyPoint<T>*>(&cpp_val); | |
} | |
}; | |
template<typename T> | |
struct ConvertToCpp<cv::Point_<T>, NoMappingTrait> | |
{ | |
inline cv::Point operator()(const MyPoint<T>& julia_val) const | |
{ | |
return *reinterpret_cast<const cv::Point_<T>*>(&julia_val); | |
} | |
}; | |
} | |
JLCXX_MODULE define_julia_module(jlcxx::Module& mod) | |
{ | |
mod.method("readpoint", [](cv::Point p) { return p.x + p.y; }); | |
mod.method("readpointcref", [](const cv::Point& p) { return p.x + p.y; }); | |
mod.method("readpoint", [](cv::Point2l p) { return p.x + p.y; }); | |
mod.method("readpointcref", [](const cv::Point2l& p) { return p.x + p.y; }); | |
mod.method("readpoint", [](cv::Point2d p) { return p.x + p.y; }); | |
mod.method("readpointcref", [](const cv::Point2d& p) { return p.x + p.y; }); | |
mod.method("makepoint", [](int x, int y) { return cv::Point{x,y}; }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment