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

Test adding a free function with template parameters #179

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/parametric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,38 @@ struct WrapCppVector2
}
};

struct Unwrapped {};

// Type with a third template that will not be exposed to Julia
template<typename T, int N, typename ParamT>
struct WithUnwrappedParam
{
using value_type = T;
static constexpr int ndims = N;
};

// Give the unwrapped parameterr a default value
template <typename T, int N>
using UnwrappedDefault = WithUnwrappedParam<T,N,Unwrapped>;

// Wrap the simplified type
struct WrapUnwrappedDefault
{
template<typename TypeWrapperT>
void operator()(TypeWrapperT&& wrapped)
{
typedef typename TypeWrapperT::type WrappedT;
using value_type = typename WrappedT::value_type;

// Constructor function that takes the types as parameters
// Call in Julia using e.g. ParametricTypes.make_unwrapped_default(ParametricTypes.P1,Val(Cint(2)))
wrapped.module().method("make_unwrapped_default", [](jlcxx::SingletonType<value_type>, jlcxx::Val<int, WrappedT::ndims>)
{
return WithUnwrappedParam<value_type, WrappedT::ndims, Unwrapped>();
});
}
};

} // namespace parametric

namespace jlcxx
Expand All @@ -225,6 +257,12 @@ namespace jlcxx
typedef ParameterList<T, std::integral_constant<T, Val>> type;
};

template<typename T, int N>
struct BuildParameterList<parametric::UnwrappedDefault<T, N>>
{
typedef ParameterList<T, std::integral_constant<int, N>> type;
};

using namespace parametric;

template<> struct IsMirroredType<P1> : std::false_type { };
Expand All @@ -238,6 +276,7 @@ namespace jlcxx
template<typename T1, bool B> struct IsMirroredType<Foo2<T1,B>> : std::false_type { };
template<typename T1> struct IsMirroredType<CppVector<T1>> : std::false_type { };
template<typename T1, typename T2> struct IsMirroredType<CppVector2<T1,T2>> : std::false_type { };
template<typename T, int N> struct IsMirroredType<UnwrappedDefault<T,N>> : std::false_type { };

} // namespace jlcxx

Expand Down Expand Up @@ -278,4 +317,8 @@ JLCXX_MODULE define_julia_module(jlcxx::Module& types)

types.add_type<Parametric<TypeVar<1>, TypeVar<2>>, ParameterList<TypeVar<1>>>("CppVector2", jlcxx::julia_type("AbstractVector"))
.apply<CppVector2<double,float>>(WrapCppVector2());

types.add_type<Parametric<TypeVar<1>, TypeVar<2>>>("UnwrappedDefault")
.apply<UnwrappedDefault<P1, 1>>(WrapUnwrappedDefault())
.apply<UnwrappedDefault<P1, 2>>(WrapUnwrappedDefault());
}
Loading