xerror.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sstream>
4 #include <stdexcept>
5 
6 namespace nmx {
7 
11 struct Error {
19  inline static std::string msg(const char *file, int line, const char *func) {
20  std::stringstream myoutput;
21  myoutput << file << "\n" << line << "\n" << func;
22  return myoutput.str();
23  }
24 
33  template<class T>
34  inline static std::string msgx(const T &msg, const char *file, int line, const char *func) {
35  std::stringstream myoutput;
36  myoutput << msg << "\n" << file << "\n" << line << "\n" << func;
37  return myoutput.str();
38  }
39 }; //Error
40 
44 struct Check {
50  template<class X = std::runtime_error>
51  void inline static error_if(const std::string &s, bool arg) {
52  if (arg) {
53  throw X(s);
54  }
55  }
56 
62  template<class X = std::runtime_error>
63  void inline static error_if_not(const std::string &s, bool arg) {
64  if (!arg) {
65  throw X(s);
66  }
67  }
68 
75  template<class X = std::invalid_argument>
76  void inline static all(const std::string &s, std::initializer_list<bool> lst) {
77  size_t counter = 0;
78  //iteriere über alle Bedingungen. Jede Bedingung ist ein
79  // Element der Liste und wird deswegen einem Index zugeordnet.
80  for (auto itr = lst.begin(); itr != lst.end(); itr++) {
81  if (!*itr) {
82  // wenn eine nicht erfüllt, schreibe den Index in
83  // die Nachricht
84  std::stringstream sstream;
85  sstream << s << " invalid argument:" << counter;
86  throw X(sstream.str());
87  }
88  counter++;
89  }
90  }
91 
97  template<class T, class X = std::invalid_argument>
98  void inline static ptr(const std::string &s, T *ptr) {
99  if (ptr == nullptr) {
100  std::stringstream sstream;
101  sstream << "nullpointer:" << s;
102  throw X(sstream.str());
103  }
104  }
105 }; //Check
106 
107 } // namespace nmx
108 
113 #define nmx_msg nmx::Error::msg(__FILE__, __LINE__, __PRETTY_FUNCTION__)
114 
119 #define nmx_msg_txt(msg) nmx::Error::msgx(msg, __FILE__, __LINE__, __PRETTY_FUNCTION__)
120 
125 #define nmx_msgx(msg) nmx::Error::msgx(#msg, __FILE__, __LINE__, __PRETTY_FUNCTION__)
static void ptr(const std::string &s, T *ptr)
null_ptr teste Zeiger auf Gültigkeit
Definition: xerror.h:98
static void error_if(const std::string &s, bool arg)
error_if Fehler wenn Bedingung erfüllt ist
Definition: xerror.h:51
int func(double x, const double yin[], double yout[], void *params)
func rechte Seite der Differenzialgleichung
Definition: x036.h:24
static void error_if_not(const std::string &s, bool arg)
error_if_not Fehler wenn Bedingung nicht erfüllt ist
Definition: xerror.h:63
The Error struct erzeugt Fehlermeldungen.
Definition: xerror.h:11
static std::string msg(const char *file, int line, const char *func)
msg Text einer Fehlermeldung zusammengesetzt aus
Definition: xerror.h:19
static std::string msgx(const T &msg, const char *file, int line, const char *func)
info Text einer Fehlermeldung zusammengesetzt aus
Definition: xerror.h:34
The Check struct Testfunktionen zum Auffinden von Fehlern.
Definition: xerror.h:44
static void all(const std::string &s, std::initializer_list< bool > lst)
input teste mehrere Bedingungen auf einmal
Definition: xerror.h:76