ARTS 2.5.4 (git: bcd8c674)
double_imanip.h
Go to the documentation of this file.
1/* Copyright (C) 2021 Oliver Lemke <oliver.lemke@uni-hamburg.de>
2
3 This program is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 2, or (at your option) any
6 later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 USA. */
17
19// File description
21
30#ifndef double_imanip_h
31#define double_imanip_h
32
33#include <fstream>
34#include <limits>
35#include <stdexcept>
36
37#include "debug.h"
38
39#include <fast_float/fast_float.h>
40
43 public:
44 const double_imanip& operator>>(double& x) const {
45 std::istream& is = *in;
46 std::string buf;
47
48 // Read to the buffer
49 is >> buf;
50 ARTS_USER_ERROR_IF(is.fail(), "Cannot read from stream")
51
52 // Actual conversion
53 const auto res =
54 fast_float::from_chars(buf.c_str(), buf.c_str() + buf.size(), x);
55
56 // Error (only std::errc::invalid_argument possible)
57 ARTS_USER_ERROR_IF(res.ec == std::errc::invalid_argument,
58 "The argument: \n\n'",
59 buf,
60 R"--('
61
62is not convertible to a valid double. At the very least it
63cannot be converted to one using the standard string-to-double
64routine
65)--")
66
67 // Put the stream to be where it is supposed to be
68 std::size_t n = std::distance(buf.c_str(), res.ptr);
69 while (n++ < buf.size()) is.unget();
70
71 return *this;
72 }
73
74 std::istream& operator>>(const double_imanip&) const { return *in; }
76 friend const double_imanip& operator>>(std::istream& in,
77 const double_imanip& dm);
78
79 private:
80 mutable std::istream* in;
81};
82
83inline const double_imanip& operator>>(std::istream& in,
84 const double_imanip& dm) {
85 dm.in = &in;
86 return dm;
87}
88
89#endif
Input manipulator class for doubles to enable nan and inf parsing.
Definition: double_imanip.h:42
std::istream * in
Definition: double_imanip.h:75
const double_imanip & operator>>(double &x) const
Definition: double_imanip.h:44
Helper macros for debugging.
#define ARTS_USER_ERROR_IF(condition,...)
Definition: debug.h:134
const double_imanip & operator>>(std::istream &in, const double_imanip &dm)
Definition: double_imanip.h:83