Skip to content

Commit

Permalink
invert
Browse files Browse the repository at this point in the history
  • Loading branch information
eggrobin committed Mar 24, 2024
1 parent 9c38c80 commit ffcacbd
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 114 deletions.
4 changes: 2 additions & 2 deletions base/not_null.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ struct NotNullStorage<
// reference to its template argument.
template<typename Pointer>
using _checked_not_null = typename std::enable_if<
!is_instance_of_v<not_null, typename std::remove_reference<Pointer>::type>,
!instance_of<typename std::remove_reference<Pointer>::type, not_null>,
not_null<typename std::remove_reference<Pointer>::type>>::type;

// We cannot refer to the template |not_null| inside of |not_null|.
template<typename Pointer>
inline constexpr bool is_instance_of_not_null_v =
is_instance_of_v<not_null, Pointer>;
instance_of<Pointer, not_null>;

// |not_null<Pointer>| is a wrapper for a non-null object of type |Pointer|.
// |Pointer| should be a C-style pointer or a smart pointer. |Pointer| must not
Expand Down
21 changes: 13 additions & 8 deletions base/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ template<typename T0, typename... Ts>
constexpr bool all_different_v<T0, Ts...> =
(!std::is_same_v<T0, Ts> && ...) && all_different_v<Ts...>;


template<template<typename...> typename T, typename U>
// The order of template parameters matches std::is_base_of; the type name
// should be read as an infix, U is [an] instance of T.
template<typename U, template<typename...> typename T>
struct is_instance_of : std::false_type {};

template<template<typename...> typename T, typename... Args>
struct is_instance_of<T, T<Args...>> : std::true_type {};
struct is_instance_of<T<Args...>, T> : std::true_type {};


template<template<typename...> typename T,
Expand Down Expand Up @@ -56,13 +57,17 @@ struct other_type<T2, T1, T2> {
using internal::all_different_v;

// True if and only if U is an instance of T.
template<template<typename...> typename T, typename U>
inline constexpr bool is_instance_of_v = internal::is_instance_of<T, U>::value;
// The order of template parameters matches std::is_base_of; the type name
// should be read as an infix, U is [an] instance of T.
template<typename U, template<typename...> typename T>
inline constexpr bool is_instance_of_v = internal::is_instance_of<U, T>::value;

// Note the argument inversion so that we can use instance_of<T> in a requires
// clause.
// The order of template parameters allows for
// requires { { expression } -> instance_of<T>; }
// or
// template<instance_of<T> U>
template<typename U, template<typename...> typename T>
concept instance_of = is_instance_of_v<T, U>;
concept instance_of = is_instance_of_v<U, T>;

// True if and only if T and U are the same template.
template<template<typename...> typename T, template<typename...> typename U>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class EmbeddedExplicitGeneralizedRungeKuttaNyströmIntegrator
public:
using ODE = ODE_;
static_assert(
is_instance_of_v<ExplicitSecondOrderOrdinaryDifferentialEquation, ODE>);
instance_of<ODE, ExplicitSecondOrderOrdinaryDifferentialEquation>);
using typename Integrator<ODE>::AppendState;
using typename AdaptiveStepSizeIntegrator<ODE>::Parameters;
using typename AdaptiveStepSizeIntegrator<ODE>::ToleranceToErrorRatio;
Expand Down
2 changes: 1 addition & 1 deletion integrators/embedded_explicit_runge_kutta_integrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class EmbeddedExplicitRungeKuttaIntegrator
public:
using ODE = ODE_;
static_assert(
is_instance_of_v<ExplicitFirstOrderOrdinaryDifferentialEquation, ODE>);
instance_of<ODE, ExplicitFirstOrderOrdinaryDifferentialEquation>);
using typename Integrator<ODE>::AppendState;
using typename AdaptiveStepSizeIntegrator<ODE>::Parameters;
using typename AdaptiveStepSizeIntegrator<ODE>::ToleranceToErrorRatio;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class EmbeddedExplicitRungeKuttaNyströmIntegrator
: public AdaptiveStepSizeIntegrator<ODE_> {
public:
using ODE = ODE_;
static_assert(is_instance_of_v<SpecialSecondOrderDifferentialEquation, ODE>);
static_assert(instance_of<ODE, SpecialSecondOrderDifferentialEquation>);
using typename Integrator<ODE>::AppendState;
using typename AdaptiveStepSizeIntegrator<ODE>::Parameters;
using typename AdaptiveStepSizeIntegrator<ODE>::ToleranceToErrorRatio;
Expand Down
3 changes: 1 addition & 2 deletions integrators/explicit_runge_kutta_integrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class ExplicitRungeKuttaIntegrator
: public FixedStepSizeIntegrator<ODE_> {
public:
using ODE = ODE_;
static_assert(
is_instance_of_v<ExplicitFirstOrderOrdinaryDifferentialEquation, ODE>);
static_assert(instance_of<ODE, ExplicitFirstOrderOrdinaryDifferentialEquation>);

Check warning on line 54 in integrators/explicit_runge_kutta_integrator.hpp

View workflow job for this annotation

GitHub Actions / check-cpp

whitespace/line_length

Lines should be <= 80 characters long
using typename Integrator<ODE>::AppendState;

static constexpr auto order = Method::order;
Expand Down
178 changes: 85 additions & 93 deletions integrators/integrators_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,39 +481,37 @@ void FixedStepSizeIntegrator<ODE_>::Instance::WriteToMessage(

#define PRINCIPIA_READ_FSS_INTEGRATOR_INSTANCE_ERK(method) LOG(FATAL) << "NYI"

#define PRINCIPIA_READ_FSS_INTEGRATOR_INSTANCE_SLMS(method) \
if constexpr (is_instance_of_v<SpecialSecondOrderDifferentialEquation, \
ODE>) { \
auto const& integrator = \
SymmetricLinearMultistepIntegrator<methods::method, ODE>(); \
return ReadSlmsInstanceFromMessage( \
extension, problem, append_state, step, integrator); \
} else { \
base::noreturn(); \
#define PRINCIPIA_READ_FSS_INTEGRATOR_INSTANCE_SLMS(method) \
if constexpr (instance_of<ODE, SpecialSecondOrderDifferentialEquation>) { \
auto const& integrator = \
SymmetricLinearMultistepIntegrator<methods::method, ODE>(); \
return ReadSlmsInstanceFromMessage( \
extension, problem, append_state, step, integrator); \
} else { \
base::noreturn(); \
}

#define PRINCIPIA_READ_FSS_INTEGRATOR_INSTANCE_SPRK(method) \
if constexpr (is_instance_of_v<DecomposableFirstOrderDifferentialEquation, \
ODE>) { \
return ReadSprkFromMessage< \
not_null<std::unique_ptr<typename Integrator<ODE>::Instance>>, \
ODE, \
methods::method, \
methods::method::first_same_as_last>( \
extension, problem, append_state, step); \
} else { \
base::noreturn(); \
#define PRINCIPIA_READ_FSS_INTEGRATOR_INSTANCE_SPRK(method) \
if constexpr (instance_of<ODE, \
DecomposableFirstOrderDifferentialEquation>) { \
return ReadSprkFromMessage< \
not_null<std::unique_ptr<typename Integrator<ODE>::Instance>>, \
ODE, \
methods::method, \
methods::method::first_same_as_last>( \
extension, problem, append_state, step); \
} else { \
base::noreturn(); \
}

#define PRINCIPIA_READ_FSS_INTEGRATOR_INSTANCE_SRKN(method) \
if constexpr (is_instance_of_v<SpecialSecondOrderDifferentialEquation, \
ODE>) { \
auto const& integrator = \
SymplecticRungeKuttaNyströmIntegrator<methods::method, ODE>(); \
return ReadSrknInstanceFromMessage( \
extension, problem, append_state, step, integrator); \
} else { \
base::noreturn(); \
#define PRINCIPIA_READ_FSS_INTEGRATOR_INSTANCE_SRKN(method) \
if constexpr (instance_of<ODE, SpecialSecondOrderDifferentialEquation>) { \
auto const& integrator = \
SymplecticRungeKuttaNyströmIntegrator<methods::method, ODE>(); \
return ReadSrknInstanceFromMessage( \
extension, problem, append_state, step, integrator); \
} else { \
base::noreturn(); \
}

template<typename ODE_>
Expand Down Expand Up @@ -564,35 +562,32 @@ FixedStepSizeIntegrator<ODE_>::Instance::Instance(
CHECK_NE(Time(), step_);
}

#define PRINCIPIA_READ_FSS_INTEGRATOR_ELMS(method) \
if constexpr (is_instance_of_v< \
ExplicitFirstOrderOrdinaryDifferentialEquation, \
ODE>) { \
return ExplicitLinearMultistepIntegrator<methods::method, ODE>(); \
} else { \
base::noreturn(); \
#define PRINCIPIA_READ_FSS_INTEGRATOR_ELMS(method) \
if constexpr (instance_of<ODE, \
ExplicitFirstOrderOrdinaryDifferentialEquation>) { \
return ExplicitLinearMultistepIntegrator<methods::method, ODE>(); \
} else { \
base::noreturn(); \
}

#define PRINCIPIA_READ_FSS_INTEGRATOR_ERK(method) \
if constexpr (is_instance_of_v< \
ExplicitFirstOrderOrdinaryDifferentialEquation, \
ODE>) { \
return ExplicitRungeKuttaIntegrator<methods::method, ODE>(); \
} else { \
base::noreturn(); \
#define PRINCIPIA_READ_FSS_INTEGRATOR_ERK(method) \
if constexpr (instance_of<ODE, \
ExplicitFirstOrderOrdinaryDifferentialEquation>) { \
return ExplicitRungeKuttaIntegrator<methods::method, ODE>(); \
} else { \
base::noreturn(); \
}

#define PRINCIPIA_READ_FSS_INTEGRATOR_SLMS(method) \
if constexpr (is_instance_of_v<SpecialSecondOrderDifferentialEquation, \
ODE>) { \
return SymmetricLinearMultistepIntegrator<methods::method, ODE>(); \
} else { \
base::noreturn(); \
#define PRINCIPIA_READ_FSS_INTEGRATOR_SLMS(method) \
if constexpr (instance_of<ODE, SpecialSecondOrderDifferentialEquation>) { \
return SymmetricLinearMultistepIntegrator<methods::method, ODE>(); \
} else { \
base::noreturn(); \
}

#define PRINCIPIA_READ_FSS_INTEGRATOR_SPRK(method) \
if constexpr (is_instance_of_v<DecomposableFirstOrderDifferentialEquation, \
ODE>) { \
if constexpr (instance_of<ODE, \
DecomposableFirstOrderDifferentialEquation>) { \
serialization::FixedStepSizeIntegratorInstance instance; \
*instance.mutable_integrator() = message; \
return ReadSprkFromMessage<FixedStepSizeIntegrator<ODE> const&, \
Expand All @@ -603,12 +598,11 @@ FixedStepSizeIntegrator<ODE_>::Instance::Instance(
base::noreturn(); \
}

#define PRINCIPIA_READ_FSS_INTEGRATOR_SRKN(method) \
if constexpr (is_instance_of_v<SpecialSecondOrderDifferentialEquation, \
ODE>) { \
return SymplecticRungeKuttaNyströmIntegrator<methods::method, ODE>(); \
} else { \
base::noreturn(); \
#define PRINCIPIA_READ_FSS_INTEGRATOR_SRKN(method) \
if constexpr (instance_of<ODE, SpecialSecondOrderDifferentialEquation>) { \
return SymplecticRungeKuttaNyströmIntegrator<methods::method, ODE>(); \
} else { \
base::noreturn(); \
}

template<typename ODE_>
Expand Down Expand Up @@ -717,28 +711,27 @@ void AdaptiveStepSizeIntegrator<ODE_>::Instance::WriteToMessage(
integrator().WriteToMessage(extension->mutable_integrator());
}

#define PRINCIPIA_READ_ASS_INTEGRATOR_INSTANCE_EEGRKN(method) \
if constexpr (is_instance_of_v< \
ExplicitSecondOrderOrdinaryDifferentialEquation, \
ODE>) { \
auto const& integrator = \
EmbeddedExplicitGeneralizedRungeKuttaNyströmIntegrator< \
methods::method, \
ODE>(); \
return ReadEegrknInstanceFromMessage(extension, \
problem, \
append_state, \
tolerance_to_error_ratio, \
parameters, \
step, \
first_use, \
integrator); \
} else { \
base::noreturn(); \
#define PRINCIPIA_READ_ASS_INTEGRATOR_INSTANCE_EEGRKN(method) \
if constexpr (instance_of< \
ODE, \
ExplicitSecondOrderOrdinaryDifferentialEquation>) { \
auto const& integrator = \
EmbeddedExplicitGeneralizedRungeKuttaNyströmIntegrator< \
methods::method, \
ODE>(); \
return ReadEegrknInstanceFromMessage(extension, \
problem, \
append_state, \
tolerance_to_error_ratio, \
parameters, \
step, \
first_use, \
integrator); \
} else { \
base::noreturn(); \
}
#define PRINCIPIA_READ_ASS_INTEGRATOR_INSTANCE_EERKN(method) \
if constexpr (is_instance_of_v<SpecialSecondOrderDifferentialEquation, \
ODE>) { \
if constexpr (instance_of<ODE, SpecialSecondOrderDifferentialEquation>) { \
auto const& integrator = \
EmbeddedExplicitRungeKuttaNyströmIntegrator<methods::method, ODE>(); \
return ReadEerknInstanceFromMessage(extension, \
Expand Down Expand Up @@ -831,24 +824,23 @@ AdaptiveStepSizeIntegrator<ODE_>::Instance::Instance(
CHECK_LT(parameters.safety_factor, 1);
}

#define PRINCIPIA_READ_ASS_INTEGRATOR_EEGRKN(method) \
if constexpr (is_instance_of_v< \
ExplicitSecondOrderOrdinaryDifferentialEquation, \
ODE>) { \
return EmbeddedExplicitGeneralizedRungeKuttaNyströmIntegrator< \
methods::method, \
ODE>(); \
} else { \
base::noreturn(); \
#define PRINCIPIA_READ_ASS_INTEGRATOR_EEGRKN(method) \
if constexpr (instance_of< \
ODE, \
ExplicitSecondOrderOrdinaryDifferentialEquation>) { \
return EmbeddedExplicitGeneralizedRungeKuttaNyströmIntegrator< \
methods::method, \
ODE>(); \
} else { \
base::noreturn(); \
}

#define PRINCIPIA_READ_ASS_INTEGRATOR_EERKN(method) \
if constexpr (is_instance_of_v<SpecialSecondOrderDifferentialEquation, \
ODE>) { \
return EmbeddedExplicitRungeKuttaNyströmIntegrator<methods::method, \
ODE>(); \
} else { \
base::noreturn(); \
#define PRINCIPIA_READ_ASS_INTEGRATOR_EERKN(method) \
if constexpr (instance_of<ODE, SpecialSecondOrderDifferentialEquation>) { \
return EmbeddedExplicitRungeKuttaNyströmIntegrator<methods::method, \
ODE>(); \
} else { \
base::noreturn(); \
}

#define PRINCIPIA_READ_ASS_INTEGRATOR_EERK(method) LOG(FATAL) << "NYI"
Expand Down
2 changes: 1 addition & 1 deletion integrators/symmetric_linear_multistep_integrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SymmetricLinearMultistepIntegrator
: public FixedStepSizeIntegrator<ODE_> {
public:
using ODE = ODE_;
static_assert(is_instance_of_v<SpecialSecondOrderDifferentialEquation, ODE>);
static_assert(instance_of<ODE, SpecialSecondOrderDifferentialEquation>);
using AppendState = typename Integrator<ODE>::AppendState;

static constexpr int order = Method::order;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class SymplecticPartitionedRungeKuttaIntegrator
: public FixedStepSizeIntegrator<ODE_> {
public:
using ODE = ODE_;
static_assert(
is_instance_of_v<DecomposableFirstOrderDifferentialEquation, ODE>);
static_assert(instance_of<ODE, DecomposableFirstOrderDifferentialEquation>);
using AppendState = typename Integrator<ODE>::AppendState;

static constexpr auto time_reversible = Method::time_reversible;
Expand Down
Loading

0 comments on commit ffcacbd

Please sign in to comment.