add a more specific example to explain how values are rejected and how atoi() fails

This commit is contained in:
Axel Kohlmeyer
2022-02-14 15:50:36 -05:00
parent 37cd4ba2ea
commit f84790ba62
3 changed files with 25 additions and 11 deletions

View File

@ -48,15 +48,19 @@ split into lines, parsed, and applied only to atoms the MPI rank
For splitting a string (incrementally) into words and optionally For splitting a string (incrementally) into words and optionally
converting those to numbers, the :cpp:class:`Tokenizer converting those to numbers, the :cpp:class:`Tokenizer
<LAMMPS_NS::Tokenizer>` and :cpp:class:`ValueTokenizer <LAMMPS_NS::Tokenizer>` and :cpp:class:`ValueTokenizer
<LAMMPS_NS::ValueTokenizer>` can be used. Those provide a superset <LAMMPS_NS::ValueTokenizer>` can be used. Those provide a superset of
of the functionality of ``strtok()`` from the C-library and the latter the functionality of ``strtok()`` from the C-library and the latter also
also includes conversion to different types. Any errors while processing includes conversion to different types. Any errors while processing the
the string in those classes will result in an exception, which can string in those classes will result in an exception, which can be caught
be caught and the error processed as needed. Unlike C-library functions and the error processed as needed. Unlike the C-library functions
like ``atoi()``, ``atof()``, ``strtol()``, or ``strtod()`` the ``atoi()``, ``atof()``, ``strtol()``, or ``strtod()`` the conversion
conversion to numbers first checks of the string is a valid number will check if the converted text is a valid integer of floating point
and thus will not silently return an unexpected or incorrect value. number and will not silently return an unexpected or incorrect value.
For example, ``atoi()`` will return 12 when converting "12.5" while the
ValueTokenizer class will throw an :cpp:class:`InvalidIntegerException
<LAMMPS_NS::InvalidIntegerException>` if
:cpp:func:`ValueTokenizer::next_int()
<LAMMPS_NS::ValueTokenizer::next_int>` is called on the same string.
Fix contributions to instantaneous energy, virial, and cumulative energy Fix contributions to instantaneous energy, virial, and cumulative energy
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -343,11 +343,11 @@ This code example should produce the following output:
.. doxygenclass:: LAMMPS_NS::InvalidIntegerException .. doxygenclass:: LAMMPS_NS::InvalidIntegerException
:project: progguide :project: progguide
:members: what :members:
.. doxygenclass:: LAMMPS_NS::InvalidFloatException .. doxygenclass:: LAMMPS_NS::InvalidFloatException
:project: progguide :project: progguide
:members: what :members:
---------- ----------

View File

@ -52,10 +52,15 @@ class Tokenizer {
std::vector<std::string> as_vector(); std::vector<std::string> as_vector();
}; };
/** General Tokenizer exception class */
class TokenizerException : public std::exception { class TokenizerException : public std::exception {
std::string message; std::string message;
public: public:
// remove unused default constructor
TokenizerException() = delete;
/** Thrown during retrieving or skipping tokens /** Thrown during retrieving or skipping tokens
* *
* \param msg String with error message * \param msg String with error message
@ -67,7 +72,10 @@ class TokenizerException : public std::exception {
const char *what() const noexcept override { return message.c_str(); } const char *what() const noexcept override { return message.c_str(); }
}; };
/** Exception thrown by ValueTokenizer when trying to convert an invalid integer string */
class InvalidIntegerException : public TokenizerException { class InvalidIntegerException : public TokenizerException {
public: public:
/** Thrown during converting string to integer number /** Thrown during converting string to integer number
* *
@ -78,6 +86,8 @@ class InvalidIntegerException : public TokenizerException {
} }
}; };
/** Exception thrown by ValueTokenizer when trying to convert an floating point string */
class InvalidFloatException : public TokenizerException { class InvalidFloatException : public TokenizerException {
public: public:
/** Thrown during converting string to floating point number /** Thrown during converting string to floating point number