Browse Source

finished pika asserts

vlod 3 years ago
parent
commit
0458164280

+ 5 - 1
Pika/CMakeLists.txt

@@ -40,9 +40,13 @@ file(GLOB_RECURSE PIKA_SOURCES_CORE_STD		CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOUR
 
 
 
+#pikaCore
+add_executable(pikaCore)
+
+#add defines
+target_compile_definitions(pikaCore PUBLIC PIKA_DEVELOPMENT)
 
 
-add_executable(pikaCore)
 set_property(TARGET pikaCore PROPERTY CXX_STANDARD 17)
 target_sources(pikaCore PRIVATE 
 	"${PIKA_SOURCES_CORE_CONFIG}" "${PIKA_SOURCES_CORE_EDITOR}" "${PIKA_SOURCES_CORE_RUNTIME}" "${PIKA_SOURCES_CORE_STD}")

+ 33 - 9
Pika/core/coreConfig/pikaConfig.h

@@ -1,22 +1,46 @@
 #pragma once
+//////////////////////////////////////////
+//pikaConfig.h
+//Luta Vlad(c) 2022
+//https://github.com/meemknight/PikaEngine
+//////////////////////////////////////////
 
 
-#pragma region Platform
 
+//////////////////////////////////////////
+//All macros
+//////////////////////////////////////////
+//
+//constants
+//
+//PIKA_WINDOWS
+//PIKA_LINUX
+//PIKA_DEVELOPMENT
+//PIKA_PRODUCTION
+// 
+//  
+//////////////////////////////////////////
+//
+//functions
+//
+// PIKA_PERMA_ASSERT
+// PIKA_DEVELOPMENT_ONLY_ASSERT
+//
+//
+//////////////////////////////////////////
 
 
-#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__) || defined(__NT__)
 
-#define PIKA_WINDOWS 1
+///chose the assert function to be run on perma assert in production code
 
-#elif defined(__linux__)
+//#define PIKA_INTERNAL_CURRENT_ASSERT_FUNCTION pika::assert::assertFunctionProduction
+#define PIKA_INTERNAL_CURRENT_ASSERT_FUNCTION pika::assert::assertFunctionDevelopment
+//#define PIKA_INTERNAL_CURRENT_ASSERT_FUNCTION pika::assert::assertFunctionToLog
+//#define PIKA_INTERNAL_CURRENT_ASSERT_FUNCTION pika::assert::terminate
+//#define PIKA_INTERNAL_CURRENT_ASSERT_FUNCTION //remove all asserts in production
 
-#define PIKA_LINUX 1
 
-#else
 
-#error "Pika supports only windows and linux"
 
-#endif
 
-#pragma endregion
+#include <pikaConfigInternal.h>

+ 21 - 0
Pika/core/coreConfig/pikaConfigInternal.h

@@ -0,0 +1,21 @@
+#pragma once
+
+#pragma region Platform
+
+
+
+#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__) || defined(__NT__)
+
+#define PIKA_WINDOWS 1
+
+#elif defined(__linux__)
+
+#define PIKA_LINUX 1
+
+#else
+
+#error "Pika supports only windows and linux"
+
+#endif
+
+#pragma endregion

+ 1 - 1
Pika/core/pikaRuntime/pikaMain.cpp

@@ -35,7 +35,7 @@ int main()
 	gl2d::init();
 	renderer.create();
 
-	pika::assert::assertFunctionInternal("abc", __FILE__, __LINE__);
+	PIKA_PERMA_ASSERT(0, "abc");
 
 	while (!glfwWindowShouldClose(window))
 	{

+ 30 - 2
Pika/core/pikaSTD/assert/assert.cpp

@@ -2,6 +2,8 @@
 #include "assert.h"
 #include <cstdlib>
 #include <cstdio>
+#include <assert.h>
+
 
 
 namespace pika
@@ -10,7 +12,7 @@ namespace pika
 	namespace assert
 	{
 
-		void terminate()
+		void terminate(...)
 		{
 			std::exit(1);
 		}
@@ -20,7 +22,7 @@ namespace pika
 
 	#include <Windows.h>
 
-		void assertFunctionInternal(const char *expression, const char *file,
+		void assertFunctionDevelopment(const char *expression, const char *file,
 			int line, const char *comment)
 		{
 			char buffer[1024] = {};
@@ -106,6 +108,12 @@ namespace pika
 	#elif defined(PIKA_LINUX)
 
 
+		void assertFunctionDevelopment(const char *expression, const char *file,
+			int line, const char *comment)
+		{
+			terminate();
+		}
+
 		void assertFunctionProduction(const char *expression, const char *file,
 			int line, const char *comment)
 		{
@@ -115,6 +123,26 @@ namespace pika
 
 	#endif
 
+		void assertFunctionToLog(const char *expression, const char *file, int line, const char *comment)
+		{
+
+			char buffer[1024] = {};
+
+			std::snprintf(buffer, sizeof(buffer),
+				"Assertion failed\n"
+				"Expression: \n%s\n"
+				"File: %s\n"
+				"Line: %d\n"
+				"Comment: \n%s\n"
+				, expression
+				, file
+				, line
+				, comment
+			);
+
+			//todo(vlod): log to a file
+
+		}
 
 	}
 

+ 32 - 5
Pika/core/pikaSTD/assert/assert.h

@@ -2,17 +2,16 @@
 #include <pikaConfig.h>
 
 
-
 namespace pika
 {
 
 	namespace assert
 	{
 
+		//arguments don't do anything here
+		void terminate(...);
 
-		void terminate();
-
-		void assertFunctionInternal(
+		void assertFunctionDevelopment(
 			const char *expression, 
 			const char *file,
 			int line, 
@@ -26,7 +25,35 @@ namespace pika
 			const char *comment = nullptr
 		);
 
+		void assertFunctionToLog(
+			const char *expression,
+			const char *file,
+			int line,
+			const char *comment = nullptr);
 
 	}
 
-}
+}
+
+
+
+#define PIKA_PERMA_ASSERT(expression, comment) (void)(			\
+			(!!(expression)) ||									\
+			(PIKA_INTERNAL_CURRENT_ASSERT_FUNCTION(expression,			\
+				__FILE__, __LINE__, comment), 0)				\
+)
+
+
+#ifdef PIKA_DEVELOPMENT
+
+#define PIKA_DEVELOPMENT_ONLY_ASSERT(expression, comment) (void)(			\
+			(!!(expression)) ||												\
+			(pika::assert::assertFunctionDevelopment(expression,			\
+				__FILE__, __LINE__, comment), 0)							\
+)
+
+#elif defined(PIKA_PRODUCTION)
+
+#define PIKA_DEVELOPMENT_ONLY_ASSERT(expression, comment)
+
+#endif