Quellcode durchsuchen

Add window api factory

Patrick-Christopher Mattulat vor 6 Tagen
Ursprung
Commit
8c35b0e7b1

+ 11 - 0
CMakeLists.txt

@@ -14,6 +14,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
 ################################
 
 set(MODULE_NAME_CYCLE ls-atlantis-cycle)
+set(MODULE_NAME_FACTORY ls-atlantis-factory)
 set(MODULE_NAME_INTERFACE ls-atlantis-interface)
 set(MODULE_NAME_WINDOW ls-atlantis-window)
 
@@ -62,6 +63,15 @@ include(cmake/ls-atlantis-interface-tests.cmake)
 include(cmake/ls-atlantis-window.cmake)
 include(cmake/ls-atlantis-window-tests.cmake)
 
+################################################################
+################################################################
+# Module: Factory
+################################################################
+################################################################
+
+include(cmake/ls-atlantis-factory.cmake)
+include(cmake/ls-atlantis-factory-tests.cmake)
+
 ################################################################
 ################################################################
 # Example
@@ -79,6 +89,7 @@ include(cmake/ls-atlantis-example.cmake)
 add_custom_target(ls-atlantis-tests
     DEPENDS
         ${MODULE_NAME_CYCLE}-tests
+        ${MODULE_NAME_FACTORY}-tests
         ${MODULE_NAME_INTERFACE}-tests
         ${MODULE_NAME_WINDOW}-tests
 )

+ 1 - 0
README.md

@@ -39,6 +39,7 @@ In Process: It contains the following modules:
 | Name                  | Description                                                                                                                                                                                                                                         |
 |-----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
 | ls-atlantis-cycle     | This module provides functionalities like engine setup, game loop logic and engine run time information.                                                                                                                                            |
+| ls-atlantis-factory   | This module provides factories for building interface implementations.                                                                                                                                                                              |
 | ls-atlantis-interface | __Consumer:__ This module provides all interfaces all other engine modules can implement. This would avoid circular dependencies through out the project. This module can be imported. This module must not import any library dependencies itself. |
 | ls-atlantis-window    | This module provides functionalities for window creation and handling.                                                                                                                                                                              |
 

+ 38 - 0
cmake/ls-atlantis-factory-tests.cmake

@@ -0,0 +1,38 @@
+################################################################
+################################################################
+# Module-Test: Factory
+################################################################
+################################################################
+
+################################
+# Test Files: Factory Module
+################################
+
+set(ATLANTIS_FACTORY_TEST_SOURCES
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/factory/WindowApiFactoryTest.cpp
+)
+
+################################
+# Test Case Creation: Factory Module
+################################
+
+add_executable(${MODULE_NAME_FACTORY}-tests ${ATLANTIS_FACTORY_TEST_SOURCES})
+
+target_link_libraries(${MODULE_NAME_FACTORY}-tests PRIVATE
+    ${MODULE_NAME_CYCLE}
+    ${MODULE_NAME_FACTORY}
+    GTest::gtest_main
+)
+
+gtest_discover_tests(${MODULE_NAME_FACTORY}-tests DISCOVERY_MODE PRE_TEST)
+
+if (WIN32)
+    add_custom_command(TARGET ${MODULE_NAME_FACTORY}-tests POST_BUILD
+        COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:${MODULE_NAME_FACTORY}-tests>
+            $<TARGET_RUNTIME_DLLS:${MODULE_NAME_FACTORY}-tests>
+        COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:${MODULE_NAME_FACTORY}-tests>
+            "$<$<CONFIG:Debug>:${LS_STD_DLLS_DEBUG}>"
+            "$<$<CONFIG:Release>:${LS_STD_DLLS_RELEASE}>"
+        COMMAND_EXPAND_LISTS
+    )
+endif ()

+ 23 - 0
cmake/ls-atlantis-factory.cmake

@@ -0,0 +1,23 @@
+################################################################
+################################################################
+# Module: Factory
+################################################################
+################################################################
+
+################################
+# Source Files: Factory Module
+################################
+
+set(ATLANTIS_FACTORY_SOURCES
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/factory/WindowApiFactory.cpp
+)
+
+################################
+# Library Creation: Factory Module
+################################
+
+add_library(${MODULE_NAME_FACTORY} SHARED ${ATLANTIS_FACTORY_SOURCES})
+
+target_link_libraries(${MODULE_NAME_FACTORY} PUBLIC
+    ${MODULE_NAME_WINDOW}
+)

+ 0 - 19
include/cycle/StatusCodes.hpp

@@ -1,19 +0,0 @@
-/*
- * author: Patrick-Christopher Mattulat
- * e-mail: webmaster@lynarstudios.com
- */
-
-#ifndef LS_ATLANTIS_ENGINE_CYCLE_STATUS_CODES_HPP
-#define LS_ATLANTIS_ENGINE_CYCLE_STATUS_CODES_HPP
-
-namespace ls::atlantis::cycle
-{
-  enum StatusCodes
-  {
-    OK = 1,
-    WINDOW_API_LOADING_FAILED = 2,
-    WINDOW_CREATION_FAILED = 3
-  };
-}
-
-#endif

+ 27 - 0
include/factory/WindowApiFactory.hpp

@@ -0,0 +1,27 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#ifndef LS_ATLANTIS_ENGINE_FACTORY_WINDOW_API_FACTORY_HPP
+#define LS_ATLANTIS_ENGINE_FACTORY_WINDOW_API_FACTORY_HPP
+
+#include <Export.hpp>
+#include <glossary/WindowApiTypes.hpp>
+#include <interface/IWindowApi.hpp>
+#include <memory>
+
+namespace ls::atlantis::factory
+{
+  class LS_ATLANTIS_DYNAMIC_GOAL WindowApiFactory
+  {
+    public:
+
+      WindowApiFactory();
+      ~WindowApiFactory();
+
+      [[nodiscard]] static ::std::shared_ptr<ls::atlantis::interfaces::IWindowApi> build(const ls::atlantis::glossary::WindowApiTypes &_windowApis);
+  };
+}
+
+#endif

+ 19 - 0
include/glossary/StatusCodes.hpp

@@ -0,0 +1,19 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#ifndef LS_ATLANTIS_ENGINE_GLOSSARY_STATUS_CODES_HPP
+#define LS_ATLANTIS_ENGINE_GLOSSARY_STATUS_CODES_HPP
+
+namespace ls::atlantis::glossary
+{
+  enum StatusCodes
+  {
+    OK = 0,
+    WINDOW_API_LOADING_FAILED = 1,
+    WINDOW_CREATION_FAILED = 2
+  };
+}
+
+#endif

+ 18 - 0
include/glossary/WindowApiTypes.hpp

@@ -0,0 +1,18 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#ifndef LS_ATLANTIS_ENGINE_GLOSSARY_WINDOW_API_TYPES_HPP
+#define LS_ATLANTIS_ENGINE_GLOSSARY_WINDOW_API_TYPES_HPP
+
+namespace ls::atlantis::glossary
+{
+  enum WindowApiTypes
+  {
+    NONE_SELECTED = 0,
+    SDL_WINDOW_API = 1
+  };
+}
+
+#endif

+ 2 - 0
include/interface/IWindowApi.hpp

@@ -7,6 +7,7 @@
 #define LS_ATLANTIS_ENGINE_INTERFACE_WINDOW_API_HPP
 
 #include <Export.hpp>
+#include <glossary/WindowApiTypes.hpp>
 #include <interface/AStatusCode.hpp>
 #include <memory>
 
@@ -19,6 +20,7 @@ namespace ls::atlantis::interfaces
       IWindowApi();
       virtual ~IWindowApi();
 
+      virtual ls::atlantis::glossary::WindowApiTypes getWindowApiType() = 0;
       virtual ::std::shared_ptr<AStatusCode> init() = 0;
   };
 }

+ 2 - 0
include/window/SdlWindowApi.hpp

@@ -23,6 +23,7 @@ namespace ls::atlantis::window
       explicit SdlWindowApi(const ::std::shared_ptr<ls::atlantis::interfaces::ISdlApi> &_sdlApi);
       ~SdlWindowApi() override;
 
+      [[nodiscard]] ls::atlantis::glossary::WindowApiTypes getWindowApiType() override;
       [[nodiscard]] ::std::shared_ptr<ls::atlantis::interfaces::AStatusCode> init() override;
 
     private:
@@ -32,6 +33,7 @@ namespace ls::atlantis::window
 
       ::std::shared_ptr<ls::atlantis::interfaces::ISdlApi> sdlApi{};
       ::std::unique_ptr<SDL_Window, ::std::function<void(SDL_Window *)>> window{nullptr, [](SDL_Window *) {}};
+      ls::atlantis::glossary::WindowApiTypes windowApiType{};
   };
 }
 

+ 2 - 2
output/Example.cpp

@@ -3,10 +3,10 @@
  * e-mail: webmaster@lynarstudios.com
  */
 
-#include <cycle/StatusCodes.hpp>
+#include <glossary/StatusCodes.hpp>
 #include <window/SdlWindowApi.hpp>
 
-using ls::atlantis::cycle::StatusCodes;
+using ls::atlantis::glossary::StatusCodes;
 using ls::atlantis::interfaces::AStatusCode;
 using ls::atlantis::window::SdlWindowApi;
 using ::std::make_shared;

+ 2 - 2
source/cycle/StatusCodeOk.cpp

@@ -3,11 +3,11 @@
  * e-mail: webmaster@lynarstudios.com
  */
 
+#include <glossary/StatusCodes.hpp>
 #include <cycle/StatusCodeOk.hpp>
-#include <cycle/StatusCodes.hpp>
 
 using ls::atlantis::cycle::StatusCodeOk;
-using ls::atlantis::cycle::StatusCodes;
+using ls::atlantis::glossary::StatusCodes;
 using ls::atlantis::interfaces::AStatusCode;
 
 StatusCodeOk::StatusCodeOk() : AStatusCode(StatusCodes::OK, "ok")

+ 2 - 2
source/cycle/StatusCodeWindowApiLoadingFailed.cpp

@@ -3,11 +3,11 @@
  * e-mail: webmaster@lynarstudios.com
  */
 
+#include <glossary/StatusCodes.hpp>
 #include <cycle/StatusCodeWindowApiLoadingFailed.hpp>
-#include <cycle/StatusCodes.hpp>
 
 using ls::atlantis::cycle::StatusCodeWindowApiLoadingFailed;
-using ls::atlantis::cycle::StatusCodes;
+using ls::atlantis::glossary::StatusCodes;
 using ls::atlantis::interfaces::AStatusCode;
 
 StatusCodeWindowApiLoadingFailed::StatusCodeWindowApiLoadingFailed() : AStatusCode(StatusCodes::WINDOW_API_LOADING_FAILED, "window api loading failed")

+ 2 - 2
source/cycle/StatusCodeWindowCreationFailed.cpp

@@ -3,11 +3,11 @@
  * e-mail: webmaster@lynarstudios.com
  */
 
+#include <glossary/StatusCodes.hpp>
 #include <cycle/StatusCodeWindowCreationFailed.hpp>
-#include <cycle/StatusCodes.hpp>
 
 using ls::atlantis::cycle::StatusCodeWindowCreationFailed;
-using ls::atlantis::cycle::StatusCodes;
+using ls::atlantis::glossary::StatusCodes;
 using ls::atlantis::interfaces::AStatusCode;
 
 StatusCodeWindowCreationFailed::StatusCodeWindowCreationFailed() : AStatusCode(StatusCodes::WINDOW_CREATION_FAILED, "window creation failed")

+ 39 - 0
source/factory/WindowApiFactory.cpp

@@ -0,0 +1,39 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#include <factory/WindowApiFactory.hpp>
+#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <window/SdlWindowApi.hpp>
+
+using ls::atlantis::factory::WindowApiFactory;
+using ls::atlantis::glossary::WindowApiTypes;
+using ls::atlantis::interfaces::IWindowApi;
+using ls::atlantis::window::SdlWindowApi;
+using ls::std::core::IllegalArgumentException;
+using ::std::make_shared;
+using ::std::shared_ptr;
+
+WindowApiFactory::WindowApiFactory() = default;
+
+WindowApiFactory::~WindowApiFactory() = default;
+
+shared_ptr<IWindowApi> WindowApiFactory::build(const WindowApiTypes &_windowApis)
+{
+  shared_ptr<IWindowApi> windowApi{};
+
+  switch (_windowApis)
+  {
+    case WindowApiTypes::NONE_SELECTED:
+    {
+      throw IllegalArgumentException{};
+    }
+    case WindowApiTypes::SDL_WINDOW_API:
+    {
+      windowApi = make_shared<SdlWindowApi>();
+    } break;
+  }
+
+  return windowApi;
+}

+ 9 - 2
source/window/SdlWindowApi.cpp

@@ -14,11 +14,12 @@
 using ls::atlantis::cycle::StatusCodeOk;
 using ls::atlantis::cycle::StatusCodeWindowApiLoadingFailed;
 using ls::atlantis::cycle::StatusCodeWindowCreationFailed;
+using ls::atlantis::glossary::WindowApiTypes;
 using ls::atlantis::interfaces::AStatusCode;
 using ls::atlantis::interfaces::ISdlApi;
-using ls::std::core::NullPointerArgumentEvaluator;
 using ls::atlantis::window::SdlApi;
 using ls::atlantis::window::SdlWindowApi;
+using ls::std::core::NullPointerArgumentEvaluator;
 using ::std::make_shared;
 using ::std::shared_ptr;
 
@@ -27,7 +28,8 @@ SdlWindowApi::SdlWindowApi() : SdlWindowApi(make_shared<SdlApi>())
 
 SdlWindowApi::SdlWindowApi(const shared_ptr<ISdlApi> &_sdlApi)
   : sdlApi(_sdlApi),
-    window(nullptr, [this](SDL_Window *_window) { this->sdlApi->DestroyWindow(_window); })
+    window(nullptr, [this](SDL_Window *_window) { this->sdlApi->DestroyWindow(_window); }),
+    windowApiType(WindowApiTypes::SDL_WINDOW_API)
 {
   NullPointerArgumentEvaluator(_sdlApi).evaluate();
 }
@@ -37,6 +39,11 @@ SdlWindowApi::~SdlWindowApi()
   sdlApi->Quit();
 }
 
+WindowApiTypes SdlWindowApi::getWindowApiType()
+{
+  return this->windowApiType;
+}
+
 shared_ptr<AStatusCode> SdlWindowApi::init()
 {
   shared_ptr<AStatusCode> statusCode = this->_initApi();

+ 2 - 2
test/cycle/StatusCodeOkTest.cpp

@@ -3,12 +3,12 @@
  * e-mail: webmaster@lynarstudios.com
  */
 
+#include <glossary/StatusCodes.hpp>
 #include <cycle/StatusCodeOk.hpp>
-#include <cycle/StatusCodes.hpp>
 #include <gtest/gtest.h>
 
 using ls::atlantis::cycle::StatusCodeOk;
-using ls::atlantis::cycle::StatusCodes;
+using ls::atlantis::glossary::StatusCodes;
 using ::std::string;
 using ::testing::Test;
 

+ 2 - 2
test/cycle/StatusCodeWindowApiLoadingFailedTest.cpp

@@ -3,12 +3,12 @@
  * e-mail: webmaster@lynarstudios.com
  */
 
+#include <glossary/StatusCodes.hpp>
 #include <cycle/StatusCodeWindowApiLoadingFailed.hpp>
-#include <cycle/StatusCodes.hpp>
 #include <gtest/gtest.h>
 
 using ls::atlantis::cycle::StatusCodeWindowApiLoadingFailed;
-using ls::atlantis::cycle::StatusCodes;
+using ls::atlantis::glossary::StatusCodes;
 using ::std::string;
 using ::testing::Test;
 

+ 2 - 2
test/cycle/StatusCodeWindowCreationFailedTest.cpp

@@ -3,12 +3,12 @@
  * e-mail: webmaster@lynarstudios.com
  */
 
+#include <glossary/StatusCodes.hpp>
 #include <cycle/StatusCodeWindowCreationFailed.hpp>
-#include <cycle/StatusCodes.hpp>
 #include <gtest/gtest.h>
 
 using ls::atlantis::cycle::StatusCodeWindowCreationFailed;
-using ls::atlantis::cycle::StatusCodes;
+using ls::atlantis::glossary::StatusCodes;
 using ::std::string;
 using ::testing::Test;
 

+ 6 - 0
test/cycle/mock/WindowApiMock.cpp

@@ -8,6 +8,7 @@
 
 using ls::atlantis::cycle::StatusCodeOk;
 using ls::atlantis::cycle::test::WindowApiMock;
+using ls::atlantis::glossary::WindowApiTypes;
 using ls::atlantis::interfaces::AStatusCode;
 using ::std::make_shared;
 using ::std::shared_ptr;
@@ -16,6 +17,11 @@ WindowApiMock::WindowApiMock() = default;
 
 WindowApiMock::~WindowApiMock() = default;
 
+WindowApiTypes WindowApiMock::getWindowApiType()
+{
+  return WindowApiTypes::NONE_SELECTED;
+}
+
 shared_ptr<AStatusCode> WindowApiMock::init()
 {
   return make_shared<StatusCodeOk>();

+ 1 - 0
test/cycle/mock/WindowApiMock.hpp

@@ -17,6 +17,7 @@ namespace ls::atlantis::cycle::test
       WindowApiMock();
       ~WindowApiMock() override;
 
+      [[nodiscard]] ls::atlantis::glossary::WindowApiTypes getWindowApiType() override;
       [[nodiscard]] ::std::shared_ptr<ls::atlantis::interfaces::AStatusCode> init() override;
   };
 }

+ 34 - 0
test/factory/WindowApiFactoryTest.cpp

@@ -0,0 +1,34 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#include <factory/WindowApiFactory.hpp>
+#include <glossary/WindowApiTypes.hpp>
+#include <gtest/gtest.h>
+#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <test/cycle/mock/WindowApiMock.hpp>
+
+using ls::atlantis::cycle::test::WindowApiMock;
+using ls::atlantis::factory::WindowApiFactory;
+using ls::atlantis::glossary::WindowApiTypes;
+using ls::std::core::IllegalArgumentException;
+using ::std::make_shared;
+using ::std::shared_ptr;
+using ::testing::Test;
+
+namespace
+{
+  class WindowApiFactoryTest : public Test
+  {
+    public:
+
+      WindowApiFactoryTest() = default;
+      ~WindowApiFactoryTest() override = default;
+  };
+
+  TEST_F(WindowApiFactoryTest, build_noneSelected)
+  {
+    ASSERT_THROW(static_cast<void>(WindowApiFactory::build(WindowApiTypes::NONE_SELECTED)), IllegalArgumentException);
+  }
+}

+ 5 - 5
test/window/SdlWindowApiTest.cpp

@@ -4,15 +4,15 @@
  */
 
 #include <gmock/gmock.h>
-#include <gtest/gtest.h>
 #include <test/window/mock/SdlApiMock.hpp>
-#include <window/SdlWindowApi.hpp>
-#include <cycle/StatusCodes.hpp>
-#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <glossary/StatusCodes.hpp>
+#include <gtest/gtest.h>
 #include <interface/AStatusCode.hpp>
+#include <ls-std/core/exception/IllegalArgumentException.hpp>
 #include <string>
+#include <window/SdlWindowApi.hpp>
 
-using ls::atlantis::cycle::StatusCodes;
+using ls::atlantis::glossary::StatusCodes;
 using ls::atlantis::interfaces::AStatusCode;
 using ls::atlantis::interfaces::ISdlApi;
 using ls::atlantis::window::SdlWindowApi;