/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2026 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see .
Class
Foam::BinaryExpression
Description
Binary expression class. Wraps a two sub-types which may be other
expressions or expressionable containers or constant values. Applies a
binary operation to the output of these sub-expressions.
Note the specialisation of this class. This stores a sub-expression if it
is provided as an r-value, thereby ensuring that it remains in scope as
long as the outer expression does. And it takes a constant reference if a
sub-expression is provided as a reference, which ensures that copies are
not made unnecessarily.
\*---------------------------------------------------------------------------*/
#ifndef BinaryExpression_H
#define BinaryExpression_H
#include "expressionContainers.H"
#include "expressionOperate.H"
#include "expressionName.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class BinaryExpression Declaration
\*---------------------------------------------------------------------------*/
template
struct BinaryExpression
:
public expression::expression
{
A a;
B b;
BinaryExpression(A&& a, B&& b)
:
a(std::move(a)),
b(std::move(b))
{}
};
template
class BinaryExpression
:
public expression::expression
{
const ConditionalIfBaseOf, nil> tb_;
public:
A a;
const B& b;
BinaryExpression(A&& a, const B& b)
:
a(std::move(a)),
b(b)
{}
BinaryExpression(A&& a, const tmp& tb)
:
tb_(tb, true),
a(std::move(a)),
b(tb_())
{
tb.clear();
}
};
template
class BinaryExpression
:
public expression::expression
{
const ConditionalIfBaseOf, nil> ta_;
public:
const A& a;
B b;
BinaryExpression(const A& a, B&& b)
:
a(a),
b(std::move(b))
{}
BinaryExpression(const tmp& ta, B&& b)
:
ta_(ta, true),
a(ta_()),
b(std::move(b))
{
ta.clear();
}
};
template
class BinaryExpression
:
public expression::expression
{
const ConditionalIfBaseOf, nil> ta_;
const ConditionalIfBaseOf, nil> tb_;
public:
const A& a;
const B& b;
BinaryExpression(const A& a, const B& b)
:
a(a),
b(b)
{}
BinaryExpression(const tmp& ta, const B& b)
:
ta_(ta, true),
a(ta_()),
b(b)
{
ta.clear();
}
BinaryExpression(const A& a, const tmp& tb)
:
tb_(tb, true),
a(a),
b(tb_())
{
tb.clear();
}
BinaryExpression(const tmp& ta, const tmp& tb)
:
ta_(ta, true),
tb_(tb, true),
a(ta_()),
b(tb_())
{
ta.clear();
tb.clear();
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace expression
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Helper type definitions and functions for forwarding the sub-expressions as
// arguments. If the type is a binary expression, then forward the
// sub-expression, otherwise forward the type itself.
template
struct ArgA
{
using type = Type;
};
template
struct ArgB
{
using type = Type;
};
template
struct ArgA>
{
using type = A;
};
template
struct ArgB>
{
using type = B;
};
template
using ArgAType = typename ArgA::type;
template
using ArgBType = typename ArgB::type;
template
const Type& argA(const Type& t)
{
return t;
}
template
const Type& argB(const Type& t)
{
return t;
}
template
Type& argA(Type& t)
{
return t;
}
template
Type& argB(Type& t)
{
return t;
}
template
const A& argA(const BinaryExpression& e)
{
return e.a;
}
template
const B& argB(const BinaryExpression& e)
{
return e.b;
}
template
A& argA(BinaryExpression& e)
{
return e.a;
}
template
B& argB(BinaryExpression& e)
{
return e.b;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Specialise Expressionable for the binary expression. A binary expression is
// considered expressionable if either of it's sub-expressions are
// expressionable.
template
struct Expressionable>
:
public std::conditional
<
(
Expressionable>::value < 0
|| Expressionable>::value < 0
),
notExpressionable,
typename std::conditional
<
(
Expressionable>::value
> Expressionable>::value
),
Expressionable>,
Expressionable>
>::type
>::type
{};
template
struct Expressionable, Arguments>
:
public std::conditional
<
(
Expressionable
<
std::decay_t,
Arguments ...>
>::value
> Expressionable
<
std::decay_t,
Arguments ...>
>::value
),
Expressionable
<
std::decay_t,
Arguments ...>
>,
Expressionable
<
std::decay_t,
Arguments ...>
>
>::type
{};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Specialise AllContainers and TypeContainers for the binary expression so
// that access is forwarded to the sub-expressions
template
struct AllContainers
<
BinaryExpression,
EnableIfExpressionable>
>
{
using allContainersA = AllContainers>;
using allContainersB = AllContainers>;
static const unsigned n = allContainersA::n + allContainersB::n;
using type =
typename std::conditional
<
(
Expressionable::value
> Expressionable::value
),
typename allContainersA::type,
typename allContainersB::type
>::type;
template
static std::enable_if_t
<
(I < allContainersA::n),
typename Property::type
> get
(
const BinaryExpression& e
)
{
return allContainersA::template get(e.a);
}
template
static std::enable_if_t
<
(I >= allContainersA::n),
typename Property::type
> get
(
const BinaryExpression& e
)
{
return allContainersB::template get
(
e.b
);
}
template
static void get
(
const BinaryExpression& e,
FixedList& result
)
{
allContainersA::template get(e.a, result);
allContainersB::template get
(
e.b,
result
);
}
};
template
struct TypeContainers
<
BinaryExpression,
Type,
EnableIfNotBaseOf>
>
{
using typeContainersA = TypeContainers, Type>;
using typeContainersB = TypeContainers, Type>;
static const unsigned n = typeContainersA::n + typeContainersB::n;
template
static std::enable_if_t
<
(I < typeContainersA::n),
typename Property::type
> get
(
const BinaryExpression& e
)
{
return typeContainersA::template get(e.a);
}
template
static std::enable_if_t
<
(I >= typeContainersA::n),
typename Property::type
> get
(
const BinaryExpression& e
)
{
return typeContainersB::template get
(
e.b
);
}
template
static void get
(
const BinaryExpression& e,
FixedList& result
)
{
typeContainersA::template get(e.a, result);
typeContainersB::template get
(
e.b,
result
);
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Specialise Operate for the binary expression so that operations are
// forwarded to the sub-expressions.
//
// Note different specialisations for expressionable and non-expressionable
// binary expressions. The non-expressionable specialisation access returns a
// value as calculated by the operation, whilst the expressionable
// specialisation returns another expression containing the accessed
// sub-types. In this way, the expressions can be nested, with each level
// generating an appropriate type to which an element of an expressionable
// container (container of containers, etc...) can be assigned.
template
struct BinaryExpressionOperateBase
{
using operateA =
Operate, Arguments ...>>;
using operateB =
Operate, Arguments ...>>;
struct compareA
{
static bool compare
(
const BinaryExpression& e,
const Args& ... args
)
{
return operateA::compare(e.a, argA(args) ...);
}
};
struct compareB
{
static bool compare
(
const BinaryExpression& e,
const Args& ... args
)
{
return operateB::compare(e.b, argB(args) ...);
}
};
static bool compare
(
const BinaryExpression& e,
const Args& ... args
)
{
return
std::conditional
<
(Expressionable>::value > 0),
compareA,
compareB
>::type::compare(e, args ...);
}
static void advance
(
const BinaryExpression& e,
Args& ... args
)
{
operateA::advance(e.a, argA(args) ...);
operateB::advance(e.b, argB(args) ...);
}
};
template
struct Operate
<
BinaryExpression,
Arguments,
EnableIfNotExpressionable, Args ...>
>
:
public BinaryExpressionOperateBase
{
using operateA =
typename BinaryExpressionOperateBase::operateA;
using operateB =
typename BinaryExpressionOperateBase::operateB;
using type =
decltype
(
Op::value
(
std::declval(),
std::declval()
)
);
static type access
(
const BinaryExpression& e,
const Args& ... args
)
{
return
Op::value
(
operateA::access(e.a, argA(args) ...),
operateB::access(e.b, argB(args) ...)
);
}
};
template
struct Operate
<
BinaryExpression,
Arguments,
EnableIfExpressionable, Args ...>
>
:
public BinaryExpressionOperateBase
{
using operateA =
typename BinaryExpressionOperateBase::operateA;
using operateB =
typename BinaryExpressionOperateBase::operateB;
using type =
BinaryExpression
<
typename operateA::type,
typename operateB::type,
Op
>;
static type access
(
const BinaryExpression& e,
const Args& ... args
)
{
return
type
(
operateA::access(e.a, argA(args) ...),
operateB::access(e.b, argB(args) ...)
);
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Specialise Operation for the binary expression. Take the base operator type
// and precedence from the operation parameter.
template
struct Operation>
{
using type = Op;
static const signed char precedence = Op::precedence;
};
//- Specialise Name for the binary expression. Wraps the names of the
// sub-expressions in the name of the operation, adding brackets if the
// relative precedence of the operations requires it.
template
struct Name
<
BinaryExpression,
EnableIfExpressionable>
>
{
using nameA = Name>;
using nameB = Name>;
static word& append(word& name, const BinaryExpression& e)
{
name.append(Op::prefix);
if
(
Op::precedence != Operation::precedence
&& Operation::precedence != Operation::precedence
&& Op::precedence > Operation::precedence
)
{
name.append('(');
nameA::append(name, e.a);
name.append(')');
}
else
{
nameA::append(name, e.a);
}
name.append(Op::infix);
if
(
Op::precedence != Operation::precedence
&& Operation::precedence != Operation::precedence
&& (
Op::precedence > Operation::precedence
|| (
Op::precedence == Operation::precedence
&& !Op::associative
)
)
)
{
name.append('(');
nameB::append(name, e.b);
name.append(')');
}
else
{
nameB::append(name, e.b);
}
return name.append(Op::postfix);
}
static word value(const BinaryExpression& e)
{
word name;
return append(name, e);
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace expression
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //