From 0cc87016128b75f68d6acd1d7508a5f5b779adf2 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Wed, 5 Apr 2017 22:11:41 +0200
Subject: [PATCH] better error checking

---
 Numeric/mathEvaluator.cpp | 20 +++++++++++++-------
 Numeric/mathEvaluator.h   |  8 ++++----
 2 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/Numeric/mathEvaluator.cpp b/Numeric/mathEvaluator.cpp
index 1dea4e31a4..06500a01a2 100644
--- a/Numeric/mathEvaluator.cpp
+++ b/Numeric/mathEvaluator.cpp
@@ -8,8 +8,10 @@
 #if defined(HAVE_MATHEX)
 
 mathEvaluator::mathEvaluator(std::vector<std::string> &expressions,
-                             std::vector<std::string> &variables)
+                             const std::vector<std::string> &variables)
 {
+  static std::string lastError;
+
   _expressions.resize(expressions.size());
   _variables.resize(variables.size(), 0.);
   bool error = false;
@@ -22,11 +24,14 @@ mathEvaluator::mathEvaluator(std::vector<std::string> &expressions,
       _expressions[i]->parse();
     }
     catch(smlib::mathex::error e) {
-      Msg::Error(e.what());
-      std::string pos(_expressions[i]->stopposition(), ' ');
-      pos.push_back('^');
-      Msg::Error(expressions[i].c_str());
-      Msg::Error(pos.c_str());
+      if(e.what() + expressions[i] != lastError){
+        lastError = e.what() + expressions[i];
+        Msg::Error(e.what());
+        std::string pos(_expressions[i]->stopposition(), ' ');
+        pos.push_back('^');
+        Msg::Error(expressions[i].c_str());
+        Msg::Error(pos.c_str());
+      }
       error = true;
     }
   }
@@ -34,6 +39,7 @@ mathEvaluator::mathEvaluator(std::vector<std::string> &expressions,
     for(unsigned int i = 0; i < _expressions.size(); i++)
       delete(_expressions[i]);
     _expressions.clear();
+    expressions.clear();
   }
 }
 
@@ -43,7 +49,7 @@ mathEvaluator::~mathEvaluator()
     delete(_expressions[i]);
 }
 
-bool mathEvaluator::eval(std::vector<double> &values, std::vector<double> &res)
+bool mathEvaluator::eval(const std::vector<double> &values, std::vector<double> &res)
 {
   if(values.size() != _variables.size()){
     Msg::Error("Given %d value(s) for %d variable(s)", values.size(), _variables.size());
diff --git a/Numeric/mathEvaluator.h b/Numeric/mathEvaluator.h
index 310cc4b523..93aac398b9 100644
--- a/Numeric/mathEvaluator.h
+++ b/Numeric/mathEvaluator.h
@@ -24,11 +24,11 @@ class mathEvaluator{
   // variables. If an error occurs the vector of expressions is
   // cleared.
   mathEvaluator(std::vector<std::string> &expressions,
-                std::vector<std::string> &variables);
+                const std::vector<std::string> &variables);
   ~mathEvaluator();
   // evaluate the expression(s) using the given values and fill the
   // result vector. Returns true if the evaluation succeeded.
-  bool eval(std::vector<double> &values, std::vector<double> &res);
+  bool eval(const std::vector<double> &values, std::vector<double> &res);
 };
 
 #else
@@ -36,14 +36,14 @@ class mathEvaluator{
 class mathEvaluator{
  public:
   mathEvaluator(std::vector<std::string> &expressions,
-                std::vector<std::string> &variables)
+                const std::vector<std::string> &variables)
   {
     Msg::Error("Gmsh must be compiled with MathEx support to evaluate math "
                "expressions");
     expressions.clear();
   }
   ~mathEvaluator(){}
-  bool eval(std::vector<double> &values, std::vector<double> &res)
+  bool eval(const std::vector<double> &values, std::vector<double> &res)
   {
     return false;
   }
-- 
GitLab