From 8086228976bcdd798ca3e7cc1a83bd578dce766d Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sat, 15 Apr 2023 21:54:13 -0400 Subject: [PATCH] make communicator protected and use accessor instead. simplify --- src/error.cpp | 9 +++------ src/error.h | 2 +- src/exceptions.h | 12 +++++++----- src/library.cpp | 10 +++++----- src/main.cpp | 2 +- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/error.cpp b/src/error.cpp index 9f0a27ff5c..4aaa93aa31 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -279,8 +279,7 @@ void Error::done(int status) } /* ---------------------------------------------------------------------- - return the last error message reported by LAMMPS (only used if - compiled with -DLAMMPS_EXCEPTIONS) + return the last error message reported by LAMMPS ------------------------------------------------------------------------- */ std::string Error::get_last_error() const @@ -289,8 +288,7 @@ std::string Error::get_last_error() const } /* ---------------------------------------------------------------------- - return the type of the last error reported by LAMMPS (only used if - compiled with -DLAMMPS_EXCEPTIONS) + return the type of the last error reported by LAMMPS ------------------------------------------------------------------------- */ ErrorType Error::get_last_error_type() const @@ -300,10 +298,9 @@ ErrorType Error::get_last_error_type() const /* ---------------------------------------------------------------------- set the last error message and error type - (only used if compiled with -DLAMMPS_EXCEPTIONS) ------------------------------------------------------------------------- */ -void Error::set_last_error(const std::string &msg, ErrorType type) +void Error::set_last_error(const char *msg, ErrorType type) { last_error_message = msg; last_error_type = type; diff --git a/src/error.h b/src/error.h index 4280b76a2a..95a58c81e3 100644 --- a/src/error.h +++ b/src/error.h @@ -64,7 +64,7 @@ class Error : protected Pointers { std::string get_last_error() const; ErrorType get_last_error_type() const; - void set_last_error(const std::string &msg, ErrorType type = ERROR_NORMAL); + void set_last_error(const char *msg, ErrorType type = ERROR_NORMAL); private: std::string last_error_message; diff --git a/src/exceptions.h b/src/exceptions.h index 5c4aa99971..7d9aabfda0 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -22,21 +22,23 @@ namespace LAMMPS_NS { class LAMMPSException : public std::exception { public: - std::string message; - LAMMPSException(const std::string &msg) : message(msg) {} - const char *what() const noexcept override { return message.c_str(); } + + protected: + std::string message; }; class LAMMPSAbortException : public LAMMPSException { public: - MPI_Comm universe; - LAMMPSAbortException(const std::string &msg, MPI_Comm _universe) : LAMMPSException(msg), universe(_universe) { } + MPI_Comm get_universe() const { return universe; } + + protected: + MPI_Comm universe; }; enum ErrorType { ERROR_NONE = 0, ERROR_NORMAL = 1, ERROR_ABORT = 2 }; diff --git a/src/library.cpp b/src/library.cpp index c30638a342..135f7d65ef 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -97,15 +97,15 @@ static void ptr_argument_warning() #define END_CAPTURE \ catch(LAMMPSAbortException &ae) { \ int nprocs = 0; \ - MPI_Comm_size(ae.universe, &nprocs ); \ + MPI_Comm_size(ae.get_universe(), &nprocs ); \ \ if (nprocs > 1) { \ - error->set_last_error(ae.message, ERROR_ABORT); \ + error->set_last_error(ae.what(), ERROR_ABORT); \ } else { \ - error->set_last_error(ae.message, ERROR_NORMAL); \ + error->set_last_error(ae.what(), ERROR_NORMAL); \ } \ } catch(LAMMPSException &e) { \ - error->set_last_error(e.message, ERROR_NORMAL); \ + error->set_last_error(e.what(), ERROR_NORMAL); \ } // ---------------------------------------------------------------------- @@ -172,7 +172,7 @@ void *lammps_open(int argc, char **argv, MPI_Comm comm, void **ptr) lmp = new LAMMPS(argc, argv, comm); if (ptr) *ptr = (void *) lmp; } catch(LAMMPSException &e) { - fprintf(stderr, "LAMMPS Exception: %s\n", e.message); + fprintf(stderr, "LAMMPS Exception: %s\n", e.what()); if (ptr) *ptr = nullptr; } catch (fmt::format_error &fe) { fprintf(stderr, "fmt::format_error: %s\n", fe.what()); diff --git a/src/main.cpp b/src/main.cpp index 3ebc710a24..caf1d4d53c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,7 +78,7 @@ int main(int argc, char **argv) delete lammps; } catch (LAMMPSAbortException &ae) { finalize(); - MPI_Abort(ae.universe, 1); + MPI_Abort(ae.get_universe(), 1); } catch (LAMMPSException &) { finalize(); MPI_Barrier(lammps_comm);