Skip to content

Commit

Permalink
Minor changes, updated asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Jun 12, 2024
1 parent 5c9ec4c commit 756d8a5
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 56 deletions.
69 changes: 35 additions & 34 deletions Sources/Shared/Asserts.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#pragma once

#include "Common.h" // For DEATH_HELPER_PICK
#include "Common.h" // for DEATH_HELPER_PICK

#if !defined(DEATH_NO_ASSERT) && (!defined(DEATH_ASSERT) || !defined(DEATH_CONSTEXPR_ASSERT) || !defined(DEATH_ASSERT_UNREACHABLE))
# if !defined(DEATH_DEBUG) && defined(DEATH_TRACE)
# include <cstdlib>
# else
#if !defined(DEATH_NO_ASSERT) && (!defined(DEATH_ASSERT) || !defined(DEATH_DEBUG_ASSERT) || !defined(DEATH_CONSTEXPR_ASSERT) || !defined(DEATH_DEBUG_CONSTEXPR_ASSERT) || !defined(DEATH_ASSERT_UNREACHABLE))
# if defined(DEATH_STANDARD_ASSERT)
# include <cassert>
# elif defined(DEATH_TRACE)
# include <cstdlib>
# endif
#endif

// Event Tracing
#if defined(DEATH_TRACE)

// If symbol is #defined with no value, supply internal function name
#if DEATH_PASTE(DEATH_TRACE, 1) == 1 || DEATH_PASTE(DEATH_TRACE, 1) == 11
# undef DEATH_TRACE
# define DEATH_TRACE __WriteTrace
#endif
// If DEATH_TRACE symbol is #defined with no value, supply internal function name
# if DEATH_PASTE(DEATH_TRACE, 1) == 1 || DEATH_PASTE(DEATH_TRACE, 1) == 11
# undef DEATH_TRACE
# define DEATH_TRACE __WriteTrace
# endif

enum class TraceLevel {
Unknown,
Expand All @@ -28,6 +28,7 @@ enum class TraceLevel {
Fatal
};

/** @brief This functions needs to be provided by the target application to enable logging */
void DEATH_TRACE(TraceLevel level, const char* fmt, ...);

# if defined(DEATH_TARGET_GCC) || defined(DEATH_TARGET_CLANG)
Expand Down Expand Up @@ -60,58 +61,58 @@ void DEATH_TRACE(TraceLevel level, const char* fmt, ...);
#if !defined(DEATH_ASSERT)
# if defined(DEATH_NO_ASSERT) || (!defined(DEATH_STANDARD_ASSERT) && !defined(DEATH_TRACE))
# define DEATH_ASSERT(condition, returnValue, message, ...) do {} while (false)
# elif !defined(DEATH_STANDARD_ASSERT) && defined(DEATH_TRACE)
# elif defined(DEATH_STANDARD_ASSERT)
# define DEATH_ASSERT(condition, returnValue, message, ...) assert(condition)
# else
# define DEATH_ASSERT(condition, returnValue, message, ...) \
do { \
if(!(condition)) { \
LOGF(message, ##__VA_ARGS__); \
return returnValue; \
} \
} while(false)
# else
# define DEATH_ASSERT(condition, returnValue, message, ...) assert(condition)
# endif
#endif

/** @brief Debug-only assertion macro */
#if !defined(DEATH_DEBUG_ASSERT)
# if defined(DEATH_NO_ASSERT) || !defined(DEATH_DEBUG) || (!defined(DEATH_STANDARD_ASSERT) && !defined(DEATH_TRACE))
# define DEATH_DEBUG_ASSERT(condition, ...) do {} while (false)
# elif !defined(DEATH_STANDARD_ASSERT) && defined(DEATH_TRACE)
# define _DEATH_DEBUG_ASSERT0(condition, ...) \
do { \
if(!(condition)) { \
# elif defined(DEATH_STANDARD_ASSERT)
# define DEATH_DEBUG_ASSERT(condition, ...) assert(condition)
# else
# define _DEATH_DEBUG_ASSERT0(condition, ...) \
do { \
if(!(condition)) { \
LOGF("Assertion (" #condition ") failed at \"" __FILE__ ":" DEATH_LINE_STRING "\""); \
std::abort(); \
} \
std::abort(); \
} \
} while(false)
# define _DEATH_DEBUG_ASSERTn(condition, returnValue, message, ...) \
do { \
if(!(condition)) { \
LOGF(message, ##__VA_ARGS__); \
return returnValue; \
} \
do { \
if(!(condition)) { \
LOGF(message, ##__VA_ARGS__); \
return returnValue; \
} \
} while(false)
# define DEATH_DEBUG_ASSERT(...) DEATH_HELPER_EXPAND(DEATH_HELPER_PICK(__VA_ARGS__, _DEATH_DEBUG_ASSERTn, _DEATH_DEBUG_ASSERTn, _DEATH_DEBUG_ASSERTn, _DEATH_DEBUG_ASSERTn, _DEATH_DEBUG_ASSERTn, _DEATH_DEBUG_ASSERTn, _DEATH_DEBUG_ASSERT0, )(__VA_ARGS__))
# else
# define DEATH_DEBUG_ASSERT(condition, ...) assert(condition)
# endif
#endif

/** @brief Constexpr assertion macro */
#if !defined(DEATH_CONSTEXPR_ASSERT)
# if defined(DEATH_NO_ASSERT) || (!defined(DEATH_STANDARD_ASSERT) && !defined(DEATH_TRACE))
# define DEATH_CONSTEXPR_ASSERT(condition, message, ...) static_cast<void>(0)
# elif !defined(DEATH_STANDARD_ASSERT) && defined(DEATH_TRACE)
# elif defined(DEATH_STANDARD_ASSERT)
# define DEATH_CONSTEXPR_ASSERT(condition, message, ...) \
static_cast<void>((condition) ? 0 : ([&]() { \
LOGF(message, ##__VA_ARGS__); \
std::abort(); \
assert(!#condition); \
}(), 0))
# else
# define DEATH_CONSTEXPR_ASSERT(condition, message, ...) \
static_cast<void>((condition) ? 0 : ([&]() { \
assert(!#condition); \
LOGF(message, ##__VA_ARGS__); \
std::abort(); \
}(), 0))
# endif
#endif
Expand All @@ -135,13 +136,13 @@ void DEATH_TRACE(TraceLevel level, const char* fmt, ...);
# else
# define DEATH_ASSERT_UNREACHABLE() std::abort()
# endif
# elif !defined(DEATH_STANDARD_ASSERT) && defined(DEATH_TRACE)
# elif defined(DEATH_STANDARD_ASSERT)
# define DEATH_ASSERT_UNREACHABLE() assert(!"Unreachable code")
# else
# define DEATH_ASSERT_UNREACHABLE() \
do { \
LOGF("Reached unreachable code at " __FILE__ ":" DEATH_LINE_STRING); \
LOGF("Reached unreachable code at \"" __FILE__ ":" DEATH_LINE_STRING "\""); \
std::abort(); \
} while (false)
# else
# define DEATH_ASSERT_UNREACHABLE() assert(!"Unreachable code")
# endif
#endif
2 changes: 1 addition & 1 deletion Sources/Shared/Containers/ArrayView.h
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ namespace Death { namespace Containers {
template<class T> template<std::size_t size_> constexpr StaticArrayView<size_, T> ArrayView<T>::slice(std::size_t begin) const {
static_assert(begin + size_ <= _size, "Slice needs to have a positive size");
return DEATH_DEBUG_CONSTEXPR_ASSERT(begin + size_ <= _size,
"Containers::ArrayView::slice(): Slice [%zu:%zu] out of range for %zu elements", begin_, begin + size_, _size),
"Containers::ArrayView::slice(): Slice [%zu:%zu] out of range for %zu elements", begin, begin + size_, _size),
StaticArrayView<size_, T>{_data + begin};
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Shared/IO/AndroidAssetStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#if defined(DEATH_TARGET_ANDROID)

#include <sys/stat.h> // For open()
#include <fcntl.h> // For open()
#include <unistd.h> // For close()
#include <sys/stat.h> // for open()
#include <fcntl.h> // for open()
#include <unistd.h> // for close()

namespace Death { namespace IO {
//###==##====#=====--==~--~=~- --- -- - - - -
Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/IO/AndroidAssetStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "../Containers/String.h"
#include "../Containers/StringView.h"

#include <android_native_app_glue.h> // For android_app
#include <android_native_app_glue.h> // for android_app
#include <android/asset_manager.h>

namespace Death { namespace IO {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Shared/IO/FileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
# endif
#else
# include <cerrno>
# include <sys/stat.h> // For open()
# include <fcntl.h> // For open()
# include <unistd.h> // For close()
# include <sys/stat.h> // for open()
# include <fcntl.h> // for open()
# include <unistd.h> // for close()
#endif

// `_nolock` functions are not supported by VC-LTL msvcrt, `_unlocked` functions are not supported on Android and Apple
Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/IO/PakFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "FileStream.h"
#include "FileSystem.h"

#include <cstdio> // For FILE
#include <cstdio> // for FILE
#include <memory>

namespace Death { namespace IO {
Expand Down
8 changes: 7 additions & 1 deletion Sources/nCine/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ void DEATH_TRACE(TraceLevel level, const char* fmt, ...)
length = vsnprintf(logEntry, MaxEntryLength, fmt, args);
va_end(args);

__android_log_write(priority, NCINE_APP, logEntry);
std::int32_t result = __android_log_write(priority, NCINE_APP, logEntry);
std::int32_t n = 0;
while (result == -11 /*EAGAIN*/ && n < 2) {
::usleep(5000); // in microseconds
result = __android_log_write(priority, NCINE_APP, logEntry);
n++;
}
#elif defined(DEATH_TARGET_SWITCH)
va_list args;
va_start(args, fmt);
Expand Down
9 changes: 5 additions & 4 deletions Sources/nCine/Backends/GlfwGfxDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ namespace nCine
/// The GLFW based graphics device
class GlfwGfxDevice : public IGfxDevice
{
friend class GlfwInputManager;
friend class GlfwMouseState;
friend class GlfwKeyboardState;
friend class ImGuiDrawing;

public:
GlfwGfxDevice(const WindowMode& windowMode, const GLContextInfo& glContextInfo, const DisplayMode& displayMode);
~GlfwGfxDevice() override;
Expand Down Expand Up @@ -104,10 +109,6 @@ namespace nCine

/// Callback for `glfwSetErrorCallback()`
static void errorCallback(int error, const char* description);

friend class GlfwInputManager;
friend class GlfwMouseState;
friend class GlfwKeyboardState;
};

}
Expand Down
2 changes: 1 addition & 1 deletion Sources/nCine/Backends/GlfwInputManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ namespace nCine
static KeyMod keyModValueToEnum(int keymod);
static int enumToKeySymValue(KeySym keysym);

friend class GlfwGfxDevice; // for `setWindowPosition()`
friend class GlfwGfxDevice; // for setWindowPosition()
};

inline const MouseState& GlfwInputManager::mouseState() const
Expand Down
8 changes: 6 additions & 2 deletions Sources/nCine/Backends/ImGuiGlfwInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
#include "../Input/ImGuiJoyMappedInput.h"

#if defined(DEATH_TARGET_APPLE)
# define GLFW_EXPOSE_NATIVE_COCOA
# if !defined(GLFW_EXPOSE_NATIVE_COCOA)
# define GLFW_EXPOSE_NATIVE_COCOA
# endif
#elif defined(DEATH_TARGET_WINDOWS)
# undef APIENTRY
# define GLFW_EXPOSE_NATIVE_WIN32
# if !defined(GLFW_EXPOSE_NATIVE_WIN32)
# define GLFW_EXPOSE_NATIVE_WIN32
# endif
#endif
#if defined(DEATH_TARGET_APPLE) || defined(DEATH_TARGET_WINDOWS)
# if defined(__HAS_LOCAL_GLFW)
Expand Down
4 changes: 2 additions & 2 deletions Sources/nCine/Graphics/GfxCapabilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "GfxCapabilities.h"
#include "../../Common.h"

#include <cstdio> // for sscanf()
#include <cstring> // for checkGLExtension()
#include <cstdio> // for sscanf()
#include <cstring> // for checkGLExtension()

#include <Containers/ArrayView.h>

Expand Down
2 changes: 1 addition & 1 deletion Sources/nCine/Graphics/RenderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "../ServiceLocator.h"
#include "../../Common.h"

#include <cstddef> // for `offsetof()`
#include <cstddef> // for offsetof()
#include <cstring>

#if defined(WITH_EMBEDDED_SHADERS)
Expand Down
4 changes: 2 additions & 2 deletions Sources/nCine/Threading/PosixThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include "../../Common.h"

#include <unistd.h> // for sysconf()
#include <sched.h> // for sched_yield()
#include <unistd.h> // for sysconf()
#include <sched.h> // for sched_yield()
#include <cstring>
#include <utility>

Expand Down

0 comments on commit 756d8a5

Please sign in to comment.