Update include/base headers for C++11/14 (see issue #3140)

See the issue for update guidelines.
This commit is contained in:
Marshall Greenblatt
2021-06-17 15:40:57 -04:00
parent 6d80ec69d7
commit 43f9baa23a
57 changed files with 5466 additions and 11523 deletions

View File

@ -27,7 +27,7 @@
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// CancelableCallback is a wrapper around base::Callback that allows
// cancellation of a callback. CancelableCallback takes a reference on the
// wrapped callback until this object is destroyed or Reset()/Cancel() are
@ -52,29 +52,26 @@
// to the message loop, the intensive test runs, the message loop is run,
// then the callback is cancelled.
//
// RunLoop run_loop;
//
// void TimeoutCallback(const std::string& timeout_message) {
// FAIL() << timeout_message;
// MessageLoop::current()->QuitWhenIdle();
// run_loop.QuitWhenIdle();
// }
//
// CancelableClosure timeout(base::Bind(&TimeoutCallback, "Test timed out."));
// MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(),
// 4000) // 4 seconds to run.
// CancelableOnceClosure timeout(
// base::BindOnce(&TimeoutCallback, "Test timed out."));
// ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, timeout.callback(),
// TimeDelta::FromSeconds(4));
// RunIntensiveTest();
// MessageLoop::current()->Run();
// run_loop.Run();
// timeout.Cancel(); // Hopefully this is hit before the timeout callback runs.
//
#ifndef CEF_INCLUDE_BASE_CEF_CANCELABLE_CALLBACK_H_
#define CEF_INCLUDE_BASE_CEF_CANCELABLE_CALLBACK_H_
#pragma once
#if defined(BASE_CANCELABLE_CALLBACK_H_)
// Do nothing if the Chromium header has already been included.
// This can happen in cases where Chromium code is used directly by the
// client application. When using Chromium code directly always include
// the Chromium header first to avoid type conflicts.
#elif defined(USING_CHROMIUM_INCLUDES)
#if defined(USING_CHROMIUM_INCLUDES)
// When building CEF include the Chromium header directly.
#include "base/cancelable_callback.h"
#else // !USING_CHROMIUM_INCLUDES
@ -82,209 +79,110 @@
// If the Chromium implementation diverges the below implementation should be
// updated to match.
#include <utility>
#include "include/base/cef_bind.h"
#include "include/base/cef_build.h"
#include "include/base/cef_callback.h"
#include "include/base/cef_logging.h"
#include "include/base/cef_macros.h"
#include "include/base/cef_weak_ptr.h"
#include "include/base/internal/cef_callback_internal.h"
#include "include/base/cef_logging.h"
#include "include/base/cef_compiler_specific.h"
#include "include/base/cef_weak_ptr.h"
namespace base {
namespace internal {
template <typename Sig>
class CancelableCallback;
template <>
class CancelableCallback<void(void)> {
template <typename CallbackType>
class CancelableCallbackImpl {
public:
CancelableCallback() : weak_factory_(this) {}
CancelableCallbackImpl() = default;
CancelableCallbackImpl(const CancelableCallbackImpl&) = delete;
CancelableCallbackImpl& operator=(const CancelableCallbackImpl&) = delete;
// |callback| must not be null.
explicit CancelableCallback(const base::Callback<void(void)>& callback)
: weak_factory_(this), callback_(callback) {
DCHECK(!callback.is_null());
InitializeForwarder();
explicit CancelableCallbackImpl(CallbackType callback)
: callback_(std::move(callback)) {
DCHECK(callback_);
}
~CancelableCallback() {}
~CancelableCallbackImpl() = default;
// Cancels and drops the reference to the wrapped callback.
void Cancel() {
weak_factory_.InvalidateWeakPtrs();
forwarder_.Reset();
weak_ptr_factory_.InvalidateWeakPtrs();
callback_.Reset();
}
// Returns true if the wrapped callback has been cancelled.
bool IsCancelled() const { return callback_.is_null(); }
bool IsCancelled() const {
return callback_.is_null();
}
// Sets |callback| as the closure that may be cancelled. |callback| may not
// be null. Outstanding and any previously wrapped callbacks are cancelled.
void Reset(const base::Callback<void(void)>& callback) {
DCHECK(!callback.is_null());
void Reset(CallbackType callback) {
DCHECK(callback);
// Outstanding tasks (e.g., posted to a message loop) must not be called.
Cancel();
// |forwarder_| is no longer valid after Cancel(), so re-bind.
InitializeForwarder();
callback_ = callback;
callback_ = std::move(callback);
}
// Returns a callback that can be disabled by calling Cancel().
const base::Callback<void(void)>& callback() const { return forwarder_; }
CallbackType callback() const {
if (!callback_)
return CallbackType();
CallbackType forwarder;
MakeForwarder(&forwarder);
return forwarder;
}
private:
void Forward() { callback_.Run(); }
// Helper method to bind |forwarder_| using a weak pointer from
// |weak_factory_|.
void InitializeForwarder() {
forwarder_ = base::Bind(&CancelableCallback<void(void)>::Forward,
weak_factory_.GetWeakPtr());
template <typename... Args>
void MakeForwarder(RepeatingCallback<void(Args...)>* out) const {
using ForwarderType = void (CancelableCallbackImpl::*)(Args...);
ForwarderType forwarder = &CancelableCallbackImpl::ForwardRepeating;
*out = BindRepeating(forwarder, weak_ptr_factory_.GetWeakPtr());
}
// Used to ensure Forward() is not run when this object is destroyed.
base::WeakPtrFactory<CancelableCallback<void(void)>> weak_factory_;
template <typename... Args>
void MakeForwarder(OnceCallback<void(Args...)>* out) const {
using ForwarderType = void (CancelableCallbackImpl::*)(Args...);
ForwarderType forwarder = &CancelableCallbackImpl::ForwardOnce;
*out = BindOnce(forwarder, weak_ptr_factory_.GetWeakPtr());
}
// The wrapper closure.
base::Callback<void(void)> forwarder_;
template <typename... Args>
void ForwardRepeating(Args... args) {
callback_.Run(std::forward<Args>(args)...);
}
template <typename... Args>
void ForwardOnce(Args... args) {
weak_ptr_factory_.InvalidateWeakPtrs();
std::move(callback_).Run(std::forward<Args>(args)...);
}
// The stored closure that may be cancelled.
base::Callback<void(void)> callback_;
DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
CallbackType callback_;
mutable base::WeakPtrFactory<CancelableCallbackImpl> weak_ptr_factory_{this};
};
template <typename A1>
class CancelableCallback<void(A1)> {
public:
CancelableCallback() : weak_factory_(this) {}
} // namespace internal
// |callback| must not be null.
explicit CancelableCallback(const base::Callback<void(A1)>& callback)
: weak_factory_(this), callback_(callback) {
DCHECK(!callback.is_null());
InitializeForwarder();
}
// Consider using base::WeakPtr directly instead of base::CancelableCallback for
// the task cancellation.
template <typename Signature>
using CancelableOnceCallback =
internal::CancelableCallbackImpl<OnceCallback<Signature>>;
using CancelableOnceClosure = CancelableOnceCallback<void()>;
~CancelableCallback() {}
template <typename Signature>
using CancelableRepeatingCallback =
internal::CancelableCallbackImpl<RepeatingCallback<Signature>>;
using CancelableRepeatingClosure = CancelableRepeatingCallback<void()>;
// Cancels and drops the reference to the wrapped callback.
void Cancel() {
weak_factory_.InvalidateWeakPtrs();
forwarder_.Reset();
callback_.Reset();
}
// Returns true if the wrapped callback has been cancelled.
bool IsCancelled() const { return callback_.is_null(); }
// Sets |callback| as the closure that may be cancelled. |callback| may not
// be null. Outstanding and any previously wrapped callbacks are cancelled.
void Reset(const base::Callback<void(A1)>& callback) {
DCHECK(!callback.is_null());
// Outstanding tasks (e.g., posted to a message loop) must not be called.
Cancel();
// |forwarder_| is no longer valid after Cancel(), so re-bind.
InitializeForwarder();
callback_ = callback;
}
// Returns a callback that can be disabled by calling Cancel().
const base::Callback<void(A1)>& callback() const { return forwarder_; }
private:
void Forward(A1 a1) const { callback_.Run(a1); }
// Helper method to bind |forwarder_| using a weak pointer from
// |weak_factory_|.
void InitializeForwarder() {
forwarder_ = base::Bind(&CancelableCallback<void(A1)>::Forward,
weak_factory_.GetWeakPtr());
}
// Used to ensure Forward() is not run when this object is destroyed.
base::WeakPtrFactory<CancelableCallback<void(A1)>> weak_factory_;
// The wrapper closure.
base::Callback<void(A1)> forwarder_;
// The stored closure that may be cancelled.
base::Callback<void(A1)> callback_;
DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
};
template <typename A1, typename A2>
class CancelableCallback<void(A1, A2)> {
public:
CancelableCallback() : weak_factory_(this) {}
// |callback| must not be null.
explicit CancelableCallback(const base::Callback<void(A1, A2)>& callback)
: weak_factory_(this), callback_(callback) {
DCHECK(!callback.is_null());
InitializeForwarder();
}
~CancelableCallback() {}
// Cancels and drops the reference to the wrapped callback.
void Cancel() {
weak_factory_.InvalidateWeakPtrs();
forwarder_.Reset();
callback_.Reset();
}
// Returns true if the wrapped callback has been cancelled.
bool IsCancelled() const { return callback_.is_null(); }
// Sets |callback| as the closure that may be cancelled. |callback| may not
// be null. Outstanding and any previously wrapped callbacks are cancelled.
void Reset(const base::Callback<void(A1, A2)>& callback) {
DCHECK(!callback.is_null());
// Outstanding tasks (e.g., posted to a message loop) must not be called.
Cancel();
// |forwarder_| is no longer valid after Cancel(), so re-bind.
InitializeForwarder();
callback_ = callback;
}
// Returns a callback that can be disabled by calling Cancel().
const base::Callback<void(A1, A2)>& callback() const { return forwarder_; }
private:
void Forward(A1 a1, A2 a2) const { callback_.Run(a1, a2); }
// Helper method to bind |forwarder_| using a weak pointer from
// |weak_factory_|.
void InitializeForwarder() {
forwarder_ = base::Bind(&CancelableCallback<void(A1, A2)>::Forward,
weak_factory_.GetWeakPtr());
}
// Used to ensure Forward() is not run when this object is destroyed.
base::WeakPtrFactory<CancelableCallback<void(A1, A2)>> weak_factory_;
// The wrapper closure.
base::Callback<void(A1, A2)> forwarder_;
// The stored closure that may be cancelled.
base::Callback<void(A1, A2)> callback_;
DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
};
typedef CancelableCallback<void(void)> CancelableClosure;
template <typename Signature>
using CancelableCallback = CancelableRepeatingCallback<Signature>;
using CancelableClosure = CancelableCallback<void()>;
} // namespace base