Skip to content

Commit

Permalink
Merge pull request #2965 from stan-dev/feature/simplify-meta
Browse files Browse the repository at this point in the history
Expanding 5 macros `STAN_ADD_REQUIRE_*` directly into code.
  • Loading branch information
syclik committed Apr 13, 2024
2 parents de0c1a7 + 66b92b5 commit b9d0a33
Show file tree
Hide file tree
Showing 36 changed files with 2,880 additions and 636 deletions.
543 changes: 427 additions & 116 deletions doxygen/contributor_help_pages/require_meta.md

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions stan/math/prim/meta/is_arena_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ namespace stan {
template <typename T, typename = void>
struct is_arena_matrix : std::false_type {};

STAN_ADD_REQUIRE_UNARY(arena_matrix, is_arena_matrix, require_eigens_types);
STAN_ADD_REQUIRE_CONTAINER(arena_matrix, is_arena_matrix, require_eigens_types);
STAN_ADD_REQUIRE_UNARY_INNER(arena_matrix, is_arena_matrix,
require_eigens_types);
/*! \ingroup require_eigen_types */
/*! \defgroup arena_matrix_types arena_matrix */
/*! \addtogroup arena_matrix_types */
/*! @{ */

/*! \brief Require type satisfies @ref is_arena_matrix */
/*! @tparam T the type to check */
template <typename T>
using require_arena_matrix_t = require_t<is_arena_matrix<std::decay_t<T>>>;
/*! @} */

} // namespace stan
#endif
78 changes: 76 additions & 2 deletions stan/math/prim/meta/is_autodiff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,82 @@ struct is_autodiff
: bool_constant<math::disjunction<is_var<std::decay_t<T>>,
is_fvar<std::decay_t<T>>>::value> {};

STAN_ADD_REQUIRE_UNARY(autodiff, is_autodiff, require_stan_scalar_real);
STAN_ADD_REQUIRE_UNARY_INNER(autodiff, is_autodiff, require_stan_scalar_real);
/*! \ingroup require_stan_scalar_real */
/*! \defgroup autodiff_types autodiff */
/*! \addtogroup autodiff_types */
/*! @{ */

/*! \brief Require type satisfies @ref is_autodiff */
/*! @tparam T the type to check */
template <typename T>
using require_autodiff_t = require_t<is_autodiff<std::decay_t<T>>>;

/*! \brief Require type does not satisfy @ref is_autodiff */
/*! @tparam T the type to check */
template <typename T>
using require_not_autodiff_t = require_not_t<is_autodiff<std::decay_t<T>>>;

/*! \brief Require all of the types satisfy @ref is_autodiff */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_all_autodiff_t
= require_all_t<is_autodiff<std::decay_t<Types>>...>;

/*! \brief Require any of the types satisfy @ref is_autodiff */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_any_autodiff_t
= require_any_t<is_autodiff<std::decay_t<Types>>...>;

/*! \brief Require none of the types satisfy @ref is_autodiff */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_all_not_autodiff_t
= require_all_not_t<is_autodiff<std::decay_t<Types>>...>;

/*! \brief Require at least one of the types do not satisfy @ref is_autodiff */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_any_not_autodiff_t
= require_any_not_t<is_autodiff<std::decay_t<Types>>...>;
/*! @} */

/*! \ingroup require_stan_scalar_real */
/*! \addtogroup autodiff_types */
/*! @{ */

/*! \brief Require value type does not satisfy @ref is_autodiff */
/*! @tparam T A type with a valid overload of @ref value_type available */
template <typename T>
using require_not_vt_autodiff
= require_not_t<is_autodiff<value_type_t<std::decay_t<T>>>>;

/*! \brief Require none of the value types satisfy @ref is_autodiff */
/*! @tparam Types The types with a valid overload of @ref value_type available
*/
template <typename... Types>
using require_all_not_vt_autodiff
= require_all_not_t<is_autodiff<value_type_t<std::decay_t<Types>>>...>;

/*! \brief Require scalar type satisfies @ref is_autodiff */
/*! @tparam T A type with a valid overload of @ref scalar_type available */
template <typename T>
using require_st_autodiff
= require_t<is_autodiff<scalar_type_t<std::decay_t<T>>>>;

/*! \brief Require scalar type does not satisfy @ref is_autodiff */
/*! @tparam T A type with a valid overload of @ref scalar_type available */
template <typename T>
using require_not_st_autodiff
= require_not_t<is_autodiff<scalar_type_t<std::decay_t<T>>>>;

/*! \brief Require any of the scalar types satisfy is_autodiff */
/*! @tparam Types The types with a valid overload of @ref scalar_type available
*/
template <typename... Types>
using require_any_st_autodiff
= require_any_t<is_autodiff<scalar_type_t<std::decay_t<Types>>>...>;
/*! @} */

} // namespace stan

Expand Down
55 changes: 53 additions & 2 deletions stan/math/prim/meta/is_complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,59 @@ struct scalar_type<T, std::enable_if_t<is_complex<T>::value>> {
using type = std::complex<typename std::decay_t<T>::value_type>;
};

STAN_ADD_REQUIRE_UNARY(complex, is_complex, require_stan_scalar_complex);
STAN_ADD_REQUIRE_UNARY_INNER(complex, is_complex, require_stan_scalar_complex);
/*! \ingroup require_stan_scalar_complex */
/*! \defgroup complex_types complex */
/*! \addtogroup complex_types */
/*! @{ */

/*! \brief Require type satisfies @ref is_complex */
/*! @tparam T the type to check */
template <typename T>
using require_complex_t = require_t<is_complex<std::decay_t<T>>>;

/*! \brief Require type does not satisfy @ref is_complex */
/*! @tparam T the type to check */
template <typename T>
using require_not_complex_t = require_not_t<is_complex<std::decay_t<T>>>;

/*! \brief Require all of the types satisfy @ref is_complex */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_all_complex_t = require_all_t<is_complex<std::decay_t<Types>>...>;

/*! \brief Require any of the types satisfy @ref is_complex */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_any_complex_t = require_any_t<is_complex<std::decay_t<Types>>...>;

/*! \brief Require none of the types satisfy @ref is_complex */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_all_not_complex_t
= require_all_not_t<is_complex<std::decay_t<Types>>...>;
/*! @} */

/*! \ingroup require_stan_scalar_complex */
/*! \addtogroup complex_types */
/*! @{ */

/*! \brief Require value type satisfies @ref is_complex */
/*! @tparam T A type with a valid overload of @ref value_type available */
template <typename T>
using require_vt_complex = require_t<is_complex<value_type_t<std::decay_t<T>>>>;

/*! \brief Require value type does not satisfy @ref is_complex */
/*! @tparam T A type with a valid overload of @ref value_type available */
template <typename T>
using require_not_vt_complex
= require_not_t<is_complex<value_type_t<std::decay_t<T>>>>;

/*! \brief Require scalar type does not satisfy @ref is_complex */
/*! @tparam T A type with a valid overload of @ref scalar_type available */
template <typename T>
using require_not_st_complex
= require_not_t<is_complex<scalar_type_t<std::decay_t<T>>>>;
/*! @} */

/**
* If the `value_type` of the type `T` is of type
Expand Down
3 changes: 0 additions & 3 deletions stan/math/prim/meta/is_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,5 @@ template <typename T>
struct is_constant<T, require_eigen_t<T>>
: bool_constant<is_constant<typename std::decay_t<T>::Scalar>::value> {};

STAN_ADD_REQUIRE_UNARY(constant, is_constant, require_stan_scalar_real);
STAN_ADD_REQUIRE_UNARY_INNER(constant, is_constant, require_stan_scalar_real);

} // namespace stan
#endif
61 changes: 59 additions & 2 deletions stan/math/prim/meta/is_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,65 @@ template <typename Container>
using is_container = bool_constant<
math::disjunction<is_eigen<Container>, is_std_vector<Container>>::value>;

STAN_ADD_REQUIRE_UNARY(container, is_container, general_types);
STAN_ADD_REQUIRE_CONTAINER(container, is_container, general_types);
/*! \ingroup general_types */
/*! \defgroup container_types container */
/*! \addtogroup container_types */
/*! @{ */

/*! \brief Require type satisfies @ref is_container */
/*! @tparam T the type to check */
template <typename T>
using require_container_t = require_t<is_container<std::decay_t<T>>>;

/*! \brief Require type does not satisfy @ref is_container */
/*! @tparam T the type to check */
template <typename T>
using require_not_container_t = require_not_t<is_container<std::decay_t<T>>>;

/*! \brief Require all of the types satisfy @ref is_container */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_all_container_t
= require_all_t<is_container<std::decay_t<Types>>...>;

/*! \brief Require any of the types satisfy @ref is_container */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_any_container_t
= require_any_t<is_container<std::decay_t<Types>>...>;

/*! \brief Require none of the types satisfy @ref is_container */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_all_not_container_t
= require_all_not_t<is_container<std::decay_t<Types>>...>;
/*! @} */

/*! \ingroup general_types */
/*! \defgroup container_types container */
/*! \addtogroup container_types */
/*! @{ */

/*! \brief Require type satisfies @ref is_container */
/*! and scalar type satisfies `TypeCheck` */
/*! @tparam TypeCheck The type trait to check the scalar type against */
/*! @tparam Check The type to test @ref is_container for and whose @ref
* scalar_type is checked with `TypeCheck` */
template <template <class...> class TypeCheck, class... Check>
using require_container_st
= require_t<container_type_check_base<is_container, scalar_type_t,
TypeCheck, Check...>>;

/*! \brief Require type does not satisfy @ref is_container */
/*! or scalar type does not satisfy `TypeCheck` */
/*! @tparam TypeCheck The type trait to check the scalar type against */
/*! @tparam Check The type to test @ref is_container for and whose @ref
* scalar_type is checked with `TypeCheck` */
template <template <class...> class TypeCheck, class... Check>
using require_not_container_st
= require_not_t<container_type_check_base<is_container, scalar_type_t,
TypeCheck, Check...>>;
/*! @} */

} // namespace stan

Expand Down
5 changes: 0 additions & 5 deletions stan/math/prim/meta/is_container_or_var_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ using is_container_or_var_matrix
= bool_constant<math::disjunction<is_container<Container>,
is_var_matrix<Container>>::value>;

STAN_ADD_REQUIRE_UNARY(container_or_var_matrix, is_container_or_var_matrix,
general_types);
STAN_ADD_REQUIRE_CONTAINER(container_or_var_matrix, is_container_or_var_matrix,
general_types);

} // namespace stan

#endif
19 changes: 16 additions & 3 deletions stan/math/prim/meta/is_dense_dynamic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,22 @@ struct is_dense_dynamic_impl<T, require_t<is_var<T>>>
template <typename T>
using is_dense_dynamic = internal::is_dense_dynamic_impl<std::decay_t<T>>;

STAN_ADD_REQUIRE_UNARY(dense_dynamic, is_dense_dynamic, require_eigens_types);
STAN_ADD_REQUIRE_CONTAINER(dense_dynamic, is_dense_dynamic,
require_eigens_types);
/*! \ingroup require_eigens_types */
/*! \defgroup dense_dynamic_types dense_dynamic */
/*! \addtogroup dense_dynamic_types */
/*! @{ */

/*! \brief Require type satisfies @ref is_dense_dynamic */
/*! @tparam T the type to check */
template <typename T>
using require_dense_dynamic_t = require_t<is_dense_dynamic<std::decay_t<T>>>;

/*! \brief Require all of the types satisfy @ref is_dense_dynamic */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_all_dense_dynamic_t
= require_all_t<is_dense_dynamic<std::decay_t<Types>>...>;
/*! @} */

} // namespace stan

Expand Down
45 changes: 41 additions & 4 deletions stan/math/prim/meta/is_double_or_int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,47 @@ struct is_double_or_int
math::disjunction<std::is_same<double, std::decay_t<T>>,
std::is_same<int, std::decay_t<T>>>::value> {};

STAN_ADD_REQUIRE_UNARY(double_or_int, is_double_or_int,
require_stan_scalar_real);
STAN_ADD_REQUIRE_UNARY_INNER(double_or_int, is_double_or_int,
require_stan_scalar_real);
/*! \ingroup require_stan_scalar_real */
/*! \defgroup double_or_int_types double_or_int */
/*! \addtogroup double_or_int_types */
/*! @{ */

/*! \brief Require type satisfies @ref is_double_or_int */
/*! @tparam T the type to check */
template <typename T>
using require_double_or_int_t = require_t<is_double_or_int<std::decay_t<T>>>;

/*! \brief Require type does not satisfy @ref is_double_or_int */
/*! @tparam T the type to check */
template <typename T>
using require_not_double_or_int_t
= require_not_t<is_double_or_int<std::decay_t<T>>>;

/*! \brief Require all of the types satisfy @ref is_double_or_int */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_all_double_or_int_t
= require_all_t<is_double_or_int<std::decay_t<Types>>...>;

/*! \brief Require any of the types satisfy @ref is_double_or_int */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_any_double_or_int_t
= require_any_t<is_double_or_int<std::decay_t<Types>>...>;

/*! \brief Require none of the types satisfy @ref is_double_or_int */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_all_not_double_or_int_t
= require_all_not_t<is_double_or_int<std::decay_t<Types>>...>;

/*! \brief Require at least one of the types do not satisfy @ref
* is_double_or_int */
/*! @tparam Types The types that are checked */
template <typename... Types>
using require_any_not_double_or_int_t
= require_any_not_t<is_double_or_int<std::decay_t<Types>>...>;
/*! @} */

} // namespace stan
#endif
Loading

0 comments on commit b9d0a33

Please sign in to comment.