Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
NodeVisitor.h
1/*
2** Copyright 2007-2024, RTE (https://www.rte-france.com)
3** See AUTHORS.txt
4** SPDX-License-Identifier: MPL-2.0
5** This file is part of Antares-Simulator,
6** Adequacy and Performance assessment for interconnected energy networks.
7**
8** Antares_Simulator is free software: you can redistribute it and/or modify
9** it under the terms of the Mozilla Public Licence 2.0 as published by
10** the Mozilla Foundation, either version 2 of the License, or
11** (at your option) any later version.
12**
13** Antares_Simulator is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16** Mozilla Public Licence 2.0 for more details.
17**
18** You should have received a copy of the Mozilla Public Licence 2.0
19** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20*/
21#pragma once
22#include <functional>
23#include <optional>
24#include <typeindex>
25#include <vector>
26
27#include <antares/expressions/IName.h>
28#include <antares/expressions/nodes/Node.h>
29#include <antares/expressions/nodes/NodesForwardDeclaration.h>
30#include <antares/expressions/visitors/InvalidNode.h>
31
32namespace Antares::Expressions::Visitors
33{
34// we use LogSink because the inclusion of <antares/logs/logs.h> somehow results in the
35// inclusion of <windows.h> (very bad idea in a header!) which conflict with antlr4 headers (defines
36// in the former become enums in the latter etc...)
37struct LogSink
38{
39 using LogFunction = std::function<void(const std::string&)>;
40
41 LogFunction info;
42 LogFunction warning;
43 LogFunction error;
44};
45
46LogSink RedirectToAntaresLogs();
47
48template<class RetT, class VisitorT, class NodeT, class... Args>
49RetT tryVisit(const Nodes::Node* node, VisitorT& visitor, Args... args)
50{
51 auto* x = dynamic_cast<const NodeT*>(node);
52 return visitor.visit(x, args...);
53}
54
55template<class R, class... Args>
56class NodeVisitor;
57
58template<class R, class... Args>
60{
61 using FunctionT = R (*)(const Nodes::Node*, NodeVisitor<R, Args...>&, Args... args);
62
70 template<class... NodeTypes>
71 static auto NodesVisitList()
72 {
73 std::unordered_map<std::type_index, FunctionT> nodeDispatchFunctions;
74 (
75 [&nodeDispatchFunctions] {
76 nodeDispatchFunctions[typeid(NodeTypes)] = &tryVisit<R,
78 NodeTypes>;
79 }(),
80 ...);
81 return nodeDispatchFunctions;
82 }
83};
84
85template<class R, class... Args>
86class NodeVisitor: public IName
87{
88public:
89 virtual ~NodeVisitor() = default;
90
105 R dispatch(const Nodes::Node* node, Args... args)
106 {
107 if (!node)
108 {
109 throw InvalidNode("null");
110 }
111
112 const static auto nodeVisitList = NodeVisitsProvider<R, Args...>::template NodesVisitList<
128
129 try
130 {
131 return nodeVisitList.at(typeid(*node))(node, *this, args...);
132 }
133 catch (std::exception&)
134 {
135 log_.error("Antares::Expressions::Visitor: could not visit the node!");
136 throw;
137 }
138 }
139
148 virtual R visit(const Nodes::SumNode*, Args... args) = 0;
157 virtual R visit(const Nodes::SubtractionNode*, Args... args) = 0;
166 virtual R visit(const Nodes::MultiplicationNode*, Args... args) = 0;
175 virtual R visit(const Nodes::DivisionNode*, Args... args) = 0;
184 virtual R visit(const Nodes::EqualNode*, Args... args) = 0;
185
194 virtual R visit(const Nodes::LessThanOrEqualNode*, Args... args) = 0;
195
204 virtual R visit(const Nodes::GreaterThanOrEqualNode*, Args... args) = 0;
205
214 virtual R visit(const Nodes::NegationNode*, Args... args) = 0;
215
224 virtual R visit(const Nodes::LiteralNode*, Args... args) = 0;
225
234 virtual R visit(const Nodes::VariableNode*, Args... args) = 0;
235
244 virtual R visit(const Nodes::ParameterNode*, Args... args) = 0;
245
254 virtual R visit(const Nodes::PortFieldNode*, Args... args) = 0;
255
264 virtual R visit(const Nodes::PortFieldSumNode*, Args... args) = 0;
265
274 virtual R visit(const Nodes::ComponentVariableNode*, Args... args) = 0;
275
284 virtual R visit(const Nodes::ComponentParameterNode*, Args... args) = 0;
285
286private:
287 // we use LogSink because the inclusion of <antares/logs/logs.h> somehow results in the
288 // inclusion of <windows.h> (very bad idea in a header!) which conflict with antlr4 headers
289 // (defines in the former become enums in the latter etc...)
290 LogSink log_ = RedirectToAntaresLogs();
291};
292} // namespace Antares::Expressions::Visitors
Definition IName.h:27
Represents a component parameter node in a syntax tree.
Definition ComponentNode.h:80
Represents a component variable node in a syntax tree.
Definition ComponentNode.h:66
Represents a division node in a syntax tree.
Definition DivisionNode.h:31
Represents an equality comparison node in a syntax tree.
Definition EqualNode.h:31
Represents a greater than or equal comparison node in a syntax tree.
Definition GreaterThanOrEqualNode.h:31
Represents a less than or equal comparison node in a syntax tree.
Definition LessThanOrEqualNode.h:31
Represents a literal node in a syntax tree, storing a double value.
Definition LiteralNode.h:11
Represents a multiplication node in a syntax tree.
Definition MultiplicationNode.h:31
Represents a negation node in a syntax tree.
Definition NegationNode.h:31
Base class for nodes in a syntax tree.
Definition Node.h:30
Represents a parameter node in a syntax tree, storing a string value.
Definition ParameterNode.h:19
Represents a port field node in a syntax tree.
Definition PortFieldNode.h:33
Represents a port field node where the expression is a sum.
Definition PortFieldSumNode.h:33
Represents a subtraction node in a syntax tree.
Definition SubtractionNode.h:31
Represents a variable node in a syntax tree, storing a string value.
Definition VariableNode.h:20
virtual R visit(const Nodes::ComponentParameterNode *, Args... args)=0
Visits a ComponentParameterNode.
virtual R visit(const Nodes::PortFieldSumNode *, Args... args)=0
Visits a PortFieldSumNode.
virtual R visit(const Nodes::DivisionNode *, Args... args)=0
Visits a DivisionNode and processes its children.
virtual R visit(const Nodes::PortFieldNode *, Args... args)=0
Visits a PortFieldNode.
R dispatch(const Nodes::Node *node, Args... args)
Definition NodeVisitor.h:105
virtual R visit(const Nodes::MultiplicationNode *, Args... args)=0
Visits a MultiplicationNode and processes its children.
virtual R visit(const Nodes::LiteralNode *, Args... args)=0
Visits a LiteralNode.
virtual R visit(const Nodes::ComponentVariableNode *, Args... args)=0
Visits a ComponentVariableNode.
virtual R visit(const Nodes::GreaterThanOrEqualNode *, Args... args)=0
Visits a GreaterThanOrEqualNode and processes its children.
virtual R visit(const Nodes::NegationNode *, Args... args)=0
Visits a NegationNode and processes its child.
virtual R visit(const Nodes::VariableNode *, Args... args)=0
Visits a VariableNode.
virtual R visit(const Nodes::SumNode *, Args... args)=0
Visits a SumNode and processes its children.
virtual R visit(const Nodes::ParameterNode *, Args... args)=0
Visits a ParameterNode.
virtual R visit(const Nodes::LessThanOrEqualNode *, Args... args)=0
Visits a LessThanOrEqualNode and processes its children.
virtual R visit(const Nodes::SubtractionNode *, Args... args)=0
Visits a SubtractionNode and processes its children.
virtual R visit(const Nodes::EqualNode *, Args... args)=0
Visits an EqualNode and processes its children.
Definition NodeVisitor.h:38
static auto NodesVisitList()
Definition NodeVisitor.h:71