Skip to content

Commit

Permalink
Test adding a free function with template parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
barche committed Jan 15, 2025
1 parent dd11c21 commit 0f7cecf
Showing 1 changed file with 43 additions and 0 deletions.
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());
}

0 comments on commit 0f7cecf

Please sign in to comment.