OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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 {
26 // The error handler
28 template <typename Iterator>
30 template <typename, typename, typename>
31 struct result {
32 typedef void type;
33 };
34
35 error_handler(Iterator first, Iterator last) : first(first), last(last) {}
36
37 template <typename Message, typename What>
38 void operator()(Message const& message, What const& what, Iterator err_pos) const {
39 int line;
40 Iterator line_start = get_pos(err_pos, line);
41 if (err_pos != last) {
42 std::cout << message << what << " line " << line << ':' << std::endl;
43 std::cout << get_line(line_start) << std::endl;
44 for (; line_start != err_pos; ++line_start)
45 std::cout << ' ';
46 std::cout << '^' << std::endl;
47 } else {
48 std::cout << "Unexpected end of file. ";
49 std::cout << message << what << " line " << line << std::endl;
50 }
51 }
52
53 Iterator get_pos(Iterator err_pos, int& line) const {
54 line = 1;
55 Iterator i = first;
56 Iterator line_start = first;
57 while (i != err_pos) {
58 bool eol = false;
59 if (i != err_pos && *i == '\r') // CR
60 {
61 eol = true;
62 line_start = ++i;
63 }
64 if (i != err_pos && *i == '\n') // LF
65 {
66 eol = true;
67 line_start = ++i;
68 }
69 if (eol)
70 ++line;
71 else
72 ++i;
73 }
74 return line_start;
75 }
76
77 std::string get_line(Iterator err_pos) const {
78 Iterator i = err_pos;
79 // position i to the next EOL
80 while (i != last && (*i != '\r' && *i != '\n'))
81 ++i;
82 return std::string(err_pos, i);
83 }
84
85 Iterator first;
86 Iterator last;
87 std::vector<Iterator> iters;
88 };
89} // namespace SDDS
90
91#endif
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)