Make exceptions control flow and functions optional
This commit is contained in:
@ -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
|
||||
|
||||
48
src/error.h
48
src/error.h
@ -15,48 +15,14 @@
|
||||
#define LMP_ERROR_H
|
||||
|
||||
#include "pointers.h"
|
||||
#include <string>
|
||||
#include <exception>
|
||||
|
||||
#ifdef LAMMPS_EXCEPTIONS
|
||||
#include "exceptions.h"
|
||||
#endif
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
58
src/exceptions.h
Normal file
58
src/exceptions.h
Normal file
@ -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
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user