From 8b441b72a90508e00db5534685e8b4a2ecb8fbde Mon Sep 17 00:00:00 2001 From: Christophe Geuzaine <cgeuzaine@uliege.be> Date: Thu, 24 Mar 2022 17:13:19 +0100 Subject: [PATCH] always throw std::runtime_error() --- api/GenApi.py | 7 +- api/gmsh.h_cwrap | 7 +- contrib/onelab/OnelabClients.cpp | 4 +- doc/texinfo/api.texi | 6 +- src/common/GmshSocket.h | 17 +- src/common/gmshLocalNetworkClient.cpp | 6 +- src/fltk/pluginWindow.cpp | 2 +- src/geo/SOrientedBoundingBox.cpp | 4 +- src/mesh/DivideAndConquer.cpp | 8 +- src/mesh/meshGFace.cpp | 8 +- src/numeric/HierarchicalBasisH1Brick.cpp | 7 +- src/numeric/HierarchicalBasisH1Line.cpp | 7 +- src/numeric/HierarchicalBasisH1Pri.cpp | 9 +- src/numeric/HierarchicalBasisH1Quad.cpp | 9 +- src/numeric/HierarchicalBasisH1Tetra.cpp | 8 +- src/numeric/HierarchicalBasisH1Tria.cpp | 9 +- src/numeric/HierarchicalBasisHcurlBrick.cpp | 5 +- src/numeric/HierarchicalBasisHcurlBrick.h | 7 +- src/numeric/HierarchicalBasisHcurlLine.cpp | 7 +- src/numeric/HierarchicalBasisHcurlLine.h | 7 +- src/numeric/HierarchicalBasisHcurlPri.cpp | 13 +- src/numeric/HierarchicalBasisHcurlPri.h | 11 +- src/numeric/HierarchicalBasisHcurlQuad.cpp | 6 +- src/numeric/HierarchicalBasisHcurlQuad.h | 7 +- src/numeric/HierarchicalBasisHcurlTetra.cpp | 10 +- src/numeric/HierarchicalBasisHcurlTetra.h | 9 +- src/numeric/HierarchicalBasisHcurlTria.cpp | 7 +- src/numeric/HierarchicalBasisHcurlTria.h | 9 +- src/numeric/OrthogonalPoly.cpp | 17 +- src/numeric/OrthogonalPoly.h | 6 + src/plugin/PluginManager.cpp | 13 +- utils/solvers/c++/GmshSocket.h | 202 ++++++++++---------- 32 files changed, 251 insertions(+), 203 deletions(-) diff --git a/api/GenApi.py b/api/GenApi.py index 69c9009e1b..93b0eb2b64 100644 --- a/api/GenApi.py +++ b/api/GenApi.py @@ -1050,6 +1050,7 @@ cwrap_header = """// {0} #include <string> #include <utility> #include <functional> +#include <stdexcept> #ifndef M_PI #define M_PI (3.14159265358979323846) @@ -1573,7 +1574,7 @@ class API: if name == 'getLastError': # special case for getLastError() function fcwrap.write( indent + " " + - 'if(ierr) throw "Could not get last error";\n') + 'if(ierr) throw std::runtime_error("Could not get last error");\n') else: fcwrap.write(indent + " " + "if(ierr) throwLastError();\n") @@ -1621,11 +1622,11 @@ class API: fcwrap.write( ' gmshLoggerGetLastError(&api_error_, &ierr);\n') fcwrap.write( - ' if(ierr) throw "Could not get last error";\n') + ' if(ierr) throw std::runtime_error("Could not get last error");\n') fcwrap.write( ' std::string error = std::string(api_error_);\n') fcwrap.write(' gmshFree(api_error_);\n') - fcwrap.write(' throw error;\n') + fcwrap.write(' throw std::runtime_error(error);\n') fcwrap.write(" }\n\n") fcwrap.write("}\n\n") for module in self.modules: diff --git a/api/gmsh.h_cwrap b/api/gmsh.h_cwrap index 7336645671..ce2ba6c1cc 100644 --- a/api/gmsh.h_cwrap +++ b/api/gmsh.h_cwrap @@ -26,6 +26,7 @@ #include <string> #include <utility> #include <functional> +#include <stdexcept> #ifndef M_PI #define M_PI (3.14159265358979323846) @@ -112,10 +113,10 @@ namespace gmsh { int ierr = 0; char *api_error_; gmshLoggerGetLastError(&api_error_, &ierr); - if(ierr) throw "Could not get last error"; + if(ierr) throw std::runtime_error("Could not get last error"); std::string error = std::string(api_error_); gmshFree(api_error_); - throw error; + throw std::runtime_error(error); } } @@ -5747,7 +5748,7 @@ namespace gmsh { // Top-level functions int ierr = 0; char *api_error_; gmshLoggerGetLastError(&api_error_, &ierr); - if(ierr) throw "Could not get last error"; + if(ierr) throw std::runtime_error("Could not get last error"); error = std::string(api_error_); gmshFree(api_error_); } diff --git a/contrib/onelab/OnelabClients.cpp b/contrib/onelab/OnelabClients.cpp index 5f62b3db88..523d1d1b45 100644 --- a/contrib/onelab/OnelabClients.cpp +++ b/contrib/onelab/OnelabClients.cpp @@ -335,8 +335,8 @@ bool localNetworkSolverClient::run() try { sock = socketConnection->Start(exe.c_str(), command.c_str(), sockname.c_str(), 10); - } catch(const char *err) { - OLMsg::Error("%s (on socket '%s')", err, sockname.c_str()); + } catch(std::runtime_error &e) { + OLMsg::Error("%s (on socket '%s')", e.what(), sockname.c_str()); sock = -1; } diff --git a/doc/texinfo/api.texi b/doc/texinfo/api.texi index 1731c3cef8..e16279fa69 100644 --- a/doc/texinfo/api.texi +++ b/doc/texinfo/api.texi @@ -3173,7 +3173,7 @@ Set the numerical list option @code{option} to value @code{value} for field @item Language-specific definition: @url{@value{GITLAB-PREFIX}/api/gmsh.h#L1788,C++}, @url{@value{GITLAB-PREFIX}/api/gmshc.h#L1572,C}, @url{@value{GITLAB-PREFIX}/api/gmsh.py#L4162,Python}, @url{@value{GITLAB-PREFIX}/api/gmsh.jl#L3601,Julia} @item Examples: -C++ (@url{@value{GITLAB-PREFIX}/tutorials/c++/t10.cpp#L48,t10.cpp}), Python (@url{@value{GITLAB-PREFIX}/tutorials/python/t10.py#L44,t10.py}, @url{@value{GITLAB-PREFIX}/examples/api/extend_field.py#L23,extend_field.py}, @url{@value{GITLAB-PREFIX}/examples/api/naca_boundary_layer_2d.py#L106,naca_boundary_layer_2d.py}) +C++ (@url{@value{GITLAB-PREFIX}/tutorials/c++/t10.cpp#L48,t10.cpp}), Python (@url{@value{GITLAB-PREFIX}/tutorials/python/t10.py#L44,t10.py}, @url{@value{GITLAB-PREFIX}/examples/api/extend_field.py#L23,extend_field.py}, @url{@value{GITLAB-PREFIX}/examples/api/naca_boundary_layer_2d.py#L106,naca_boundary_layer_2d.py}, @url{@value{GITLAB-PREFIX}/examples/api/ocean.py#L4544,ocean.py}) @end table @item gmsh/model/mesh/field/getNumbers @@ -3552,7 +3552,7 @@ integer value @item Language-specific definition: @url{@value{GITLAB-PREFIX}/api/gmsh.h#L2001,C++}, @url{@value{GITLAB-PREFIX}/api/gmshc.h#L1757,C}, @url{@value{GITLAB-PREFIX}/api/gmsh.py#L4633,Python}, @url{@value{GITLAB-PREFIX}/api/gmsh.jl#L4017,Julia} @item Examples: -Python (@url{@value{GITLAB-PREFIX}/examples/api/ocean.py#L4,ocean.py}) +Python (@url{@value{GITLAB-PREFIX}/examples/api/ocean.py#L9,ocean.py}) @end table @item gmsh/model/geo/addPointOnGeometry @@ -3573,7 +3573,7 @@ integer value @item Language-specific definition: @url{@value{GITLAB-PREFIX}/api/gmsh.h#L2013,C++}, @url{@value{GITLAB-PREFIX}/api/gmshc.h#L1768,C}, @url{@value{GITLAB-PREFIX}/api/gmsh.py#L4662,Python}, @url{@value{GITLAB-PREFIX}/api/gmsh.jl#L4038,Julia} @item Examples: -Python (@url{@value{GITLAB-PREFIX}/examples/api/ocean.py#L6,ocean.py}) +Python (@url{@value{GITLAB-PREFIX}/examples/api/ocean.py#L11,ocean.py}) @end table @item gmsh/model/geo/extrude diff --git a/src/common/GmshSocket.h b/src/common/GmshSocket.h index 2ccbc0e868..e7339af087 100644 --- a/src/common/GmshSocket.h +++ b/src/common/GmshSocket.h @@ -28,6 +28,7 @@ #include "GmshConfig.h" #include <string> +#include <stdexcept> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -361,7 +362,7 @@ public: unlink(_sockname.c_str()); // create a socket tmpsock = socket(PF_UNIX, SOCK_STREAM, 0); - if(tmpsock < 0) throw "Couldn't create socket"; + if(tmpsock < 0) throw std::runtime_error("Couldn't create socket"); // bind the socket to its name struct sockaddr_un addr_un; memset((char *)&addr_un, 0, sizeof(addr_un)); @@ -369,12 +370,12 @@ public: addr_un.sun_family = AF_UNIX; if(bind(tmpsock, (struct sockaddr *)&addr_un, sizeof(addr_un)) < 0) { CloseSocket(tmpsock); - throw "Couldn't bind socket to name"; + throw std::runtime_error("Couldn't bind socket to name"); } // change permissions on the socket name in case it has to be rm'd later chmod(_sockname.c_str(), 0666); #else - throw "Unix sockets not available on Windows"; + throw std::runtime_error("Unix sockets not available on Windows"); #endif } else { @@ -393,7 +394,7 @@ public: #else if(tmpsock == (int)INVALID_SOCKET) #endif - throw "Couldn't create socket"; + throw std::runtime_error("Couldn't create socket"); // bind the socket to its name struct sockaddr_in addr_in; memset((char *)&addr_in, 0, sizeof(addr_in)); @@ -402,7 +403,7 @@ public: addr_in.sin_port = htons(_portno); // random assign if _portno == 0 if(bind(tmpsock, (struct sockaddr *)&addr_in, sizeof(addr_in)) < 0) { CloseSocket(tmpsock); - throw "Couldn't bind socket to name"; + throw std::runtime_error("Couldn't bind socket to name"); } if(!_portno) { // retrieve name if randomly assigned port socklen_t addrlen = sizeof(addr_in); @@ -428,14 +429,14 @@ public: // them automatically rejected) if(listen(tmpsock, 20)) { CloseSocket(tmpsock); - throw "Socket listen failed"; + throw std::runtime_error("Socket listen failed"); } // wait until we get data int ret = NonBlockingWait(0.001, timeout, tmpsock); if(ret) { CloseSocket(tmpsock); - if(ret == 2) { throw "Socket listening timeout"; } + if(ret == 2) { throw std::runtime_error("Socket listening timeout"); } else { return -1; // stopped listening } @@ -457,7 +458,7 @@ public: setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); } CloseSocket(tmpsock); - if(_sock < 0) throw "Socket accept failed"; + if(_sock < 0) throw std::runtime_error("Socket accept failed"); return _sock; } int Shutdown() diff --git a/src/common/gmshLocalNetworkClient.cpp b/src/common/gmshLocalNetworkClient.cpp index 9da3b0d795..3d442d889d 100644 --- a/src/common/gmshLocalNetworkClient.cpp +++ b/src/common/gmshLocalNetworkClient.cpp @@ -163,9 +163,9 @@ public: int sock; try { sock = Start(exe, args, sockname, CTX::instance()->solver.timeout); - } catch(const char *err) { - Msg::Error("Abnormal server termination (%s on socket %s)", err, - sockname.c_str()); + } catch(std::runtime_error &e) { + Msg::Error("Abnormal server termination (%s on socket %s)", + e.what(), sockname.c_str()); sock = -1; } diff --git a/src/fltk/pluginWindow.cpp b/src/fltk/pluginWindow.cpp index 6ae41d4e37..75fcea5846 100644 --- a/src/fltk/pluginWindow.cpp +++ b/src/fltk/pluginWindow.cpp @@ -192,7 +192,7 @@ static void plugin_run_cb(Fl_Widget *w, void *data) pp->execute(nullptr); add_scripting(pp, nullptr); } - } catch(GMSH_Plugin *err) { + } catch(...) { char tmp[256]; pp->catchErrorMessage(tmp); Msg::Warning("%s", tmp); diff --git a/src/geo/SOrientedBoundingBox.cpp b/src/geo/SOrientedBoundingBox.cpp index 9c3d993272..d308bcdc96 100644 --- a/src/geo/SOrientedBoundingBox.cpp +++ b/src/geo/SOrientedBoundingBox.cpp @@ -306,8 +306,8 @@ SOrientedBoundingBox::buildOBB(std::vector<SPoint3> &vertices) try { record.MakeMeshWithPoints(); - } catch(const char *err) { - Msg::Error("%s", err); + } catch(std::runtime_error &e) { + Msg::Error("%s", e.what()); } std::vector<Segment> convex_hull; diff --git a/src/mesh/DivideAndConquer.cpp b/src/mesh/DivideAndConquer.cpp index 7a5d36bcbc..57842c6374 100644 --- a/src/mesh/DivideAndConquer.cpp +++ b/src/mesh/DivideAndConquer.cpp @@ -15,6 +15,7 @@ // Warning: point positions must be PERTURBED by a small random // value to avoid 3 aligned points or 4 cocyclical points! +#include <stdexcept> #include "GmshMessage.h" #include "DivideAndConquer.h" #include "Numeric.h" @@ -157,8 +158,8 @@ Segment DocRecord::UpperCommonTangent(DT vl, DT vr) int DocRecord::Qtest(PointNumero h, PointNumero i, PointNumero j, PointNumero k) { if((h == i) && (h == j) && (h == k)) { - throw "Identical points in triangulation: increase element size " - "or Mesh.RandomFactor"; + throw std::runtime_error("Identical points in triangulation: " + "increase element size or Mesh.RandomFactor"); return 0; } @@ -873,7 +874,8 @@ void DocRecord::Voronoi() void DocRecord::setPoints(fullMatrix<double> *p) { - if(numPoints != p->size1()) throw; + if(numPoints != p->size1()) + throw std::runtime_error("Incompatible number of points"); for(int i = 0; i < p->size1(); i++) { x(i) = (*p)(i, 0); y(i) = (*p)(i, 1); diff --git a/src/mesh/meshGFace.cpp b/src/mesh/meshGFace.cpp index 66248d137e..b635ee2921 100644 --- a/src/mesh/meshGFace.cpp +++ b/src/mesh/meshGFace.cpp @@ -1346,8 +1346,8 @@ bool meshGenerator(GFace *gf, int RECUR_ITER, bool repairSelfIntersecting1dMesh, Msg::Debug("Meshing of the convex hull (%d points)", points.size()); try { doc.MakeMeshWithPoints(); - } catch(const char *err) { - Msg::Error("%s", err); + } catch(std::runtime_error &e) { + Msg::Error("%s", e.what()); } Msg::Debug("Meshing of the convex hull (%d points) done", points.size()); @@ -2381,8 +2381,8 @@ static bool meshGeneratorPeriodic(GFace *gf, int RECUR_ITER, try { doc.MakeMeshWithPoints(); - } catch(const char *err) { - Msg::Error("%s", err); + } catch(std::runtime_error &e) { + Msg::Error("%s", e.what()); } for(int i = 0; i < doc.numTriangles; i++) { diff --git a/src/numeric/HierarchicalBasisH1Brick.cpp b/src/numeric/HierarchicalBasisH1Brick.cpp index f2edc411a1..821ba8264e 100644 --- a/src/numeric/HierarchicalBasisH1Brick.cpp +++ b/src/numeric/HierarchicalBasisH1Brick.cpp @@ -4,11 +4,12 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). #include <algorithm> +#include <stdexcept> #include "HierarchicalBasisH1Brick.h" HierarchicalBasisH1Brick::HierarchicalBasisH1Brick(int order) @@ -51,7 +52,7 @@ double HierarchicalBasisH1Brick::_affineCoordinate(const int &j, case(4): return 0.5 * (1 - v); case(5): return 0.5 * (1 + w); case(6): return 0.5 * (1 - w); - default: throw std::string("j must be : 1<=j<=6"); + default: throw std::runtime_error("j must be : 1<=j<=6"); } } inline void HierarchicalBasisH1Brick::_someProduct(double const &u, diff --git a/src/numeric/HierarchicalBasisH1Line.cpp b/src/numeric/HierarchicalBasisH1Line.cpp index afbaa34753..a2afdb3234 100644 --- a/src/numeric/HierarchicalBasisH1Line.cpp +++ b/src/numeric/HierarchicalBasisH1Line.cpp @@ -4,10 +4,11 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). +#include <stdexcept> #include "HierarchicalBasisH1Line.h" HierarchicalBasisH1Line::HierarchicalBasisH1Line(int pe) @@ -36,7 +37,7 @@ double HierarchicalBasisH1Line::_affineCoordinate(int j, double u) switch(j) { case(1): return 0.5 * (1 + u); case(2): return 0.5 * (1 - u); - default: throw std::string("j must be : 1<=j<=2"); + default: throw std::runtime_error("j must be : 1<=j<=2"); } } diff --git a/src/numeric/HierarchicalBasisH1Pri.cpp b/src/numeric/HierarchicalBasisH1Pri.cpp index 5db5fc2d9b..041c924e1b 100644 --- a/src/numeric/HierarchicalBasisH1Pri.cpp +++ b/src/numeric/HierarchicalBasisH1Pri.cpp @@ -4,10 +4,13 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). + +#include <stdexcept> #include "HierarchicalBasisH1Pri.h" + HierarchicalBasisH1Pri::HierarchicalBasisH1Pri(int order) { _nvertex = 6; @@ -49,7 +52,7 @@ double HierarchicalBasisH1Pri::_affineCoordinate(const int &j, const double &u, case(3): return 0.5 * (1 + u); case(4): return 0.5 * (1 + w); case(5): return 0.5 * (1 - w); - default: throw std::string("j must be : 1<=j<=5"); + default: throw std::runtime_error("j must be : 1<=j<=5"); } } void HierarchicalBasisH1Pri::generateBasis(double const &u, double const &v, diff --git a/src/numeric/HierarchicalBasisH1Quad.cpp b/src/numeric/HierarchicalBasisH1Quad.cpp index 66fb2fcc64..4ef514fdc9 100644 --- a/src/numeric/HierarchicalBasisH1Quad.cpp +++ b/src/numeric/HierarchicalBasisH1Quad.cpp @@ -5,6 +5,7 @@ // // Contributed by Ismail Badia. +#include <stdexcept> #include "HierarchicalBasisH1Quad.h" HierarchicalBasisH1Quad::HierarchicalBasisH1Quad(int pf1, int pf2, int pe0, @@ -22,9 +23,11 @@ HierarchicalBasisH1Quad::HierarchicalBasisH1Quad(int pf1, int pf2, int pe0, _nBubbleFunction = 0; _pf1 = pf1; _pf2 = pf2; - if(pe1 > pf2 || pe3 > pf2) { throw std::string("pe1 and pe3 must be <=pf2"); } + if(pe1 > pf2 || pe3 > pf2) { + throw std::runtime_error("pe1 and pe3 must be <=pf2"); + } if(pe0 > pf1 || pe2 > pf1) { - throw std::string("pe0 and pe2 must be <=pf1"); + throw std::runtime_error("pe0 and pe2 must be <=pf1"); } _pOrderEdge[0] = pe0; _pOrderEdge[1] = pe1; @@ -66,7 +69,7 @@ double HierarchicalBasisH1Quad::_affineCoordinate(int const &j, double const &u, case(2): return 0.5 * (1 - u); case(3): return 0.5 * (1 + v); case(4): return 0.5 * (1 - v); - default: throw std::string("j must be : 1<=j<=4"); + default: throw std::runtime_error("j must be : 1<=j<=4"); } } diff --git a/src/numeric/HierarchicalBasisH1Tetra.cpp b/src/numeric/HierarchicalBasisH1Tetra.cpp index ff69247263..e4f6f8b63a 100644 --- a/src/numeric/HierarchicalBasisH1Tetra.cpp +++ b/src/numeric/HierarchicalBasisH1Tetra.cpp @@ -4,11 +4,13 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). +#include <stdexcept> #include "HierarchicalBasisH1Tetra.h" + HierarchicalBasisH1Tetra::HierarchicalBasisH1Tetra(int order) { _nvertex = 4; @@ -42,7 +44,7 @@ double HierarchicalBasisH1Tetra::_affineCoordinate(const int &j, case(2): return -0.5 * (1 + u + v + w); case(3): return 0.5 * (1 + u); case(4): return 0.5 * (1 + w); - default: throw std::string("j must be : 1<=j<=4"); + default: throw std::runtime_error("j must be : 1<=j<=4"); } } diff --git a/src/numeric/HierarchicalBasisH1Tria.cpp b/src/numeric/HierarchicalBasisH1Tria.cpp index f76d6d3dd4..78de6a98c4 100644 --- a/src/numeric/HierarchicalBasisH1Tria.cpp +++ b/src/numeric/HierarchicalBasisH1Tria.cpp @@ -4,10 +4,11 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). +#include <stdexcept> #include "HierarchicalBasisH1Tria.h" HierarchicalBasisH1Tria::HierarchicalBasisH1Tria(int pf, int pe0, int pe1, @@ -25,7 +26,7 @@ HierarchicalBasisH1Tria::HierarchicalBasisH1Tria(int pf, int pe0, int pe1, _pf = pf; if(pe0 > pf || pe2 > pf || pe1 > pf) { - throw std::string("pe0, pe1 and pe2 must be <=pf"); + throw std::runtime_error("pe0, pe1 and pe2 must be <=pf"); } _pOrderEdge[0] = pe0; _pOrderEdge[1] = pe1; @@ -61,7 +62,7 @@ double HierarchicalBasisH1Tria::_affineCoordinate(int const &j, double const &u, case(1): return 0.5 * (1 + v); case(2): return -0.5 * (u + v); case(3): return 0.5 * (1 + u); - default: throw std::string("j must be : 1<=j<=3"); + default: throw std::runtime_error("j must be : 1<=j<=3"); } } diff --git a/src/numeric/HierarchicalBasisHcurlBrick.cpp b/src/numeric/HierarchicalBasisHcurlBrick.cpp index 018f6ec417..8876b16b98 100644 --- a/src/numeric/HierarchicalBasisHcurlBrick.cpp +++ b/src/numeric/HierarchicalBasisHcurlBrick.cpp @@ -5,6 +5,7 @@ // // Contributed by Ismail Badia. +#include <stdexcept> #include <algorithm> #include "HierarchicalBasisHcurlBrick.h" @@ -48,7 +49,7 @@ double HierarchicalBasisHcurlBrick::_affineCoordinate(const int &j, case(4): return 0.5 * (1 - v); case(5): return 0.5 * (1 + w); case(6): return 0.5 * (1 - w); - default: throw std::string("j must be : 1<=j<=6"); + default: throw std::runtime_error("j must be : 1<=j<=6"); } } @@ -598,7 +599,7 @@ void HierarchicalBasisHcurlBrick::orientOneFace( } } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } } } diff --git a/src/numeric/HierarchicalBasisHcurlBrick.h b/src/numeric/HierarchicalBasisHcurlBrick.h index cf5e16f9b8..4a3c970359 100644 --- a/src/numeric/HierarchicalBasisHcurlBrick.h +++ b/src/numeric/HierarchicalBasisHcurlBrick.h @@ -4,13 +4,14 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). #ifndef HIERARCHICAL_BASIS_HCURL_BRICK_H #define HIERARCHICAL_BASIS_HCURL_BRICK_H +#include <stdexcept> #include "HierarchicalBasisHcurl.h" /* @@ -62,7 +63,7 @@ public: generateCurlBasis(u, v, w, edgeBasis, faceBasis, bubbleBasis); } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } }; virtual void diff --git a/src/numeric/HierarchicalBasisHcurlLine.cpp b/src/numeric/HierarchicalBasisHcurlLine.cpp index 109520e048..df80614b06 100644 --- a/src/numeric/HierarchicalBasisHcurlLine.cpp +++ b/src/numeric/HierarchicalBasisHcurlLine.cpp @@ -4,10 +4,11 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth , Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +#include <stdexcept> #include "HierarchicalBasisHcurlLine.h" HierarchicalBasisHcurlLine::HierarchicalBasisHcurlLine(int order) @@ -36,7 +37,7 @@ double HierarchicalBasisHcurlLine::_affineCoordinate(int j, double u) switch(j) { case(1): return 0.5 * (1 + u); case(2): return 0.5 * (1 - u); - default: throw std::string("j must be : 1<=j<=2"); + default: throw std::runtime_error("j must be : 1<=j<=2"); } } diff --git a/src/numeric/HierarchicalBasisHcurlLine.h b/src/numeric/HierarchicalBasisHcurlLine.h index 4847ba7dc1..55cc8c6e98 100644 --- a/src/numeric/HierarchicalBasisHcurlLine.h +++ b/src/numeric/HierarchicalBasisHcurlLine.h @@ -4,13 +4,14 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). #ifndef HIERARCHICAL_BASIS_HCURL_LINE_H #define HIERARCHICAL_BASIS_HCURL_LINE_H +#include <stdexcept> #include "HierarchicalBasisHcurl.h" /* * @@ -47,7 +48,7 @@ public: generateCurlBasis(u, v, w, edgeBasis, faceBasis, bubbleBasis); } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } }; virtual void diff --git a/src/numeric/HierarchicalBasisHcurlPri.cpp b/src/numeric/HierarchicalBasisHcurlPri.cpp index dd253d5883..60df87de06 100644 --- a/src/numeric/HierarchicalBasisHcurlPri.cpp +++ b/src/numeric/HierarchicalBasisHcurlPri.cpp @@ -4,10 +4,13 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). + +#include <stdexcept> #include "HierarchicalBasisHcurlPri.h" + HierarchicalBasisHcurlPri::HierarchicalBasisHcurlPri(int order) { _nvertex = 6; @@ -55,7 +58,7 @@ double HierarchicalBasisHcurlPri::_affineCoordinate(const int &j, case(3): return 0.5 * (1 + u); case(4): return 0.5 * (1 + w); case(5): return 0.5 * (1 - w); - default: throw std::string("j must be : 1<=j<=5"); + default: throw std::runtime_error("j must be : 1<=j<=5"); } } @@ -790,7 +793,7 @@ void HierarchicalBasisHcurlPri::orientOneFace( } } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } } } @@ -1185,7 +1188,7 @@ void HierarchicalBasisHcurlPri::orientOneFace( } } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } } } diff --git a/src/numeric/HierarchicalBasisHcurlPri.h b/src/numeric/HierarchicalBasisHcurlPri.h index d85ed91320..44042a52c1 100644 --- a/src/numeric/HierarchicalBasisHcurlPri.h +++ b/src/numeric/HierarchicalBasisHcurlPri.h @@ -4,14 +4,17 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). + #ifndef HIERARCHICAL_BASIS_HCURL_PRI_H #define HIERARCHICAL_BASIS_HCURL_PRI_H -#include "HierarchicalBasisHcurl.h" +#include <stdexcept> #include <math.h> +#include "HierarchicalBasisHcurl.h" + /** * MPrism @@ -68,7 +71,7 @@ public: generateCurlBasis(u, v, w, edgeBasis, faceBasis, bubbleBasis); } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } } diff --git a/src/numeric/HierarchicalBasisHcurlQuad.cpp b/src/numeric/HierarchicalBasisHcurlQuad.cpp index 8a9aa4bba7..803fa21426 100644 --- a/src/numeric/HierarchicalBasisHcurlQuad.cpp +++ b/src/numeric/HierarchicalBasisHcurlQuad.cpp @@ -5,7 +5,9 @@ // // Contributed by Ismail Badia. +#include <stdexcept> #include "HierarchicalBasisHcurlQuad.h" + HierarchicalBasisHcurlQuad::HierarchicalBasisHcurlQuad(int order) { @@ -42,7 +44,7 @@ double HierarchicalBasisHcurlQuad::_affineCoordinate(int const &j, case(2): return 0.5 * (1 - u); case(3): return 0.5 * (1 + v); case(4): return 0.5 * (1 - v); - default: throw std::string("j must be : 1<=j<=4"); + default: throw std::runtime_error("j must be : 1<=j<=4"); } } @@ -366,7 +368,7 @@ void HierarchicalBasisHcurlQuad::orientOneFace( } } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } } } diff --git a/src/numeric/HierarchicalBasisHcurlQuad.h b/src/numeric/HierarchicalBasisHcurlQuad.h index 18825d0b44..74dbc24134 100644 --- a/src/numeric/HierarchicalBasisHcurlQuad.h +++ b/src/numeric/HierarchicalBasisHcurlQuad.h @@ -4,13 +4,14 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). #ifndef HIERARCHICAL_BASIS_HCURL_QUAD_H #define HIERARCHICAL_BASIS_HCURL_QUAD_H +#include <stdexcept> #include "HierarchicalBasisHcurl.h" /* @@ -51,7 +52,7 @@ public: generateCurlBasis(u, v, w, edgeBasis, faceBasis, bubbleBasis); } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } }; virtual void diff --git a/src/numeric/HierarchicalBasisHcurlTetra.cpp b/src/numeric/HierarchicalBasisHcurlTetra.cpp index dc87d4df24..144317c137 100644 --- a/src/numeric/HierarchicalBasisHcurlTetra.cpp +++ b/src/numeric/HierarchicalBasisHcurlTetra.cpp @@ -4,11 +4,13 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). +#include <stdexcept> #include "HierarchicalBasisHcurlTetra.h" + HierarchicalBasisHcurlTetra::HierarchicalBasisHcurlTetra(int order) { _nvertex = 4; @@ -49,7 +51,7 @@ double HierarchicalBasisHcurlTetra::_affineCoordinate(const int &j, case(2): return -0.5 * (1 + u + v + w); case(3): return 0.5 * (1 + u); case(4): return 0.5 * (1 + w); - default: throw std::string("j must be : 1<=j<=4"); + default: throw std::runtime_error("j must be : 1<=j<=4"); } } @@ -917,7 +919,7 @@ void HierarchicalBasisHcurlTetra::orientOneFace( } } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } } } diff --git a/src/numeric/HierarchicalBasisHcurlTetra.h b/src/numeric/HierarchicalBasisHcurlTetra.h index d1125668cc..97cfb89ec5 100644 --- a/src/numeric/HierarchicalBasisHcurlTetra.h +++ b/src/numeric/HierarchicalBasisHcurlTetra.h @@ -4,15 +4,16 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). #ifndef HIERARCHICAL_BASIS_HCURL_TETRA_H #define HIERARCHICAL_BASIS_HCURL_TETRA_H -#include "HierarchicalBasisHcurl.h" +#include <stdexcept> #include <math.h> +#include "HierarchicalBasisHcurl.h" /* * MTetrahedron @@ -68,7 +69,7 @@ public: generateCurlBasis(u, v, w, edgeBasis, faceBasis, bubbleBasis); } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } } diff --git a/src/numeric/HierarchicalBasisHcurlTria.cpp b/src/numeric/HierarchicalBasisHcurlTria.cpp index 11c152454d..22c921ada1 100644 --- a/src/numeric/HierarchicalBasisHcurlTria.cpp +++ b/src/numeric/HierarchicalBasisHcurlTria.cpp @@ -5,8 +5,9 @@ // // Contributed by Ismail Badia. -#include "HierarchicalBasisHcurlTria.h" +#include <stdexcept> #include <iostream> +#include "HierarchicalBasisHcurlTria.h" HierarchicalBasisHcurlTria::HierarchicalBasisHcurlTria(int order) @@ -49,7 +50,7 @@ double HierarchicalBasisHcurlTria::_affineCoordinate(int const &j, case(1): return 0.5 * (1 + v); case(2): return -0.5 * (u + v); case(3): return 0.5 * (1 + u); - default: throw std::string("j must be : 1<=j<=3"); + default: throw std::runtime_error("j must be : 1<=j<=3"); } } @@ -743,7 +744,7 @@ void HierarchicalBasisHcurlTria::orientOneFace( } } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } } } diff --git a/src/numeric/HierarchicalBasisHcurlTria.h b/src/numeric/HierarchicalBasisHcurlTria.h index 96954dd3da..8977126415 100644 --- a/src/numeric/HierarchicalBasisHcurlTria.h +++ b/src/numeric/HierarchicalBasisHcurlTria.h @@ -4,15 +4,16 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. // // Contributed by Ismail Badia. + // Reference : "Higher-Order Finite Element Methods"; Pavel Solin, Karel -// Segeth , -// Ivo Dolezel , Chapman and Hall/CRC; Edition : Har/Cdr (2003). +// Segeth, Ivo Dolezel, Chapman and Hall/CRC; Edition : Har/Cdr (2003). #ifndef HIERARCHICAL_BASIS_HCURL_TRIA_H #define HIERARCHICAL_BASIS_HCURL_TRIA_H -#include "HierarchicalBasisHcurl.h" +#include <stdexcept> #include <math.h> +#include "HierarchicalBasisHcurl.h" /* * MTriangle @@ -53,7 +54,7 @@ public: generateCurlBasis(u, v, w, edgeBasis, faceBasis, bubbleBasis); } else { - throw std::string("unknown typeFunction"); + throw std::runtime_error("unknown typeFunction"); } } virtual void diff --git a/src/numeric/OrthogonalPoly.cpp b/src/numeric/OrthogonalPoly.cpp index 36c1c8b456..0b00538996 100644 --- a/src/numeric/OrthogonalPoly.cpp +++ b/src/numeric/OrthogonalPoly.cpp @@ -1,3 +1,8 @@ +// Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle +// +// See the LICENSE.txt file in the Gmsh root directory for license information. +// Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. + #include "OrthogonalPoly.h" double OrthogonalPoly::EvalLobatto(int order, double x) @@ -114,7 +119,7 @@ double OrthogonalPoly::EvalLobatto(int order, double x) L = L * 1. / 2048. * pow(29. / 2., 0.5); return L; - default: throw std::string("Lobatto functions are written for orders =< 15"); + default: throw std::runtime_error("Lobatto functions are written for orders =< 15"); } } @@ -225,7 +230,7 @@ double OrthogonalPoly::EvalDLobatto(int order, double x) dL = dL * 1. / 2048. * pow(29. / 2., 0.5); return dL; - default: throw std::string("Lobatto functions are written for orders =< 15"); + default: throw std::runtime_error("Lobatto functions are written for orders =< 15"); } } @@ -316,7 +321,7 @@ double OrthogonalPoly::EvalKernelFunction(int order, double x) xsquare * (965770 - 334305 * xsquare)))))); phi = phi * 1. / 512. * pow(29. / 2., 0.5); return phi; - default: throw std::string("Lobatto functions are written for orders =< 15"); + default: throw std::runtime_error("Lobatto functions are written for orders =< 15"); } } @@ -402,7 +407,7 @@ double OrthogonalPoly::EvalDKernelFunction(int order, double x) xsquare * (10623470 - 4345965 * xsquare))))); dphi = dphi * 1. / 512. * pow(29. / 2., 0.5); return dphi; - default: throw std::string("Lobatto functions are written for orders =< 15"); + default: throw std::runtime_error("Lobatto functions are written for orders =< 15"); } } @@ -452,7 +457,7 @@ double OrthogonalPoly::EvalLegendre(int order, double x) 63; L = 1. / 256. * L; return L; - default: throw std::string("Legendre functions are written for orders =< 10"); + default: throw std::runtime_error("Legendre functions are written for orders =< 10"); } } @@ -498,6 +503,6 @@ double OrthogonalPoly::EvalDLegendre(int order, double x) xsquare * (540540 + xsquare * (-875160 + 461890 * xsquare)))); dL = 1. / 256. * dL; return dL; - default: throw std::string("Legendre functions are written for orders =< 10"); + default: throw std::runtime_error("Legendre functions are written for orders =< 10"); } } diff --git a/src/numeric/OrthogonalPoly.h b/src/numeric/OrthogonalPoly.h index 10fb236aa1..7a5feade3e 100644 --- a/src/numeric/OrthogonalPoly.h +++ b/src/numeric/OrthogonalPoly.h @@ -1,8 +1,14 @@ +// Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle +// +// See the LICENSE.txt file in the Gmsh root directory for license information. +// Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. + #ifndef _ORTHOGONALPOLY_H_ #define _ORTHOGONALPOLY_H_ #include <math.h> #include <string> + namespace OrthogonalPoly { // Lobatto Orthogonal Shape Functions in Horner's form diff --git a/src/plugin/PluginManager.cpp b/src/plugin/PluginManager.cpp index 7c9cb4b489..098b86d524 100644 --- a/src/plugin/PluginManager.cpp +++ b/src/plugin/PluginManager.cpp @@ -4,6 +4,7 @@ // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues. #include <map> +#include <stdexcept> #include <stdlib.h> #include "GmshConfig.h" #include "StringUtils.h" @@ -119,7 +120,7 @@ int PluginManager::action(const std::string &pluginName, const std::string &action, void *data) { GMSH_Plugin *plugin = find(pluginName); - if(!plugin) throw "Unknown plugin name"; + if(!plugin) throw std::runtime_error("Unknown plugin name"); if(action == "Run") { Msg::Info("Running Plugin(%s)...", pluginName.c_str()); @@ -128,7 +129,7 @@ int PluginManager::action(const std::string &pluginName, return tag; } else - throw "Unknown plugin action"; + throw std::runtime_error("Unknown plugin action"); } void PluginManager::setPluginOption(const std::string &pluginName, @@ -136,7 +137,7 @@ void PluginManager::setPluginOption(const std::string &pluginName, const std::string &value) { GMSH_Plugin *plugin = find(pluginName); - if(!plugin) throw "Unknown plugin name"; + if(!plugin) throw std::runtime_error("Unknown plugin name"); for(int i = 0; i < plugin->getNbOptionsStr(); i++) { StringXString *sxs = plugin->getOptionStr(i); @@ -145,7 +146,7 @@ void PluginManager::setPluginOption(const std::string &pluginName, return; } } - throw "Unknown plugin option name"; + throw std::runtime_error("Unknown plugin option name"); } void PluginManager::setPluginOption(const std::string &pluginName, @@ -153,7 +154,7 @@ void PluginManager::setPluginOption(const std::string &pluginName, double const value) { GMSH_Plugin *plugin = find(pluginName); - if(!plugin) throw "Unknown plugin name"; + if(!plugin) throw std::runtime_error("Unknown plugin name"); for(int i = 0; i < plugin->getNbOptions(); i++) { StringXNumber *sxn = plugin->getOption(i); @@ -162,7 +163,7 @@ void PluginManager::setPluginOption(const std::string &pluginName, return; } } - throw "Unknown plugin option name"; + throw std::runtime_error("Unknown plugin option name"); } PluginManager *PluginManager::instance() diff --git a/utils/solvers/c++/GmshSocket.h b/utils/solvers/c++/GmshSocket.h index c54faafb97..c07d1a93b7 100644 --- a/utils/solvers/c++/GmshSocket.h +++ b/utils/solvers/c++/GmshSocket.h @@ -28,6 +28,7 @@ //#include "GmshConfig.h" #include <string> +#include <stdexcept> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -55,42 +56,44 @@ typedef int socklen_t; typedef int socklen_t; #endif -class GmshSocket{ - public: +class GmshSocket { +public: // types of messages that can be exchanged (never use values greater // that 65535: if we receive a type > 65535 we assume that we // receive data from a machine with a different byte ordering, and // we swap the bytes in the payload) - enum MessageType{ - GMSH_START = 1, - GMSH_STOP = 2, - GMSH_INFO = 10, - GMSH_WARNING = 11, - GMSH_ERROR = 12, - GMSH_PROGRESS = 13, - GMSH_MERGE_FILE = 20, - GMSH_PARSE_STRING = 21, - GMSH_VERTEX_ARRAY = 22, - GMSH_PARAMETER = 23, - GMSH_PARAMETER_QUERY = 24, + enum MessageType { + GMSH_START = 1, + GMSH_STOP = 2, + GMSH_INFO = 10, + GMSH_WARNING = 11, + GMSH_ERROR = 12, + GMSH_PROGRESS = 13, + GMSH_MERGE_FILE = 20, + GMSH_PARSE_STRING = 21, + GMSH_VERTEX_ARRAY = 22, + GMSH_PARAMETER = 23, + GMSH_PARAMETER_QUERY = 24, GMSH_PARAMETER_QUERY_ALL = 25, GMSH_PARAMETER_QUERY_END = 26, - GMSH_CONNECT = 27, - GMSH_OLPARSE = 28, + GMSH_CONNECT = 27, + GMSH_OLPARSE = 28, GMSH_PARAMETER_NOT_FOUND = 29, - GMSH_SPEED_TEST = 30, - GMSH_PARAMETER_CLEAR = 31, - GMSH_PARAMETER_UPDATE = 32, - GMSH_OPEN_PROJECT = 33, - GMSH_CLIENT_CHANGED = 34, + GMSH_SPEED_TEST = 30, + GMSH_PARAMETER_CLEAR = 31, + GMSH_PARAMETER_UPDATE = 32, + GMSH_OPEN_PROJECT = 33, + GMSH_CLIENT_CHANGED = 34, GMSH_PARAMETER_WITHOUT_CHOICES = 35, GMSH_PARAMETER_QUERY_WITHOUT_CHOICES = 36, - GMSH_OPTION_1 = 100, - GMSH_OPTION_2 = 101, - GMSH_OPTION_3 = 102, - GMSH_OPTION_4 = 103, - GMSH_OPTION_5 = 104}; - protected: + GMSH_OPTION_1 = 100, + GMSH_OPTION_2 = 101, + GMSH_OPTION_3 = 102, + GMSH_OPTION_4 = 103, + GMSH_OPTION_5 = 104 + }; + +protected: // the socket descriptor int _sock; // the socket name @@ -135,10 +138,9 @@ class GmshSocket{ for(int i = 0; i < n; i++) { char *a = &array[i * size]; memcpy(x, a, size); - for(int c = 0; c < size; c++) - a[size - 1 - c] = x[c]; + for(int c = 0; c < size; c++) a[size - 1 - c] = x[c]; } - delete [] x; + delete[] x; } // sleep for some milliseconds void _sleep(int ms) @@ -149,7 +151,8 @@ class GmshSocket{ Sleep(ms); #endif } - public: + +public: GmshSocket() : _sock(0), _sent(0), _received(0) { #if defined(WIN32) && !defined(__CYGWIN__) @@ -167,7 +170,7 @@ class GmshSocket{ // we check for available data and return immediately, i.e., we do // polling). Returns 1 when data is available, 0 when nothing happened before // the time delay, -1 on error. - int Select(int seconds, int microseconds, int socket=-1) + int Select(int seconds, int microseconds, int socket = -1) { int s = (socket < 0) ? _sock : socket; struct timeval tv; @@ -192,14 +195,14 @@ class GmshSocket{ { SendMessage(type, (int)strlen(str), str); } - void Info(const char *str){ SendString(GMSH_INFO, str); } - void Warning(const char *str){ SendString(GMSH_WARNING, str); } - void Error(const char *str){ SendString(GMSH_ERROR, str); } - void Progress(const char *str){ SendString(GMSH_PROGRESS, str); } - void MergeFile(const char *str){ SendString(GMSH_MERGE_FILE, str); } - void OpenProject(const char *str){ SendString(GMSH_OPEN_PROJECT, str); } - void ParseString(const char *str){ SendString(GMSH_PARSE_STRING, str); } - void SpeedTest(const char *str){ SendString(GMSH_SPEED_TEST, str); } + void Info(const char *str) { SendString(GMSH_INFO, str); } + void Warning(const char *str) { SendString(GMSH_WARNING, str); } + void Error(const char *str) { SendString(GMSH_ERROR, str); } + void Progress(const char *str) { SendString(GMSH_PROGRESS, str); } + void MergeFile(const char *str) { SendString(GMSH_MERGE_FILE, str); } + void OpenProject(const char *str) { SendString(GMSH_OPEN_PROJECT, str); } + void ParseString(const char *str) { SendString(GMSH_PARSE_STRING, str); } + void SpeedTest(const char *str) { SendString(GMSH_SPEED_TEST, str); } void Option(int num, const char *str) { if(num < 1) num = 1; @@ -209,15 +212,15 @@ class GmshSocket{ int ReceiveHeader(int *type, int *len, int *swap) { *swap = 0; - if(_receiveData(type, sizeof(int)) > 0){ - if(*type > 65535){ + if(_receiveData(type, sizeof(int)) > 0) { + if(*type > 65535) { // the data comes from a machine with different endianness and // we must swap the bytes *swap = 1; - _swapBytes((char*)type, sizeof(int), 1); + _swapBytes((char *)type, sizeof(int), 1); } - if(_receiveData(len, sizeof(int)) > 0){ - if(*swap) _swapBytes((char*)len, sizeof(int), 1); + if(_receiveData(len, sizeof(int)) > 0) { + if(*swap) _swapBytes((char *)len, sizeof(int), 1); return 1; } } @@ -251,24 +254,25 @@ class GmshSocket{ shutdown(s, SHUT_RDWR); #endif } - unsigned long int SentBytes(){ return _sent; } - unsigned long int ReceivedBytes(){ return _received; } + unsigned long int SentBytes() { return _sent; } + unsigned long int ReceivedBytes() { return _received; } }; class GmshClient : public GmshSocket { - public: +public: GmshClient() : GmshSocket() {} - ~GmshClient(){} + ~GmshClient() {} int Connect(const char *sockname) { - if(strstr(sockname, "/") || strstr(sockname, "\\") || !strstr(sockname, ":")){ + if(strstr(sockname, "/") || strstr(sockname, "\\") || + !strstr(sockname, ":")) { #if !defined(WIN32) || defined(__CYGWIN__) // UNIX socket (testing ":" is not enough with Windows paths) _sock = socket(PF_UNIX, SOCK_STREAM, 0); if(_sock < 0) return -1; // try to connect socket to given name struct sockaddr_un addr_un; - memset((char *) &addr_un, 0, sizeof(addr_un)); + memset((char *)&addr_un, 0, sizeof(addr_un)); addr_un.sun_family = AF_UNIX; strcpy(addr_un.sun_path, sockname); for(int tries = 0; tries < 5; tries++) { @@ -280,7 +284,7 @@ class GmshClient : public GmshSocket { return -1; // Unix sockets are not available on Windows #endif } - else{ + else { // TCP/IP socket _sock = socket(AF_INET, SOCK_STREAM, 0); if(_sock < 0) return -1; @@ -292,26 +296,25 @@ class GmshClient : public GmshSocket { int portno = atoi(port + 1); char *remote = strdup(sockname); int remotelen = (int)(strlen(remote) - strlen(port)); - if(remotelen > 0) - strncpy(remote, sockname, remotelen); - if(remotelen >= 0) - remote[remotelen] = '\0'; + if(remotelen > 0) strncpy(remote, sockname, remotelen); + if(remotelen >= 0) remote[remotelen] = '\0'; struct hostent *server; - if(!(server = gethostbyname(remote))){ + if(!(server = gethostbyname(remote))) { CloseSocket(_sock); free(remote); return -3; // no such host } free(remote); struct sockaddr_in addr_in; - memset((char *) &addr_in, 0, sizeof(addr_in)); + memset((char *)&addr_in, 0, sizeof(addr_in)); addr_in.sin_family = AF_INET; - memcpy((char *)&addr_in.sin_addr.s_addr, (char *)server->h_addr, server->h_length); + memcpy((char *)&addr_in.sin_addr.s_addr, (char *)server->h_addr, + server->h_length); addr_in.sin_port = htons(portno); for(int tries = 0; tries < 5; tries++) { - if(connect(_sock, (struct sockaddr *)&addr_in, sizeof(addr_in)) >= 0){ + if(connect(_sock, (struct sockaddr *)&addr_in, sizeof(addr_in)) >= 0) { return _sock; - } + } _sleep(100); } } @@ -328,27 +331,30 @@ class GmshClient : public GmshSocket { #endif SendString(GMSH_START, tmp); } - void Stop(){ SendString(GMSH_STOP, "Goodbye!"); } - void Disconnect(){ CloseSocket(_sock); } + void Stop() { SendString(GMSH_STOP, "Goodbye!"); } + void Disconnect() { CloseSocket(_sock); } }; -class GmshServer : public GmshSocket{ - private: +class GmshServer : public GmshSocket { +private: int _portno; - public: + +public: GmshServer() : GmshSocket(), _portno(-1) {} - virtual ~GmshServer(){} - virtual int NonBlockingSystemCall(const std::string &exe, const std::string &args) = 0; - virtual int NonBlockingWait(double waitint, double timeout, int socket=-1) = 0; + virtual ~GmshServer() {} + virtual int NonBlockingSystemCall(const std::string &exe, + const std::string &args) = 0; + virtual int NonBlockingWait(double waitint, double timeout, + int socket = -1) = 0; // start the client by launching "exe args" (args is supposed to contain // '%s' where the socket name should appear) - int Start(const std::string &exe, const std::string &args, const std::string &sockname, - double timeout) + int Start(const std::string &exe, const std::string &args, + const std::string &sockname, double timeout) { _sockname = sockname; int tmpsock; if(strstr(_sockname.c_str(), "/") || strstr(_sockname.c_str(), "\\") || - !strstr(_sockname.c_str(), ":")){ + !strstr(_sockname.c_str(), ":")) { // UNIX socket (testing ":" is not enough with Windows paths) _portno = -1; #if !defined(WIN32) || defined(__CYGWIN__) @@ -356,23 +362,23 @@ class GmshServer : public GmshSocket{ unlink(_sockname.c_str()); // create a socket tmpsock = socket(PF_UNIX, SOCK_STREAM, 0); - if(tmpsock < 0) throw "Couldn't create socket"; + if(tmpsock < 0) throw std::runtime_error("Couldn't create socket"); // bind the socket to its name struct sockaddr_un addr_un; - memset((char *) &addr_un, 0, sizeof(addr_un)); + memset((char *)&addr_un, 0, sizeof(addr_un)); strcpy(addr_un.sun_path, _sockname.c_str()); addr_un.sun_family = AF_UNIX; - if(bind(tmpsock, (struct sockaddr *)&addr_un, sizeof(addr_un)) < 0){ + if(bind(tmpsock, (struct sockaddr *)&addr_un, sizeof(addr_un)) < 0) { CloseSocket(tmpsock); - throw "Couldn't bind socket to name"; + throw std::runtime_error("Couldn't bind socket to name"); } // change permissions on the socket name in case it has to be rm'd later chmod(_sockname.c_str(), 0666); #else - throw "Unix sockets not available on Windows"; + throw std::runtime_error("Unix sockets not available on Windows"); #endif } - else{ + else { // TCP/IP socket: valid names are either explicit ("hostname:12345") // or implicit ("hostname:", "hostname: ", "hostname:0") in which case // the system attributes at random an available port @@ -388,65 +394,63 @@ class GmshServer : public GmshSocket{ #else if(tmpsock == (int)INVALID_SOCKET) #endif - throw "Couldn't create socket"; + throw std::runtime_error("Couldn't create socket"); // bind the socket to its name struct sockaddr_in addr_in; - memset((char *) &addr_in, 0, sizeof(addr_in)); + memset((char *)&addr_in, 0, sizeof(addr_in)); addr_in.sin_family = AF_INET; addr_in.sin_addr.s_addr = INADDR_ANY; addr_in.sin_port = htons(_portno); // random assign if _portno == 0 - if(bind(tmpsock, (struct sockaddr *)&addr_in, sizeof(addr_in)) < 0){ + if(bind(tmpsock, (struct sockaddr *)&addr_in, sizeof(addr_in)) < 0) { CloseSocket(tmpsock); - throw "Couldn't bind socket to name"; + throw std::runtime_error("Couldn't bind socket to name"); } - if(!_portno){ // retrieve name if randomly assigned port + if(!_portno) { // retrieve name if randomly assigned port socklen_t addrlen = sizeof(addr_in); getsockname(tmpsock, (struct sockaddr *)&addr_in, &addrlen); _portno = ntohs(addr_in.sin_port); - int pos = (int)_sockname.find(':'); // remove trailing ' ' or '0' + int pos = (int)_sockname.find(':'); // remove trailing ' ' or '0' char tmp[256]; - sprintf(tmp, "%s:%d", _sockname.substr(0, pos).c_str(), _portno); + sprintf(tmp, "%s:%d", _sockname.substr(0, pos).c_str(), _portno); _sockname.assign(tmp); } } - if(exe.size() || args.size()){ + if(exe.size() || args.size()) { char s[1024]; sprintf(s, args.c_str(), _sockname.c_str()); NonBlockingSystemCall(exe, s); // starts the solver } - else{ + else { timeout = 0.; // no command launched: don't set a timeout } // listen on socket (queue up to 20 connections before having // them automatically rejected) - if(listen(tmpsock, 20)){ + if(listen(tmpsock, 20)) { CloseSocket(tmpsock); - throw "Socket listen failed"; + throw std::runtime_error("Socket listen failed"); } // wait until we get data int ret = NonBlockingWait(0.001, timeout, tmpsock); - if(ret){ + if(ret) { CloseSocket(tmpsock); - if(ret == 2){ - throw "Socket listening timeout"; - } - else{ + if(ret == 2) { throw std::runtime_error("Socket listening timeout"); } + else { return -1; // stopped listening } } // accept connection request - if(_portno < 0){ + if(_portno < 0) { #if !defined(WIN32) || defined(__CYGWIN__) struct sockaddr_un from_un; socklen_t len = sizeof(from_un); _sock = accept(tmpsock, (struct sockaddr *)&from_un, &len); #endif } - else{ + else { struct sockaddr_in from_in; socklen_t len = sizeof(from_in); _sock = accept(tmpsock, (struct sockaddr *)&from_in, &len); @@ -454,15 +458,13 @@ class GmshServer : public GmshSocket{ setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); } CloseSocket(tmpsock); - if(_sock < 0) - throw "Socket accept failed"; + if(_sock < 0) throw std::runtime_error("Socket accept failed"); return _sock; } int Shutdown() { #if !defined(WIN32) || defined(__CYGWIN__) - if(_portno < 0) - unlink(_sockname.c_str()); + if(_portno < 0) unlink(_sockname.c_str()); #endif ShutdownSocket(_sock); CloseSocket(_sock); -- GitLab