1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <expressionnodefactory.hxx>
27 /* Implementation of ExpressionNodeFactory class */
29 namespace slideshow::internal
33 class ConstantValueExpression
: public ExpressionNode
36 explicit ConstantValueExpression( double rValue
) :
41 virtual double operator()( double /*t*/ ) const override
46 virtual bool isConstant() const override
55 class TValueExpression
: public ExpressionNode
62 virtual double operator()( double t
) const override
67 virtual bool isConstant() const override
73 /** Base class for following binary functions (*+-/)
75 Does not pay off to have all this as a template, since
76 we'd have to hold the functor as a member (+33% object
79 class BinaryExpressionBase
: public ExpressionNode
82 BinaryExpressionBase( std::shared_ptr
<ExpressionNode
> pFirstArg
,
83 std::shared_ptr
<ExpressionNode
> pSecondArg
) :
84 mpFirstArg(std::move( pFirstArg
)),
85 mpSecondArg(std::move( pSecondArg
))
89 virtual bool isConstant() const override
92 mpFirstArg
->isConstant() &&
93 mpSecondArg
->isConstant();
97 std::shared_ptr
<ExpressionNode
> mpFirstArg
;
98 std::shared_ptr
<ExpressionNode
> mpSecondArg
;
101 class PlusExpression
: public BinaryExpressionBase
104 PlusExpression( const std::shared_ptr
<ExpressionNode
>& rFirstArg
,
105 const std::shared_ptr
<ExpressionNode
>& rSecondArg
) :
106 BinaryExpressionBase( rFirstArg
, rSecondArg
)
110 virtual double operator()( double t
) const override
112 return (*mpFirstArg
)(t
) + (*mpSecondArg
)(t
);
116 class MinusExpression
: public BinaryExpressionBase
119 MinusExpression( const std::shared_ptr
<ExpressionNode
>& rFirstArg
,
120 const std::shared_ptr
<ExpressionNode
>& rSecondArg
) :
121 BinaryExpressionBase( rFirstArg
, rSecondArg
)
125 virtual double operator()( double t
) const override
127 return (*mpFirstArg
)(t
) - (*mpSecondArg
)(t
);
131 class MultipliesExpression
: public BinaryExpressionBase
134 MultipliesExpression( const std::shared_ptr
<ExpressionNode
>& rFirstArg
,
135 const std::shared_ptr
<ExpressionNode
>& rSecondArg
) :
136 BinaryExpressionBase( rFirstArg
, rSecondArg
)
140 virtual double operator()( double t
) const override
142 return (*mpFirstArg
)(t
) * (*mpSecondArg
)(t
);
146 class DividesExpression
: public BinaryExpressionBase
149 DividesExpression( const std::shared_ptr
<ExpressionNode
>& rFirstArg
,
150 const std::shared_ptr
<ExpressionNode
>& rSecondArg
) :
151 BinaryExpressionBase( rFirstArg
, rSecondArg
)
155 virtual double operator()( double t
) const override
157 return (*mpFirstArg
)(t
) / (*mpSecondArg
)(t
);
161 class MinExpression
: public BinaryExpressionBase
164 MinExpression( const std::shared_ptr
<ExpressionNode
>& rFirstArg
,
165 const std::shared_ptr
<ExpressionNode
>& rSecondArg
) :
166 BinaryExpressionBase( rFirstArg
, rSecondArg
)
170 virtual double operator()( double t
) const override
172 return ::std::min( (*mpFirstArg
)(t
), (*mpSecondArg
)(t
) );
176 class MaxExpression
: public BinaryExpressionBase
179 MaxExpression( const std::shared_ptr
<ExpressionNode
>& rFirstArg
,
180 const std::shared_ptr
<ExpressionNode
>& rSecondArg
) :
181 BinaryExpressionBase( rFirstArg
, rSecondArg
)
185 virtual double operator()( double t
) const override
187 return ::std::max( (*mpFirstArg
)(t
), (*mpSecondArg
)(t
) );
192 std::shared_ptr
<ExpressionNode
> ExpressionNodeFactory::createConstantValueExpression( double rConstantValue
)
194 return std::make_shared
<ConstantValueExpression
>(rConstantValue
);
197 std::shared_ptr
<ExpressionNode
> ExpressionNodeFactory::createValueTExpression()
199 return std::make_shared
<TValueExpression
>();
202 std::shared_ptr
<ExpressionNode
> ExpressionNodeFactory::createPlusExpression( const std::shared_ptr
<ExpressionNode
>& rLHS
,
203 const std::shared_ptr
<ExpressionNode
>& rRHS
)
205 return std::make_shared
<PlusExpression
>(rLHS
, rRHS
);
208 std::shared_ptr
<ExpressionNode
> ExpressionNodeFactory::createMinusExpression( const std::shared_ptr
<ExpressionNode
>& rLHS
,
209 const std::shared_ptr
<ExpressionNode
>& rRHS
)
211 return std::make_shared
<MinusExpression
>(rLHS
, rRHS
);
214 std::shared_ptr
<ExpressionNode
> ExpressionNodeFactory::createMultipliesExpression( const std::shared_ptr
<ExpressionNode
>& rLHS
,
215 const std::shared_ptr
<ExpressionNode
>& rRHS
)
217 return std::make_shared
<MultipliesExpression
>(rLHS
, rRHS
);
220 std::shared_ptr
<ExpressionNode
> ExpressionNodeFactory::createDividesExpression( const std::shared_ptr
<ExpressionNode
>& rLHS
,
221 const std::shared_ptr
<ExpressionNode
>& rRHS
)
223 return std::make_shared
<DividesExpression
>(rLHS
, rRHS
);
226 std::shared_ptr
<ExpressionNode
> ExpressionNodeFactory::createMinExpression ( const std::shared_ptr
<ExpressionNode
>& rOuterFunction
,
227 const std::shared_ptr
<ExpressionNode
>& rInnerFunction
)
229 return std::make_shared
<MinExpression
>(rOuterFunction
, rInnerFunction
);
232 std::shared_ptr
<ExpressionNode
> ExpressionNodeFactory::createMaxExpression ( const std::shared_ptr
<ExpressionNode
>& rOuterFunction
,
233 const std::shared_ptr
<ExpressionNode
>& rInnerFunction
)
235 return std::make_shared
<MaxExpression
>(rOuterFunction
, rInnerFunction
);
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */