OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
error_handler.hpp
Go to the documentation of this file.
1 //
2 // Copyright & License: See Copyright.readme in src directory
3 //
4 
9 #ifndef ERROR_HANDLER_HPP_
10 #define ERROR_HANDLER_HPP_
11 
12 #include <iostream>
13 #include <string>
14 #include <vector>
15 
16 namespace SDDS
17 {
19  // The error handler
21  template <typename Iterator>
23  {
24  template <typename, typename, typename>
25  struct result { typedef void type; };
26 
27  error_handler(Iterator first, Iterator last)
28  : first(first), last(last) {}
29 
30  template <typename Message, typename What>
31  void operator()(
32  Message const& message,
33  What const& what,
34  Iterator err_pos) const
35  {
36  int line;
37  Iterator line_start = get_pos(err_pos, line);
38  if (err_pos != last)
39  {
40  std::cout << message << what << " line " << line << ':' << std::endl;
41  std::cout << get_line(line_start) << std::endl;
42  for (; line_start != err_pos; ++line_start)
43  std::cout << ' ';
44  std::cout << '^' << std::endl;
45  }
46  else
47  {
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  {
55  line = 1;
56  Iterator i = first;
57  Iterator line_start = first;
58  while (i != err_pos)
59  {
60  bool eol = false;
61  if (i != err_pos && *i == '\r') // CR
62  {
63  eol = true;
64  line_start = ++i;
65  }
66  if (i != err_pos && *i == '\n') // LF
67  {
68  eol = true;
69  line_start = ++i;
70  }
71  if (eol)
72  ++line;
73  else
74  ++i;
75  }
76  return line_start;
77  }
78 
79  std::string get_line(Iterator err_pos) const
80  {
81  Iterator i = err_pos;
82  // position i to the next EOL
83  while (i != last && (*i != '\r' && *i != '\n'))
84  ++i;
85  return std::string(err_pos, i);
86  }
87 
88  Iterator first;
89  Iterator last;
90  std::vector<Iterator> iters;
91  };
92 }
93 
94 #endif
95 
void operator()(Message const &message, What const &what, Iterator err_pos) const
std::vector< Iterator > iters
Iterator get_pos(Iterator err_pos, int &line) const
error_handler(Iterator first, Iterator last)
std::string get_line(Iterator err_pos) const
The WHAT command.
Definition: What.h:28
Inform & endl(Inform &inf)
Definition: Inform.cpp:42