diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a9e2c4767349b49f85bd7aed2e621b2c96df426..1c7eb767cdd2e0a6e239a62c0c9245b69bec7242 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,9 +82,7 @@ endif() # Demo project if (FGGL_EXAMPLES) - add_subdirectory(demo) - target_compile_options( demo PRIVATE -Wall -Wextra -Wodr -Wdouble-promotion -fno-strict-aliasing -fno-strict-overflow ) - set_property(TARGET demo PROPERTY INTERPROCEDURAL_OPTIMIZATION True) + add_subdirectory(demo EXCLUDE_FROM_ALL) endif() ## diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt index 62fc6639b0cadd796f2837254feb0f6832a1f97d..665ed51474f54a11c3ad1aa4fa49d6c028001e30 100644 --- a/demo/CMakeLists.txt +++ b/demo/CMakeLists.txt @@ -14,6 +14,10 @@ add_executable(demo demo/hexboard/camera.cpp ) +# set build flags +target_compile_options( demo PRIVATE -Wall -Wextra -Wodr -Wdouble-promotion -fno-strict-aliasing -fno-strict-overflow ) +set_property(TARGET demo PROPERTY INTERPROCEDURAL_OPTIMIZATION True) + target_include_directories(demo PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include diff --git a/fggl/assets/module.cpp b/fggl/assets/module.cpp index 5532515e1ba71fa669143d5b3565cbdc5e216bdf..670029946bd9f4ec7795f107821ba75f6a57a1c3 100644 --- a/fggl/assets/module.cpp +++ b/fggl/assets/module.cpp @@ -20,7 +20,7 @@ namespace fggl::assets { - auto AssetFolders::factory(modules::ModuleService service, modules::Services &services) -> bool { + auto AssetFolders::factory(modules::ServiceName service, modules::Services &services) -> bool { if (service == Loader::service) { auto *storage = services.get<data::Storage>(); auto *checkin = services.get<CheckinAdapted>(); diff --git a/fggl/assets/packed/module.cpp b/fggl/assets/packed/module.cpp index 44e9e56353d2859d336e303772150e68871da937..4d9dc9e115b0affc414af23ed686a263bbb482a5 100644 --- a/fggl/assets/packed/module.cpp +++ b/fggl/assets/packed/module.cpp @@ -20,7 +20,7 @@ namespace fggl::assets { - auto PackedAssets::factory(modules::ModuleService service, modules::Services &services) -> bool { + auto PackedAssets::factory(modules::ServiceName service, modules::Services &services) -> bool { if (service == RawCheckin::service) { services.create<RawCheckin>(); return true; diff --git a/fggl/audio/fallback/audio.cpp b/fggl/audio/fallback/audio.cpp index 50edca4b81a45437df6fbbff5e64bdc43cd226b9..32dd285023317d05527973b70c573f96256e0f59 100644 --- a/fggl/audio/fallback/audio.cpp +++ b/fggl/audio/fallback/audio.cpp @@ -26,7 +26,7 @@ namespace fggl::audio { void NullAudioService::play(const fggl::audio::AudioClipShort &, bool) { } - auto NullAudio::factory(modules::ModuleService service, modules::Services &services) -> bool{ + auto NullAudio::factory(modules::ServiceName service, modules::Services &services) -> bool{ if (service == SERVICE_AUDIO_PLAYBACK) { services.bind<audio::AudioService, audio::NullAudioService>(); return true; diff --git a/fggl/audio/openal/module.cpp b/fggl/audio/openal/module.cpp index 909dc63c9885d61be82ad3c024bb781680aa2254..843cd4218817dbe8dee5354eca9dd80c72373a66 100644 --- a/fggl/audio/openal/module.cpp +++ b/fggl/audio/openal/module.cpp @@ -20,7 +20,7 @@ namespace fggl::audio { - auto OpenAL::factory(modules::ModuleService service, modules::Services &services) -> bool { + auto OpenAL::factory(modules::ServiceName service, modules::Services &services) -> bool { if (service == SERVICE_AUDIO_PLAYBACK) { auto* assets = services.get<assets::AssetManager>(); diff --git a/fggl/data/assimp/module.cpp b/fggl/data/assimp/module.cpp index d7852e26a475695a7493ea3354f6ce8d46627a6a..453edfbc91f0ecbc5705685bdf0d5ebccf85462a 100644 --- a/fggl/data/assimp/module.cpp +++ b/fggl/data/assimp/module.cpp @@ -314,7 +314,7 @@ namespace fggl::data::models { return true; } - auto AssimpModule::factory(modules::ModuleService service, modules::Services &serviceManager) -> bool { + auto AssimpModule::factory(modules::ServiceName service, modules::Services &serviceManager) -> bool { if ( service == MODEL_PROVIDER ) { auto* assetLoader = serviceManager.get<assets::Loader>(); assetLoader->setFactory( MODEL_MULTI3D, load_assimp_model, assets::LoadType::PATH ); diff --git a/fggl/data/module.cpp b/fggl/data/module.cpp index 1872b5f34dc97757bc648438b61b48c5dfba30c7..cbf91275b990fafacce76cac5e75cc768534a545 100644 --- a/fggl/data/module.cpp +++ b/fggl/data/module.cpp @@ -20,7 +20,7 @@ namespace fggl::data { - auto LocalStorage::factory(modules::ModuleService service, modules::Services &data) -> bool { + auto LocalStorage::factory(modules::ServiceName service, modules::Services &data) -> bool { if (service == SERVICE_STORAGE) { // FIXME: no easy way to set the application name auto pathConfig = fggl::platform::calc_engine_paths("fggl-demo"); diff --git a/fggl/entity/module.cpp b/fggl/entity/module.cpp index 29256f52ffb8cf02929d4ce261967936aa74cc82..11688728c3893d4bad1c2b0b293697461e9c6e1b 100644 --- a/fggl/entity/module.cpp +++ b/fggl/entity/module.cpp @@ -65,7 +65,7 @@ namespace fggl::entity { } - auto ECS::factory(modules::ModuleService service, modules::Services &services) -> bool { + auto ECS::factory(modules::ServiceName service, modules::Services &services) -> bool { if (service == EntityFactory::service) { auto *factory = services.create<EntityFactory>(services); install_component_factories(factory); diff --git a/fggl/gfx/ogl4/module.cpp b/fggl/gfx/ogl4/module.cpp index 8757be785b4cd38776e9b0af719f82a32585d52d..14974f18781b19577440b3e8806ab0e788ae0261 100644 --- a/fggl/gfx/ogl4/module.cpp +++ b/fggl/gfx/ogl4/module.cpp @@ -169,7 +169,7 @@ namespace fggl::gfx { light.quadratic = spec.get<float>("quadratic", 0.000007F); } - auto OpenGL4::factory(modules::ModuleService service, modules::Services &services) -> bool { + auto OpenGL4::factory(modules::ServiceName service, modules::Services &services) -> bool { if (service == WindowGraphics::service) { // setup the thing responsible for graphics auto *storage = services.get<data::Storage>(); diff --git a/fggl/phys/null.cpp b/fggl/phys/null.cpp index e993987c88c341adf901a320505b959dbb815646..9df68b27de9787c0e779880a3e36e26cc5092e26 100644 --- a/fggl/phys/null.cpp +++ b/fggl/phys/null.cpp @@ -20,7 +20,7 @@ namespace fggl::phys { - auto NullPhysics::factory(modules::ModuleService serviceName, modules::Services &serviceManager) -> bool { + auto NullPhysics::factory(modules::ServiceName serviceName, modules::Services &serviceManager) -> bool { if (serviceName == phys::PhysicsProvider::service) { serviceManager.bind<phys::PhysicsProvider, NullPhysicsProvider>(); return true; diff --git a/include/fggl/assets/loader.hpp b/include/fggl/assets/loader.hpp index d7c053a396966db20f1540f99a50f6d76a8bb8f6..beedcc2711aa337dc82b417ca8da18a25bd57d1c 100644 --- a/include/fggl/assets/loader.hpp +++ b/include/fggl/assets/loader.hpp @@ -46,7 +46,7 @@ namespace fggl::assets { class Loader { public: - constexpr const static modules::ModuleService service = modules::make_service("fggl::assets::Loader"); + constexpr const static auto service = modules::make_service("fggl::assets::Loader"); explicit inline Loader(data::Storage *storage, CheckinAdapted *checkin) : m_storage(storage), m_checkin(checkin) {} diff --git a/include/fggl/assets/manager.hpp b/include/fggl/assets/manager.hpp index ef1ffee15a0b737154f9fac4e6f829261e45b4e1..86dd5c1cfea4495ddee24ee365cce37a88f8cd09 100644 --- a/include/fggl/assets/manager.hpp +++ b/include/fggl/assets/manager.hpp @@ -66,7 +66,7 @@ namespace fggl::assets { class AssetManager { public: - constexpr const static modules::ModuleService service = modules::make_service("fggl::assets::Manager"); + constexpr const static modules::ServiceName service = modules::make_service("fggl::assets::Manager"); AssetManager() = default; virtual ~AssetManager() = default; diff --git a/include/fggl/assets/module.hpp b/include/fggl/assets/module.hpp index 388268e7bb034b532f59502cc90fa4f9392e3cda..81e09abb65fe1bcbdfd93aa7acf84735763f86ce 100644 --- a/include/fggl/assets/module.hpp +++ b/include/fggl/assets/module.hpp @@ -31,15 +31,15 @@ namespace fggl::assets { struct AssetFolders { constexpr static const char *name = "fggl::assets::Folders"; - constexpr static const std::array<modules::ModuleService, 2> provides = { + constexpr static const std::array<modules::ServiceName, 2> provides = { Loader::service, AssetManager::service }; - constexpr static const std::array<modules::ModuleService, 2> depends = { + constexpr static const std::array<modules::ServiceName, 2> depends = { data::Storage::service, CheckinAdapted::service }; - static bool factory(modules::ModuleService name, modules::Services &serviceManager); + static bool factory(modules::ServiceName name, modules::Services &serviceManager); }; } // namespace fggl::assets diff --git a/include/fggl/assets/packed/adapter.hpp b/include/fggl/assets/packed/adapter.hpp index 1c864921efc722cac2e6ff76900772b103073b22..d2299850391801620a421fd26b26602587ce2dc9 100644 --- a/include/fggl/assets/packed/adapter.hpp +++ b/include/fggl/assets/packed/adapter.hpp @@ -64,7 +64,7 @@ namespace fggl::assets { */ class CheckinAdapted { public: - constexpr const static modules::ModuleService service = modules::make_service("fggl::assets::checkin::debug"); + constexpr const static auto service = modules::make_service("fggl::assets::checkin::debug"); using FilePredicate = std::function<AssetTypeID(const std::filesystem::path&)>; using FileLoader = std::function<bool(const std::filesystem::path&, MemoryBlock& block)>; diff --git a/include/fggl/assets/packed/direct.hpp b/include/fggl/assets/packed/direct.hpp index f10796d1d63aa734ae07ac468c22502a00e9533e..e184ab48210b8c6c5de152115adb88e02da6e79d 100644 --- a/include/fggl/assets/packed/direct.hpp +++ b/include/fggl/assets/packed/direct.hpp @@ -37,7 +37,7 @@ namespace fggl::assets { class RawCheckin { public: - constexpr const static modules::ModuleService service = modules::make_service("fggl::assets::checkin"); + constexpr const static auto service = modules::make_service("fggl::assets::checkin"); using DecodeAndCheckFunc = std::function<void(AssetGUID, MemoryBlock& block)>; void check(AssetID, AssetTypeID, MemoryBlock& block) const; diff --git a/include/fggl/assets/packed/module.hpp b/include/fggl/assets/packed/module.hpp index 8ecc4b2583d2a5f20c2af511b4804b07073026af..c99b99d738f560ce667c729085ec8ddf9c2e6c21 100644 --- a/include/fggl/assets/packed/module.hpp +++ b/include/fggl/assets/packed/module.hpp @@ -30,14 +30,14 @@ namespace fggl::assets { struct PackedAssets { constexpr static const char *name = "fggl::assets::packed"; - constexpr static const std::array<modules::ModuleService, 2> provides = { + constexpr static const std::array<modules::ServiceName, 2> provides = { RawCheckin::service, CheckinAdapted::service }; - constexpr static const std::array<modules::ModuleService, 1> depends = { + constexpr static const std::array<modules::ServiceName, 1> depends = { data::Storage::service }; - static bool factory(modules::ModuleService name, modules::Services &serviceManager); + static bool factory(modules::ServiceName name, modules::Services &serviceManager); }; } // namespace fggl::assets diff --git a/include/fggl/audio/audio.hpp b/include/fggl/audio/audio.hpp index a31eb6d57fb8ba149e29f129afceae81211e1983..3c021a4fbf6681e357aa66f9c90d9fe247543e15 100644 --- a/include/fggl/audio/audio.hpp +++ b/include/fggl/audio/audio.hpp @@ -57,7 +57,7 @@ namespace fggl::audio { constexpr auto ASSET_CLIP_SHORT = assets::make_asset_type("Audio:Clip:Short"); constexpr auto ASSET_CLIP_BYTE = assets::make_asset_type("Audio:Clip:Byte"); - constexpr modules::ModuleService SERVICE_AUDIO_PLAYBACK = modules::make_service("fggl::audio::AudioService"); + constexpr auto SERVICE_AUDIO_PLAYBACK = modules::make_service("fggl::audio::AudioService"); /** * @@ -65,7 +65,7 @@ namespace fggl::audio { */ class AudioService { public: - constexpr static const modules::ModuleService service = SERVICE_AUDIO_PLAYBACK; + constexpr static const modules::ServiceName service = SERVICE_AUDIO_PLAYBACK; virtual void play(const assets::AssetGUID &asset, bool looping = false) = 0; virtual void play(const AudioClipShort &clip, bool looping = false) = 0; diff --git a/include/fggl/audio/null_audio.hpp b/include/fggl/audio/null_audio.hpp index 10df67a212b201f33de1456b2ffd55d070f79e92..fbcd9b3896799c11d77753514221df71e2ff9064 100644 --- a/include/fggl/audio/null_audio.hpp +++ b/include/fggl/audio/null_audio.hpp @@ -39,11 +39,11 @@ namespace fggl::audio { struct NullAudio { constexpr static const char *name = "fggl::audio::NULL"; - constexpr static const std::array<modules::ModuleService, 1> provides = { + constexpr static const std::array<modules::ServiceName, 1> provides = { SERVICE_AUDIO_PLAYBACK }; - constexpr static const std::array<modules::ModuleService, 0> depends = {}; - bool factory(modules::ModuleService, modules::Services&); + constexpr static const std::array<modules::ServiceName, 0> depends = {}; + bool factory(modules::ServiceName, modules::Services&); }; diff --git a/include/fggl/audio/openal/module.hpp b/include/fggl/audio/openal/module.hpp index 4ca559d7a17da573aeea85e388ed008aa26f812f..28e6d1fa1fe93bba82f33f3d19caef773ed663e5 100644 --- a/include/fggl/audio/openal/module.hpp +++ b/include/fggl/audio/openal/module.hpp @@ -35,14 +35,14 @@ namespace fggl::audio { struct OpenAL { constexpr static const char *name = "fggl::audio::OpenAL"; - constexpr static const std::array<modules::ModuleService, 1> provides = { + constexpr static const std::array<modules::ServiceName, 1> provides = { SERVICE_AUDIO_PLAYBACK }; - constexpr static const std::array<modules::ModuleService, 2> depends = { + constexpr static const std::array<modules::ServiceName, 2> depends = { assets::AssetManager::service, assets::CheckinAdapted::service }; - static bool factory(modules::ModuleService name, modules::Services &serviceManager); + static bool factory(modules::ServiceName name, modules::Services &serviceManager); }; diff --git a/include/fggl/data/assimp/module.hpp b/include/fggl/data/assimp/module.hpp index a10e11422f0a60af3e0a1359663cd74d7722f819..98e7a50e30075afc111860f128a530d442df788b 100644 --- a/include/fggl/data/assimp/module.hpp +++ b/include/fggl/data/assimp/module.hpp @@ -51,14 +51,14 @@ namespace fggl::data::models { struct AssimpModule { constexpr static const char *name = "fggl::data::Assimp"; - constexpr static const std::array<modules::ModuleService, 1> provides = { + constexpr static const std::array<modules::ServiceName, 1> provides = { MODEL_PROVIDER }; - constexpr static const std::array<modules::ModuleService, 2> depends = { + constexpr static const std::array<modules::ServiceName, 2> depends = { assets::Loader::service, assets::CheckinAdapted::service }; - static bool factory(modules::ModuleService service, modules::Services &serviceManager); + static bool factory(modules::ServiceName service, modules::Services &serviceManager); }; } diff --git a/include/fggl/data/module.hpp b/include/fggl/data/module.hpp index f4c9c43db705c4dafdddf0ffde1f8e6c0bf2c054..dc9cd5b9dd371d9566e4a4f422b49097703a23d1 100644 --- a/include/fggl/data/module.hpp +++ b/include/fggl/data/module.hpp @@ -27,11 +27,11 @@ namespace fggl::data { struct LocalStorage { constexpr static const char *name = "fggl::data::Storage"; - constexpr static const std::array<modules::ModuleService, 1> provides = { + constexpr static const std::array<modules::ServiceName, 1> provides = { SERVICE_STORAGE }; - constexpr static const std::array<modules::ModuleService, 0> depends = {}; - static bool factory(modules::ModuleService service, modules::Services &serviceManager); + constexpr static const std::array<modules::ServiceName, 0> depends = {}; + static bool factory(modules::ServiceName service, modules::Services &serviceManager); }; } // namespace fggl::data diff --git a/include/fggl/data/storage.hpp b/include/fggl/data/storage.hpp index 0b992595ecac218d4c58ba4de2390e76e7ebb5ea..79fb3a16562b3680737b7eea6b056fa7cdca8190 100644 --- a/include/fggl/data/storage.hpp +++ b/include/fggl/data/storage.hpp @@ -35,11 +35,11 @@ namespace fggl::data { enum StorageType { Data, Config, Cache }; - constexpr const modules::ModuleService SERVICE_STORAGE = modules::make_service("fggl::data::Storage"); + constexpr const auto SERVICE_STORAGE = modules::make_service("fggl::data::Storage"); class Storage { public: - constexpr static modules::ModuleService service = SERVICE_STORAGE; + constexpr static auto service = SERVICE_STORAGE; Storage(fggl::platform::EnginePaths paths) : m_paths(std::move(paths)) {} diff --git a/include/fggl/display/glfw/module.hpp b/include/fggl/display/glfw/module.hpp index 8b0940f187ea4278c1a4c833261a3826eb67f993..b844fa330aa10af040c8e8f8451fa600bddbe4fc 100644 --- a/include/fggl/display/glfw/module.hpp +++ b/include/fggl/display/glfw/module.hpp @@ -29,18 +29,18 @@ namespace fggl::display { struct GLFW { constexpr static const char *name = "fggl::display::glfw"; - constexpr static const std::array<modules::ModuleService, 1> provides = { + constexpr static const std::array<modules::ServiceName, 1> provides = { WindowService::service }; - constexpr static const std::array<modules::ModuleService, 2> depends = { + constexpr static const std::array<modules::ServiceName, 2> depends = { fggl::input::Input::service, fggl::gfx::WindowGraphics::service }; - static bool factory(modules::ModuleService name, modules::Services &serviceManager); + static bool factory(modules::ServiceName name, modules::Services &serviceManager); }; - bool GLFW::factory(modules::ModuleService service, modules::Services &services) { + bool GLFW::factory(modules::ServiceName service, modules::Services &services) { if (service == WindowService::service) { auto input = services.get<input::Input>(); auto graphics = services.get<gfx::WindowGraphics>(); diff --git a/include/fggl/display/window.hpp b/include/fggl/display/window.hpp index 1d0f9f7272e3f37c8bdd7e71061cf7e875af87dd..59c59aae78638ecb02a1887573bc5d3b6e6d6b6c 100644 --- a/include/fggl/display/window.hpp +++ b/include/fggl/display/window.hpp @@ -59,7 +59,7 @@ namespace fggl::display { class WindowService { public: - constexpr static const modules::ModuleService + constexpr static const auto service = modules::make_service("fggl::display::WindowService"); virtual Window *create() = 0; diff --git a/include/fggl/entity/loader/loader.hpp b/include/fggl/entity/loader/loader.hpp index 86e6d4aa95b1dc04f75ec43f3176e19d91020f9f..5ae0442025821aafaf3f6874132f58188e72266f 100644 --- a/include/fggl/entity/loader/loader.hpp +++ b/include/fggl/entity/loader/loader.hpp @@ -44,7 +44,7 @@ namespace fggl::entity { class EntityFactory { public: - constexpr static const modules::ModuleService service = modules::make_service("fggl::entity:Factory"); + constexpr static const modules::ServiceName service = modules::make_service("fggl::entity:Factory"); inline EntityFactory(modules::Services &services) : m_services(services) {} diff --git a/include/fggl/entity/module.hpp b/include/fggl/entity/module.hpp index cb27abc094d2cb3e6cf2c5a9b4f70d59abd79d09..e3cd5d541a196ebdc37d0dc18650a065b2d46b6d 100644 --- a/include/fggl/entity/module.hpp +++ b/include/fggl/entity/module.hpp @@ -31,14 +31,14 @@ namespace fggl::entity { struct ECS { constexpr static const char *name = "fggl::entity::ECS"; - constexpr static const std::array<modules::ModuleService, 1> provides = { + constexpr static const std::array<modules::ServiceName, 1> provides = { EntityFactory::service }; - constexpr static const std::array<modules::ModuleService, 2> depends = { + constexpr static const std::array<modules::ServiceName, 2> depends = { assets::Loader::service, assets::CheckinAdapted::service }; - static bool factory(modules::ModuleService name, modules::Services &serviceManager); + static bool factory(modules::ServiceName name, modules::Services &serviceManager); }; void install_component_factories(EntityFactory *factory); diff --git a/include/fggl/gfx/interfaces.hpp b/include/fggl/gfx/interfaces.hpp index 4e1953d7c051239d6a7b5090e2c7aebc6f7d6320..bde4367da27a4ed7e501f6740975d9cd9cfb73db 100644 --- a/include/fggl/gfx/interfaces.hpp +++ b/include/fggl/gfx/interfaces.hpp @@ -34,7 +34,7 @@ namespace fggl::gfx { class Graphics { public: - constexpr static const modules::ModuleService service = modules::make_service("fggl::gfx::Graphics"); + constexpr static const auto service = modules::make_service("fggl::gfx::Graphics"); virtual ~Graphics() = default; virtual void clear() = 0; diff --git a/include/fggl/gfx/ogl4/module.hpp b/include/fggl/gfx/ogl4/module.hpp index abde2d309df64630823c95ec1fe306c7fa299771..8cdb6ad797775722d31dd4b5e1c08a7d6d9a38a7 100644 --- a/include/fggl/gfx/ogl4/module.hpp +++ b/include/fggl/gfx/ogl4/module.hpp @@ -38,10 +38,10 @@ namespace fggl::gfx { struct OpenGL4 { constexpr static const char *name = "fggl::gfx::OpenGL4"; - constexpr static const std::array<modules::ModuleService, 1> provides = { + constexpr static const std::array<modules::ServiceName, 1> provides = { WindowGraphics::service }; - constexpr static const std::array<modules::ModuleService, 5> depends = { + constexpr static const std::array<modules::ServiceName, 5> depends = { data::Storage::service, assets::AssetManager::service, assets::CheckinAdapted::service, @@ -49,7 +49,7 @@ namespace fggl::gfx { entity::EntityFactory::service }; - static bool factory(modules::ModuleService name, modules::Services &serviceManager); + static bool factory(modules::ServiceName name, modules::Services &serviceManager); }; } //namespace fggl::gfx diff --git a/include/fggl/gfx/setup.hpp b/include/fggl/gfx/setup.hpp index 9f1a65c3e618029f323d66226829f67b1935951a..804bc00e771221912403fc4d3f994551c5e84d34 100644 --- a/include/fggl/gfx/setup.hpp +++ b/include/fggl/gfx/setup.hpp @@ -37,7 +37,7 @@ namespace fggl::gfx { class WindowGraphics { public: - constexpr const static modules::ModuleService service = modules::make_service("fggl::gfx::WindowGraphics"); + constexpr const static auto service = modules::make_service("fggl::gfx::WindowGraphics"); WindowGraphics() = default; virtual ~WindowGraphics() = default; diff --git a/include/fggl/gui/fonts.hpp b/include/fggl/gui/fonts.hpp index 902f85ebaf8610db6462b0a2d6b9e71bd873db26..4983a0a8ac3398464c28135d0e5e5bc359349a6c 100644 --- a/include/fggl/gui/fonts.hpp +++ b/include/fggl/gui/fonts.hpp @@ -71,7 +71,7 @@ namespace fggl::gui { class FontLibrary { public: - constexpr static const modules::ModuleService service = modules::make_service("fggl::gui::font"); + constexpr static const auto service = modules::make_service("fggl::gui::font"); explicit FontLibrary(data::Storage *storage); ~FontLibrary(); diff --git a/include/fggl/gui/module.hpp b/include/fggl/gui/module.hpp index cfe103430160db19c8e09dba13ec4bd95def553d..14723b4867715dd5a0685551a1b74449252e33af 100644 --- a/include/fggl/gui/module.hpp +++ b/include/fggl/gui/module.hpp @@ -39,18 +39,18 @@ namespace fggl::gui { struct FreeType { constexpr static const char *name = "fggl::gui::FreeType"; - constexpr static const std::array<modules::ModuleService, 2> provides = { + constexpr static const std::array<modules::ServiceName, 2> provides = { FontLibrary::service, model::WidgetFactory::service }; - constexpr static const std::array<modules::ModuleService, 2> depends = { + constexpr static const std::array<modules::ServiceName, 2> depends = { data::Storage::service, assets::CheckinAdapted::service }; - static bool factory(modules::ModuleService name, modules::Services &serviceManager); + static bool factory(modules::ServiceName name, modules::Services &serviceManager); }; - bool FreeType::factory(modules::ModuleService service, modules::Services &services) { + bool FreeType::factory(modules::ServiceName service, modules::Services &services) { if (service == FontLibrary::service) { auto storage = services.get<data::Storage>(); services.create<FontLibrary>(storage); diff --git a/include/fggl/input/input.hpp b/include/fggl/input/input.hpp index 446499422b61928e3dcf351eda61aeb98afd7e24..dbdd0a27b37d5e55d4d76e94e29adf7ea7344991 100644 --- a/include/fggl/input/input.hpp +++ b/include/fggl/input/input.hpp @@ -24,11 +24,11 @@ namespace fggl::input { - constexpr const modules::ModuleService SERVICE_INPUT = modules::make_service("fggl::input::Input"); + constexpr const auto SERVICE_INPUT = modules::make_service("fggl::input::Input"); class Input { public: - constexpr static const modules::ModuleService service = SERVICE_INPUT; + constexpr static const auto service = SERVICE_INPUT; Input() = default; void frame(float dt); diff --git a/include/fggl/input/module.hpp b/include/fggl/input/module.hpp index 224e402b9d2c17f421b10b0ad3ee98c19f104043..b550ab016b12a5a90ef5016c9a0027fc0bbe0d36 100644 --- a/include/fggl/input/module.hpp +++ b/include/fggl/input/module.hpp @@ -27,14 +27,14 @@ namespace fggl::input { struct Generic { constexpr static const char *name = "fggl::input::Generic"; - constexpr static const std::array<modules::ModuleService, 1> provides = { + constexpr static const std::array<modules::ServiceName, 1> provides = { SERVICE_INPUT }; - constexpr static const std::array<modules::ModuleService, 0> depends = {}; - static bool factory(modules::ModuleService service, modules::Services &services); + constexpr static const std::array<modules::ServiceName, 0> depends = {}; + static bool factory(modules::ServiceName service, modules::Services &services); }; - bool Generic::factory(modules::ModuleService service, modules::Services &services) { + bool Generic::factory(modules::ServiceName service, modules::Services &services) { if (service == SERVICE_INPUT) { services.create<input::Input>(); return true; diff --git a/include/fggl/modules/manager.hpp b/include/fggl/modules/manager.hpp index 6279702891a1a9224cfb5645399820b56c91e9b3..522553a05b0334e9ecb760719fab006488dc725c 100644 --- a/include/fggl/modules/manager.hpp +++ b/include/fggl/modules/manager.hpp @@ -144,6 +144,23 @@ namespace fggl::modules { public: Manager() = default; + template<ServiceType T> + class Service { + public: + inline Service(Manager* manager) : m_manager(manager) {} + + inline T* operator->() { + if ( m_ptr == nullptr ) { + m_ptr = m_manager->get<T>(); + } + return m_ptr; + } + + private: + Manager* m_manager; + std::shared_ptr<T> m_ptr; + }; + inline void addVirtual(const Config &config) { assert(!m_locked); @@ -153,7 +170,7 @@ namespace fggl::modules { } } - template<typename T> + template<ModuleType T> void use() { assert(!m_locked); @@ -196,12 +213,18 @@ namespace fggl::modules { return true; } - template<typename T> + template<ServiceType T> T *get() const { assert(m_locked); return m_services.template get<T>(); } + template<ServiceType T> + Service<T> getLazy() const { + assert(m_locked); + return { this }; + } + void resolve() { assert( !m_locked ); if (!buildGraph()) { @@ -245,7 +268,7 @@ namespace fggl::modules { Services m_services; std::map<ModuleIdentifier, Config> m_modules; DependencyGraph<ModuleIdentifier> m_dependencies; - std::map<ModuleService, ModuleIdentifier> m_serviceProviders; + std::map<ServiceName, ModuleIdentifier> m_serviceProviders; }; diff --git a/include/fggl/modules/module.hpp b/include/fggl/modules/module.hpp index e3ba6c2c83eb3e7c379f5c77ea834474d7a726e7..4d0944ef96ac47cd3247a862740b903eed03716e 100644 --- a/include/fggl/modules/module.hpp +++ b/include/fggl/modules/module.hpp @@ -26,53 +26,39 @@ #include <memory> #include "fggl/util/safety.hpp" +#include "service.hpp" namespace fggl::modules { + template<typename T> + concept ModuleType = requires(T type) { + { T::provides }; + { T::depends }; + }; + using ModuleIdentifier = std::string; - using ModuleService = util::OpaqueName<std::string_view, struct ModuleServiceTag>; - constexpr ModuleService make_service(const std::string_view name) { - return ModuleService::make(name); - } + using ServiceFactory = std::function<bool(ServiceName , Services &)>; + struct Config { + ModuleIdentifier name; + std::vector<ServiceName> provides; + std::vector<ServiceName> depends; + ServiceFactory factory = nullptr; + }; - class Services { + class Module { public: - template<typename Svc, typename Impl, typename ...Args> - void bind(Args... args) { - static_assert(std::is_base_of_v<Svc, Impl>, "Service type must be assignable from implementation type"); - m_services[Svc::service] = std::make_shared<Impl>(args...); - } + virtual ~Module() = default; - template<typename Svc, typename ...Args> - Svc *create(Args... args) { - auto svc = std::make_shared<Svc>(args...); - m_services[Svc::service] = svc; - return svc.get(); - } + // copying modules is bad + Module(const Module&) = delete; + Module& operator=(const Module&) = delete; - template<typename Svc> - void provide(std::shared_ptr<Svc> service) { - m_services[Svc::service] = service; - } + // moving modules is bad + Module(Module&&) = delete; + Module& operator=(Module&&) = delete; - template<typename S> - S *get() const { - auto serviceWrapper = m_services.at(S::service); - auto ptr = std::static_pointer_cast<S>(serviceWrapper); - return ptr.get(); - } - - private: - std::map<ModuleService, std::shared_ptr<void>> m_services; - }; - - using ServiceFactory = std::function<bool(ModuleService, Services &)>; - struct Config { - ModuleIdentifier name; - std::vector<ModuleService> provides; - std::vector<ModuleService> depends; - ServiceFactory factory = nullptr; + virtual auto create(ServiceName, Services&) -> bool = 0; }; } // namespace fggl::modules diff --git a/include/fggl/modules/service.hpp b/include/fggl/modules/service.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a1b679b3a4bd874594d4f63b7d9bdb28d8486f29 --- /dev/null +++ b/include/fggl/modules/service.hpp @@ -0,0 +1,75 @@ +/* + * This file is part of FGGL. + * + * FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with FGGL. + * If not, see <https://www.gnu.org/licenses/>. + */ + +// +// Created by webpigeon on 25/03/23. +// + +#ifndef FGGL_MODULES_SERVICE_HPP +#define FGGL_MODULES_SERVICE_HPP + +#include <map> +#include <memory> + +#include "fggl/util/safety.hpp" + +namespace fggl::modules { + + using ServiceName = util::OpaqueName<std::string_view, struct ModuleServiceTag>; + constexpr ServiceName make_service(const std::string_view name) { + return ServiceName::make(name); + } + + template<typename T> + concept ServiceType = requires(T* type) { + { T::service }; + }; + + template<typename T, typename U> + concept Derived = std::is_base_of<U, T>::value; + + class Services { + public: + template<ServiceType Svc, Derived<Svc> Impl, typename ...Args> + void bind(Args... args) { + static_assert(std::is_base_of_v<Svc, Impl>, "Service type must be assignable from implementation type"); + m_services[Svc::service] = std::make_shared<Impl>(args...); + } + + template<ServiceType Svc, typename ...Args> + Svc *create(Args... args) { + auto svc = std::make_shared<Svc>(args...); + m_services[Svc::service] = svc; + return svc.get(); + } + + template<ServiceType Svc> + void provide(std::shared_ptr<Svc> service) { + m_services[Svc::service] = service; + } + + template<ServiceType S> + S *get() const { + auto serviceWrapper = m_services.at(S::service); + auto ptr = std::static_pointer_cast<S>(serviceWrapper); + return ptr.get(); + } + + private: + std::map<ServiceName, std::shared_ptr<void>> m_services; + }; + +} // namespace fggl::modules + +#endif //FGGL_MODULES_SERVICE_HPP diff --git a/include/fggl/phys/null.hpp b/include/fggl/phys/null.hpp index 276e22fe07e34d57c4a4a9224c44471bc85c5cb9..d39324dea52b3a203d3a88070d0c5fef316c96ac 100644 --- a/include/fggl/phys/null.hpp +++ b/include/fggl/phys/null.hpp @@ -70,13 +70,13 @@ namespace fggl::phys { struct NullPhysics { constexpr static const char *name = "fggl::phys::null"; - constexpr static const std::array<modules::ModuleService, 1> provides = { + constexpr static const std::array<modules::ServiceName, 1> provides = { phys::PhysicsProvider::service }; - constexpr static const std::array<modules::ModuleService, 1> depends = { + constexpr static const std::array<modules::ServiceName, 1> depends = { entity::EntityFactory::service }; - static bool factory(modules::ModuleService serviceName, modules::Services &serviceManager); + static bool factory(modules::ServiceName serviceName, modules::Services &serviceManager); }; } // namespace fggl::phys diff --git a/include/fggl/phys/service.hpp b/include/fggl/phys/service.hpp index 496529f328cf24bd9b57fda2404722a50bee4eec..c637ca2f19aaa44df12c00234a9be0b3834cc5a6 100644 --- a/include/fggl/phys/service.hpp +++ b/include/fggl/phys/service.hpp @@ -27,7 +27,7 @@ namespace fggl::phys { class PhysicsProvider { public: - constexpr static const modules::ModuleService service = modules::make_service("fggl::phys::service"); + constexpr static const auto service = modules::make_service("fggl::phys::service"); virtual ~PhysicsProvider() = default; virtual PhysicsEngine *create(entity::EntityManager *entityManager, entity::EntityFactory *factory) = 0; }; diff --git a/include/fggl/script/engine.hpp b/include/fggl/script/engine.hpp index fb566cf21009ac979a9a9510c0a5eefffd0efc8f..07ae3b37cb6c3e70affae0a5e23ff919c78d2110 100644 --- a/include/fggl/script/engine.hpp +++ b/include/fggl/script/engine.hpp @@ -46,7 +46,7 @@ namespace fggl::script { class ScriptProvider { public: - constexpr static const modules::ModuleService service = modules::make_service("fggl::script::service"); + constexpr static const modules::ServiceName service = modules::make_service("fggl::script::service"); virtual ScriptEngine* create() = 0; }; diff --git a/integrations/lua/include/fggl/script/lua/module.hpp b/integrations/lua/include/fggl/script/lua/module.hpp index c3e2c72e9118843b692da367ffde937c3f0ac4a6..6eda012719cf05b8ef4d7b3aa9ff316d94931bf0 100644 --- a/integrations/lua/include/fggl/script/lua/module.hpp +++ b/integrations/lua/include/fggl/script/lua/module.hpp @@ -35,14 +35,14 @@ namespace fggl::script::lua { struct Lua { constexpr static const char* name = "fggl::script::lua"; - constexpr static const std::array<modules::ModuleService, 1> provides = { + constexpr static const std::array<modules::ServiceName, 1> provides = { script::ScriptProvider::service }; - constexpr static const std::array<modules::ModuleService, 2> depends = { + constexpr static const std::array<modules::ServiceName, 2> depends = { data::SERVICE_STORAGE, assets::CheckinAdapted::service }; - static bool factory(modules::ModuleService name, modules::Services& serviceManager); + static bool factory(modules::ServiceName name, modules::Services& serviceManager); }; } // namespace fggl::script::lua diff --git a/integrations/lua/src/module.cpp b/integrations/lua/src/module.cpp index 5356a4bced164ddf023c2115fddee0f1ca99fb3e..bfe91a2e96d12a4bff7daeeb8692c0f7bcab7429 100644 --- a/integrations/lua/src/module.cpp +++ b/integrations/lua/src/module.cpp @@ -28,10 +28,10 @@ namespace fggl::script::lua { return assets::INVALID_ASSET_TYPE; } - bool Lua::factory(modules::ModuleService service, modules::Services &serviceManager) { + bool Lua::factory(modules::ServiceName service, modules::Services &serviceManager) { if ( service == ScriptProvider::service ) { - auto storageService = serviceManager.get<data::Storage>(); + auto *storageService = serviceManager.get<data::Storage>(); serviceManager.bind<ScriptProvider,LuaScriptProvider>(storageService); auto *assetPacker = serviceManager.get<assets::CheckinAdapted>();