ewmscp  ..
throwcall.h
Go to the documentation of this file.
1 #ifndef __throwcall_h__
2 #define __throwcall_h__
3 
4 /*
5  template functions to do exceptions for standard syscalls
6  Copyright (C) 2018 Juergen Hannappel
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21 
22 #include <system_error>
23 #include <sstream>
24 
25 namespace throwcall {
26  template <typename T> void conCatString(std::ostringstream& msg, const T& begin) {
27  msg << begin;
28  }
29  template <typename T, typename ... Args> inline void conCatString(std::ostringstream& msg,
30  const T& begin,
31  const Args& ... args) {
32  msg << begin;
33  if (sizeof...(args)) {
34  conCatString(msg, args...);
35  }
36  }
40  template <typename T, typename ... Args> inline void good0(T call, const Args& ... args) {
41  if (__builtin_expect(call, 0)) {
42  std::ostringstream msg;
43  conCatString(msg, args...);
44  throw std::system_error(errno, std::system_category(), msg.str());
45  }
46  };
54  template <typename T, typename t, typename ... Args> inline T badval(T call, t badvalue, const Args& ... args) {
55  if (__builtin_expect(call == badvalue, false)) {
56  std::ostringstream msg;
57  conCatString(msg, args...);
58  throw std::system_error(errno, std::system_category(), msg.str());
59  }
60  return call;
61  };
62 }
63 #endif
throwcall::conCatString
void conCatString(std::ostringstream &msg, const T &begin)
Definition: throwcall.h:26
throwcall::badval
T badval(T call, t badvalue, const Args &... args)
template function to wrap system calls that return a special bad value on failure
Definition: throwcall.h:54
throwcall
Definition: throwcall.h:25
throwcall::good0
void good0(T call, const Args &... args)
template function to wrap system calls that return 0 on success
Definition: throwcall.h:40