LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_status.h
Go to the documentation of this file.
1 /*
2  LibNRG - Networking for Real-time Games
3 
4  Copyright (C) 2012-2014 Alex Baines <alex@abaines.me.uk>
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you must not
15  claim that you wrote the original software. If you use this software
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20  3. This notice may not be removed or altered from any source distribution.
21 */
25 #ifndef NRG_STATUS_H
26 #define NRG_STATUS_H
27 #include <errno.h>
28 #include <string.h>
29 #include <type_traits>
30 
31 namespace nrg {
32 
34 struct Status {
36  Status() : Status(true){}
37 
39  Status(bool ok, int err = 0)
40  : type(ok ? Ok : SystemError)
41  , sys_errno(ok ? 0 : err)
42  , desc(ok ? "No error." : "System call returned error."){
43 
44  }
45 
47  Status(const char* custom)
48  : type(InternalError)
49  , sys_errno(0)
50  , desc(custom){
51 
52  }
53 
55  enum : int {
56  Ok,
57  InternalError,
58  SystemError
59  } type;
60 
62  int sys_errno;
63 
65  const char* desc;
66 
68  operator bool() const {
69  return type == Ok;
70  }
71 };
72 
74 struct StatusOK : Status {
75  StatusOK() : Status(true){}
76 };
77 
79 struct StatusErr : Status {
80  StatusErr(int e = errno) : Status(false, e){}
81 };
82 
84 static inline const char* strerr_r(int eno, char* buf, size_t sz){
85 #ifdef _WIN32
86  strerror_s(buf, sz, eno);
87  return buf;
88 #else
89  auto r = strerror_r(eno, buf, sz);
90 
91  if(std::is_same<decltype(r), int>::value){
92  return buf;
93  } else {
94  return r;
95  }
96 #endif
97 }
98 
99 }
100 
101 #endif
int sys_errno
The associated errno for the error, or 0 if not a system error.
Definition: nrg_status.h:62
Status(const char *custom)
Constructor for custom errors, with the specified error string.
Definition: nrg_status.h:47
Derived class for system errors.
Definition: nrg_status.h:79
enum nrg::Status::@0 type
Enumeration for error type.
Derived class for non-error statuses.
Definition: nrg_status.h:74
Class to wrap system and internal errors.
Definition: nrg_status.h:34
Status(bool ok, int err=0)
Constructor with the given error-status and errno for system-errors.
Definition: nrg_status.h:39
const char * desc
Statically-allocated description of the error.
Definition: nrg_status.h:65
Status()
Default Constructor, creates a no-error status.
Definition: nrg_status.h:36