From 719d7c65b6a0406194f3924d47d5f4f4030c2869 Mon Sep 17 00:00:00 2001 From: Richard Berger <richard.berger@temple.edu> Date: Fri, 16 Sep 2016 18:52:59 -0400 Subject: [PATCH] Make exceptions control flow and functions optional --- src/error.cpp | 9 +++++++- src/error.h | 48 +++++++++------------------------------ src/exceptions.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ src/library.cpp | 7 ++++++ src/library.h | 2 ++ 5 files changed, 85 insertions(+), 39 deletions(-) create mode 100644 src/exceptions.h diff --git a/src/error.cpp b/src/error.cpp index 237984bfaf..5c24d94832 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -22,7 +22,12 @@ using namespace LAMMPS_NS; /* ---------------------------------------------------------------------- */ -Error::Error(LAMMPS *lmp) : Pointers(lmp), last_error_message(NULL), last_error_type(ERROR_NONE) {} +Error::Error(LAMMPS *lmp) : Pointers(lmp) { +#ifdef LAMMPS_EXCEPTIONS + last_error_message = NULL; + last_error_type = ERROR_NONE; +#endif +} /* ---------------------------------------------------------------------- called by all procs in universe @@ -198,6 +203,7 @@ void Error::done(int status) exit(status); } +#ifdef LAMMPS_EXCEPTIONS /* ---------------------------------------------------------------------- return the last error message reported by LAMMPS (only used if compiled with -DLAMMPS_EXCEPTIONS) @@ -235,3 +241,4 @@ void Error::set_last_error(const char * msg, ErrorType type) } last_error_type = type; } +#endif diff --git a/src/error.h b/src/error.h index 0fa3475d1e..b66fe4a13b 100644 --- a/src/error.h +++ b/src/error.h @@ -15,48 +15,14 @@ #define LMP_ERROR_H #include "pointers.h" -#include <string> -#include <exception> -namespace LAMMPS_NS { - -class LAMMPSException : public std::exception -{ -public: - std::string message; - - LAMMPSException(std::string msg) : message(msg) { - } - - ~LAMMPSException() throw() { - } - - virtual const char * what() const throw() { - return message.c_str(); - } -}; - -class LAMMPSAbortException : public LAMMPSException { -public: - MPI_Comm universe; - - LAMMPSAbortException(std::string msg, MPI_Comm universe) : - LAMMPSException(msg), - universe(universe) - { - } -}; +#ifdef LAMMPS_EXCEPTIONS +#include "exceptions.h" +#endif -enum ErrorType { - ERROR_NONE = 0, - ERROR_NORMAL = 1, - ERROR_ABORT = 2 -}; +namespace LAMMPS_NS { class Error : protected Pointers { - char * last_error_message; - ErrorType last_error_type; - public: Error(class LAMMPS *); @@ -70,9 +36,15 @@ class Error : protected Pointers { void message(const char *, int, const char *, int = 1); void done(int = 0); // 1 would be fully backwards compatible +#ifdef LAMMPS_EXCEPTIONS char * get_last_error() const; ErrorType get_last_error_type() const; void set_last_error(const char * msg, ErrorType type = ERROR_NORMAL); + + private: + char * last_error_message; + ErrorType last_error_type; +#endif }; } diff --git a/src/exceptions.h b/src/exceptions.h new file mode 100644 index 0000000000..121991afc5 --- /dev/null +++ b/src/exceptions.h @@ -0,0 +1,58 @@ +/* -*- c++ -*- ---------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +#ifndef LMP_EXCEPTIONS_H +#define LMP_EXCEPTIONS_H + +#include <mpi.h> +#include <string> +#include <exception> + +namespace LAMMPS_NS { + +class LAMMPSException : public std::exception +{ +public: + std::string message; + + LAMMPSException(std::string msg) : message(msg) { + } + + ~LAMMPSException() throw() { + } + + virtual const char * what() const throw() { + return message.c_str(); + } +}; + +class LAMMPSAbortException : public LAMMPSException { +public: + MPI_Comm universe; + + LAMMPSAbortException(std::string msg, MPI_Comm universe) : + LAMMPSException(msg), + universe(universe) + { + } +}; + +enum ErrorType { + ERROR_NONE = 0, + ERROR_NORMAL = 1, + ERROR_ABORT = 2 +}; + +} + +#endif diff --git a/src/library.cpp b/src/library.cpp index 4d493c979b..ae00f106d1 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -109,6 +109,8 @@ void lammps_file(void *ptr, char *str) char *lammps_command(void *ptr, char *str) { LAMMPS * lmp = (LAMMPS *) ptr; + +#ifdef LAMMPS_EXCEPTIONS Error * error = lmp->error; try { @@ -127,6 +129,9 @@ char *lammps_command(void *ptr, char *str) error->set_last_error(e.message.c_str(), ERROR_NORMAL); return NULL; } +#else + return lmp->input->one(str); +#endif } /* ---------------------------------------------------------------------- @@ -611,6 +616,7 @@ void lammps_scatter_atoms(void *ptr, char *name, } } +#ifdef LAMMPS_EXCEPTIONS /* ---------------------------------------------------------------------- Check if a new error message ------------------------------------------------------------------------- */ @@ -640,3 +646,4 @@ int lammps_get_last_error_message(void *ptr, char * buffer, int buffer_size) { } return 0; } +#endif diff --git a/src/library.h b/src/library.h index 5cb128fdb9..43e10d8c75 100644 --- a/src/library.h +++ b/src/library.h @@ -45,8 +45,10 @@ int lammps_get_natoms(void *); void lammps_gather_atoms(void *, char *, int, int, void *); void lammps_scatter_atoms(void *, char *, int, int, void *); +#ifdef LAMMPS_EXCEPTION int lammps_has_error(void *); int lammps_get_last_error_message(void *, char *, int); +#endif #ifdef __cplusplus } -- GitLab