> -----Ursprüngliche Nachricht-----
> Von: Gert Wollny [mailto:[email protected]]
> Gesendet: Donnerstag, 27. Juni 2013 10:46
> An: Florian Dommert
> Betreff: Re: AW: [NLopt-discuss] unsigned vs size_t in add_constraints
> 
> Hello,
> 
> > So just removing the try--catch statement and leaving only the
> > if-statement is fine?
> 
> Yes, that should be fine. Also note that the methods in nlopt are all
named using
> lowercase+underscore , so  convertSize_t should probably be called
> convert_size_t_to_unsigned.
> 

After this insightful comments, thank you very much Gert, I have rewritten
the patch and now also took care, that I edited nlopt.hpp-in.

> Personally, I would throw an std::invalid_argument instead of a
runtime_error,
> but in the end that's probably just a matter of taste.
> 
> >I have not much experience with exception handling so far.
> 
> Well, the basic idea is that an exception is something that can not
> (easily) be handled by normal flow control and/or happens very rarely.
> For instance  constructors have no return value, therefore, errors in
constructing
> an object are best reported by an exception. Or if you are deep inside a
calling
> chain propagating an error with return values is quite cumbersome,
throwing an
> exception and catching it where it actually makes sense to handle it makes
the
> code easier to read - especially if the error about something that usually
doesn't
> happen.
> 
> Best regards,
> Gert
> 
> 
> 

diff -Naur api//nlopt-in.hpp my_api/nlopt-in.hpp
--- api//nlopt-in.hpp   2011-05-26 19:49:53 +0200
+++ my_api/nlopt-in.hpp 2013-06-27 11:10:58 +0200
@@ -258,6 +258,15 @@
     result last_optimize_result() const { return last_result; }
     double last_optimum_value() const { return last_optf; }
 
+       unsigned convert_size_t_to_unsigned(size_t invalue) {
+       
+                       if(static_cast<unsigned>(invalue)!=invalue)
+                               throw std::runtime_error("too many 
constraints");
+                       else
+                               return static_cast<unsigned>(invalue);          
+       
+       }
+
     // accessors:
     algorithm get_algorithm() const {
       if (!o) throw std::runtime_error("uninitialized nlopt::opt");
@@ -350,7 +359,8 @@
       if (!d) throw std::bad_alloc();
       d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
       d->munge_destroy = d->munge_copy = NULL;
-      mythrow(nlopt_add_inequality_mconstraint(o, tol.size(), mymfunc, d, 
+         unsigned tolsize=convert_size_t_to_unsigned(tol.size());
+      mythrow(nlopt_add_inequality_mconstraint(o, tolsize, mymfunc, d, 
                                               tol.empty() ? NULL : &tol[0]));
     }
 
@@ -379,7 +389,8 @@
       if (!d) throw std::bad_alloc();
       d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
       d->munge_destroy = d->munge_copy = NULL;
-      mythrow(nlopt_add_equality_mconstraint(o, tol.size(), mymfunc, d, 
+         unsigned tolsize=convert_size_t_to_unsigned(tol.size());
+      mythrow(nlopt_add_equality_mconstraint(o, tolsize, mymfunc, d, 
                                             tol.empty() ? NULL : &tol[0]));
     }
 
@@ -409,7 +420,9 @@
       if (!d) throw std::bad_alloc();
       d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
       d->munge_destroy = md; d->munge_copy = mc;
-      mythrow(nlopt_add_inequality_mconstraint(o, tol.size(), mymfunc, d, 
+         unsigned tolsize=convert_size_t_to_unsigned(tol.size());
+
+      mythrow(nlopt_add_inequality_mconstraint(o, tolsize, mymfunc, d, 
                                               tol.empty() ? NULL : &tol[0]));
     }
     void add_equality_mconstraint(mfunc mf, void *f_data, 
@@ -419,7 +432,8 @@
       if (!d) throw std::bad_alloc();
       d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
       d->munge_destroy = md; d->munge_copy = mc;
-      mythrow(nlopt_add_equality_mconstraint(o, tol.size(), mymfunc, d, 
+         unsigned tolsize=convert_size_t_to_unsigned(tol.size());
+      mythrow(nlopt_add_equality_mconstraint(o, tolsize, mymfunc, d, 
                                             tol.empty() ? NULL : &tol[0]));
     }
 
diff -Naur api//nlopt.hpp my_api/nlopt.hpp
--- api//nlopt.hpp      2012-07-20 21:47:07 +0200
+++ my_api/nlopt.hpp    2013-06-27 11:11:38 +0200
@@ -128,6 +128,8 @@
     forced_stop() : std::runtime_error("nlopt forced stop") {}
   };
 
+  
+
   //////////////////////////////////////////////////////////////////////
 
   class opt {
@@ -317,6 +319,15 @@
     result last_optimize_result() const { return last_result; }
     double last_optimum_value() const { return last_optf; }
 
+       unsigned convert_size_t_to_unsigned(size_t invalue) {
+       
+                       if(static_cast<unsigned>(invalue)!=invalue)
+                               throw std::runtime_error("too many 
constraints");
+                       else
+                               return static_cast<unsigned>(invalue);          
+       
+       }
+
     // accessors:
     algorithm get_algorithm() const {
       if (!o) throw std::runtime_error("uninitialized nlopt::opt");
@@ -409,7 +420,9 @@
       if (!d) throw std::bad_alloc();
       d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
       d->munge_destroy = d->munge_copy = NULL;
-      mythrow(nlopt_add_inequality_mconstraint(o, tol.size(), mymfunc, d, 
+         unsigned tolsize=convert_size_t_to_unsigned(tol.size());
+         
+      mythrow(nlopt_add_inequality_mconstraint(o, tolsize, mymfunc, d, 
                                               tol.empty() ? NULL : &tol[0]));
     }
 
@@ -438,7 +451,9 @@
       if (!d) throw std::bad_alloc();
       d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
       d->munge_destroy = d->munge_copy = NULL;
-      mythrow(nlopt_add_equality_mconstraint(o, tol.size(), mymfunc, d, 
+         unsigned tolsize=convert_size_t_to_unsigned(tol.size());
+
+      mythrow(nlopt_add_equality_mconstraint(o, tolsize, mymfunc, d, 
                                             tol.empty() ? NULL : &tol[0]));
     }
 
@@ -468,7 +483,8 @@
       if (!d) throw std::bad_alloc();
       d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
       d->munge_destroy = md; d->munge_copy = mc;
-      mythrow(nlopt_add_inequality_mconstraint(o, tol.size(), mymfunc, d, 
+         unsigned tolsize=convert_size_t_to_unsigned(tol.size());
+      mythrow(nlopt_add_inequality_mconstraint(o, tolsize, mymfunc, d, 
                                               tol.empty() ? NULL : &tol[0]));
     }
     void add_equality_mconstraint(mfunc mf, void *f_data, 
@@ -478,7 +494,8 @@
       if (!d) throw std::bad_alloc();
       d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
       d->munge_destroy = md; d->munge_copy = mc;
-      mythrow(nlopt_add_equality_mconstraint(o, tol.size(), mymfunc, d, 
+         unsigned tolsize=convert_size_t_to_unsigned(tol.size());
+      mythrow(nlopt_add_equality_mconstraint(o, tolsize, mymfunc, d, 
                                             tol.empty() ? NULL : &tol[0]));
     }
 
_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss

Reply via email to