mirror of
https://github.com/panda3d/panda3d.git
synced 2025-09-28 07:48:37 -04:00
event: New C++ AsyncTaskManager::add() no longer uses std::function
std::function has unnecessary overhead, better to just create an AsyncTask subclass in-place storing the closure This obsoletes FunctionAsyncTask, it will be removed in a future commit
This commit is contained in:
parent
72c891c0df
commit
7baeaf3809
@ -34,14 +34,32 @@ get_clock() {
|
|||||||
|
|
||||||
#ifndef CPPPARSER
|
#ifndef CPPPARSER
|
||||||
/**
|
/**
|
||||||
* Adds a new task which calls the indicated function to the task manager.
|
* Adds a new task to the task manager which calls the indicated callable.
|
||||||
* Returns the newly created FunctionAsyncTask object.
|
* This method is defined as a more convenient alternative to subclassing
|
||||||
|
* AsyncTask.
|
||||||
|
*
|
||||||
|
* This given callable allowed to be any object defining a call operator that
|
||||||
|
* accepts an AsyncTask pointer and returns a DoneStatus.
|
||||||
|
*
|
||||||
|
* Returns the newly created AsyncTask object.
|
||||||
*
|
*
|
||||||
* @since 1.11.0
|
* @since 1.11.0
|
||||||
*/
|
*/
|
||||||
|
template<class Callable>
|
||||||
INLINE AsyncTask *AsyncTaskManager::
|
INLINE AsyncTask *AsyncTaskManager::
|
||||||
add(const std::string &name, FunctionAsyncTask::TaskFunction function) {
|
add(const std::string &name, Callable callable) {
|
||||||
AsyncTask *task = new FunctionAsyncTask(name, std::move(function));
|
class InlineTask final : public AsyncTask {
|
||||||
|
public:
|
||||||
|
InlineTask(Callable callable) : _callable(std::move(callable)) {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual DoneStatus do_task() override final {
|
||||||
|
return _callable(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
Callable _callable;
|
||||||
|
};
|
||||||
|
AsyncTask *task = new InlineTask(std::move(callable));
|
||||||
add(task);
|
add(task);
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,8 @@ PUBLISHED:
|
|||||||
|
|
||||||
void add(AsyncTask *task);
|
void add(AsyncTask *task);
|
||||||
#ifndef CPPPARSER
|
#ifndef CPPPARSER
|
||||||
INLINE AsyncTask *add(const std::string &name, FunctionAsyncTask::TaskFunction function);
|
template<class Callable>
|
||||||
|
INLINE AsyncTask *add(const std::string &name, Callable callable);
|
||||||
#endif
|
#endif
|
||||||
bool has_task(AsyncTask *task) const;
|
bool has_task(AsyncTask *task) const;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user