athena/include/athena/Global.hpp

266 lines
14 KiB
C++
Raw Normal View History

2014-04-20 02:14:15 -07:00
#ifndef GLOBAL_HPP
#define GLOBAL_HPP
#include <iostream>
2016-03-04 15:00:12 -08:00
#include "athena/Types.hpp"
2014-04-20 02:14:15 -07:00
#ifdef _MSC_VER
#pragma warning(disable : 4996)
2015-06-19 21:40:15 -07:00
2017-12-05 19:22:03 -08:00
#if defined(WINAPI_FAMILY) && WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP
#define WINDOWS_STORE 1
#else
#define WINDOWS_STORE 0
#endif
2015-06-19 21:40:15 -07:00
#include <sys/stat.h>
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
2017-01-03 10:32:56 -08:00
#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
2015-06-19 21:40:15 -07:00
#endif
#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
2017-01-03 10:32:56 -08:00
#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
2015-06-19 21:40:15 -07:00
#endif
#if !defined(S_ISLNK)
#define S_ISLNK(m) 0
#endif
2015-11-14 15:59:10 -08:00
#define PRISize "Iu"
#else
#define PRISize "zu"
#endif
2017-01-03 10:32:56 -08:00
// clang-format off
#ifndef AT_PRETTY_FUNCTION
2015-05-03 20:57:00 -07:00
# if defined(__PRETTY_FUNCTION__) || defined(__GNUC__)
# define AT_PRETTY_FUNCTION __PRETTY_FUNCTION__
# elif defined(__FUNCSIG__)
# define AT_PRETTY_FUNCTION __FUNCSIG__
2014-06-02 20:09:40 -07:00
# elif defined(__FUNCTION__)
# define AT_PRETTY_FUNCTION __FUNCTION__
2014-06-02 20:09:40 -07:00
# elif defined(__FUNC__)
# define AT_PRETTY_FUNCTION __FUNC__
2014-06-02 20:09:40 -07:00
# elif defined(__func__)
# define AT_PRETTY_FUNCTION __func__
2014-06-02 20:09:40 -07:00
# else
# define AT_PRETTY_FUNCTION "<unknown>"
2014-06-02 20:09:40 -07:00
# endif
2014-05-14 22:42:20 -07:00
#endif
2017-01-03 10:32:56 -08:00
// clang-format on
2018-09-20 10:47:48 -07:00
#if defined(GEKKO) || defined(__SWITCH__)
#include "gekko_support.h"
typedef struct stat atStat64_t;
#define atStat64 stat
2015-06-19 21:40:15 -07:00
#elif _WIN32
typedef struct _stat64 atStat64_t;
2016-10-19 17:51:38 -07:00
#define atStat64 _stat64
#elif __APPLE__ || __FreeBSD__
typedef struct stat atStat64_t;
#define atStat64 stat
#else
typedef struct stat64 atStat64_t;
2016-10-15 12:17:06 -07:00
#define atStat64 stat64
#endif
2014-04-20 02:14:15 -07:00
#ifndef BLOCKSZ
#define BLOCKSZ 512
#endif
2016-04-03 19:26:47 -07:00
#define ROUND_UP_256(val) (((val) + 255) & ~255)
2016-04-03 19:31:25 -07:00
#define ROUND_UP_64(val) (((val) + 63) & ~63)
#define ROUND_UP_32(val) (((val) + 31) & ~31)
#define ROUND_UP_16(val) (((val) + 15) & ~15)
2017-01-03 21:24:31 -08:00
#define ROUND_UP_8(val) (((val) + 7) & ~7)
2016-03-30 23:17:05 -07:00
#define ROUND_UP_4(val) (((val) + 3) & ~3)
2016-04-03 19:26:47 -07:00
#define _XSTR(s) _STR(s)
#define _STR(s) #s
#ifndef ENABLE_BITWISE_ENUM
2017-01-03 10:32:56 -08:00
#define ENABLE_BITWISE_ENUM(type) \
constexpr type operator|(type a, type b) \
{ \
using T = std::underlying_type_t<type>; \
return type(static_cast<T>(a) | static_cast<T>(b)); \
} \
constexpr type operator&(type a, type b) \
{ \
using T = std::underlying_type_t<type>; \
return type(static_cast<T>(a) & static_cast<T>(b)); \
} \
inline type& operator|=(type& a, const type& b) \
{ \
using T = std::underlying_type_t<type>; \
a = type(static_cast<T>(a) | static_cast<T>(b)); \
return a; \
} \
inline type& operator&=(type& a, const type& b) \
{ \
using T = std::underlying_type_t<type>; \
a = type(static_cast<T>(a) & static_cast<T>(b)); \
return a; \
} \
inline type operator~(const type& key) \
{ \
using T = std::underlying_type_t<type>; \
return type(~static_cast<T>(key)); \
}
#endif
2015-11-20 17:11:23 -08:00
2016-03-04 15:00:12 -08:00
namespace athena
2014-04-20 02:14:15 -07:00
{
2015-07-21 22:37:22 -07:00
namespace error
{
enum class Level
2015-07-21 22:37:22 -07:00
{
Message,
Warning,
Error,
Fatal
2015-07-21 22:37:22 -07:00
};
}
2015-06-15 19:29:53 -07:00
enum SeekOrigin
2014-04-20 02:14:15 -07:00
{
Begin,
Current,
End
};
2015-06-15 19:29:53 -07:00
enum Endian
2014-04-20 02:14:15 -07:00
{
Little,
Big
2014-04-20 02:14:15 -07:00
};
namespace io
{
template <Endian DNAE>
struct DNA;
template <Endian DNAE>
struct DNAV;
template <class T>
2018-02-26 22:02:24 -08:00
using __IsDNARecord = typename std::disjunction<std::is_base_of<DNA<Endian::Big>, T>,
std::is_base_of<DNA<Endian::Little>, T>>;
template <class T>
2018-02-26 22:02:24 -08:00
inline constexpr bool __IsDNARecord_v = __IsDNARecord<T>::value;
template <class T>
using __IsDNAVRecord = typename std::disjunction<std::is_base_of<DNAV<Endian::Big>, T>,
std::is_base_of<DNAV<Endian::Little>, T>>;
template <class T>
inline constexpr bool __IsDNAVRecord_v = __IsDNAVRecord<T>::value;
}
2014-04-20 02:14:15 -07:00
} // Athena
2017-01-03 10:32:56 -08:00
typedef void (*atEXCEPTION_HANDLER)(athena::error::Level level, const char* file, const char* function, int line,
const char* fmt, ...);
2015-05-18 16:28:17 -07:00
atEXCEPTION_HANDLER atGetExceptionHandler();
2016-02-03 13:33:08 -08:00
/**
* atSetExceptionHandler is only meant to be used a the start and end of an application's lifetime,
* this function cannot be considered thread-safe, therefore modifying during runtime is not recommended.
*/
2015-05-18 16:28:17 -07:00
void atSetExceptionHandler(atEXCEPTION_HANDLER func);
2016-03-04 15:00:12 -08:00
std::ostream& operator<<(std::ostream& os, const athena::SeekOrigin& origin);
std::ostream& operator<<(std::ostream& os, const athena::Endian& endian);
2015-07-21 22:37:22 -07:00
#ifdef _MSC_VER
#ifndef NDEBUG
2017-01-03 10:32:56 -08:00
#define atDebug(fmt, ...) \
do \
{ \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
} while (0)
2015-07-21 22:37:22 -07:00
#else
#define atDebug(fmt, ...)
#endif
2017-01-03 10:32:56 -08:00
#define atMessage(fmt, ...) \
do \
{ \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
} while (0)
#define atWarning(fmt, ...) \
do \
{ \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(athena::error::Level::Warning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
} while (0)
#define atError(fmt, ...) \
do \
{ \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(athena::error::Level::Error, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
} while (0)
#define atFatal(fmt, ...) \
do \
{ \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(athena::error::Level::Fatal, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
} while (0)
2015-07-21 22:37:22 -07:00
#elif defined(__GNUC__)
#ifndef NDEBUG
2017-01-03 10:32:56 -08:00
#define atDebug(fmt...) \
do \
{ \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while (0)
2015-07-21 22:37:22 -07:00
#else // _MSC_VER
#define atDebug(fmt, ...)
#endif // NDEBUG
2017-01-03 10:32:56 -08:00
#define atMessage(fmt...) \
do \
{ \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while (0)
#define atWarning(fmt...) \
do \
{ \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(athena::error::Level::Warning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while (0)
#define atError(fmt...) \
do \
{ \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(athena::error::Level::Error, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while (0)
#define atFatal(fmt...) \
do \
{ \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(athena::error::Level::Fatal, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while (0)
2015-07-21 22:37:22 -07:00
#endif // defined(__GNUC__)
2014-04-20 02:14:15 -07:00
#endif // GLOBAL_HPP