From e1ba5baf65c2aca4c04fa2fd7263eed32ca6c63b Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Thu, 8 Aug 2019 10:48:13 -0600 Subject: [PATCH] Added std_cxx17::apply() method. --- doc/news/changes/minor/20190808LucaHeltai | 3 ++ include/deal.II/base/std_cxx17/tuple.h | 58 +++++++++++++++++++++++ tests/base/stdcxx17_apply.cc | 39 +++++++++++++++ tests/base/stdcxx17_apply.output | 2 + 4 files changed, 102 insertions(+) create mode 100644 doc/news/changes/minor/20190808LucaHeltai create mode 100644 include/deal.II/base/std_cxx17/tuple.h create mode 100644 tests/base/stdcxx17_apply.cc create mode 100644 tests/base/stdcxx17_apply.output diff --git a/doc/news/changes/minor/20190808LucaHeltai b/doc/news/changes/minor/20190808LucaHeltai new file mode 100644 index 0000000000..c974d5151e --- /dev/null +++ b/doc/news/changes/minor/20190808LucaHeltai @@ -0,0 +1,3 @@ +New: Added std_cxx17::apply() method. +
+(Luca Heltai, 2019/08/08) diff --git a/include/deal.II/base/std_cxx17/tuple.h b/include/deal.II/base/std_cxx17/tuple.h new file mode 100644 index 0000000000..038ffcb43a --- /dev/null +++ b/include/deal.II/base/std_cxx17/tuple.h @@ -0,0 +1,58 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2019 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- +#ifndef dealii_cxx17_tuple_h +#define dealii_cxx17_tuple_h + +#include + +#ifndef DEAL_II_WITH_CXX17 +# include +#endif + +#include + +DEAL_II_NAMESPACE_OPEN +namespace std_cxx17 +{ +#ifndef DEAL_II_WITH_CXX17 + template + auto + apply_impl(F &&fn, Tuple &&t, std_cxx14::index_sequence) + -> decltype(std::forward(fn)(std::get(std::forward(t))...)) + { + return std::forward(fn)(std::get(std::forward(t))...); + } + + template + auto + apply(F &&fn, Tuple &&t) -> decltype(apply_impl( + std::forward(fn), + std::forward(t), + std_cxx14::make_index_sequence< + std::tuple_size::type>::value>())) + { + std::size_t constexpr tSize = + std::tuple_size::type>::value; + return apply_impl(std::forward(fn), + std::forward(t), + std_cxx14::make_index_sequence()); + } +#else + using std::apply; +#endif // DEAL_II_WITH_CXX17 +} // namespace std_cxx17 +DEAL_II_NAMESPACE_CLOSE + +#endif diff --git a/tests/base/stdcxx17_apply.cc b/tests/base/stdcxx17_apply.cc new file mode 100644 index 0000000000..7426c87f19 --- /dev/null +++ b/tests/base/stdcxx17_apply.cc @@ -0,0 +1,39 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2017 - 2018 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// test the implementation of the std_cxx17::apply function + + +#include +#include + +#include "../tests.h" + + +void +example_function(const Point<2> &p, const double &d, const unsigned int i = 3) +{ + deallog << "P: " << p << ", d: " << d << ", i: " << i << std::endl; +} + + +int +main() +{ + initlog(); + auto tup = std::make_tuple(Point<2>(.5, .5), 2.0, 3u); + std_cxx17::apply(example_function, tup); +} diff --git a/tests/base/stdcxx17_apply.output b/tests/base/stdcxx17_apply.output new file mode 100644 index 0000000000..8f2ac511fb --- /dev/null +++ b/tests/base/stdcxx17_apply.output @@ -0,0 +1,2 @@ + +DEAL::P: 0.500000 0.500000, d: 2.00000, i: 3 -- 2.39.5