Bug 1874317 - Remove unused Scoped{RunnableMethod,Task}Factory classes. r=nika

Differential Revision: https://phabricator.services.mozilla.com/D199161
This commit is contained in:
Jed Davis 2024-01-30 02:13:47 +00:00
parent 2734eef616
commit b978a24fa3

View file

@ -52,135 +52,6 @@ void DispatchTupleToFunction(Function function, std::tuple<Args...>& arg) {
details::CallFunction(std::index_sequence_for<Args...>{}, function, arg);
}
// Scoped Factories ------------------------------------------------------------
//
// These scoped factory objects can be used by non-refcounted objects to safely
// place tasks in a message loop. Each factory guarantees that the tasks it
// produces will not run after the factory is destroyed. Commonly, factories
// are declared as class members, so the class' tasks will automatically cancel
// when the class instance is destroyed.
//
// Exampe Usage:
//
// class MyClass {
// private:
// // This factory will be used to schedule invocations of SomeMethod.
// ScopedRunnableMethodFactory<MyClass> some_method_factory_;
//
// public:
// // It is safe to suppress warning 4355 here.
// MyClass() : some_method_factory_(this) { }
//
// void SomeMethod() {
// // If this function might be called directly, you might want to revoke
// // any outstanding runnable methods scheduled to call it. If it's not
// // referenced other than by the factory, this is unnecessary.
// some_method_factory_.RevokeAll();
// ...
// }
//
// void ScheduleSomeMethod() {
// // If you'd like to only only have one pending task at a time, test for
// // |empty| before manufacturing another task.
// if (!some_method_factory_.empty())
// return;
//
// // The factories are not thread safe, so always invoke on
// // |MessageLoop::current()|.
// MessageLoop::current()->PostDelayedTask(
// some_method_factory_.NewRunnableMethod(&MyClass::SomeMethod),
// kSomeMethodDelayMS);
// }
// };
// A ScopedTaskFactory produces tasks of type |TaskType| and prevents them from
// running after it is destroyed.
template <class TaskType>
class ScopedTaskFactory : public RevocableStore {
public:
ScopedTaskFactory() {}
// Create a new task.
inline TaskType* NewTask() { return new TaskWrapper(this); }
class TaskWrapper : public TaskType {
public:
explicit TaskWrapper(RevocableStore* store) : revocable_(store) {}
NS_IMETHOD Run() override {
if (!revocable_.revoked()) TaskType::Run();
return NS_OK;
}
~TaskWrapper() { NS_ASSERT_OWNINGTHREAD(TaskWrapper); }
private:
Revocable revocable_;
NS_DECL_OWNINGTHREAD
DISALLOW_EVIL_CONSTRUCTORS(TaskWrapper);
};
private:
DISALLOW_EVIL_CONSTRUCTORS(ScopedTaskFactory);
};
// A ScopedRunnableMethodFactory creates runnable methods for a specified
// object. This is particularly useful for generating callbacks for
// non-reference counted objects when the factory is a member of the object.
template <class T>
class ScopedRunnableMethodFactory : public RevocableStore {
public:
explicit ScopedRunnableMethodFactory(T* object) : object_(object) {}
template <class Method, typename... Elements>
inline already_AddRefed<mozilla::Runnable> NewRunnableMethod(
Method method, Elements&&... elements) {
typedef std::tuple<std::decay_t<Elements>...> ArgsTuple;
typedef RunnableMethod<Method, ArgsTuple> Runnable;
typedef typename ScopedTaskFactory<Runnable>::TaskWrapper TaskWrapper;
RefPtr<TaskWrapper> task = new TaskWrapper(this);
task->Init(object_, method,
std::make_tuple(std::forward<Elements>(elements)...));
return task.forget();
}
protected:
template <class Method, class Params>
class RunnableMethod : public mozilla::Runnable {
public:
RunnableMethod()
: mozilla::Runnable("ScopedRunnableMethodFactory::RunnableMethod") {}
void Init(T* obj, Method meth, Params&& params) {
obj_ = obj;
meth_ = meth;
params_ = std::forward<Params>(params);
}
NS_IMETHOD Run() override {
DispatchTupleToMethod(obj_, meth_, params_);
return NS_OK;
}
private:
T* MOZ_UNSAFE_REF(
"The validity of this pointer must be enforced by "
"external factors.") obj_;
Method meth_;
Params params_;
DISALLOW_EVIL_CONSTRUCTORS(RunnableMethod);
};
private:
T* object_;
DISALLOW_EVIL_CONSTRUCTORS(ScopedRunnableMethodFactory);
};
// General task implementations ------------------------------------------------
// Task to delete an object