Debian Codegraph @

kiwi/expression.h

raw
1 /*-----------------------------------------------------------------------------
2 | Copyright (c) 2013-2026, Nucleic Development Team.
3 |
4 | Distributed under the terms of the Modified BSD License.
5 |
6 | The full license is in the file LICENSE, distributed with this software.
7 |----------------------------------------------------------------------------*/
8 #pragma once
9 #include <vector>
10 #include "term.h"
11
12 namespace kiwi
13 {
14
15 class Expression
16 {
17
18 public:
19 Expression(double constant = 0.0) : m_constant(constant) {}
20
21 Expression(const Term &term, double constant = 0.0) : m_terms(1, term), m_constant(constant) {}
22
23 Expression(std::vector<Term> terms, double constant = 0.0) : m_terms(std::move(terms)), m_constant(constant) {}
24
25 Expression(const Expression&) = default;
26
27 // Could be marked noexcept but for a bug in the GCC of the manylinux1 image
28 Expression(Expression&&) = default;
29
30 ~Expression() = default;
31
32 const std::vector<Term> &terms() const
33 {
34 return m_terms;
35 }
36
37 double constant() const
38 {
39 return m_constant;
40 }
41
42 double value() const
43 {
44 double result = m_constant;
45
46 for (const Term &term : m_terms)
47 result += term.value();
48
49 return result;
50 }
51
52 Expression& operator=(const Expression&) = default;
53
54 // Could be marked noexcept but for a bug in the GCC of the manylinux1 image
55 Expression& operator=(Expression&&) = default;
56
57 private:
58 std::vector<Term> m_terms;
59 double m_constant;
60 };
61
62 } // namespace kiwi