Skip to content

Commit

Permalink
Foundation: Add Host Operating System detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagghiu committed Jun 27, 2024
1 parent 1f33202 commit 95d90ab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Libraries/Foundation/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ T& fieldOffset(R& object)

#undef SC_LANGUAGE_CPP_VERSION

#if !SC_COMPILER_MSVC || SC_LANGUAGE_CPP_AT_LEAST_20
#if SC_LANGUAGE_CPP_AT_LEAST_20
#define SC_LANGUAGE_LIKELY [[likely]] ///< Use `[[likely]]` if available
#define SC_LANGUAGE_UNLIKELY [[unlikely]] ///< Use `[[unlikely]]` if available
#else
Expand Down
26 changes: 24 additions & 2 deletions Libraries/Foundation/Foundation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,32 @@ using ssize_t = SSIZE_T;
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else

#if SC_PLATFORM_APPLE
#include <TargetConditionals.h>
#endif

#include <execinfo.h> // backtrace
#include <unistd.h> // _exit
#endif

SC::OperatingSystem::Type SC::OperatingSystem::getHostOS()
{
#if SC_PLATFORM_WINDOWS
return Windows;
#elif SC_PLATFORM_LINUX
return Linux;
#elif SC_PLATFORM_EMSCRIPTEN
return emscripten;
#elif SC_PLATFORM_APPLE
#if TARGET_OS_IPHONE
return iOS;
#else
return macOS;
#endif
#endif
}

//--------------------------------------------------------------------
// Assert
//--------------------------------------------------------------------
Expand Down Expand Up @@ -103,8 +125,8 @@ bool SC::Assert::printBacktrace(void** backtraceBuffer, size_t backtraceBufferSi
SC::size_t SC::Assert::captureBacktrace(size_t framesToSkip, void** backtraceBuffer, size_t backtraceBufferSizeInBytes,
uint32_t* hash)
{
const size_t framesToCapture = backtraceBufferSizeInBytes / sizeof(void*);
constexpr auto maxVal = static_cast<size_t>(static_cast<int>(MaxValue()));
const size_t framesToCapture = backtraceBufferSizeInBytes / sizeof(void*);
constexpr auto maxVal = static_cast<size_t>(static_cast<int>(MaxValue()));
if (framesToCapture > maxVal || (backtraceBuffer == nullptr))
{
return 0;
Expand Down
16 changes: 16 additions & 0 deletions Libraries/Foundation/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ enum class Platform
Emscripten,
};

/// @brief Holds information about operating system
struct OperatingSystem
{
enum Type
{
macOS,
iOS,
Emscripten,
Windows,
Linux,
};

/// @brief Returns the currently active host operating system
[[nodiscard]] static Type getHostOS();
};

#if defined(__APPLE__)

#define SC_PLATFORM_APPLE 1 ///< True (1) when code is compiled on macOS and iOS
Expand Down

0 comments on commit 95d90ab

Please sign in to comment.