ARTS 2.5.11 (git: 6827797f)
double_imanip.h
Go to the documentation of this file.
1
2// File description
4
13#ifndef double_imanip_h
14#define double_imanip_h
15
16#include <fstream>
17#include <limits>
18#include <stdexcept>
19
20#include "debug.h"
21
22#include <fast_float/fast_float.h>
23
26 public:
27 const double_imanip& operator>>(double& x) const {
28 std::istream& is = *in;
29 std::string buf;
30
31 // Read to the buffer
32 is >> buf;
33 ARTS_USER_ERROR_IF(is.fail(), "Cannot read from stream")
34
35 // Actual conversion
36 const auto res =
37 fast_float::from_chars(buf.c_str(), buf.c_str() + buf.size(), x);
38
39 // Error (only std::errc::invalid_argument possible)
40 ARTS_USER_ERROR_IF(res.ec == std::errc::invalid_argument,
41 "The argument: \n\n'",
42 buf,
43 R"--('
44
45is not convertible to a valid double. At the very least it
46cannot be converted to one using the standard string-to-double
47routine
48)--")
49
50 // Put the stream to be where it is supposed to be
51 std::size_t n = std::distance(buf.c_str(), res.ptr);
52 while (n++ < buf.size()) is.unget();
53
54 return *this;
55 }
56
57 std::istream& operator>>(const double_imanip&) const { return *in; }
59 friend const double_imanip& operator>>(std::istream& in,
60 const double_imanip& dm);
61
62 private:
63 mutable std::istream* in;
64};
65
66inline const double_imanip& operator>>(std::istream& in,
67 const double_imanip& dm) {
68 dm.in = &in;
69 return dm;
70}
71
72#endif
Input manipulator class for doubles to enable nan and inf parsing.
Definition: double_imanip.h:25
std::istream * in
Definition: double_imanip.h:58
friend const double_imanip & operator>>(std::istream &in, const double_imanip &dm)
Definition: double_imanip.h:66
const double_imanip & operator>>(double &x) const
Definition: double_imanip.h:27
Helper macros for debugging.
#define ARTS_USER_ERROR_IF(condition,...)
Definition: debug.h:135
const double_imanip & operator>>(std::istream &in, const double_imanip &dm)
Definition: double_imanip.h:66