platform.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include <nall/intrinsics.hpp>
  3. namespace Math {
  4. static const long double e = 2.71828182845904523536;
  5. static const long double Pi = 3.14159265358979323846;
  6. }
  7. #if defined(PLATFORM_WINDOWS)
  8. #include <nall/windows/guard.hpp>
  9. #include <initguid.h>
  10. #include <cguid.h>
  11. #include <winsock2.h>
  12. #include <ws2tcpip.h>
  13. #include <windows.h>
  14. #include <direct.h>
  15. #include <io.h>
  16. #include <wchar.h>
  17. #include <shlobj.h>
  18. #include <shellapi.h>
  19. #include <nall/windows/guard.hpp>
  20. #include <nall/windows/utf8.hpp>
  21. #endif
  22. #include <atomic>
  23. #include <limits>
  24. #include <mutex>
  25. #include <utility>
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <limits.h>
  29. #include <math.h>
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <time.h>
  35. #include <utime.h>
  36. #include <fcntl.h>
  37. #include <unistd.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #if !defined(PLATFORM_WINDOWS)
  41. #include <dlfcn.h>
  42. #include <unistd.h>
  43. #include <pwd.h>
  44. #include <grp.h>
  45. #include <sys/socket.h>
  46. #include <sys/wait.h>
  47. #include <netinet/in.h>
  48. #include <netdb.h>
  49. #include <poll.h>
  50. #endif
  51. #if defined(COMPILER_MICROSOFT)
  52. #define va_copy(dest, src) ((dest) = (src))
  53. #endif
  54. #if defined(PLATFORM_WINDOWS)
  55. #undef IN
  56. #undef OUT
  57. #undef interface
  58. #define dllexport __declspec(dllexport)
  59. #define MSG_NOSIGNAL 0
  60. extern "C" {
  61. using pollfd = WSAPOLLFD;
  62. }
  63. inline auto access(const char* path, int amode) -> int { return _waccess(nall::utf16_t(path), amode); }
  64. inline auto getcwd(char* buf, size_t size) -> char* { wchar_t wpath[PATH_MAX] = L""; if(!_wgetcwd(wpath, size)) return nullptr; strcpy(buf, nall::utf8_t(wpath)); return buf; }
  65. inline auto mkdir(const char* path, int mode) -> int { return _wmkdir(nall::utf16_t(path)); }
  66. inline auto poll(struct pollfd fds[], unsigned long nfds, int timeout) -> int { return WSAPoll(fds, nfds, timeout); }
  67. inline auto putenv(const char* value) -> int { return _wputenv(nall::utf16_t(value)); }
  68. inline auto realpath(const char* file_name, char* resolved_name) -> char* { wchar_t wfile_name[PATH_MAX] = L""; if(!_wfullpath(wfile_name, nall::utf16_t(file_name), PATH_MAX)) return nullptr; strcpy(resolved_name, nall::utf8_t(wfile_name)); return resolved_name; }
  69. inline auto rename(const char* oldname, const char* newname) -> int { return _wrename(nall::utf16_t(oldname), nall::utf16_t(newname)); }
  70. namespace nall {
  71. //network functions take void*, not char*. this allows them to be used without casting
  72. inline auto recv(int socket, void* buffer, size_t length, int flags) -> ssize_t {
  73. return ::recv(socket, (char*)buffer, length, flags);
  74. }
  75. inline auto send(int socket, const void* buffer, size_t length, int flags) -> ssize_t {
  76. return ::send(socket, (const char*)buffer, length, flags);
  77. }
  78. inline auto setsockopt(int socket, int level, int option_name, const void* option_value, socklen_t option_len) -> int {
  79. return ::setsockopt(socket, level, option_name, (const char*)option_value, option_len);
  80. }
  81. }
  82. #else
  83. #define dllexport
  84. #endif
  85. #if defined(PLATFORM_MACOS)
  86. #define MSG_NOSIGNAL 0
  87. #endif
  88. #if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
  89. #define noinline __attribute__((noinline))
  90. #define alwaysinline inline __attribute__((always_inline))
  91. #elif defined(COMPILER_MICROSOFT)
  92. #define noinline __declspec(noinline)
  93. #define alwaysinline inline __forceinline
  94. #else
  95. #define noinline
  96. #define alwaysinline inline
  97. #endif
  98. //P0627: [[unreachable]] -- impossible to simulate with identical syntax, must omit brackets ...
  99. #if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
  100. #define unreachable __builtin_unreachable()
  101. #else
  102. #define unreachable throw
  103. #endif
  104. #define export $export
  105. #define register $register