Skip to content

Commit

Permalink
Use overload_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Jun 15, 2023
1 parent 769adbc commit b381ad8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
5 changes: 1 addition & 4 deletions src/binding/julia/Container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ void define_julia_Container(jlcxx::Module &mod)
using ContainerT = typename decltype(type)::type;
using key_type = typename ContainerT::key_type;
using mapped_type = typename ContainerT::mapped_type;
using size_type = typename ContainerT::size_type;
static_assert(std::is_same_v<Eltype, mapped_type>);
static_assert(std::is_same_v<Keytype, key_type>);

Expand All @@ -62,9 +61,7 @@ void define_julia_Container(jlcxx::Module &mod)
type.method("cxx_count", &ContainerT::count);
type.method("cxx_contains", &ContainerT::contains);
type.method(
"cxx_delete!",
static_cast<size_type (ContainerT::*)(const key_type &)>(
&ContainerT::erase));
"cxx_delete!", overload_cast<const key_type &>(&ContainerT::erase));
type.method("cxx_keys", [](const ContainerT &cont) {
std::vector<key_type> res;
res.reserve(cont.size());
Expand Down
4 changes: 1 addition & 3 deletions src/binding/julia/Iteration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ void define_julia_Iteration(jlcxx::Module &mod)
type.method("cxx_set_dt!", &Iteration::setDt<double>);
type.method("cxx_time_unit_SI", &Iteration::timeUnitSI);
type.method("cxx_set_time_unit_SI!", &Iteration::setTimeUnitSI);
type.method(
"cxx_close",
static_cast<Iteration &(Iteration::*)(bool)>(&Iteration::close));
type.method("cxx_close", overload_cast<bool>(&Iteration::close));
type.method("cxx_open", &Iteration::open);
type.method("cxx_closed", &Iteration::closed);
type.method("cxx_meshes", [](Iteration &iter) -> Container<Mesh> & {
Expand Down
3 changes: 1 addition & 2 deletions src/binding/julia/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ void define_julia_Mesh(jlcxx::Module &mod)

type.method("cxx_geometry", &Mesh::geometry);
type.method(
"cxx_set_geometry!",
static_cast<Mesh &(Mesh::*)(Mesh::Geometry g)>(&Mesh::setGeometry));
"cxx_set_geometry!", overload_cast<Mesh::Geometry>(&Mesh::setGeometry));
type.method("cxx_geometry_parameters", &Mesh::geometryParameters);
type.method("cxx_set_geometry_parameters!", &Mesh::setGeometryParameters);
type.method("cxx_data_order", &Mesh::dataOrder);
Expand Down
3 changes: 1 addition & 2 deletions src/binding/julia/RecordComponent_load_chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ struct UseType
{
type.method(
"cxx_load_" + datatypeToString(determineDatatype<T>()),
static_cast<void (RecordComponent::*)(
std::shared_ptr<T>, Offset, Extent)>(
overload_cast<std::shared_ptr<T>, Offset, Extent>(
&RecordComponent::loadChunk<T>));
}
};
Expand Down
3 changes: 1 addition & 2 deletions src/binding/julia/RecordComponent_store_chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ struct UseType
{
type.method(
"cxx_store_chunk_" + datatypeToString(determineDatatype<T>()),
static_cast<void (RecordComponent::*)(
std::shared_ptr<T>, Offset, Extent)>(
overload_cast<std::shared_ptr<T>, Offset, Extent>(
&RecordComponent::storeChunk<T>));
}
};
Expand Down
3 changes: 1 addition & 2 deletions src/binding/julia/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ void define_julia_Series(jlcxx::Module &mod)
type.method("cxx_software", &Series::software);
type.method(
"cxx_set_software!",
static_cast<Series &(Series::*)(const std::string &,
const std::string &)>(
overload_cast<const std::string &, const std::string &>(
&Series::setSoftware));
type.method(
"cxx_set_software!",
Expand Down
42 changes: 42 additions & 0 deletions src/binding/julia/defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,48 @@ using sized_uint_t = typename sized_uint<I>::type;

using array_double_7 = std::array<double, 7>;

// From pybind11, see
// share/openPMD/thirdParty/pybind11/include/pybind11/detail/common.h
// in the source tree
struct non_const_tag
{};
struct const_tag
{};

namespace detail
{
template <typename... Args>
struct overload_cast_impl
{
template <typename Return>
constexpr auto operator()(Return (*pf)(Args...)) const noexcept
-> decltype(pf)
{
return pf;
}

template <typename Return, typename Class>
constexpr auto
operator()(Return (Class::*pmf)(Args...), non_const_tag = {}) const noexcept
-> decltype(pmf)
{
return pmf;
}

template <typename Return, typename Class>
constexpr auto
operator()(Return (Class::*pmf)(Args...) const, const_tag) const noexcept
-> decltype(pmf)
{
return pmf;
}
};
} // namespace detail

template <typename... Args>
static constexpr ::detail::overload_cast_impl<Args...> overload_cast{};
constexpr const_tag const const_;

/**
* Generalizes the repeated application of a function template for all
* scalar openPMD datatypes.
Expand Down

0 comments on commit b381ad8

Please sign in to comment.