Antares Simulator
Power System Simulator
NodeVisitor.h
1 /*
2 ** Copyright 2007-2025, 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 <typeindex>
23 #include <unordered_map>
24 
25 #include <antares/expressions/IName.h>
26 #include <antares/expressions/nodes/Node.h>
27 #include <antares/expressions/nodes/NodesForwardDeclaration.h>
28 #include <antares/expressions/visitors/InvalidNode.h>
29 
30 namespace Antares::Expressions::Visitors
31 {
32 // we use logError because the inclusion of <antares/logs/logs.h> somehow results in the
33 // inclusion of <windows.h> (very bad idea in a header!) which conflict with antlr4 headers (defines
34 // in the former become enums in the latter etc...)
35 void logError(const std::string& msg);
36 
37 template<class RetT, class VisitorT, class NodeT, class... Args>
38 RetT tryVisit(const Nodes::Node* node, VisitorT& visitor, Args... args)
39 {
40  auto* x = dynamic_cast<const NodeT*>(node);
41  return visitor.visit(x, args...);
42 }
43 
44 template<class R, class... Args>
45 class NodeVisitor;
46 
47 template<class R, class... Args>
49 {
50  using FunctionT = R (*)(const Nodes::Node*, NodeVisitor<R, Args...>&, Args... args);
51 
59  template<class... NodeTypes>
60  static auto NodesVisitList()
61  {
62  std::unordered_map<std::type_index, FunctionT> nodeDispatchFunctions;
63  (
64  [&nodeDispatchFunctions] {
65  nodeDispatchFunctions[typeid(NodeTypes)] = &tryVisit<R,
67  NodeTypes>;
68  }(),
69  ...);
70  return nodeDispatchFunctions;
71  }
72 };
73 
74 template<class R, class... Args>
75 class NodeVisitor: public IName
76 {
77 public:
78  ~NodeVisitor() override = default;
79 
94  R dispatch(const Nodes::Node* node, Args... args)
95  {
96  if (!node)
97  {
98  throw InvalidNode("null");
99  }
100 
101  const static auto nodeVisitList = NodeVisitsProvider<R, Args...>::template NodesVisitList<
120 
121  try
122  {
123  return nodeVisitList.at(typeid(*node))(node, *this, args...);
124  }
125  catch (std::exception& e)
126  {
127  using namespace std::string_literals;
128  logError("Antares::Expressions::Visitor: could not visit the node! "s + e.what());
129  throw;
130  }
131  }
132 
141  virtual R visit(const Nodes::SumNode*, Args... args) = 0;
150  virtual R visit(const Nodes::SubtractionNode*, Args... args) = 0;
159  virtual R visit(const Nodes::MultiplicationNode*, Args... args) = 0;
168  virtual R visit(const Nodes::DivisionNode*, Args... args) = 0;
177  virtual R visit(const Nodes::EqualNode*, Args... args) = 0;
178 
187  virtual R visit(const Nodes::LessThanOrEqualNode*, Args... args) = 0;
188 
197  virtual R visit(const Nodes::GreaterThanOrEqualNode*, Args... args) = 0;
198 
207  virtual R visit(const Nodes::NegationNode*, Args... args) = 0;
208 
217  virtual R visit(const Nodes::LiteralNode*, Args... args) = 0;
218 
227  virtual R visit(const Nodes::VariableNode*, Args... args) = 0;
228 
237  virtual R visit(const Nodes::ParameterNode*, Args... args) = 0;
238 
247  virtual R visit(const Nodes::PortFieldNode*, Args... args) = 0;
248 
257  virtual R visit(const Nodes::PortFieldSumNode*, Args... args) = 0;
258 
267  virtual R visit(const Nodes::TimeShiftNode*, Args... args) = 0;
268 
277  virtual R visit(const Nodes::TimeIndexNode*, Args... args) = 0;
286  virtual R visit(const Nodes::TimeSumNode*, Args... args) = 0;
295  virtual R visit(const Nodes::AllTimeSumNode*, Args... args) = 0;
296 
305  virtual R visit(const Nodes::FunctionNode*, Args... args) = 0;
306 };
307 
308 } // namespace Antares::Expressions::Visitors
Definition: IName.h:27
Represents a AllTimeSumNode node in a syntax tree.
Definition: AllTimeSumNode.h:31
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
AST node representing a function expression (max, min, pow, ...).
Definition: FunctionNode.h:63
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:14
Represents a port field node in a syntax tree.
Definition: PortFieldNode.h:32
Represents a port field node where the expression is a sum.
Definition: PortFieldSumNode.h:32
Represents a subtraction node in a syntax tree.
Definition: SubtractionNode.h:31
Definition: SumNode.h:29
Definition: TimeIndexNode.h:28
Definition: TimeShiftNode.h:29
Definition: TimeSumNode.h:28
Represents a variable node in a syntax tree, storing a string value.
Definition: VariableNode.h:20
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::TimeSumNode *, Args... args)=0
Visits a TimeSumNode.
virtual R visit(const Nodes::TimeIndexNode *, Args... args)=0
Visits a TimeIndexNode.
virtual R visit(const Nodes::AllTimeSumNode *, Args... args)=0
Visits a AllTimeSumNode.
virtual R visit(const Nodes::PortFieldNode *, Args... args)=0
Visits a PortFieldNode.
R dispatch(const Nodes::Node *node, Args... args)
Definition: NodeVisitor.h:94
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::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::FunctionNode *, Args... args)=0
Visits a FunctionNode.
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::TimeShiftNode *, Args... args)=0
Visits a TimeShiftNode.
virtual R visit(const Nodes::EqualNode *, Args... args)=0
Visits an EqualNode and processes its children.
static auto NodesVisitList()
Definition: NodeVisitor.h:60