Переглянути джерело

Asnyc ops are only created when actually needed (to save on allocations)

Marko Pintera 12 роки тому
батько
коміт
f63561bf5c

+ 13 - 5
CamelotCore/Include/CmCommandQueue.h

@@ -61,27 +61,35 @@ namespace CamelotFramework
 	{
 #if CM_DEBUG_MODE
 		QueuedCommand(boost::function<void(AsyncOp&)> _callback, UINT32 _debugId, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
-			:callbackWithReturnValue(_callback), debugId(_debugId), returnsValue(true), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId)
+			:callbackWithReturnValue(_callback), debugId(_debugId), returnsValue(true), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId),
+			asyncOp(cm_new<AsyncOp>())
 		{ }
 
 		QueuedCommand(boost::function<void()> _callback, UINT32 _debugId, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
-			:callback(_callback), debugId(_debugId), returnsValue(false), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId)
+			:callback(_callback), debugId(_debugId), returnsValue(false), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId), asyncOp(nullptr)
 		{ }
 
 		UINT32 debugId;
 #else
 		QueuedCommand(boost::function<void(AsyncOp&)> _callback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
-			:callbackWithReturnValue(_callback), returnsValue(true), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId)
+			:callbackWithReturnValue(_callback), returnsValue(true), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId),
+			asyncOp(cm_new<AsyncOp>())
 		{ }
 
 		QueuedCommand(boost::function<void()> _callback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
-			:callback(_callback), returnsValue(false), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId)
+			:callback(_callback), returnsValue(false), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId), asyncOp(nullptr)
 		{ }
 #endif
 
+		~QueuedCommand()
+		{
+			if(asyncOp != nullptr)
+				cm_delete(asyncOp);
+		}
+
 		boost::function<void()> callback;
 		boost::function<void(AsyncOp&)> callbackWithReturnValue;
-		AsyncOp asyncOp;
+		AsyncOp* asyncOp;
 		bool returnsValue;
 		UINT32 callbackId;
 		bool notifyWhenComplete;

+ 4 - 4
CamelotCore/Source/CmCommandQueue.cpp

@@ -55,7 +55,7 @@ namespace CamelotFramework
 		playback(commands);
 #endif
 
-		return newCommand.asyncOp;
+		return *newCommand.asyncOp;
 	}
 
 	void CommandQueueBase::queue(boost::function<void()> commandCallback, bool _notifyWhenComplete, UINT32 _callbackId)
@@ -106,13 +106,13 @@ namespace CamelotFramework
 
 			if(command.returnsValue)
 			{
-				command.callbackWithReturnValue(command.asyncOp);
+				command.callbackWithReturnValue(*command.asyncOp);
 
-				if(!command.asyncOp.hasCompleted())
+				if(!command.asyncOp->hasCompleted())
 				{
 					LOGDBG("Async operation return value wasn't resolved properly. Resolving automatically to nullptr. " \
 						"Make sure to complete the operation before returning from the command callback method.");
-					command.asyncOp.completeOperation(nullptr);
+					command.asyncOp->completeOperation(nullptr);
 				}
 			}
 			else