Просмотр исходного кода

Modules can be created by passing parameters instead of constructing the instances externally

Marko Pintera 11 лет назад
Родитель
Сommit
4221419a6b

+ 5 - 5
BansheeEditor/Source/BsEditorApplication.cpp

@@ -75,7 +75,7 @@ namespace BansheeEngine
 
 		const String& renderSystemLibraryName = getLibraryNameForRenderSystem(renderSystemPlugin);
 		gBansheeApp().startUp(renderWindowDesc, renderSystemLibraryName, "BansheeForwardRenderer"); // TODO - Make renderer and resource cache dir customizable
-		EditorGUI::startUp(cm_new<EditorGUI>());
+		EditorGUI::startUp();
 
 		{
 			auto inputConfig = VirtualInput::instance().getConfiguration();
@@ -89,13 +89,13 @@ namespace BansheeEngine
 			inputConfig->registerButton("Delete", BC_DELETE);
 		}
 
-		ProjectLibrary::startUp(cm_new<ProjectLibrary>(getActiveProjectPath()));
+		ProjectLibrary::startUp(getActiveProjectPath());
 
-		UndoRedo::startUp(cm_new<UndoRedo>());
-		EditorWindowManager::startUp(cm_new<EditorWindowManager>());
+		UndoRedo::startUp();
+		EditorWindowManager::startUp();
 
 		MainEditorWindow* mainWindow = MainEditorWindow::create(gApplication().getPrimaryWindow());
-		EditorWidgetManager::startUp(cm_new<EditorWidgetManager>());
+		EditorWidgetManager::startUp();
 
 		gApplication().loadPlugin("SBansheeEditor"); // Managed part of the editor
 

+ 1 - 1
BansheeEditor/Source/BsMainEditorWindow.cpp

@@ -78,7 +78,7 @@ namespace BansheeEngine
 		AABox dbgBox(Vector3(-300, -200, 1000), Vector3(300, 300, 1500));
 		//DrawHelper3D::instance().drawAABox(sceneCamera, dbgBox, Color::Green, 250.0f);
 
-		ProfilerOverlay::startUp(cm_new<ProfilerOverlay>(sceneCamera->getViewport()));
+		ProfilerOverlay::startUp(sceneCamera->getViewport());
 		ProfilerOverlay::instance().show();
 	}
 

+ 1 - 1
BansheeEditorExec/BsEditorExec.cpp

@@ -12,7 +12,7 @@ int CALLBACK WinMain(
 	_In_  int nCmdShow
 	)
 {
-	EditorApplication::startUp(cm_new<EditorApplication>(RenderSystemPlugin::OpenGL));
+	EditorApplication::startUp(RenderSystemPlugin::OpenGL);
 	EditorApplication::instance().runMainLoop();
 	EditorApplication::shutDown();
 

+ 10 - 10
BansheeEngine/Source/BsApplication.cpp

@@ -38,23 +38,23 @@ namespace BansheeEngine
 		
 		gApplication().startUp(desc);
 
-		VirtualInput::startUp(cm_new<VirtualInput>());
-		ScriptManager::startUp(cm_new<ScriptManager>());
-		GUIManager::startUp(cm_new<GUIManager>());
-		GUIMaterialManager::startUp(cm_new<GUIMaterialManager>());
-		OverlayManager::startUp(cm_new<OverlayManager>());
+		VirtualInput::startUp();
+		ScriptManager::startUp();
+		GUIManager::startUp();
+		GUIMaterialManager::startUp();
+		OverlayManager::startUp();
 
-		BuiltinMaterialManager::startUp(cm_new<BuiltinMaterialManager>());
+		BuiltinMaterialManager::startUp();
 		BuiltinMaterialManager::instance().addFactory(cm_new<D3D9BuiltinMaterialFactory>());
 		BuiltinMaterialManager::instance().addFactory(cm_new<D3D11BuiltinMaterialFactory>());
 		BuiltinMaterialManager::instance().addFactory(cm_new<GLBuiltinMaterialFactory>());
 		BuiltinMaterialManager::instance().setActive(desc.renderSystem);
 
-		DrawHelper2D::startUp(cm_new<DrawHelper2D>());
-		DrawHelper3D::startUp(cm_new<DrawHelper3D>());
+		DrawHelper2D::startUp();
+		DrawHelper3D::startUp();
 
-		BuiltinResources::startUp(cm_new<BuiltinResources>());
-		Cursor::startUp(cm_new<Cursor>());
+		BuiltinResources::startUp();
+		Cursor::startUp();
 
 		gApplication().loadPlugin("BansheeMono", &mMonoPlugin);
 		gApplication().loadPlugin("SBansheeEngine", &mSBansheeEnginePlugin); // Scripting interface

+ 2 - 2
BansheeEngine/Source/BsGUIManager.cpp

@@ -87,10 +87,10 @@ namespace BansheeEngine
 		mInputCaret = cm_new<GUIInputCaret, PoolAlloc>();
 		mInputSelection = cm_new<GUIInputSelection, PoolAlloc>();
 
-		DragAndDropManager::startUp(cm_new<DragAndDropManager>());
+		DragAndDropManager::startUp();
 		mDragEndedConn = DragAndDropManager::instance().onDragEnded.connect(std::bind(&GUIManager::onMouseDragEnded, this, _1, _2));
 
-		GUIDropDownBoxManager::startUp(cm_new<GUIDropDownBoxManager>());
+		GUIDropDownBoxManager::startUp();
 
 		mVertexDesc = cm_shared_ptr<VertexDataDesc>();
 		mVertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);

+ 1 - 1
BansheeMono/Source/BsMonoScriptManager.cpp

@@ -17,7 +17,7 @@ namespace BansheeEngine
 
 	void MonoScriptSystem::initialize()
 	{
-		MonoManager::startUp(cm_new<MonoManager>());
+		MonoManager::startUp();
 		mIsInitialized = true;
 	}
 

+ 1 - 1
BansheeOctreeSM/Source/BsSceneManagerPlugin.cpp

@@ -11,7 +11,7 @@ namespace BansheeEngine
 
 	extern "C" BS_SM_EXPORT void* loadPlugin()
 	{
-		SceneManager::startUp(cm_new<OctreeSceneManager>());
+		SceneManager::startUp<OctreeSceneManager>();
 
 		return nullptr;
 	}

+ 20 - 20
CamelotCore/Source/CmApplication.cpp

@@ -59,37 +59,37 @@ namespace BansheeEngine
 		Platform::_startUp();
 		MemStack::beginThread();
 
-		UUIDGenerator::startUp(cm_new<UUIDGenerator>());
-		Profiler::startUp(cm_new<Profiler>());
-		ThreadPool::startUp(cm_new<TThreadPool<ThreadBansheePolicy>>(numWorkerThreads));
-		TaskScheduler::startUp(cm_new<TaskScheduler>());
+		UUIDGenerator::startUp();
+		Profiler::startUp();
+		ThreadPool::startUp<TThreadPool<ThreadBansheePolicy>>((numWorkerThreads));
+		TaskScheduler::startUp();
 		TaskScheduler::instance().removeWorker();
-		CoreThread::startUp(cm_new<CoreThread>());
-		StringTable::startUp(cm_new<StringTable>());
-		DeferredCallManager::startUp(cm_new<DeferredCallManager>());
-		Time::startUp(cm_new<Time>());
-		DynLibManager::startUp(cm_new<DynLibManager>());
-		CoreObjectManager::startUp(cm_new<CoreObjectManager>());
-		GameObjectManager::startUp(cm_new<GameObjectManager>());
-		Resources::startUp(cm_new<Resources>());
-		GpuProgramManager::startUp(cm_new<GpuProgramManager>());
-		RenderSystemManager::startUp(cm_new<RenderSystemManager>());
+		CoreThread::startUp();
+		StringTable::startUp();
+		DeferredCallManager::startUp();
+		Time::startUp();
+		DynLibManager::startUp();
+		CoreObjectManager::startUp();
+		GameObjectManager::startUp();
+		Resources::startUp();
+		GpuProgramManager::startUp();
+		RenderSystemManager::startUp();
 
 		mPrimaryWindow = RenderSystemManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
 
-		Input::startUp(cm_new<Input>());
-		RendererManager::startUp(cm_new<RendererManager>());
+		Input::startUp();
+		RendererManager::startUp();
 
 		loadPlugin(desc.renderer);
 		RendererManager::instance().setActive(desc.renderer);
 
 		loadPlugin(desc.sceneManager, &mSceneManagerPlugin);
 
-		MeshManager::startUp(cm_new<MeshManager>());
-		MaterialManager::startUp(cm_new<MaterialManager>());
-		FontManager::startUp(cm_new<FontManager>());
+		MeshManager::startUp();
+		MaterialManager::startUp();
+		FontManager::startUp();
 
-		Importer::startUp(cm_new<Importer>());
+		Importer::startUp();
 
 		for(auto& importerName : desc.importers)
 			loadPlugin(importerName);

+ 6 - 6
CamelotD3D11RenderSystem/Source/CmD3D11RenderSystem.cpp

@@ -102,19 +102,19 @@ namespace BansheeEngine
 		}
 
 		// Create the texture manager for use by others		
-		TextureManager::startUp(cm_new<D3D11TextureManager>());
+		TextureManager::startUp<D3D11TextureManager>();
 
 		// Also create hardware buffer manager		
-		HardwareBufferManager::startUp(cm_new<D3D11HardwareBufferManager>(std::ref(*mDevice)));
+		HardwareBufferManager::startUp<D3D11HardwareBufferManager>(std::ref(*mDevice));
 
 		// Create render window manager
-		RenderWindowManager::startUp(cm_new<D3D11RenderWindowManager>(this));
+		RenderWindowManager::startUp<D3D11RenderWindowManager>(this);
 
 		// Create & register HLSL factory		
 		mHLSLFactory = cm_new<D3D11HLSLProgramFactory>();
 
 		// Create render state manager
-		RenderStateManager::startUp(cm_new<D3D11RenderStateManager>());
+		RenderStateManager::startUp<D3D11RenderStateManager>();
 
 		mCurrentCapabilities = createRenderSystemCapabilities();
 
@@ -125,9 +125,9 @@ namespace BansheeEngine
 
 		RenderWindowPtr primaryWindow = RenderWindow::create(mPrimaryWindowDesc);
 
-		D3D11RenderUtility::startUp(cm_new<D3D11RenderUtility>(mDevice));
+		D3D11RenderUtility::startUp(mDevice);
 
-		QueryManager::startUp(cm_new<D3D11QueryManager>());
+		QueryManager::startUp<D3D11QueryManager>();
 
 		RenderSystem::initialize_internal(asyncOp);
 

+ 1 - 1
CamelotD3D11RenderSystem/Source/CmD3D11RenderSystemFactory.cpp

@@ -5,7 +5,7 @@ namespace BansheeEngine
 {
 	void D3D11RenderSystemFactory::create()
 	{
-		RenderSystem::startUp(cm_new<D3D11RenderSystem>());
+		RenderSystem::startUp<D3D11RenderSystem>();
 	}
 
 	D3D11RenderSystemFactory::InitOnStart D3D11RenderSystemFactory::initOnStart;

+ 5 - 5
CamelotD3D9Renderer/Source/CmD3D9RenderSystem.cpp

@@ -149,16 +149,16 @@ namespace BansheeEngine
 		mDeviceManager = cm_new<D3D9DeviceManager>();
 
 		// Also create hardware buffer manager		
-		HardwareBufferManager::startUp(cm_new<D3D9HardwareBufferManager>());
+		HardwareBufferManager::startUp<D3D9HardwareBufferManager>();
 
 		// Create & register HLSL factory		
 		mHLSLProgramFactory = cm_new<D3D9HLSLProgramFactory>();
 
 		// Create render window manager
-		RenderWindowManager::startUp(cm_new<D3D9RenderWindowManager>(this));
+		RenderWindowManager::startUp<D3D9RenderWindowManager>(this);
 
 		// Create render state manager
-		RenderStateManager::startUp(cm_new<RenderStateManager>());
+		RenderStateManager::startUp();
 
 		// Create primary window and finalize initialization
 		RenderWindowPtr primaryWindow = RenderWindow::create(mPrimaryWindowDesc);
@@ -166,9 +166,9 @@ namespace BansheeEngine
 		updateRenderSystemCapabilities(d3d9renderWindow);
 
 		// Create the texture manager for use by others		
-		TextureManager::startUp(cm_new<D3D9TextureManager>());
+		TextureManager::startUp<D3D9TextureManager>();
 
-		QueryManager::startUp(cm_new<D3D9QueryManager>());
+		QueryManager::startUp<D3D9QueryManager>();
 
 		// call superclass method
 		RenderSystem::initialize_internal(asyncOp);

+ 1 - 1
CamelotD3D9Renderer/Source/CmD3D9RenderSystemFactory.cpp

@@ -11,7 +11,7 @@ namespace BansheeEngine
 		HINSTANCE hInst = GetModuleHandle( "CamelotD3D9RenderSystem.dll" );
 	#endif
 
-		RenderSystem::startUp(cm_new<D3D9RenderSystem>(hInst));
+		RenderSystem::startUp<D3D9RenderSystem>(hInst);
 	}
 
 	D3D9RenderSystemFactory::InitOnStart D3D9RenderSystemFactory::initOnStart;

+ 2 - 2
CamelotGLRenderer/Source/BsGLVertexArrayObjectManager.cpp

@@ -176,9 +176,9 @@ namespace BansheeEngine
 		stackDeallocLast(usedBuffers);
 		stackDeallocLast(streamToSeqIdx);
 
-		mVAObjects.insert(wantedVAO);
+		auto iter = mVAObjects.insert(wantedVAO);
 
-		return wantedVAO;
+		return *iter.first;
 	}
 
 	void GLVertexArrayObjectManager::notifyBufferDestroyed(const GLVertexArrayObject& vao)

+ 7 - 7
CamelotGLRenderer/Source/CmGLRenderSystem.cpp

@@ -102,11 +102,11 @@ namespace BansheeEngine
 		THROW_IF_NOT_CORE_THREAD;
 
 		mGLSupport->start();
-		RenderWindowManager::startUp(cm_new<GLRenderWindowManager>(this));
+		RenderWindowManager::startUp<GLRenderWindowManager>(this);
 
-		RenderStateManager::startUp(cm_new<RenderStateManager>());
+		RenderStateManager::startUp();
 
-		QueryManager::startUp(cm_new<GLQueryManager>());
+		QueryManager::startUp<GLQueryManager>();
 
 		// Initialize a window so we have something to create a GL context with
 		RenderWindowPtr primaryWindow = RenderWindow::create(mPrimaryWindowDesc);
@@ -140,7 +140,7 @@ namespace BansheeEngine
 
 		mCurrentCapabilities = createRenderSystemCapabilities();
 		initFromCaps(mCurrentCapabilities);
-		GLVertexArrayObjectManager::startUp(cm_new<GLVertexArrayObjectManager>());
+		GLVertexArrayObjectManager::startUp();
 		
 		mGLInitialised = true;
 		
@@ -1624,7 +1624,7 @@ namespace BansheeEngine
 			glUnmapBufferARB = glUnmapBuffer;
 		}
 
-		HardwareBufferManager::startUp(cm_new<GLHardwareBufferManager>());
+		HardwareBufferManager::startUp<GLHardwareBufferManager>();
 		checkForErrors();
 
 		// GPU Program Manager setup
@@ -1666,7 +1666,7 @@ namespace BansheeEngine
 			if(caps->hasCapability(RSC_HWRENDER_TO_TEXTURE))
 			{
 				// Create FBO manager
-				GLRTTManager::startUp(cm_new<GLRTTManager>());
+				GLRTTManager::startUp<GLRTTManager>();
 				checkForErrors();
 			}
 		}
@@ -1714,7 +1714,7 @@ namespace BansheeEngine
 		if(totalNumUniformBlocks > numCombinedUniformBlocks)
 			CM_EXCEPT(InternalErrorException, "Number of combined uniform block buffers less than the number of individual per-stage buffers!?");
 
-		TextureManager::startUp(cm_new<GLTextureManager>(std::ref(*mGLSupport))); 
+		TextureManager::startUp<GLTextureManager>(std::ref(*mGLSupport));
 
 		// Check for FSAA
 		// Enable the extension if it was enabled by the GLSupport

+ 1 - 1
CamelotGLRenderer/Source/CmGLRenderSystemFactory.cpp

@@ -5,7 +5,7 @@ namespace BansheeEngine
 {
 	void GLRenderSystemFactory::create()
 	{
-		RenderSystem::startUp(cm_new<GLRenderSystem>());
+		RenderSystem::startUp<GLRenderSystem>();
 	}
 
 	GLRenderSystemFactory::InitOnStart GLRenderSystemFactory::initOnStart;

+ 1 - 1
CamelotUtility/Include/CmDynLibManager.h

@@ -12,7 +12,7 @@ namespace BansheeEngine
 	 *			
 	 * @note	Not thread safe.
      */
-    class CM_UTILITY_EXPORT DynLibManager: public Module<DynLibManager>
+    class CM_UTILITY_EXPORT DynLibManager : public Module<DynLibManager>
     {
     public:
         DynLibManager();

+ 54 - 34
CamelotUtility/Include/CmModule.h

@@ -8,9 +8,6 @@ namespace BansheeEngine
 	/**
 	 * @brief	Represents one engine module. Essentially it is a specialized type of singleton.
 	 * 			Module must be manually started up and shut down before and after use.
-	 * 			
-	 * TODO Low priority - Use variadic templates to automatically pass parameters so I may construct the object instance internally.
-	 *			Right now I expect the caller to allocate the object using general memory allocator.
 	 */
 	template <class T>
 	class Module
@@ -22,19 +19,19 @@ namespace BansheeEngine
 		 */
 		static T& instance()
 		{
-			if(isShutDown)
+			if(isShutDown())
 			{
 				CM_EXCEPT(InternalErrorException, 
 					"Trying to access a module but it hasn't been started up yet.");
 			}
 
-			if(isDestroyed)
+			if (isDestroyed())
 			{
 				CM_EXCEPT(InternalErrorException, 
 					"Trying to access a destroyed module.");
 			}
 
-			return *_instance;
+			return *_instance();
 		}
 
 		/**
@@ -43,34 +40,52 @@ namespace BansheeEngine
 		 */
 		static T* instancePtr()
 		{
-			if(isShutDown)
+			if (isShutDown())
 			{
 				CM_EXCEPT(InternalErrorException, 
 					"Trying to access a module but it hasn't been started up yet.");
 			}
 
-			if(isDestroyed)
+			if (isDestroyed())
 			{
 				CM_EXCEPT(InternalErrorException, 
 					"Trying to access a destroyed module.");
 			}
 
-			return _instance;
+			return _instance();
 		}
 		
 		/**
-		 * @brief	Starts up the module. You must provide an initialized instance of the module,
-		 * 			allocated using the general memory allocator.
+		 * @brief	Constructs and starts the module using the specified parameters.
 		 */
-		static void startUp(T* inst)
+		template<class ...Args>
+		static void startUp(Args &&...args)
 		{
-			if(!isShutDown)
+			if (!isShutDown())
 				CM_EXCEPT(InternalErrorException, "Trying to start an already started module.");
 
-			_instance = inst;
-			isShutDown = false;
+			_instance() = cm_new<T>(std::forward<Args>(args)...);
+			isShutDown() = false;
 
-			((Module*)_instance)->onStartUp();
+			((Module*)_instance())->onStartUp();
+		}
+
+		/**
+		 * @brief	Constructs and starts a specialized type of the module. Provided type
+		 *			must derive from type the Module is initialized with.
+		 */
+		template<class SubType, class ...Args>
+		static void startUp(Args &&...args)
+		{
+			static_assert(std::is_base_of<T, SubType>::value, "Provided type is not derived from type the Module is initialized with.");
+
+			if (!isShutDown())
+				CM_EXCEPT(InternalErrorException, "Trying to start an already started module.");
+
+			_instance() = cm_new<SubType>(std::forward<Args>(args)...);
+			isShutDown() = false;
+
+			((Module*)_instance())->onStartUp();
 		}
 
 		/**
@@ -78,16 +93,16 @@ namespace BansheeEngine
 		 */
 		static void shutDown()
 		{
-			if(isShutDown)
+			if (isShutDown())
 			{
 				CM_EXCEPT(InternalErrorException, 
 					"Trying to shut down an already shut down module.");
 			}
 
-			((Module*)_instance)->onShutDown();
+			((Module*)_instance())->onShutDown();
 
-			cm_delete(_instance);
-			isShutDown = true;
+			cm_delete(_instance());
+			isShutDown() = true;
 		}
 
 		/**
@@ -95,7 +110,7 @@ namespace BansheeEngine
 		 */
 		static bool isStarted()
 		{
-			return !isShutDown && !isDestroyed;
+			return !isShutDown() && !isDestroyed();
 		}
 
 	protected:
@@ -105,8 +120,8 @@ namespace BansheeEngine
 
 		virtual ~Module()
 		{ 
-			_instance = nullptr;
-			isDestroyed = true;
+			_instance() = nullptr;
+			isDestroyed() = true;
 		}
 
 		Module(const Module&) { }
@@ -128,17 +143,22 @@ namespace BansheeEngine
 		 */
 		virtual void onShutDown() {}
 
-		static T* _instance;
-		static bool isShutDown;
-		static bool isDestroyed;
-	};
-	
-	template <class T>
-	T* Module<T>::_instance = nullptr;
+		static T*& _instance()
+		{
+			static T* inst = nullptr;
+			return inst;
+		}
 
-	template <class T>
-	bool Module<T>::isShutDown = true;
+		static bool& isDestroyed()
+		{
+			static bool inst = false;
+			return inst;
+		}
 
-	template <class T>
-	bool Module<T>::isDestroyed = false;
+		static bool& isShutDown()
+		{
+			static bool inst = true;
+			return inst;
+		}
+	};
 }

+ 3 - 3
SBansheeEngine/Source/BsScriptEnginePlugin.cpp

@@ -48,9 +48,9 @@ namespace BansheeEngine
 		mono_add_internal_call("BansheeEngine.Program::dbgTestComponentClone", &dbgTestComponentClone);
 		mono_add_internal_call("BansheeEngine.Program::reportDbgValue", &reportDbgValue);
 
-		RuntimeScriptObjects::startUp(cm_new<RuntimeScriptObjects>());
-		ScriptResourceManager::startUp(cm_new<ScriptResourceManager>());
-		ScriptGameObjectManager::startUp(cm_new<ScriptGameObjectManager>());
+		RuntimeScriptObjects::startUp();
+		ScriptResourceManager::startUp();
+		ScriptGameObjectManager::startUp();
 
 		RuntimeScriptObjects::instance().refreshScriptObjects(BansheeEngineAssemblyName);