From 7f3a8db0583fa07248cf821264d2cd98342a4a74 Mon Sep 17 00:00:00 2001 From: BIGbadEL Date: Wed, 18 Sep 2019 17:50:02 +0200 Subject: [PATCH 01/11] Created expected abstraction. --- engine/CMakeLists.txt | 5 ++- engine/src/core/error/expected.h | 53 ++++++++++++++++++++++++++++++ engine/src/core/error/unexpected.h | 16 +++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 engine/src/core/error/expected.h create mode 100644 engine/src/core/error/unexpected.h diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index aded6b14..4ad73ceb 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -95,7 +95,10 @@ add_library(bald_engine ${CMAKE_CURRENT_SOURCE_DIR}/src/core/platform/opengl/textures/opengl_texture.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/file_manager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/image.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/split_string.cpp ) + ${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/split_string.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/core/error/expected.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/core/error/unexpected.h + ) if (APPLE) target_link_libraries(bald_engine diff --git a/engine/src/core/error/expected.h b/engine/src/core/error/expected.h new file mode 100644 index 00000000..e5e25ba7 --- /dev/null +++ b/engine/src/core/error/expected.h @@ -0,0 +1,53 @@ +// +// Created by grzegorz on 18.09.19. +// + +#pragma once + +#include +#include "unexpected.h" + + +namespace Bald { + template + class expected { + union { + E valid; + U invalid; + }; + bool _isOk = true; + public: + constexpr expected() { new (&valid) E(); } + constexpr expected(const E& rhs) { new (&valid) E(rhs); } + constexpr expected(const unexpected& rhs) : _isOk(false) { new (&invalid) U(rhs.value); } + constexpr expected(E&& rhs) { new (&valid) E(std::forward(rhs)); } + constexpr expected(unexpected&& rhs): _isOk(false) { new (&invalid) U(std::forward(rhs.value)); } + + ~expected() { + if(_isOk) valid.~E(); + else invalid.~U(); + } + + constexpr expected(const expected& rhs): _isOk(rhs._isOk) { + if(rhs._isOk) new (&valid) E(rhs.valid); + else new (&invalid) U(rhs.invalid); + } + constexpr expected(expected&& rhs) noexcept : _isOk(rhs._isOk) { + if(rhs._isOk) new (&valid) E(std::move(rhs.valid)); + else new (&invalid) U(std::move(rhs.invalid)); + } + + [[nodiscard]] constexpr bool has_value() const noexcept { return _isOk; } + [[nodiscard]] constexpr bool isOk() const noexcept { return _isOk; } + constexpr operator bool() const noexcept { return _isOk; } + + [[nodiscard]] constexpr U& error() const & { return invalid; } + [[nodiscard]] U& error() & { return invalid; } + [[nodiscard]] U&& error() && { return std::move(invalid); } + + [[nodiscard]] constexpr E& value() const & { return valid; } + [[nodiscard]] E& value() & { return valid; } + [[nodiscard]] E&& value() && { return valid; } + }; +} + diff --git a/engine/src/core/error/unexpected.h b/engine/src/core/error/unexpected.h new file mode 100644 index 00000000..4449d09a --- /dev/null +++ b/engine/src/core/error/unexpected.h @@ -0,0 +1,16 @@ +// +// Created by grzegorz on 18.09.19. +// +#pragma once + +namespace Bald { + template + struct unexpected { + explicit unexpected(const T& val): value(val) { } + T value; + }; +} + + + + From f58c3e98dd51a25e9ae0b4cfe2bed3cd7341312a Mon Sep 17 00:00:00 2001 From: BIGbadEL Date: Wed, 18 Sep 2019 18:33:04 +0200 Subject: [PATCH 02/11] refactor to code to use expected. --- engine/src/core/error/expected.h | 2 +- engine/src/core/error/unexpected.h | 3 +- .../platform/opengl/shaders/opengl_shader.cpp | 13 +++++---- engine/src/core/utils/file_manager.cpp | 28 ++++++++----------- engine/src/core/utils/file_manager.h | 7 +++-- engine/tests/test_file_manager.cpp | 13 +++++---- 6 files changed, 34 insertions(+), 32 deletions(-) diff --git a/engine/src/core/error/expected.h b/engine/src/core/error/expected.h index e5e25ba7..2064feb8 100644 --- a/engine/src/core/error/expected.h +++ b/engine/src/core/error/expected.h @@ -47,7 +47,7 @@ namespace Bald { [[nodiscard]] constexpr E& value() const & { return valid; } [[nodiscard]] E& value() & { return valid; } - [[nodiscard]] E&& value() && { return valid; } + [[nodiscard]] E&& value() && { return std::move(valid); } }; } diff --git a/engine/src/core/error/unexpected.h b/engine/src/core/error/unexpected.h index 4449d09a..ee9607bd 100644 --- a/engine/src/core/error/unexpected.h +++ b/engine/src/core/error/unexpected.h @@ -6,7 +6,8 @@ namespace Bald { template struct unexpected { - explicit unexpected(const T& val): value(val) { } + constexpr explicit unexpected(const T& val): value(val) { } + constexpr operator T() const noexcept { return value; } T value; }; } diff --git a/engine/src/core/platform/opengl/shaders/opengl_shader.cpp b/engine/src/core/platform/opengl/shaders/opengl_shader.cpp index 47eff2e2..b72befce 100644 --- a/engine/src/core/platform/opengl/shaders/opengl_shader.cpp +++ b/engine/src/core/platform/opengl/shaders/opengl_shader.cpp @@ -99,8 +99,10 @@ namespace Bald::Platform::Graphics { unsigned int vertexShader; vertexShader = glCreateShader(GL_VERTEX_SHADER); - std::string vertexShaderSource = Utils::FileManager::ReadFile(m_VertexPath, - Utils::FileManager::Size::SMALL_FILE); + auto vertex_data = Utils::FileManager::ReadFile(m_VertexPath, + Utils::FileManager::Size::SMALL_FILE); + if (!vertex_data) { return 0; } //TODO: do something smarter xd + const std::string& vertexShaderSource = vertex_data.value(); const char* vertexData = vertexShaderSource.c_str(); glShaderSource(vertexShader, 1, &vertexData, nullptr); @@ -124,9 +126,10 @@ namespace Bald::Platform::Graphics { unsigned int fragmentShader; fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - - std::string fragmentShaderSource = Utils::FileManager::ReadFile(m_FragmentPath, - Utils::FileManager::Size::SMALL_FILE); + auto fragment_data = Utils::FileManager::ReadFile(m_FragmentPath, + Utils::FileManager::Size::SMALL_FILE); + if(!fragment_data) { return 0; } //TODO: do something smarter + const std::string& fragmentShaderSource = fragment_data.value(); const char* fragmentData = fragmentShaderSource.c_str(); glShaderSource(fragmentShader, 1, &fragmentData, nullptr); diff --git a/engine/src/core/utils/file_manager.cpp b/engine/src/core/utils/file_manager.cpp index 55921259..afcd2bbc 100644 --- a/engine/src/core/utils/file_manager.cpp +++ b/engine/src/core/utils/file_manager.cpp @@ -15,57 +15,51 @@ namespace Bald::Utils { - std::string FileManager::ReadFile(const char* filePath, Size size) { + expected FileManager::ReadFile(const char* filePath, Size size) { if (size == Size::SMALL_FILE) return ReadSmallFile(filePath); return ReadBigFile(filePath); } - std::string FileManager::ReadSmallFile(const char* filePath) { + expected FileManager::ReadSmallFile(const char* filePath) { FILE* file = fopen(filePath, "r"); if (!file) { CORE_LOG_WARN("[FileManager] Couldn't open the file at path: " + static_cast(filePath)); - return "[FileManager] Couldn't open the file at path: " + static_cast(filePath); + return unexpected("[FileManager] Couldn't open the file at path: " + static_cast(filePath)); } fseek(file, 0, SEEK_END); auto stringSize = static_cast(ftell(file)); fseek(file, 0, SEEK_SET); - - char* buffer; + std::string result; try { - buffer = new char[stringSize + 1]; + result.resize(stringSize); } catch (std::bad_alloc& ba) { CORE_LOG_ERROR("[FileManager] bad_alloc caught: " + static_cast(ba.what())); - return std::string("Error!"); + return unexpected(std::string("Error!")); } - if (fread(buffer, sizeof(char), stringSize, file) < stringSize) { - delete[] buffer; + if (fread(result.data(), sizeof(char), stringSize, file) < stringSize) { fclose(file); CORE_LOG_ERROR("[FileManager] Could not read whole file: " + static_cast(filePath)); - return "[FileManager] Could not read whole file: " + static_cast(filePath); + return unexpected("[FileManager] Could not read whole file: " + static_cast(filePath)); } - buffer[stringSize] = '\0'; - std::string result(buffer); - delete[] buffer; fclose(file); return result; - } #ifdef LINUX - std::string FileManager::ReadBigFile(const char* filePath) { + expected FileManager::ReadBigFile(const char* filePath) { int fileDescriptor = open(filePath, O_RDONLY, S_IRUSR | S_IWUSR); struct stat sb{}; if (fstat(fileDescriptor, &sb) == -1) { CORE_LOG_WARN("[FileManager] Couldn't get size of the file. Check if the file exists at path: " + static_cast(filePath)); - return "[FileManager] Couldn't get size of the file. Check if the file exists at path: " + static_cast(filePath); + return unexpected("[FileManager] Couldn't get size of the file. Check if the file exists at path: " + static_cast(filePath)); } auto* buffer = static_cast(mmap(nullptr, static_cast(sb.st_size), PROT_READ, MAP_PRIVATE, fileDescriptor, 0)); @@ -78,7 +72,7 @@ namespace Bald::Utils { } #elif WINDOWS - std::string FileManager::ReadBigFile(const char *filePath) { + expected FileManager::ReadBigFile(const char *filePath) { CORE_LOG_INFO("[FILE_MANAGER] Error: Windows implementation is not done yet! Using slower reading method!"); ReadSmallFile(filePath); } diff --git a/engine/src/core/utils/file_manager.h b/engine/src/core/utils/file_manager.h index b877e0d5..666467f2 100644 --- a/engine/src/core/utils/file_manager.h +++ b/engine/src/core/utils/file_manager.h @@ -5,6 +5,7 @@ #pragma once #include +#include "error/expected.h" namespace Bald::Utils { @@ -44,7 +45,7 @@ namespace Bald::Utils { * @return string */ - [[nodiscard]] static std::string ReadFile(const char* filePath, Size size); + [[nodiscard]] static expected ReadFile(const char* filePath, Size size); private: /** @@ -53,7 +54,7 @@ namespace Bald::Utils { * @param filePath Path to file * @return string */ - [[nodiscard]] static std::string ReadSmallFile(const char* filePath); + [[nodiscard]] static expected ReadSmallFile(const char* filePath); /** * @fn ReadBigFile @@ -61,7 +62,7 @@ namespace Bald::Utils { * @param filePath Path to file * @return string */ - [[nodiscard]] static std::string ReadBigFile(const char* filePath); + [[nodiscard]] static expected ReadBigFile(const char* filePath); }; } // END OF NAMESPACE Bald::Utils diff --git a/engine/tests/test_file_manager.cpp b/engine/tests/test_file_manager.cpp index 49a13cdf..00391bca 100644 --- a/engine/tests/test_file_manager.cpp +++ b/engine/tests/test_file_manager.cpp @@ -8,28 +8,31 @@ TEST(FileManager, GoodSmallFileOpening) { //NOLINT - std::string file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::SMALL_FILE); + std::string file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::SMALL_FILE).value(); EXPECT_EQ("plik testowy\ndo odczytu", file_result); } TEST(FileManager, GoodBigFileOpening) { //NOLINT - std::string file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::BIG_FILE); + std::string file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::BIG_FILE).value(); EXPECT_EQ("plik testowy\ndo odczytu", file_result); } TEST(FileManager, WrongSmallFileOpening) { //NOLINT - std::string file_result = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::SMALL_FILE); - + auto data = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::SMALL_FILE); + std::string file_result = data.error(); EXPECT_EQ("[FileManager] Couldn't open the file at path: no_such_file.cpp", file_result); + EXPECT_FALSE(data); } TEST(FileManager, WrongBigFileOpening) { //NOLINT - std::string file_result = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::BIG_FILE); + auto data = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::BIG_FILE); + std::string file_result = data.error(); EXPECT_EQ("[FileManager] Couldn't get size of the file. Check if the file exists at path: no_such_file.cpp", file_result); + EXPECT_FALSE(data); } From 38585d7cb46242eb5618654a47cb7c15dfe4c244 Mon Sep 17 00:00:00 2001 From: BIGbadEL Date: Thu, 19 Sep 2019 17:12:54 +0200 Subject: [PATCH 03/11] fixed event system --- engine/src/core/events/callback/event_function_handler.h | 2 +- engine/src/core/events/event_manager.h | 4 ++-- engine/src/core/utils/file_manager.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/engine/src/core/events/callback/event_function_handler.h b/engine/src/core/events/callback/event_function_handler.h index 15e9c920..942f4dad 100644 --- a/engine/src/core/events/callback/event_function_handler.h +++ b/engine/src/core/events/callback/event_function_handler.h @@ -26,7 +26,7 @@ namespace Bald { */ template - explicit EventFunctionHandler(F fun, Args&& ... args): EventHandler(fun, args...) { } + explicit EventFunctionHandler(F&& fun, Args&& ... args): EventHandler(std::forward(fun), std::forward(args)...) { } /** * @fn Run diff --git a/engine/src/core/events/event_manager.h b/engine/src/core/events/event_manager.h index baa5d887..debb7632 100644 --- a/engine/src/core/events/event_manager.h +++ b/engine/src/core/events/event_manager.h @@ -152,7 +152,7 @@ namespace Bald { if(m_CallbacksSync.find(Utils::get_type_id()) == m_CallbacksSync.end()) { m_CallbacksSync[Utils::get_type_id()] = std::vector{}; } - m_CallbacksSync[Utils::get_type_id()].emplace_back(new EventFunctionHandler(callback, args...)); + m_CallbacksSync[Utils::get_type_id()].emplace_back(new EventFunctionHandler(std::forward(callback), std::forward(args)...)); CORE_LOG_INFO("[EventManager] Subscribe was successful..."); return m_CallbacksSync[Utils::get_type_id()].back()->GetID(); } @@ -160,7 +160,7 @@ namespace Bald { if(m_CallbacksAsync.find(Utils::get_type_id()) == m_CallbacksAsync.end()) { m_CallbacksAsync[Utils::get_type_id()] = std::vector{}; } - m_CallbacksAsync[Utils::get_type_id()].emplace_back(new EventFunctionHandler(callback, args...)); + m_CallbacksAsync[Utils::get_type_id()].emplace_back(new EventFunctionHandler(std::forward(callback), std::forward(args)...)); CORE_LOG_INFO("[EventManager] Subscribe was successful..."); return m_CallbacksAsync[Utils::get_type_id()].back()->GetID(); } diff --git a/engine/src/core/utils/file_manager.cpp b/engine/src/core/utils/file_manager.cpp index afcd2bbc..4fe271fc 100644 --- a/engine/src/core/utils/file_manager.cpp +++ b/engine/src/core/utils/file_manager.cpp @@ -22,7 +22,7 @@ namespace Bald::Utils { } expected FileManager::ReadSmallFile(const char* filePath) { - FILE* file = fopen(filePath, "r"); + FILE* file = fopen(filePath, "rb"); if (!file) { CORE_LOG_WARN("[FileManager] Couldn't open the file at path: " + static_cast(filePath)); From 43ab01d2b60bbdae963cebc9ad0aa4f487e519c8 Mon Sep 17 00:00:00 2001 From: BIGbadEL Date: Wed, 25 Sep 2019 22:12:53 +0200 Subject: [PATCH 04/11] reformat and error handling --- .../platform/opengl/shaders/opengl_shader.cpp | 7 +++++- engine/src/core/utils/file_manager.cpp | 18 +++++++-------- engine/src/core/utils/file_manager.h | 14 +++++++---- engine/tests/test_file_manager.cpp | 23 +++++++++---------- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/engine/src/core/platform/opengl/shaders/opengl_shader.cpp b/engine/src/core/platform/opengl/shaders/opengl_shader.cpp index b72befce..b8c4ce2e 100644 --- a/engine/src/core/platform/opengl/shaders/opengl_shader.cpp +++ b/engine/src/core/platform/opengl/shaders/opengl_shader.cpp @@ -101,7 +101,12 @@ namespace Bald::Platform::Graphics { auto vertex_data = Utils::FileManager::ReadFile(m_VertexPath, Utils::FileManager::Size::SMALL_FILE); - if (!vertex_data) { return 0; } //TODO: do something smarter xd + if (!vertex_data) { + CORE_LOG_ERROR("Could't open shader: " + std::string(m_VertexPath)); + CORE_LOG_WARN("Compiling default shader instead"); + //TODO: implement default shader + return 0; + } //TODO: do something smarter xd const std::string& vertexShaderSource = vertex_data.value(); const char* vertexData = vertexShaderSource.c_str(); diff --git a/engine/src/core/utils/file_manager.cpp b/engine/src/core/utils/file_manager.cpp index 4fe271fc..2a7998a2 100644 --- a/engine/src/core/utils/file_manager.cpp +++ b/engine/src/core/utils/file_manager.cpp @@ -15,18 +15,18 @@ namespace Bald::Utils { - expected FileManager::ReadFile(const char* filePath, Size size) { + expected FileManager::ReadFile(const char* filePath, Size size) { if (size == Size::SMALL_FILE) return ReadSmallFile(filePath); return ReadBigFile(filePath); } - expected FileManager::ReadSmallFile(const char* filePath) { + expected FileManager::ReadSmallFile(const char* filePath) { FILE* file = fopen(filePath, "rb"); if (!file) { CORE_LOG_WARN("[FileManager] Couldn't open the file at path: " + static_cast(filePath)); - return unexpected("[FileManager] Couldn't open the file at path: " + static_cast(filePath)); + return unexpected(FileManager::Error::CantOpenFile); } fseek(file, 0, SEEK_END); @@ -38,13 +38,13 @@ namespace Bald::Utils { } catch (std::bad_alloc& ba) { CORE_LOG_ERROR("[FileManager] bad_alloc caught: " + static_cast(ba.what())); - return unexpected(std::string("Error!")); + return unexpected(FileManager::Error::TooBigFile); } if (fread(result.data(), sizeof(char), stringSize, file) < stringSize) { fclose(file); CORE_LOG_ERROR("[FileManager] Could not read whole file: " + static_cast(filePath)); - return unexpected("[FileManager] Could not read whole file: " + static_cast(filePath)); + return unexpected(FileManager::Error::Fail); } fclose(file); @@ -53,13 +53,13 @@ namespace Bald::Utils { #ifdef LINUX - expected FileManager::ReadBigFile(const char* filePath) { + expected FileManager::ReadBigFile(const char* filePath) { int fileDescriptor = open(filePath, O_RDONLY, S_IRUSR | S_IWUSR); struct stat sb{}; if (fstat(fileDescriptor, &sb) == -1) { CORE_LOG_WARN("[FileManager] Couldn't get size of the file. Check if the file exists at path: " + static_cast(filePath)); - return unexpected("[FileManager] Couldn't get size of the file. Check if the file exists at path: " + static_cast(filePath)); + return unexpected(FileManager::Error::CantOpenFile); } auto* buffer = static_cast(mmap(nullptr, static_cast(sb.st_size), PROT_READ, MAP_PRIVATE, fileDescriptor, 0)); @@ -72,9 +72,9 @@ namespace Bald::Utils { } #elif WINDOWS - expected FileManager::ReadBigFile(const char *filePath) { + expected FileManager::ReadBigFile(const char *filePath) { CORE_LOG_INFO("[FILE_MANAGER] Error: Windows implementation is not done yet! Using slower reading method!"); - ReadSmallFile(filePath); + return ReadSmallFile(filePath); } #endif diff --git a/engine/src/core/utils/file_manager.h b/engine/src/core/utils/file_manager.h index 666467f2..de5d5548 100644 --- a/engine/src/core/utils/file_manager.h +++ b/engine/src/core/utils/file_manager.h @@ -19,9 +19,15 @@ namespace Bald::Utils { /** * ENUM which determines size of file and therefor methods, which will be used to read it */ - enum class Size : char { + enum class Size : uint8_t { SMALL_FILE, BIG_FILE }; + + enum class Error : uint8_t { + Fail, + CantOpenFile, + TooBigFile + }; public: /** @@ -45,7 +51,7 @@ namespace Bald::Utils { * @return string */ - [[nodiscard]] static expected ReadFile(const char* filePath, Size size); + [[nodiscard]] static expected ReadFile(const char* filePath, Size size); private: /** @@ -54,7 +60,7 @@ namespace Bald::Utils { * @param filePath Path to file * @return string */ - [[nodiscard]] static expected ReadSmallFile(const char* filePath); + [[nodiscard]] static expected ReadSmallFile(const char* filePath); /** * @fn ReadBigFile @@ -62,7 +68,7 @@ namespace Bald::Utils { * @param filePath Path to file * @return string */ - [[nodiscard]] static expected ReadBigFile(const char* filePath); + [[nodiscard]] static expected ReadBigFile(const char* filePath); }; } // END OF NAMESPACE Bald::Utils diff --git a/engine/tests/test_file_manager.cpp b/engine/tests/test_file_manager.cpp index 00391bca..65613b77 100644 --- a/engine/tests/test_file_manager.cpp +++ b/engine/tests/test_file_manager.cpp @@ -8,31 +8,30 @@ TEST(FileManager, GoodSmallFileOpening) { //NOLINT - std::string file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::SMALL_FILE).value(); + auto file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::SMALL_FILE); - EXPECT_EQ("plik testowy\ndo odczytu", file_result); + EXPECT_EQ("plik testowy\ndo odczytu", file_result.value()); + EXPECT_TRUE(file_result); } TEST(FileManager, GoodBigFileOpening) { //NOLINT - std::string file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::BIG_FILE).value(); + auto file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::BIG_FILE); - EXPECT_EQ("plik testowy\ndo odczytu", file_result); + EXPECT_EQ("plik testowy\ndo odczytu", file_result.value()); + EXPECT_TRUE(file_result); } TEST(FileManager, WrongSmallFileOpening) { //NOLINT - - auto data = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::SMALL_FILE); - std::string file_result = data.error(); - EXPECT_EQ("[FileManager] Couldn't open the file at path: no_such_file.cpp", file_result); + using namespace Bald::Utils; + auto data = FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::SMALL_FILE); + EXPECT_EQ(FileManager::Error::CantOpenFile, data.error()); EXPECT_FALSE(data); } TEST(FileManager, WrongBigFileOpening) { //NOLINT - + using namespace Bald::Utils; auto data = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::BIG_FILE); - std::string file_result = data.error(); - - EXPECT_EQ("[FileManager] Couldn't get size of the file. Check if the file exists at path: no_such_file.cpp", file_result); + EXPECT_EQ(FileManager::Error::CantOpenFile, data.error()); EXPECT_FALSE(data); } From 879ce5561a772cb88342c4448dd75c57cdf4809f Mon Sep 17 00:00:00 2001 From: BIGbadEL Date: Thu, 26 Sep 2019 08:32:28 +0200 Subject: [PATCH 05/11] added error handling in shaders --- engine/CMakeLists.txt | 2 + engine/res/shaders/default.frag | 5 ++ engine/res/shaders/default.vert | 7 ++ engine/src/core/error/expected.h | 78 ++++++++++++++----- .../platform/opengl/shaders/opengl_shader.cpp | 44 +++++++---- engine/src/core/utils/file_manager.h | 2 +- 6 files changed, 100 insertions(+), 38 deletions(-) create mode 100644 engine/res/shaders/default.frag create mode 100644 engine/res/shaders/default.vert diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 4ad73ceb..c34e59f7 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -149,6 +149,8 @@ target_link_libraries(runUnitTests bald_engine) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/test_file_manager.txt test_file_manager.txt COPYONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/shaders/basic.vert res/shaders/basic.vert COPYONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/shaders/basic.frag res/shaders/basic.frag COPYONLY) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/shaders/default.vert res/shaders/default.vert COPYONLY) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/shaders/default.frag res/shaders/default.frag COPYONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/textures/fabric.jpg res/textures/fabric.jpg COPYONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/textures/lena.jpg res/textures/lena.jpg COPYONLY) diff --git a/engine/res/shaders/default.frag b/engine/res/shaders/default.frag new file mode 100644 index 00000000..49400bc9 --- /dev/null +++ b/engine/res/shaders/default.frag @@ -0,0 +1,5 @@ +#version 330 + +void main() { + gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); +} diff --git a/engine/res/shaders/default.vert b/engine/res/shaders/default.vert new file mode 100644 index 00000000..f405fbcd --- /dev/null +++ b/engine/res/shaders/default.vert @@ -0,0 +1,7 @@ +#version 330 + +layout (location = 0) in vec3 in_Position; + +void main() { + gl_Position = vec4(in_Position, 1.0); +} diff --git a/engine/src/core/error/expected.h b/engine/src/core/error/expected.h index 2064feb8..76feff6f 100644 --- a/engine/src/core/error/expected.h +++ b/engine/src/core/error/expected.h @@ -9,7 +9,7 @@ namespace Bald { - template + template class expected { union { E valid; @@ -17,37 +17,73 @@ namespace Bald { }; bool _isOk = true; public: - constexpr expected() { new (&valid) E(); } - constexpr expected(const E& rhs) { new (&valid) E(rhs); } - constexpr expected(const unexpected& rhs) : _isOk(false) { new (&invalid) U(rhs.value); } - constexpr expected(E&& rhs) { new (&valid) E(std::forward(rhs)); } - constexpr expected(unexpected&& rhs): _isOk(false) { new (&invalid) U(std::forward(rhs.value)); } + constexpr expected() { new(&valid) E(); } - ~expected() { - if(_isOk) valid.~E(); - else invalid.~U(); + constexpr expected(const E& rhs) { new(&valid) E(rhs); } + + constexpr expected(const unexpected& rhs) : + _isOk(false) { new(&invalid) U(rhs.value); } + + constexpr expected(E&& rhs) { new(&valid) E(std::forward(rhs)); } + + constexpr expected(unexpected&& rhs) : + _isOk(false) { new(&invalid) U(std::forward(rhs.value)); } + + constexpr expected(const expected& rhs) : + _isOk(rhs._isOk) { + if (rhs._isOk) new(&valid) E(rhs.valid); + else new(&invalid) U(rhs.invalid); } - constexpr expected(const expected& rhs): _isOk(rhs._isOk) { - if(rhs._isOk) new (&valid) E(rhs.valid); - else new (&invalid) U(rhs.invalid); + constexpr expected(expected&& rhs) noexcept : + _isOk(rhs._isOk) { + if (rhs._isOk) new(&valid) E(std::move(rhs.valid)); + else new(&invalid) U(std::move(rhs.invalid)); } - constexpr expected(expected&& rhs) noexcept : _isOk(rhs._isOk) { - if(rhs._isOk) new (&valid) E(std::move(rhs.valid)); - else new (&invalid) U(std::move(rhs.invalid)); + + constexpr expected& operator=(const expected& other) { + dtor(); + _isOk = other._isOk; + if (_isOk) new(&valid) E(other.valid); + else new(&invalid) U(other.invalid); + return *this; + } + + constexpr expected& operator=(expected&& other) noexcept { + dtor(); + _isOk = other._isOk; + if (_isOk) new(&valid) E(std::move(other.valid)); + else new(&invalid) U(std::move(other.invalid)); + return *this; + } + + ~expected() { + dtor(); } [[nodiscard]] constexpr bool has_value() const noexcept { return _isOk; } + [[nodiscard]] constexpr bool isOk() const noexcept { return _isOk; } + constexpr operator bool() const noexcept { return _isOk; } - [[nodiscard]] constexpr U& error() const & { return invalid; } - [[nodiscard]] U& error() & { return invalid; } - [[nodiscard]] U&& error() && { return std::move(invalid); } + [[nodiscard]] constexpr U& error() const& { return invalid; } + + [[nodiscard]] U& error()& { return invalid; } + + [[nodiscard]] U&& error()&& { return std::move(invalid); } - [[nodiscard]] constexpr E& value() const & { return valid; } - [[nodiscard]] E& value() & { return valid; } - [[nodiscard]] E&& value() && { return std::move(valid); } + [[nodiscard]] constexpr E& value() const& { return valid; } + + [[nodiscard]] E& value()& { return valid; } + + [[nodiscard]] E&& value()&& { return std::move(valid); } + + private: + void dtor() const { + if (_isOk) valid.~E(); + else invalid.~U(); + } }; } diff --git a/engine/src/core/platform/opengl/shaders/opengl_shader.cpp b/engine/src/core/platform/opengl/shaders/opengl_shader.cpp index b8c4ce2e..eeb3eb72 100644 --- a/engine/src/core/platform/opengl/shaders/opengl_shader.cpp +++ b/engine/src/core/platform/opengl/shaders/opengl_shader.cpp @@ -11,7 +11,8 @@ namespace Bald::Platform::Graphics { OpenGLShader::OpenGLShader(const char* vertexPath, const char* fragmentPath) - : m_VertexPath(vertexPath), m_FragmentPath(fragmentPath), m_ShaderID(CreateShader()) {} + : + m_VertexPath(vertexPath), m_FragmentPath(fragmentPath), m_ShaderID(CreateShader()) { } OpenGLShader::~OpenGLShader() { glDeleteProgram(m_ShaderID); @@ -66,7 +67,7 @@ namespace Bald::Platform::Graphics { } void OpenGLShader::SetUniform2i(const char* uniformName, int v0, int v1) const noexcept { - glUniform2i(GetUniformLocation(uniformName), v0,v1); + glUniform2i(GetUniformLocation(uniformName), v0, v1); } void OpenGLShader::SetUniform2i(const char* uniformName, const glm::tvec2& vec) const noexcept { @@ -95,18 +96,20 @@ namespace Bald::Platform::Graphics { unsigned OpenGLShader::CreateShader() const noexcept { int success = 0; - + using namespace Utils; unsigned int vertexShader; vertexShader = glCreateShader(GL_VERTEX_SHADER); - auto vertex_data = Utils::FileManager::ReadFile(m_VertexPath, - Utils::FileManager::Size::SMALL_FILE); + auto vertex_data = FileManager::ReadFile(m_VertexPath); if (!vertex_data) { - CORE_LOG_ERROR("Could't open shader: " + std::string(m_VertexPath)); + CORE_LOG_WARN("Could't open shader: " + std::string(m_VertexPath)); CORE_LOG_WARN("Compiling default shader instead"); - //TODO: implement default shader - return 0; - } //TODO: do something smarter xd + vertex_data = FileManager::ReadFile("../engine/res/shaders/default.vert"); + if(!vertex_data) { + CORE_LOG_ERROR("Could't open default shader"); + return 0; + } + } const std::string& vertexShaderSource = vertex_data.value(); const char* vertexData = vertexShaderSource.c_str(); @@ -114,7 +117,7 @@ namespace Bald::Platform::Graphics { glCompileShader(vertexShader); glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); - if(success == GL_FALSE) { + if (success == GL_FALSE) { int length = 0; glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &length); @@ -131,9 +134,18 @@ namespace Bald::Platform::Graphics { unsigned int fragmentShader; fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - auto fragment_data = Utils::FileManager::ReadFile(m_FragmentPath, - Utils::FileManager::Size::SMALL_FILE); - if(!fragment_data) { return 0; } //TODO: do something smarter + auto fragment_data = FileManager::ReadFile(m_FragmentPath); + + if (!fragment_data) { + CORE_LOG_WARN("Could't open shader: " + std::string(m_FragmentPath)); + CORE_LOG_WARN("Compiling default shader instead"); + fragment_data = FileManager::ReadFile("../engine/res/default.frag"); + if(!fragment_data) { + CORE_LOG_ERROR("Could't open default shader"); + return 0; + } + } + const std::string& fragmentShaderSource = fragment_data.value(); const char* fragmentData = fragmentShaderSource.c_str(); @@ -141,7 +153,7 @@ namespace Bald::Platform::Graphics { glCompileShader(fragmentShader); glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); - if(success == GL_FALSE) { + if (success == GL_FALSE) { int length = 0; glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &length); @@ -163,7 +175,7 @@ namespace Bald::Platform::Graphics { glLinkProgram(id); glGetProgramiv(id, GL_LINK_STATUS, &success); - if(success == GL_FALSE) { + if (success == GL_FALSE) { int length = 0; glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length); @@ -192,7 +204,7 @@ namespace Bald::Platform::Graphics { int OpenGLShader::GetUniformLocation(const char* uniformName) const noexcept { auto iter = m_UniformLocationCache.find(uniformName); - if(iter != m_UniformLocationCache.end()) { + if (iter != m_UniformLocationCache.end()) { return iter->second; } diff --git a/engine/src/core/utils/file_manager.h b/engine/src/core/utils/file_manager.h index de5d5548..d3f080e1 100644 --- a/engine/src/core/utils/file_manager.h +++ b/engine/src/core/utils/file_manager.h @@ -51,7 +51,7 @@ namespace Bald::Utils { * @return string */ - [[nodiscard]] static expected ReadFile(const char* filePath, Size size); + [[nodiscard]] static expected ReadFile(const char* filePath, Size size = Size::SMALL_FILE); private: /** From 8b7f5bfb079cdd76c5e02e0d5cc619816c22d268 Mon Sep 17 00:00:00 2001 From: BIGbadEL Date: Thu, 26 Sep 2019 10:36:31 +0200 Subject: [PATCH 06/11] added bald_asserts to ensure correct use of expected class --- engine/src/core/error/expected.h | 33 +++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/engine/src/core/error/expected.h b/engine/src/core/error/expected.h index 76feff6f..6ab8c85e 100644 --- a/engine/src/core/error/expected.h +++ b/engine/src/core/error/expected.h @@ -6,6 +6,7 @@ #include #include "unexpected.h" +#include "bald_assert.h" namespace Bald { @@ -67,20 +68,38 @@ namespace Bald { constexpr operator bool() const noexcept { return _isOk; } - [[nodiscard]] constexpr U& error() const& { return invalid; } + [[nodiscard]] constexpr U& error() const& { + BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk); + return invalid; + } - [[nodiscard]] U& error()& { return invalid; } + [[nodiscard]] U& error()& { + BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk); + return invalid; + } - [[nodiscard]] U&& error()&& { return std::move(invalid); } + [[nodiscard]] U&& error()&& { + BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk); + return std::move(invalid); + } - [[nodiscard]] constexpr E& value() const& { return valid; } + [[nodiscard]] constexpr E& value() const& { + BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk); + return valid; + } - [[nodiscard]] E& value()& { return valid; } + [[nodiscard]] E& value()& { + BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk); + return valid; + } - [[nodiscard]] E&& value()&& { return std::move(valid); } + [[nodiscard]] E&& value()&& { + BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk); + return std::move(valid); + } private: - void dtor() const { + void dtor() const noexcept { if (_isOk) valid.~E(); else invalid.~U(); } From fca8464399de7895e42bae91e777682475884342 Mon Sep 17 00:00:00 2001 From: BIGbadEL Date: Sun, 6 Oct 2019 20:08:56 +0200 Subject: [PATCH 07/11] deleted error --- engine/src/core/utils/file_manager.cpp | 1 - engine/vendor/imgui | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/engine/src/core/utils/file_manager.cpp b/engine/src/core/utils/file_manager.cpp index 383191c8..af80d53e 100644 --- a/engine/src/core/utils/file_manager.cpp +++ b/engine/src/core/utils/file_manager.cpp @@ -22,7 +22,6 @@ namespace Bald::Utils { } expected FileManager::ReadSmallFile(const char* filePath) { - std::string FileManager::ReadSmallFile(const char* filePath) { #ifdef WINDOWS FILE* file; fopen_s(&file, filePath, "rb"); diff --git a/engine/vendor/imgui b/engine/vendor/imgui index 9fa3163d..efa73cae 160000 --- a/engine/vendor/imgui +++ b/engine/vendor/imgui @@ -1 +1 @@ -Subproject commit 9fa3163dd8a60dedd2da3c26955a670a0349d45f +Subproject commit efa73cae865fa5ec4d59dfb7681b1e77df0506dd From a58b3d1de581c688ca87593d95176aaf13d12035 Mon Sep 17 00:00:00 2001 From: Grzegorz Litarowicz <33033686+BIGbadEL@users.noreply.github.com> Date: Sun, 6 Oct 2019 20:10:39 +0200 Subject: [PATCH 08/11] Update file_manager.cpp fixed error --- engine/src/core/utils/file_manager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/engine/src/core/utils/file_manager.cpp b/engine/src/core/utils/file_manager.cpp index 383191c8..af80d53e 100644 --- a/engine/src/core/utils/file_manager.cpp +++ b/engine/src/core/utils/file_manager.cpp @@ -22,7 +22,6 @@ namespace Bald::Utils { } expected FileManager::ReadSmallFile(const char* filePath) { - std::string FileManager::ReadSmallFile(const char* filePath) { #ifdef WINDOWS FILE* file; fopen_s(&file, filePath, "rb"); From 5948005c70df3d7bd5ffb5a67bb939cdaabe0956 Mon Sep 17 00:00:00 2001 From: BIGbadEL Date: Tue, 19 Nov 2019 16:17:32 +0100 Subject: [PATCH 09/11] attempt to use our texture class --- engine/src/core/error/expected.h | 66 +++++++++++++++--------------- engine/src/core/error/unexpected.h | 6 +-- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/engine/src/core/error/expected.h b/engine/src/core/error/expected.h index 6ab8c85e..48ddf143 100644 --- a/engine/src/core/error/expected.h +++ b/engine/src/core/error/expected.h @@ -12,49 +12,44 @@ namespace Bald { template class expected { - union { - E valid; - U invalid; - }; - bool _isOk = true; public: - constexpr expected() { new(&valid) E(); } + constexpr expected() { new(&m_Valid) E(); } - constexpr expected(const E& rhs) { new(&valid) E(rhs); } + constexpr expected(const E& rhs) { new(&m_Valid) E(rhs); } constexpr expected(const unexpected& rhs) : - _isOk(false) { new(&invalid) U(rhs.value); } + _isOk(false) { new(&m_Invalid) U(rhs.value); } - constexpr expected(E&& rhs) { new(&valid) E(std::forward(rhs)); } + constexpr expected(E&& rhs) { new(&m_Valid) E(std::move(rhs)); } constexpr expected(unexpected&& rhs) : - _isOk(false) { new(&invalid) U(std::forward(rhs.value)); } + _isOk(false) { new(&m_Invalid) U(std::move(rhs.value)); } constexpr expected(const expected& rhs) : _isOk(rhs._isOk) { - if (rhs._isOk) new(&valid) E(rhs.valid); - else new(&invalid) U(rhs.invalid); + if (_isOk) new(&m_Valid) E(rhs.m_Valid); + else new(&m_Invalid) U(rhs.m_Invalid); } constexpr expected(expected&& rhs) noexcept : _isOk(rhs._isOk) { - if (rhs._isOk) new(&valid) E(std::move(rhs.valid)); - else new(&invalid) U(std::move(rhs.invalid)); + if (_isOk) new(&m_Valid) E(std::move(rhs.m_Valid)); + else new(&m_Invalid) U(std::move(rhs.m_Invalid)); } - constexpr expected& operator=(const expected& other) { + constexpr expected& operator=(const expected& rhs) { dtor(); - _isOk = other._isOk; - if (_isOk) new(&valid) E(other.valid); - else new(&invalid) U(other.invalid); + _isOk = rhs._isOk; + if (_isOk) new(&m_Valid) E(rhs.m_Valid); + else new(&m_Invalid) U(rhs.m_Invalid); return *this; } - constexpr expected& operator=(expected&& other) noexcept { + constexpr expected& operator=(expected&& rhs) noexcept { dtor(); - _isOk = other._isOk; - if (_isOk) new(&valid) E(std::move(other.valid)); - else new(&invalid) U(std::move(other.invalid)); + _isOk = rhs._isOk; + if (_isOk) new(&m_Valid) E(std::move(rhs.m_Valid)); + else new(&m_Invalid) U(std::move(rhs.m_Invalid)); return *this; } @@ -62,47 +57,52 @@ namespace Bald { dtor(); } - [[nodiscard]] constexpr bool has_value() const noexcept { return _isOk; } - - [[nodiscard]] constexpr bool isOk() const noexcept { return _isOk; } + [[nodiscard]] constexpr bool isValid() const noexcept { return _isOk; } constexpr operator bool() const noexcept { return _isOk; } [[nodiscard]] constexpr U& error() const& { BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk); - return invalid; + return m_Invalid; } [[nodiscard]] U& error()& { BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk); - return invalid; + return m_Invalid; } [[nodiscard]] U&& error()&& { BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk); - return std::move(invalid); + return std::move(m_Invalid); } [[nodiscard]] constexpr E& value() const& { BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk); - return valid; + return m_Valid; } [[nodiscard]] E& value()& { BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk); - return valid; + return m_Valid; } [[nodiscard]] E&& value()&& { BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk); - return std::move(valid); + return std::move(m_Valid); } private: void dtor() const noexcept { - if (_isOk) valid.~E(); - else invalid.~U(); + if (_isOk) m_Valid.~E(); + else m_Invalid.~U(); } + + private: + union { + E m_Valid; + U m_Invalid; + }; + bool _isOk = true; }; } diff --git a/engine/src/core/error/unexpected.h b/engine/src/core/error/unexpected.h index ee9607bd..ca21fe7f 100644 --- a/engine/src/core/error/unexpected.h +++ b/engine/src/core/error/unexpected.h @@ -6,9 +6,9 @@ namespace Bald { template struct unexpected { - constexpr explicit unexpected(const T& val): value(val) { } - constexpr operator T() const noexcept { return value; } - T value; + constexpr explicit unexpected(const T& val): m_Value(val) { } + constexpr operator T() const noexcept { return m_Value; } + T m_Value; }; } From 655df37f4940bdcb9da62d842ef19f0872d884b8 Mon Sep 17 00:00:00 2001 From: BIGbadEL Date: Mon, 2 Dec 2019 09:40:57 +0100 Subject: [PATCH 10/11] added system that prevents accessing result of expected without calling isValid function or conversion to bool --- engine/src/core/error/expected.h | 27 ++++++++++++++++++++++++--- engine/tests/test_file_manager.cpp | 6 ++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/engine/src/core/error/expected.h b/engine/src/core/error/expected.h index 48ddf143..e84cae94 100644 --- a/engine/src/core/error/expected.h +++ b/engine/src/core/error/expected.h @@ -23,7 +23,7 @@ namespace Bald { constexpr expected(E&& rhs) { new(&m_Valid) E(std::move(rhs)); } constexpr expected(unexpected&& rhs) : - _isOk(false) { new(&m_Invalid) U(std::move(rhs.value)); } + _isOk(false) { new(&m_Invalid) U(std::move(rhs.m_Value)); } constexpr expected(const expected& rhs) : _isOk(rhs._isOk) { @@ -38,6 +38,7 @@ namespace Bald { } constexpr expected& operator=(const expected& rhs) { + if(this == &rhs) return *this; dtor(); _isOk = rhs._isOk; if (_isOk) new(&m_Valid) E(rhs.m_Valid); @@ -46,6 +47,7 @@ namespace Bald { } constexpr expected& operator=(expected&& rhs) noexcept { + if(this == &rhs) return *this; dtor(); _isOk = rhs._isOk; if (_isOk) new(&m_Valid) E(std::move(rhs.m_Valid)); @@ -57,36 +59,52 @@ namespace Bald { dtor(); } - [[nodiscard]] constexpr bool isValid() const noexcept { return _isOk; } + [[nodiscard]] constexpr bool isValid() const noexcept { +#ifdef DEBUG + m_WasChecked = true; +#endif + return _isOk; + } - constexpr operator bool() const noexcept { return _isOk; } + constexpr operator bool() const noexcept { +#ifdef DEBUG + m_WasChecked = true; +#endif + return _isOk; + } [[nodiscard]] constexpr U& error() const& { + BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked); BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk); return m_Invalid; } [[nodiscard]] U& error()& { + BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked); BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk); return m_Invalid; } [[nodiscard]] U&& error()&& { + BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked); BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk); return std::move(m_Invalid); } [[nodiscard]] constexpr E& value() const& { + BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked); BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk); return m_Valid; } [[nodiscard]] E& value()& { + BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked); BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk); return m_Valid; } [[nodiscard]] E&& value()&& { + BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked); BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk); return std::move(m_Valid); } @@ -102,6 +120,9 @@ namespace Bald { E m_Valid; U m_Invalid; }; +#ifdef DEBUG + mutable bool m_WasChecked = false; +#endif bool _isOk = true; }; } diff --git a/engine/tests/test_file_manager.cpp b/engine/tests/test_file_manager.cpp index 65613b77..aa4335ee 100644 --- a/engine/tests/test_file_manager.cpp +++ b/engine/tests/test_file_manager.cpp @@ -9,7 +9,7 @@ TEST(FileManager, GoodSmallFileOpening) { //NOLINT auto file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::SMALL_FILE); - + EXPECT_TRUE(file_result.isValid()); EXPECT_EQ("plik testowy\ndo odczytu", file_result.value()); EXPECT_TRUE(file_result); } @@ -17,7 +17,7 @@ TEST(FileManager, GoodSmallFileOpening) { //NOLINT TEST(FileManager, GoodBigFileOpening) { //NOLINT auto file_result = Bald::Utils::FileManager::ReadFile("../engine/test_file_manager.txt", Bald::Utils::FileManager::Size::BIG_FILE); - + EXPECT_TRUE(file_result.isValid()); EXPECT_EQ("plik testowy\ndo odczytu", file_result.value()); EXPECT_TRUE(file_result); } @@ -25,6 +25,7 @@ TEST(FileManager, GoodBigFileOpening) { //NOLINT TEST(FileManager, WrongSmallFileOpening) { //NOLINT using namespace Bald::Utils; auto data = FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::SMALL_FILE); + EXPECT_FALSE(data.isValid()); EXPECT_EQ(FileManager::Error::CantOpenFile, data.error()); EXPECT_FALSE(data); } @@ -32,6 +33,7 @@ TEST(FileManager, WrongSmallFileOpening) { //NOLINT TEST(FileManager, WrongBigFileOpening) { //NOLINT using namespace Bald::Utils; auto data = Bald::Utils::FileManager::ReadFile("no_such_file.cpp", Bald::Utils::FileManager::Size::BIG_FILE); + EXPECT_FALSE(data.isValid()); EXPECT_EQ(FileManager::Error::CantOpenFile, data.error()); EXPECT_FALSE(data); } From e1e23d0ce2376675a15d36e0352c7edd1dfeafa5 Mon Sep 17 00:00:00 2001 From: BIGbadEL Date: Fri, 6 Dec 2019 20:25:26 +0100 Subject: [PATCH 11/11] added apple macro in file_manager.cpp --- engine/src/core/utils/file_manager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engine/src/core/utils/file_manager.cpp b/engine/src/core/utils/file_manager.cpp index af80d53e..bbc16c3d 100644 --- a/engine/src/core/utils/file_manager.cpp +++ b/engine/src/core/utils/file_manager.cpp @@ -22,7 +22,7 @@ namespace Bald::Utils { } expected FileManager::ReadSmallFile(const char* filePath) { -#ifdef WINDOWS +#if defined(WINDOWS) || defined(APPLE) FILE* file; fopen_s(&file, filePath, "rb"); #else @@ -76,7 +76,7 @@ namespace Bald::Utils { return result; } -#elif WINDOWS +#if defined(WINDOWS) || defined(APPLE) expected FileManager::ReadBigFile(const char *filePath) { CORE_LOG_INFO("[FILE_MANAGER] Error: Windows implementation is not done yet! Using slower reading method!"); return ReadSmallFile(filePath);