From 24f1276b4288a4d30414fbed234fbc80b486c954 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 10 Oct 2018 13:03:52 +0200
Subject: [PATCH] Make all DataTree::*_map private

Introducing a new DataTree::getVariable() const method was necessary in the process.
---
 src/DataTree.cc           | 12 ++++++++++++
 src/DataTree.hh           |  7 ++++++-
 src/ModelEquationBlock.cc | 14 +++-----------
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/DataTree.cc b/src/DataTree.cc
index f63b9ede..19bc8fa0 100644
--- a/src/DataTree.cc
+++ b/src/DataTree.cc
@@ -148,6 +148,18 @@ DataTree::AddVariable(int symb_id, int lag)
   return p;
 }
 
+VariableNode *
+DataTree::getVariable(int symb_id, int lag) const
+{
+  auto it = variable_node_map.find({ symb_id, lag });
+  if (it == variable_node_map.end())
+    {
+      cerr << "DataTree::getVariable: unknown variable node for symb_id=" << symb_id << " and lag=" << lag << endl;
+      exit(EXIT_FAILURE);
+    }
+  return it->second;
+}
+
 bool
 DataTree::ParamUsedWithLeadLagInternal() const
 {
diff --git a/src/DataTree.hh b/src/DataTree.hh
index cbfaca81..8df7aed7 100644
--- a/src/DataTree.hh
+++ b/src/DataTree.hh
@@ -48,7 +48,7 @@ public:
   //! Is it possible to use leads/lags on variable nodes?
   const bool is_dynamic;
 
-protected:
+private:
   //! num_constant_id -> NumConstNode
   using num_const_node_map_t = map<int, NumConstNode *>;
   num_const_node_map_t num_const_node_map;
@@ -89,6 +89,7 @@ protected:
   using second_deriv_external_function_node_map_t = map<tuple<vector<expr_t>, int, int, int>, SecondDerivExternalFunctionNode *>;
   second_deriv_external_function_node_map_t second_deriv_external_function_node_map;
 
+protected:
   //! Stores local variables value (maps symbol ID to corresponding node)
   map<int, expr_t> local_variables_table;
   //! Stores the order of appearance of local variables in the model block. Needed following change in #563
@@ -153,6 +154,10 @@ public:
   expr_t AddNonNegativeConstant(const string &value);
   //! Adds a variable
   VariableNode *AddVariable(int symb_id, int lag = 0);
+  //! Gets a variable
+  /*! Same as AddVariable, except that it fails if the variable node has not
+      already been created */
+  VariableNode *getVariable(int symb_id, int lag = 0) const;
   //! Adds "arg1+arg2" to model tree
   expr_t AddPlus(expr_t iArg1, expr_t iArg2);
   //! Adds "arg1-arg2" to model tree
diff --git a/src/ModelEquationBlock.cc b/src/ModelEquationBlock.cc
index 7af50a3a..edb05c0d 100644
--- a/src/ModelEquationBlock.cc
+++ b/src/ModelEquationBlock.cc
@@ -219,9 +219,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model
         output << "[";
       for (size_t j = 0; j < symb_ids.size(); j++)
         {
-          auto it = variable_node_map.find({ symb_ids[j], 0 });
-          assert(it != variable_node_map.end());
-          dynamic_cast<ExprNode *>(it->second)->writeOutput(output, output_type);
+          getVariable(symb_ids[j])->ExprNode::writeOutput(output, output_type);
           if (j < symb_ids.size()-1)
             output << ",";
         }
@@ -269,11 +267,8 @@ SteadyStateModel::writeJsonSteadyStateFile(ostream &output, bool transformComput
         {
           if (j != 0)
             output << ",";
-          auto it =
-            variable_node_map.find({ symb_ids[j], 0 });
-          assert(it != variable_node_map.end());
           output << "\"";
-          dynamic_cast<ExprNode *>(it->second)->writeJsonOutput(output, {}, {}, false);
+          getVariable(symb_ids[j])->writeJsonOutput(output, {}, {}, false);
           output << "\"";
         }
       if (symb_ids.size() > 1)
@@ -401,11 +396,8 @@ Epilogue::writeEpilogueFile(const string &basename) const
   output << endl;
   for (const auto & it : def_table)
     {
-      auto node = variable_node_map.find({ it.first, 0 });
-      assert(node != variable_node_map.end());
-
       output << "    ";
-      dynamic_cast<ExprNode *>(node->second)->writeOutput(output, output_type);
+      getVariable(it.first)->ExprNode::writeOutput(output, output_type);
       output << " = ";
       it.second->writeOutput(output, output_type, temporary_terms, temporary_terms_idxs, tef_terms);
       output << ";" << endl;
-- 
GitLab