SpECTRE Documentation Coverage Report
Current view: top level - Utilities - GenerateInstantiations.hpp Hit Total Coverage
Commit: 3c072f0ce967e2e56649d3fa12aa2a0e4fe2a42e Lines: 1 2 50.0 %
Date: 2024-04-23 20:50:18
Legend: Lines: hit not hit

          Line data    Source code
       1           0 : // Distributed under the MIT License.
       2             : // See LICENSE.txt for details.
       3             : 
       4             : #pragma once
       5             : 
       6             : // IWYU pragma: begin_exports
       7             : #include <boost/parameter/name.hpp>
       8             : #include <boost/preprocessor/arithmetic/inc.hpp>
       9             : #include <boost/preprocessor/control/expr_iif.hpp>
      10             : #include <boost/preprocessor/control/iif.hpp>
      11             : #include <boost/preprocessor/control/while.hpp>
      12             : #include <boost/preprocessor/list/adt.hpp>
      13             : #include <boost/preprocessor/list/fold_left.hpp>
      14             : #include <boost/preprocessor/list/fold_right.hpp>
      15             : #include <boost/preprocessor/list/for_each_product.hpp>
      16             : #include <boost/preprocessor/list/size.hpp>
      17             : #include <boost/preprocessor/list/to_tuple.hpp>
      18             : #include <boost/preprocessor/list/transform.hpp>
      19             : #include <boost/preprocessor/logical/bitand.hpp>
      20             : #include <boost/preprocessor/logical/bool.hpp>
      21             : #include <boost/preprocessor/logical/compl.hpp>
      22             : #include <boost/preprocessor/repetition/for.hpp>
      23             : #include <boost/preprocessor/tuple/elem.hpp>
      24             : #include <boost/preprocessor/tuple/reverse.hpp>
      25             : #include <boost/preprocessor/tuple/size.hpp>
      26             : #include <boost/preprocessor/tuple/to_list.hpp>
      27             : #include <boost/preprocessor/variadic/elem.hpp>
      28             : #include <boost/preprocessor/variadic/to_list.hpp>
      29             : // IWYU pragma: end_exports
      30             : 
      31             : /// \cond
      32             : #define GENERATE_INSTANTIATIONS_DO_PRODUCT(INSTANTIATION_MACRO, LIST_OF_LISTS) \
      33             :   BOOST_PP_LIST_FOR_EACH_PRODUCT(INSTANTIATION_MACRO,                          \
      34             :                                  BOOST_PP_LIST_SIZE(LIST_OF_LISTS),            \
      35             :                                  BOOST_PP_LIST_TO_TUPLE(LIST_OF_LISTS))
      36             : 
      37             : #define GENERATE_INSTANTIATION_TUPLES_TO_LISTS(d, _, elem) \
      38             :   BOOST_PP_TUPLE_TO_LIST(BOOST_PP_TUPLE_SIZE(elem), elem)
      39             : /// \endcond
      40             : 
      41             : /*!
      42             :  * \ingroup UtilitiesGroup
      43             :  * \brief Macro useful for generating many explicit instantiations of function
      44             :  * or class templates
      45             :  *
      46             :  * It is often necessary to generate explicit instantiations of function or
      47             :  * class templates. Since the total number of explicit instantiations scales as
      48             :  * the product of the number of possible number of parameter values of each
      49             :  * template parameter, this quickly becomes tedious. This macro allows you to
      50             :  * easily generate hundreds of explicit instantiations.
      51             :  *
      52             :  * The first argument to the macro is a macro that takes two arguments and is
      53             :  * described below. The remaining arguments are macro-tuples, e.g. `(1, 2, 3)`.
      54             :  * The Cartesian product of the macro-tuples is then computed and each term is
      55             :  * passed as a tuple as the second argument to the `INSTANTIATION_MACRO`. The
      56             :  * first argument to the `INSTANTIATION_MACRO` is a Boost.Preprocessor internal
      57             :  * variable so just make it `_`. The `INSTANTIATION(_, data)` macro below serves
      58             :  * as an example. A concrete example is generating explicit instantiations of
      59             :  * the class `Index<Dim>` for `Dim = 0,1,2,3`, which you would do as follows:
      60             :  *
      61             :  * \code
      62             :  * #define GET_DIM(data) BOOST_PP_TUPLE_ELEM(0, data)
      63             :  *
      64             :  * #define INSTANTIATION(_, data)         \
      65             :  *   template class Index<GET_DIM(data)>;
      66             :  *
      67             :  * GENERATE_INSTANTIATIONS(INSTANTIATION, (0, 1, 2, 3))
      68             :  *
      69             :  * #undef GET_DIM
      70             :  * #undef INSTANTIATION
      71             :  * \endcode
      72             :  *
      73             :  * This will generate:
      74             :  *
      75             :  * \code
      76             :  * template class Index<0>;
      77             :  * template class Index<1>;
      78             :  * template class Index<2>;
      79             :  * template class Index<3>;
      80             :  * \endcode
      81             :  *
      82             :  * It is also possible to generate explicit instantiations for multiple classes
      83             :  * or functions in a single call to `GENERATE_INSTANTIATIONS`. For example, the
      84             :  * (in)equivalence operators can be generated using:
      85             :  *
      86             :  * \code
      87             :  * #define GET_DIM(data) BOOST_PP_TUPLE_ELEM(0, data)
      88             :  * #define GEN_OP(op, dim)                            \
      89             :  *   template bool operator op(const Index<dim>& lhs, \
      90             :  *                             const Index<dim>& rhs);
      91             :  * #define INSTANTIATION(_, data)         \
      92             :  *   template class Index<GET_DIM(data)>; \
      93             :  *   GEN_OP(==, GET_DIM(data))            \
      94             :  *   GEN_OP(!=, GET_DIM(data))
      95             :  *
      96             :  * GENERATE_INSTANTIATIONS(INSTANTIATION, (0, 1, 2, 3))
      97             :  *
      98             :  * #undef GET_DIM
      99             :  * #undef GEN_OP
     100             :  * #undef INSTANTIATION
     101             :  * \endcode
     102             :  *
     103             :  * which will result in the instantiations:
     104             :  *
     105             :  * \code
     106             :  * template class Index<0>;
     107             :  * template bool operator==(const Index<0>& lhs, const Index<0>& rhs);
     108             :  * template bool operator!=(const Index<0>& lhs, const Index<0>& rhs);
     109             :  * template class Index<1>;
     110             :  * template bool operator==(const Index<1>& lhs, const Index<1>& rhs);
     111             :  * template bool operator!=(const Index<1>& lhs, const Index<1>& rhs);
     112             :  * template class Index<2>;
     113             :  * template bool operator==(const Index<2>& lhs, const Index<2>& rhs);
     114             :  * template bool operator!=(const Index<2>& lhs, const Index<2>& rhs);
     115             :  * template class Index<3>;
     116             :  * template bool operator==(const Index<3>& lhs, const Index<3>& rhs);
     117             :  * template bool operator!=(const Index<3>& lhs, const Index<3>& rhs);
     118             :  * \endcode
     119             :  *
     120             :  * Now let's look at generating instantiations of member function templates of
     121             :  * class templates, which will be a common use case. In this example we generate
     122             :  * explicit instantiations of all the member function templates of the class
     123             :  * `ScalarWave::Solutions::PlaneWave`. In total, for `Dim = 1,2,3` and types
     124             :  * `double` and `DataVector` this is about 42 explicit instantiations, which
     125             :  * would be extremely annoying to write by hand. The macro code is surprisingly
     126             :  * simple:
     127             :  *
     128             :  * \code
     129             :  * #define DIM(data) BOOST_PP_TUPLE_ELEM(0, data)
     130             :  * #define DTYPE(data) BOOST_PP_TUPLE_ELEM(1, data)
     131             :  *
     132             :  * #define INSTANTIATE(_, data)                                              \
     133             :  *   template Scalar<DTYPE(data)>                                            \
     134             :  *   ScalarWave::Solutions::PlaneWave<DIM(data)>::psi(                       \
     135             :  *       const tnsr::I<DTYPE(data), DIM(data)>& x, double t) const; \
     136             :  *   template Scalar<DTYPE(data)>                                            \
     137             :  *   ScalarWave::Solutions::PlaneWave<DIM(data)>::dpsi_dt(                   \
     138             :  *       const tnsr::I<DTYPE(data), DIM(data)>& x, double t) const;
     139             :  *
     140             :  * GENERATE_INSTANTIATIONS(INSTANTIATE, (1, 2, 3), (double, DataVector))
     141             :  *
     142             :  * #undef DIM
     143             :  * #undef DTYPE
     144             :  * #undef INSTANTIATE
     145             :  * \endcode
     146             :  *
     147             :  * We don't show the result from preprocessor since for all of the member
     148             :  * functions of `PlaneWave` the total output is approximately 150 lines, but you
     149             :  * can hopefully see the benefits of generating explicit instantiations using
     150             :  * the `GENERATE_INSTANTIATIONS` way.
     151             :  *
     152             :  * One thing that can be difficult is debugging metaprograms (be they template
     153             :  * or macro-based). To this end we provide a make target `DebugPreprocessor`
     154             :  * which prints the output of running the preprocessor on the file
     155             :  * `src/Executables/DebugPreprocessor/DebugPreprocessor.cpp`.
     156             :  * Note that the output of the `GENERATE_INSTANTIATIONS` macro will be on a
     157             :  * single line, so it often proves useful to copy-paste the output into an
     158             :  * editor and run clang-format over the code so it's easier to reason about.
     159             :  */
     160           1 : #define GENERATE_INSTANTIATIONS(INSTANTIATION_MACRO, ...)                \
     161             :   GENERATE_INSTANTIATIONS_DO_PRODUCT(                                    \
     162             :       INSTANTIATION_MACRO,                                               \
     163             :       BOOST_PP_LIST_TRANSFORM(GENERATE_INSTANTIATION_TUPLES_TO_LISTS, _, \
     164             :                               BOOST_PP_VARIADIC_TO_LIST(__VA_ARGS__)))

Generated by: LCOV version 1.14