OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
error_handler.hpp
Go to the documentation of this file.
1//
2// Struct error_handler
3//
4// Copyright (c) 2015, Christof Metzger-Kraus, Helmholtz-Zentrum Berlin
5// All rights reserved
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General Public License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17#ifndef ERROR_HANDLER_HPP_
18#define ERROR_HANDLER_HPP_
19
20#include <iostream>
21#include <string>
22#include <vector>
23
24namespace SDDS
25{
27 // The error handler
29 template <typename Iterator>
31 {
32 template <typename, typename, typename>
33 struct result { typedef void type; };
34
35 error_handler(Iterator first, Iterator last)
36 : first(first), last(last) {}
37
38 template <typename Message, typename What>
40 Message const& message,
41 What const& what,
42 Iterator err_pos) const
43 {
44 int line;
45 Iterator line_start = get_pos(err_pos, line);
46 if (err_pos != last)
47 {
48 std::cout << message << what << " line " << line << ':' << std::endl;
49 std::cout << get_line(line_start) << std::endl;
50 for (; line_start != err_pos; ++line_start)
51 std::cout << ' ';
52 std::cout << '^' << std::endl;
53 }
54 else
55 {
56 std::cout << "Unexpected end of file. ";
57 std::cout << message << what << " line " << line << std::endl;
58 }
59 }
60
61 Iterator get_pos(Iterator err_pos, int& line) const
62 {
63 line = 1;
64 Iterator i = first;
65 Iterator line_start = first;
66 while (i != err_pos)
67 {
68 bool eol = false;
69 if (i != err_pos && *i == '\r') // CR
70 {
71 eol = true;
72 line_start = ++i;
73 }
74 if (i != err_pos && *i == '\n') // LF
75 {
76 eol = true;
77 line_start = ++i;
78 }
79 if (eol)
80 ++line;
81 else
82 ++i;
83 }
84 return line_start;
85 }
86
87 std::string get_line(Iterator err_pos) const
88 {
89 Iterator i = err_pos;
90 // position i to the next EOL
91 while (i != last && (*i != '\r' && *i != '\n'))
92 ++i;
93 return std::string(err_pos, i);
94 }
95
96 Iterator first;
97 Iterator last;
98 std::vector<Iterator> iters;
99 };
100}
101
102#endif
103
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
void operator()(Message const &message, What const &what, Iterator err_pos) const
std::vector< Iterator > iters
std::string get_line(Iterator err_pos) const
Iterator get_pos(Iterator err_pos, int &line) const
error_handler(Iterator first, Iterator last)