use insert function instead of for loop
[LibreOffice.git] / slideshow / source / engine / expressionnodefactory.cxx
blobf30b77eb0e5fee25dc11a8f89a20998ff0ec2695
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
23 #include <algorithm>
24 #include <utility>
27 /* Implementation of ExpressionNodeFactory class */
29 namespace slideshow::internal
31 namespace
33 class ConstantValueExpression : public ExpressionNode
35 public:
36 explicit ConstantValueExpression( double rValue ) :
37 maValue( rValue )
41 virtual double operator()( double /*t*/ ) const override
43 return maValue;
46 virtual bool isConstant() const override
48 return true;
51 private:
52 double maValue;
55 class TValueExpression : public ExpressionNode
57 public:
58 TValueExpression()
62 virtual double operator()( double t ) const override
64 return t;
67 virtual bool isConstant() const override
69 return false;
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
77 size).
79 class BinaryExpressionBase : public ExpressionNode
81 public:
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
91 return
92 mpFirstArg->isConstant() &&
93 mpSecondArg->isConstant();
96 protected:
97 std::shared_ptr<ExpressionNode> mpFirstArg;
98 std::shared_ptr<ExpressionNode> mpSecondArg;
101 class PlusExpression : public BinaryExpressionBase
103 public:
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
118 public:
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
133 public:
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
148 public:
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
163 public:
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
178 public:
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: */