From f84790ba623be9256754ad85342826ba8736aca1 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 14 Feb 2022 15:50:36 -0500 Subject: [PATCH] add a more specific example to explain how values are rejected and how atoi() fails --- doc/src/Developer_notes.rst | 22 +++++++++++++--------- doc/src/Developer_utils.rst | 4 ++-- src/tokenizer.h | 10 ++++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/doc/src/Developer_notes.rst b/doc/src/Developer_notes.rst index 23344de61b..a15354bb9a 100644 --- a/doc/src/Developer_notes.rst +++ b/doc/src/Developer_notes.rst @@ -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 converting those to numbers, the :cpp:class:`Tokenizer ` and :cpp:class:`ValueTokenizer -` can be used. Those provide a superset -of the functionality of ``strtok()`` from the C-library and the latter -also includes conversion to different types. Any errors while processing -the string in those classes will result in an exception, which can -be caught and the error processed as needed. Unlike C-library functions -like ``atoi()``, ``atof()``, ``strtol()``, or ``strtod()`` the -conversion to numbers first checks of the string is a valid number -and thus will not silently return an unexpected or incorrect value. - +` can be used. Those provide a superset of +the functionality of ``strtok()`` from the C-library and the latter also +includes conversion to different types. Any errors while processing the +string in those classes will result in an exception, which can be caught +and the error processed as needed. Unlike the C-library functions +``atoi()``, ``atof()``, ``strtol()``, or ``strtod()`` the conversion +will check if the converted text is a valid integer of floating point +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 +` if +:cpp:func:`ValueTokenizer::next_int() +` is called on the same string. Fix contributions to instantaneous energy, virial, and cumulative energy ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/doc/src/Developer_utils.rst b/doc/src/Developer_utils.rst index 39ac9c716b..a9df85c899 100644 --- a/doc/src/Developer_utils.rst +++ b/doc/src/Developer_utils.rst @@ -343,11 +343,11 @@ This code example should produce the following output: .. doxygenclass:: LAMMPS_NS::InvalidIntegerException :project: progguide - :members: what + :members: .. doxygenclass:: LAMMPS_NS::InvalidFloatException :project: progguide - :members: what + :members: ---------- diff --git a/src/tokenizer.h b/src/tokenizer.h index 03afa59836..b267e89b23 100644 --- a/src/tokenizer.h +++ b/src/tokenizer.h @@ -52,10 +52,15 @@ class Tokenizer { std::vector as_vector(); }; +/** General Tokenizer exception class */ + class TokenizerException : public std::exception { std::string message; public: + // remove unused default constructor + TokenizerException() = delete; + /** Thrown during retrieving or skipping tokens * * \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(); } }; +/** Exception thrown by ValueTokenizer when trying to convert an invalid integer string */ + class InvalidIntegerException : public TokenizerException { + public: /** 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 { public: /** Thrown during converting string to floating point number