WRApplication
External kratos "application" for multiscale time integration.
Kratos::Pipes Namespace Reference

Unix-inspired pipes in C++. More...

Classes

class  ModelPartFromModel
 Get a ModelPart from a Model by name. More...
 
struct  ProcessInfoFromModelPart
 Get the ProcessInfo of a ModelPart. More...
 
class  VariableFromProcessInfo
 Get a variable from ProcessInfo. More...
 
struct  TimeFromProcessInfo
 Get TIME from a ProcessInfo. More...
 
struct  StepFromProcessInfo
 Get STEP from a ProcessInfo. More...
 
class  Comparison
 Perform a comparison operation on the input as the left hand side. More...
 
class  IntervalPredicate
 Pipe wrapper for Detail::IntervalUtility. More...
 
class  Modulo
 Compute the mod of the input. More...
 
class  Add
 Add a constant value to the input. More...
 
class  ConstPredicate
 Return a bool regardless of the input. More...
 
struct  Traits
 Metaclass containing type information every pipe must inherit from. More...
 
class  CompoundPipe
 A composable pipe that takes the output of one pipe and feeds its result into another. More...
 
class  SingleSegmentPipeline
 An adaptor class for pipelines consisting of a single segment. More...
 

Typedefs

template<class TPipe >
using IsPipe = std::integral_constant< bool, std::is_same_v< decltype(std::declval< const TPipe >().operator()(std::declval< typename TPipe::InputType >())), typename TPipe::OutputType > &&!std::is_same_v< typename TPipe::InputType, void > &&!std::is_same_v< typename TPipe::OutputType, void > >
 Bool constant checking whether TPipe satisfies the requirements of a pipe. More...
 
template<class ... TPipes>
using Pipeline = decltype((...|std::declval< TPipes >()))
 Convenience type alias for complex pipes. More...
 

Functions

template<class TInput , class TPipe , std::enable_if_t< IsPipe< TPipe >::value &&std::is_convertible_v< TInput, typename TPipe::InputType >, bool > = true>
TPipe::OutputType operator>> (TInput &&rInput, const TPipe &rPipe)
 Operator for calling operator() of the pipe. More...
 
template<class TInputPipe , class TOutputPipe , std::enable_if_t< IsPipe< TInputPipe >::value &&IsPipe< TOutputPipe >::value, bool > = true>
CompoundPipe< TInputPipe, TOutputPipe > operator| (TInputPipe &&rInputPipe, TOutputPipe &&rOutputPipe)
 Construct a pipe that takes the output of an input pipe and feeds it into an output pipe. More...
 
template<class TInputPipe , class TOutputPipe , std::enable_if_t< IsPipe< TInputPipe >::value &&IsPipe< TOutputPipe >::value, bool > = true>
CompoundPipe< TInputPipe, TOutputPipe > operator| (const TInputPipe &rInputPipe, const TOutputPipe &rOutputPipe)
 Construct a pipe that takes the output of an input pipe and feeds it into an output pipe. More...
 

Detailed Description

Unix-inspired pipes in C++.

Author
Máté Kelemen

A pipe is a modular sequence of operations that takes an immutable input and produces an output. Modularity is provided via static polymorphism (templates) to avoid the overhead incurred by virtual functions. Therefore, there is no base class for a pipe; rather, a class must satisfy a set of requirements to be considered a Pipe. These requirements are:

  • has member type InputType
  • has member type OutputType
  • has member function operator() with the following signature:
    OutputType operator()(@a InputType) const
  • must not have side-effects (not checked)
  • must not mutate its input (not checked)
  • must be default, move, and copy constructible (not checked)
  • [optional]: should be constructible from Parameters (required for CompoundPipe).

    Pipes are meant to be stitched together to form a pipeline capable of performing more complex tasks. Two operators can be used for this purpose:

  • Kratos::Pipes::operator| takes two (possibly different type of) pipe instances and produces a new pipe that combines the two. For example:
    auto compound_pipe = pipe_1 | pipe_2;
    The resulting compound_pipe takes an input, feeds it to pipe_1, then feed the result into pipe_2, then returns its result. The benefit of this approach over operator>> is that the resulting compound_pipe is a pipe as well, and can be passed around to other functions / classes, or combined with other pipes.
  • Kratos::Pipes::operator>> feeds an input into a pipe. It is equivalent to calling operator(), but is more readable when pushing through multiple pipes. Example:

    auto result = input >> pipe_1 >> pipe_2;

    The benefit of this approach over operator| is that no compound pipe instance is constructed.

    When defining a new pipe class, make sure to derive from Traits, which defines the necessary type aliases for your new class to be considered a pipe. The integral constant IsPipe checks whether a type is a valid pipe. Usage:

    IsPipe<MyPipe>::value

    . A type must satisfy the pipe requirements in order to be usable in operator| and operator>>.

    Pipes that are constructible from Parameters can be used with CompoundPipe. CompoundPipe recursively constructs each pipe segment by passing the same Parameters instance. This can be very handy for constructing longer pipes or using pipes in generic templates.