From ae97c4eb294967bf495465411b20af75cd58fe21 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 17 May 2022 18:08:10 +0200
Subject: [PATCH] Factorization in OptionsList class

---
 src/Statement.cc | 72 ++++++++++--------------------------------------
 src/Statement.hh |  2 ++
 2 files changed, 16 insertions(+), 58 deletions(-)

diff --git a/src/Statement.cc b/src/Statement.cc
index 08fcd6a7..5720f838 100644
--- a/src/Statement.cc
+++ b/src/Statement.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2006-2021 Dynare Team
+ * Copyright © 2006-2022 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -158,63 +158,7 @@ VerbatimStatement::writeJsonOutput(ostream &output) const
 void
 OptionsList::writeOutput(ostream &output) const
 {
-  for (const auto & [name, val] : num_options)
-    output << "options_." << name << " = " << val << ";" << endl;
-
-  for (const auto & [name, vals] : paired_num_options)
-    output << "options_." << name << " = [" << vals.first << "; "
-           << vals.second << "];" << endl;
-
-  for (const auto & [name, val] : string_options)
-    output << "options_." << name << " = '" << val << "';" << endl;
-
-  for (const auto & [name, val] : date_options)
-    output << "options_." << name << " = " << val << ";" << endl;
-
-  for (const auto & [name, list] : symbol_list_options)
-    list.writeOutput("options_." + name, output);
-
-  for (const auto & [name, vals] : vector_int_options)
-    {
-      output << "options_." << name << " = ";
-      if (vals.size() > 1)
-        {
-          output << "[";
-          for (int viit : vals)
-            output << viit << ";";
-          output << "];" << endl;
-        }
-      else
-        output << vals.front() << ";" << endl;
-    }
-
-  for (const auto & [name, vals] : vector_str_options)
-    {
-      output << "options_." << name << " = ";
-      if (vals.size() > 1)
-        {
-          output << "{";
-          for (const auto &viit : vals)
-            output << "'" << viit << "';";
-          output << "};" << endl;
-        }
-      else
-        output << vals.front() << ";" << endl;
-    }
-  
-  /* vector_cellstr_options should ideally be merged into vector_str_options
-     only difference is treatment of vals.size==1, where vector_str_options
-     does not add quotes and curly brackets, i.e. allows for type conversion of
-     '2' into the number 2 
-  */
- 
-  for (const auto & [name, vals] : vector_cellstr_options)
-    {
-      output << "options_." << name << " = {";
-      for (const auto &viit : vals)
-        output << "'" << viit << "';";
-      output << "};" << endl;
-    }
+  writeOutputCommon(output, "options_");
 }
 
 void
@@ -231,6 +175,12 @@ OptionsList::writeOutput(ostream &output, const string &option_group) const
   else
     output << option_group << " = struct();" << endl;
 
+  writeOutputCommon(output, option_group);
+}
+
+void
+OptionsList::writeOutputCommon(ostream &output, const string &option_group) const
+{
   for (const auto & [name, val] : num_options)
     output << option_group << "." << name << " = " << val << ";" << endl;
 
@@ -275,6 +225,12 @@ OptionsList::writeOutput(ostream &output, const string &option_group) const
         output << vals.front() << ";" << endl;
     }
 
+  /* vector_cellstr_options should ideally be merged into vector_str_options
+     only difference is treatment of vals.size==1, where vector_str_options
+     does not add quotes and curly brackets, i.e. allows for type conversion of
+     '2' into the number 2
+  */
+
   for (const auto & [name, vals] : vector_cellstr_options)
     {
       output << option_group << "." << name << " = {";
diff --git a/src/Statement.hh b/src/Statement.hh
index 58e3e9d5..cf680cae 100644
--- a/src/Statement.hh
+++ b/src/Statement.hh
@@ -230,6 +230,8 @@ public:
   void writeOutput(ostream &output, const string &option_group) const;
   void writeJsonOutput(ostream &output) const;
   void clear();
+private:
+  void writeOutputCommon(ostream &output, const string &option_group) const;
 };
 
 #endif // ! _STATEMENT_HH
-- 
GitLab