event: Remove FunctionAsyncTask

To create a task from a lambda, it is more efficient to use the new AsyncTaskManager::add() short-hand which creates an AsyncTask subclass in-place.
This commit is contained in:
rdb 2022-02-22 17:06:25 +01:00
parent 7baeaf3809
commit b4d51c24e9
9 changed files with 2 additions and 258 deletions

View File

@ -9,7 +9,6 @@ set(P3EVENT_HEADERS
config_event.h
buttonEvent.I buttonEvent.h
buttonEventList.I buttonEventList.h
functionAsyncTask.h functionAsyncTask.I
genericAsyncTask.h genericAsyncTask.I
pointerEvent.I pointerEvent.h
pointerEventList.I pointerEventList.h
@ -29,7 +28,6 @@ set(P3EVENT_SOURCES
asyncTaskSequence.cxx
buttonEvent.cxx
buttonEventList.cxx
functionAsyncTask.cxx
genericAsyncTask.cxx
pointerEvent.cxx
pointerEventList.cxx

View File

@ -30,7 +30,6 @@
#include "clockObject.h"
#include "ordered_vector.h"
#include "indirectCompareNames.h"
#include "functionAsyncTask.h"
/**
* A class to manage a loose queue of isolated tasks, which can be performed

View File

@ -22,7 +22,6 @@
#include "event.h"
#include "eventHandler.h"
#include "eventParameter.h"
#include "functionAsyncTask.h"
#include "genericAsyncTask.h"
#include "pointerEventList.h"
@ -50,7 +49,6 @@ ConfigureFn(config_event) {
EventHandler::init_type();
EventStoreInt::init_type("EventStoreInt");
EventStoreDouble::init_type("EventStoreDouble");
FunctionAsyncTask::init_type();
GenericAsyncTask::init_type();
ButtonEventList::register_with_read_factory();

View File

@ -1,83 +0,0 @@
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file functionAsyncTask.I
* @author rdb
* @date 2021-11-29
*/
/**
*
*/
INLINE FunctionAsyncTask::
FunctionAsyncTask(const std::string &name) :
AsyncTask(name)
{
}
/**
*
*/
INLINE FunctionAsyncTask::
FunctionAsyncTask(const std::string &name, FunctionAsyncTask::TaskFunction function) :
AsyncTask(name),
_function(std::move(function))
{
}
/**
* Replaces the function that is called when the task runs.
*/
INLINE void FunctionAsyncTask::
set_function(TaskFunction function) {
_function = std::move(function);
}
/**
* Returns the function that is called when the task runs.
*/
INLINE const FunctionAsyncTask::TaskFunction &FunctionAsyncTask::
get_function() const {
return _function;
}
/**
* Replaces the function that is called when the task begins. This is an
* optional function.
*/
INLINE void FunctionAsyncTask::
set_upon_birth(BirthFunction upon_birth) {
_upon_birth = std::move(upon_birth);
}
/**
* Returns the function that is called when the task begins, or NULL if the
* function is not defined.
*/
INLINE const FunctionAsyncTask::BirthFunction &FunctionAsyncTask::
get_upon_birth() const {
return _upon_birth;
}
/**
* Replaces the function that is called when the task ends. This is an
* optional function.
*/
INLINE void FunctionAsyncTask::
set_upon_death(FunctionAsyncTask::DeathFunction upon_death) {
_upon_death = upon_death;
}
/**
* Returns the function that is called when the task ends, or NULL if the
* function is not defined.
*/
INLINE const FunctionAsyncTask::DeathFunction &FunctionAsyncTask::
get_upon_death() const {
return _upon_death;
}

View File

@ -1,81 +0,0 @@
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file functionAsyncTask.cxx
* @author rdb
* @date 2021-11-29
*/
#include "functionAsyncTask.h"
#include "pnotify.h"
#ifndef CPPPARSER
TypeHandle FunctionAsyncTask::_type_handle;
/**
* Override this function to return true if the task can be successfully
* executed, false if it cannot. Mainly intended as a sanity check when
* attempting to add the task to a task manager.
*
* This function is called with the lock held.
*/
bool FunctionAsyncTask::
is_runnable() {
return !!_function;
}
/**
* Override this function to do something useful for the task.
*
* This function is called with the lock *not* held.
*/
AsyncTask::DoneStatus FunctionAsyncTask::
do_task() {
nassertr(_function, DS_interrupt);
return _function(this);
}
/**
* Override this function to do something useful when the task has been added
* to the active queue.
*
* This function is called with the lock *not* held.
*/
void FunctionAsyncTask::
upon_birth(AsyncTaskManager *manager) {
AsyncTask::upon_birth(manager);
if (_upon_birth) {
_upon_birth(this);
}
}
/**
* Override this function to do something useful when the task has been
* removed from the active queue. The parameter clean_exit is true if the
* task has been removed because it exited normally (returning DS_done), or
* false if it was removed for some other reason (e.g.
* AsyncTaskManager::remove()). By the time this method is called, _manager
* has been cleared, so the parameter manager indicates the original
* AsyncTaskManager that owned this task.
*
* The normal behavior is to throw the done_event only if clean_exit is true.
*
* This function is called with the lock *not* held.
*/
void FunctionAsyncTask::
upon_death(AsyncTaskManager *manager, bool clean_exit) {
AsyncTask::upon_death(manager, clean_exit);
if (_upon_death) {
_upon_death(this, clean_exit);
}
}
#endif // CPPPARSER

View File

@ -1,83 +0,0 @@
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file functionAsyncTask.h
* @author rdb
* @date 2021-11-29
*/
#ifndef FUNCTIONASYNCTASK_H
#define FUNCTIONASYNCTASK_H
#include "pandabase.h"
#include "asyncTask.h"
#ifndef CPPPARSER
#include <functional>
/**
* Associates a generic std::function (eg. a lambda) with an AsyncTask object.
* You can use this when you want to create an AsyncTask without having to
* subclass.
*
* @since 1.11.0
*/
class EXPCL_PANDA_EVENT FunctionAsyncTask final : public AsyncTask {
public:
typedef std::function<DoneStatus (FunctionAsyncTask *task)> TaskFunction;
typedef std::function<void (FunctionAsyncTask *task)> BirthFunction;
typedef std::function<void (FunctionAsyncTask *task, bool clean_exit)> DeathFunction;
INLINE FunctionAsyncTask(const std::string &name = std::string());
INLINE FunctionAsyncTask(const std::string &name, TaskFunction function);
ALLOC_DELETED_CHAIN(FunctionAsyncTask);
INLINE void set_function(TaskFunction function);
INLINE const TaskFunction &get_function() const;
INLINE void set_upon_birth(BirthFunction function);
INLINE const BirthFunction &get_upon_birth() const;
INLINE void set_upon_death(DeathFunction function);
INLINE const DeathFunction &get_upon_death() const;
protected:
virtual bool is_runnable() override;
virtual DoneStatus do_task() override;
virtual void upon_birth(AsyncTaskManager *manager) override;
virtual void upon_death(AsyncTaskManager *manager, bool clean_exit) override;
private:
TaskFunction _function;
BirthFunction _upon_birth;
DeathFunction _upon_death;
public:
static TypeHandle get_class_type() {
return _type_handle;
}
static void init_type() {
AsyncTask::init_type();
register_type(_type_handle, "FunctionAsyncTask",
AsyncTask::get_class_type());
}
virtual TypeHandle get_type() const override {
return get_class_type();
}
virtual TypeHandle force_init_type() override {init_type(); return get_class_type();}
private:
static TypeHandle _type_handle;
};
#include "functionAsyncTask.I"
#endif // CPPPARSER
#endif

View File

@ -22,8 +22,6 @@
* Associates a generic C-style function pointer with an AsyncTask object.
* You can use this when you want to create an AsyncTask without having to
* subclass.
*
* @deprecated See FunctionAsyncTask instead, which is more powerful.
*/
class EXPCL_PANDA_EVENT GenericAsyncTask : public AsyncTask {
public:

View File

@ -7,7 +7,6 @@
#include "asyncTaskSequence.cxx"
#include "buttonEvent.cxx"
#include "buttonEventList.cxx"
#include "functionAsyncTask.cxx"
#include "genericAsyncTask.cxx"
#include "pointerEvent.cxx"
#include "pointerEventList.cxx"

View File

@ -1065,7 +1065,7 @@ async_ensure_ram_image(bool allow_compression, int priority) {
double delay = async_load_delay;
// This texture has not yet been queued to be reloaded. Queue it up now.
task = new FunctionAsyncTask(task_name, [=](AsyncTask *task) {
task = task_mgr->add(task_name, [=](AsyncTask *task) {
if (delay != 0.0) {
Thread::sleep(delay);
}
@ -1076,9 +1076,8 @@ async_ensure_ram_image(bool allow_compression, int priority) {
}
return AsyncTask::DS_done;
});
task->set_task_chain("texture_reload");
task->set_priority(priority);
task_mgr->add(task);
task->set_task_chain("texture_reload");
cdataw->_reload_task = task;
return (AsyncFuture *)task;
}