Athena IO Library
Global.hpp
1 #ifndef GLOBAL_HPP
2 #define GLOBAL_HPP
3 
4 #include <iostream>
5 #include "athena/Types.hpp"
6 #include "athena/Utility.hpp"
7 
8 #ifdef _MSC_VER
9 #pragma warning(disable : 4996)
10 
11 #include <sys/stat.h>
12 
13 #if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
14 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
15 #endif
16 
17 #if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
18 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
19 #endif
20 
21 #if !defined(S_ISLNK)
22 #define S_ISLNK(m) 0
23 #endif
24 
25 #define PRISize "Iu"
26 
27 #else
28 
29 #define PRISize "zu"
30 
31 #endif
32 
33 #ifndef AT_PRETTY_FUNCTION
34 # if defined(__PRETTY_FUNCTION__) || defined(__GNUC__)
35 # define AT_PRETTY_FUNCTION __PRETTY_FUNCTION__
36 # elif defined(__FUNCSIG__)
37 # define AT_PRETTY_FUNCTION __FUNCSIG__
38 # elif defined(__FUNCTION__)
39 # define AT_PRETTY_FUNCTION __FUNCTION__
40 # elif defined(__FUNC__)
41 # define AT_PRETTY_FUNCTION __FUNC__
42 # elif defined(__func__)
43 # define AT_PRETTY_FUNCTION __func__
44 # else
45 # define AT_PRETTY_FUNCTION "<unknown>"
46 # endif
47 #endif
48 
49 
50 #ifdef GEKKO
51 #include "gekko_support.h"
52 typedef struct stat stat64_t;
53 #define stat64 stat
54 #elif _WIN32
55 typedef struct _stat64 stat64_t;
56 #elif __FreeBSD__
57 typedef struct stat stat64_t;
58 #define stat64 stat
59 #define fseeko64 fseeko
60 #define ftello64 ftello
61 #else
62 typedef struct stat64 stat64_t;
63 #endif
64 
65 #ifndef BLOCKSZ
66 #define BLOCKSZ 512
67 #endif
68 
69 #define ROUND_UP_256(val) (((val) + 255) & ~255)
70 #define ROUND_UP_64(val) (((val) + 63) & ~63)
71 #define ROUND_UP_32(val) (((val) + 31) & ~31)
72 #define ROUND_UP_16(val) (((val) + 15) & ~15)
73 #define ROUND_UP_4(val) (((val) + 3) & ~3)
74 
75 #define _XSTR(s) _STR(s)
76 #define _STR(s) #s
77 
78 #ifndef ENABLE_BITWISE_ENUM
79 #define ENABLE_BITWISE_ENUM(type)\
80 constexpr type operator|(type a, type b)\
81 {\
82  using T = std::underlying_type_t<type>;\
83  return type(static_cast<T>(a) | static_cast<T>(b));\
84 }\
85 constexpr type operator&(type a, type b)\
86 {\
87  using T = std::underlying_type_t<type>;\
88  return type(static_cast<T>(a) & static_cast<T>(b));\
89 }\
90 inline type& operator|=(type& a, const type& b)\
91 {\
92  using T = std::underlying_type_t<type>;\
93  a = type(static_cast<T>(a) | static_cast<T>(b));\
94  return a;\
95 }\
96 inline type& operator&=(type& a, const type& b)\
97 {\
98  using T = std::underlying_type_t<type>;\
99  a = type(static_cast<T>(a) & static_cast<T>(b));\
100  return a;\
101 }\
102 inline type operator~(const type& key)\
103 {\
104  using T = std::underlying_type_t<type>;\
105  return type(~static_cast<T>(key));\
106 }
107 #endif
108 
109 namespace athena
110 {
111 namespace error
112 {
113 enum class Level
114 {
115  Message,
116  Warning,
117  Error,
118  Fatal
119 };
120 }
121 enum SeekOrigin
122 {
123  Begin,
124  Current,
125  End
126 };
127 
128 enum Endian
129 {
130  LittleEndian,
131  BigEndian
132 };
133 } // Athena
134 
135 typedef void (*atEXCEPTION_HANDLER)(athena::error::Level level, const char* file, const char* function, int line, const char* fmt, ...);
136 
137 atEXCEPTION_HANDLER atGetExceptionHandler();
142 void atSetExceptionHandler(atEXCEPTION_HANDLER func);
143 
144 std::ostream& operator<<(std::ostream& os, const athena::SeekOrigin& origin);
145 std::ostream& operator<<(std::ostream& os, const athena::Endian& endian);
146 
147 #ifdef _MSC_VER
148 #ifndef NDEBUG
149 #define atDebug(fmt, ...) \
150  do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
151  if (__handler) \
152  __handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
153 } while(0)
154 #else
155 #define atDebug(fmt, ...)
156 #endif
157 
158 #define atMessage(fmt, ...) \
159  do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
160  if (__handler) \
161  __handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
162 } while(0)
163 
164 #define atWarning(fmt, ...) \
165  do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
166  if (__handler) \
167  __handler(athena::error::Level::Warning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
168 } while(0)
169 
170 #define atError(fmt, ...) \
171  do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
172  if (__handler) \
173  __handler(athena::error::Level::Error, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
174 } while(0)
175 
176 #define atFatal(fmt, ...) \
177  do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
178  if (__handler) \
179  __handler(athena::error::Level::Fatal, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
180 } while(0)
181 #elif defined(__GNUC__)
182 
183 #ifndef NDEBUG
184 #define atDebug(fmt...) \
185  do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
186  if (__handler) \
187  __handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
188 } while(0)
189 #else // _MSC_VER
190 #define atDebug(fmt, ...)
191 #endif // NDEBUG
192 
193 #define atMessage(fmt...) \
194  do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
195  if (__handler) \
196  __handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
197 } while(0)
198 
199 #define atWarning(fmt...) \
200  do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
201  if (__handler) \
202  __handler(athena::error::Level::Warning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
203 } while(0)
204 
205 #define atError(fmt...) \
206  do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
207  if (__handler) \
208  __handler(athena::error::Level::Error, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
209 } while(0)
210 
211 #define atFatal(fmt...) \
212  do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
213  if (__handler) \
214  __handler(athena::error::Level::Fatal, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
215 } while(0)
216 #endif // defined(__GNUC__)
217 
218 #endif // GLOBAL_HPP