Repo created
This commit is contained in:
parent
81b91f4139
commit
f8c34fa5ee
22732 changed files with 4815320 additions and 2 deletions
334
TMessagesProj/jni/voip/webrtc/absl/functional/any_invocable.h
Normal file
334
TMessagesProj/jni/voip/webrtc/absl/functional/any_invocable.h
Normal file
|
|
@ -0,0 +1,334 @@
|
|||
// Copyright 2022 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// File: any_invocable.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// This header file defines an `absl::AnyInvocable` type that assumes ownership
|
||||
// and wraps an object of an invocable type. (Invocable types adhere to the
|
||||
// concept specified in https://en.cppreference.com/w/cpp/concepts/invocable.)
|
||||
//
|
||||
// In general, prefer `absl::AnyInvocable` when you need a type-erased
|
||||
// function parameter that needs to take ownership of the type.
|
||||
//
|
||||
// NOTE: `absl::AnyInvocable` is similar to the C++23 `std::move_only_function`
|
||||
// abstraction, but has a slightly different API and is not designed to be a
|
||||
// drop-in replacement or C++11-compatible backfill of that type.
|
||||
//
|
||||
// Credits to Matt Calabrese (https://github.com/mattcalabrese) for the original
|
||||
// implementation.
|
||||
|
||||
#ifndef ABSL_FUNCTIONAL_ANY_INVOCABLE_H_
|
||||
#define ABSL_FUNCTIONAL_ANY_INVOCABLE_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/functional/internal/any_invocable.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
#include "absl/utility/utility.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
// absl::AnyInvocable
|
||||
//
|
||||
// `absl::AnyInvocable` is a functional wrapper type, like `std::function`, that
|
||||
// assumes ownership of an invocable object. Unlike `std::function`, an
|
||||
// `absl::AnyInvocable` is more type-safe and provides the following additional
|
||||
// benefits:
|
||||
//
|
||||
// * Properly adheres to const correctness of the underlying type
|
||||
// * Is move-only so avoids concurrency problems with copied invocables and
|
||||
// unnecessary copies in general.
|
||||
// * Supports reference qualifiers allowing it to perform unique actions (noted
|
||||
// below).
|
||||
//
|
||||
// `absl::AnyInvocable` is a template, and an `absl::AnyInvocable` instantiation
|
||||
// may wrap any invocable object with a compatible function signature, e.g.
|
||||
// having arguments and return types convertible to types matching the
|
||||
// `absl::AnyInvocable` signature, and also matching any stated reference
|
||||
// qualifiers, as long as that type is moveable. It therefore provides broad
|
||||
// type erasure for functional objects.
|
||||
//
|
||||
// An `absl::AnyInvocable` is typically used as a type-erased function parameter
|
||||
// for accepting various functional objects:
|
||||
//
|
||||
// // Define a function taking an AnyInvocable parameter.
|
||||
// void my_func(absl::AnyInvocable<int()> f) {
|
||||
// ...
|
||||
// };
|
||||
//
|
||||
// // That function can accept any invocable type:
|
||||
//
|
||||
// // Accept a function reference. We don't need to move a reference.
|
||||
// int func1() { return 0; };
|
||||
// my_func(func1);
|
||||
//
|
||||
// // Accept a lambda. We use std::move here because otherwise my_func would
|
||||
// // copy the lambda.
|
||||
// auto lambda = []() { return 0; };
|
||||
// my_func(std::move(lambda));
|
||||
//
|
||||
// // Accept a function pointer. We don't need to move a function pointer.
|
||||
// func2 = &func1;
|
||||
// my_func(func2);
|
||||
//
|
||||
// // Accept an std::function by moving it. Note that the lambda is copyable
|
||||
// // (satisfying std::function requirements) and moveable (satisfying
|
||||
// // absl::AnyInvocable requirements).
|
||||
// std::function<int()> func6 = []() { return 0; };
|
||||
// my_func(std::move(func6));
|
||||
//
|
||||
// `AnyInvocable` also properly respects `const` qualifiers, reference
|
||||
// qualifiers, and the `noexcept` specification (only in C++ 17 and beyond) as
|
||||
// part of the user-specified function type (e.g.
|
||||
// `AnyInvocable<void() const && noexcept>`). These qualifiers will be applied
|
||||
// to the `AnyInvocable` object's `operator()`, and the underlying invocable
|
||||
// must be compatible with those qualifiers.
|
||||
//
|
||||
// Comparison of const and non-const function types:
|
||||
//
|
||||
// // Store a closure inside of `func` with the function type `int()`.
|
||||
// // Note that we have made `func` itself `const`.
|
||||
// const AnyInvocable<int()> func = [](){ return 0; };
|
||||
//
|
||||
// func(); // Compile-error: the passed type `int()` isn't `const`.
|
||||
//
|
||||
// // Store a closure inside of `const_func` with the function type
|
||||
// // `int() const`.
|
||||
// // Note that we have also made `const_func` itself `const`.
|
||||
// const AnyInvocable<int() const> const_func = [](){ return 0; };
|
||||
//
|
||||
// const_func(); // Fine: `int() const` is `const`.
|
||||
//
|
||||
// In the above example, the call `func()` would have compiled if
|
||||
// `std::function` were used even though the types are not const compatible.
|
||||
// This is a bug, and using `absl::AnyInvocable` properly detects that bug.
|
||||
//
|
||||
// In addition to affecting the signature of `operator()`, the `const` and
|
||||
// reference qualifiers of the function type also appropriately constrain which
|
||||
// kinds of invocable objects you are allowed to place into the `AnyInvocable`
|
||||
// instance. If you specify a function type that is const-qualified, then
|
||||
// anything that you attempt to put into the `AnyInvocable` must be callable on
|
||||
// a `const` instance of that type.
|
||||
//
|
||||
// Constraint example:
|
||||
//
|
||||
// // Fine because the lambda is callable when `const`.
|
||||
// AnyInvocable<int() const> func = [=](){ return 0; };
|
||||
//
|
||||
// // This is a compile-error because the lambda isn't callable when `const`.
|
||||
// AnyInvocable<int() const> error = [=]() mutable { return 0; };
|
||||
//
|
||||
// An `&&` qualifier can be used to express that an `absl::AnyInvocable`
|
||||
// instance should be invoked at most once:
|
||||
//
|
||||
// // Invokes `continuation` with the logical result of an operation when
|
||||
// // that operation completes (common in asynchronous code).
|
||||
// void CallOnCompletion(AnyInvocable<void(int)&&> continuation) {
|
||||
// int result_of_foo = foo();
|
||||
//
|
||||
// // `std::move` is required because the `operator()` of `continuation` is
|
||||
// // rvalue-reference qualified.
|
||||
// std::move(continuation)(result_of_foo);
|
||||
// }
|
||||
//
|
||||
// Attempting to call `absl::AnyInvocable` multiple times in such a case
|
||||
// results in undefined behavior.
|
||||
//
|
||||
// Invoking an empty `absl::AnyInvocable` results in undefined behavior:
|
||||
//
|
||||
// // Create an empty instance using the default constructor.
|
||||
// AnyInvocable<void()> empty;
|
||||
// empty(); // WARNING: Undefined behavior!
|
||||
template <class Sig>
|
||||
class AnyInvocable : private internal_any_invocable::Impl<Sig> {
|
||||
private:
|
||||
static_assert(
|
||||
std::is_function<Sig>::value,
|
||||
"The template argument of AnyInvocable must be a function type.");
|
||||
|
||||
using Impl = internal_any_invocable::Impl<Sig>;
|
||||
|
||||
public:
|
||||
// The return type of Sig
|
||||
using result_type = typename Impl::result_type;
|
||||
|
||||
// Constructors
|
||||
|
||||
// Constructs the `AnyInvocable` in an empty state.
|
||||
// Invoking it results in undefined behavior.
|
||||
AnyInvocable() noexcept = default;
|
||||
AnyInvocable(std::nullptr_t) noexcept {} // NOLINT
|
||||
|
||||
// Constructs the `AnyInvocable` from an existing `AnyInvocable` by a move.
|
||||
// Note that `f` is not guaranteed to be empty after move-construction,
|
||||
// although it may be.
|
||||
AnyInvocable(AnyInvocable&& /*f*/) noexcept = default;
|
||||
|
||||
// Constructs an `AnyInvocable` from an invocable object.
|
||||
//
|
||||
// Upon construction, `*this` is only empty if `f` is a function pointer or
|
||||
// member pointer type and is null, or if `f` is an `AnyInvocable` that is
|
||||
// empty.
|
||||
template <class F, typename = absl::enable_if_t<
|
||||
internal_any_invocable::CanConvert<Sig, F>::value>>
|
||||
AnyInvocable(F&& f) // NOLINT
|
||||
: Impl(internal_any_invocable::ConversionConstruct(),
|
||||
std::forward<F>(f)) {}
|
||||
|
||||
// Constructs an `AnyInvocable` that holds an invocable object of type `T`,
|
||||
// which is constructed in-place from the given arguments.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// AnyInvocable<int(int)> func(
|
||||
// absl::in_place_type<PossiblyImmovableType>, arg1, arg2);
|
||||
//
|
||||
template <class T, class... Args,
|
||||
typename = absl::enable_if_t<
|
||||
internal_any_invocable::CanEmplace<Sig, T, Args...>::value>>
|
||||
explicit AnyInvocable(absl::in_place_type_t<T>, Args&&... args)
|
||||
: Impl(absl::in_place_type<absl::decay_t<T>>,
|
||||
std::forward<Args>(args)...) {
|
||||
static_assert(std::is_same<T, absl::decay_t<T>>::value,
|
||||
"The explicit template argument of in_place_type is required "
|
||||
"to be an unqualified object type.");
|
||||
}
|
||||
|
||||
// Overload of the above constructor to support list-initialization.
|
||||
template <class T, class U, class... Args,
|
||||
typename = absl::enable_if_t<internal_any_invocable::CanEmplace<
|
||||
Sig, T, std::initializer_list<U>&, Args...>::value>>
|
||||
explicit AnyInvocable(absl::in_place_type_t<T>,
|
||||
std::initializer_list<U> ilist, Args&&... args)
|
||||
: Impl(absl::in_place_type<absl::decay_t<T>>, ilist,
|
||||
std::forward<Args>(args)...) {
|
||||
static_assert(std::is_same<T, absl::decay_t<T>>::value,
|
||||
"The explicit template argument of in_place_type is required "
|
||||
"to be an unqualified object type.");
|
||||
}
|
||||
|
||||
// Assignment Operators
|
||||
|
||||
// Assigns an `AnyInvocable` through move-assignment.
|
||||
// Note that `f` is not guaranteed to be empty after move-assignment
|
||||
// although it may be.
|
||||
AnyInvocable& operator=(AnyInvocable&& /*f*/) noexcept = default;
|
||||
|
||||
// Assigns an `AnyInvocable` from a nullptr, clearing the `AnyInvocable`. If
|
||||
// not empty, destroys the target, putting `*this` into an empty state.
|
||||
AnyInvocable& operator=(std::nullptr_t) noexcept {
|
||||
this->Clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Assigns an `AnyInvocable` from an existing `AnyInvocable` instance.
|
||||
//
|
||||
// Upon assignment, `*this` is only empty if `f` is a function pointer or
|
||||
// member pointer type and is null, or if `f` is an `AnyInvocable` that is
|
||||
// empty.
|
||||
template <class F, typename = absl::enable_if_t<
|
||||
internal_any_invocable::CanAssign<Sig, F>::value>>
|
||||
AnyInvocable& operator=(F&& f) {
|
||||
*this = AnyInvocable(std::forward<F>(f));
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Assigns an `AnyInvocable` from a reference to an invocable object.
|
||||
// Upon assignment, stores a reference to the invocable object in the
|
||||
// `AnyInvocable` instance.
|
||||
template <
|
||||
class F,
|
||||
typename = absl::enable_if_t<
|
||||
internal_any_invocable::CanAssignReferenceWrapper<Sig, F>::value>>
|
||||
AnyInvocable& operator=(std::reference_wrapper<F> f) noexcept {
|
||||
*this = AnyInvocable(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
|
||||
// If not empty, destroys the target.
|
||||
~AnyInvocable() = default;
|
||||
|
||||
// absl::AnyInvocable::swap()
|
||||
//
|
||||
// Exchanges the targets of `*this` and `other`.
|
||||
void swap(AnyInvocable& other) noexcept { std::swap(*this, other); }
|
||||
|
||||
// absl::AnyInvocable::operator bool()
|
||||
//
|
||||
// Returns `true` if `*this` is not empty.
|
||||
//
|
||||
// WARNING: An `AnyInvocable` that wraps an empty `std::function` is not
|
||||
// itself empty. This behavior is consistent with the standard equivalent
|
||||
// `std::move_only_function`.
|
||||
//
|
||||
// In other words:
|
||||
// std::function<void()> f; // empty
|
||||
// absl::AnyInvocable<void()> a = std::move(f); // not empty
|
||||
//
|
||||
// Invoking an empty `AnyInvocable` results in undefined behavior.
|
||||
explicit operator bool() const noexcept { return this->HasValue(); }
|
||||
|
||||
// Invokes the target object of `*this`. `*this` must not be empty.
|
||||
//
|
||||
// Note: The signature of this function call operator is the same as the
|
||||
// template parameter `Sig`.
|
||||
using Impl::operator();
|
||||
|
||||
// Equality operators
|
||||
|
||||
// Returns `true` if `*this` is empty.
|
||||
friend bool operator==(const AnyInvocable& f, std::nullptr_t) noexcept {
|
||||
return !f.HasValue();
|
||||
}
|
||||
|
||||
// Returns `true` if `*this` is empty.
|
||||
friend bool operator==(std::nullptr_t, const AnyInvocable& f) noexcept {
|
||||
return !f.HasValue();
|
||||
}
|
||||
|
||||
// Returns `false` if `*this` is empty.
|
||||
friend bool operator!=(const AnyInvocable& f, std::nullptr_t) noexcept {
|
||||
return f.HasValue();
|
||||
}
|
||||
|
||||
// Returns `false` if `*this` is empty.
|
||||
friend bool operator!=(std::nullptr_t, const AnyInvocable& f) noexcept {
|
||||
return f.HasValue();
|
||||
}
|
||||
|
||||
// swap()
|
||||
//
|
||||
// Exchanges the targets of `f1` and `f2`.
|
||||
friend void swap(AnyInvocable& f1, AnyInvocable& f2) noexcept { f1.swap(f2); }
|
||||
|
||||
private:
|
||||
// Friending other instantiations is necessary for conversions.
|
||||
template <bool /*SigIsNoexcept*/, class /*ReturnType*/, class... /*P*/>
|
||||
friend class internal_any_invocable::CoreImpl;
|
||||
};
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_FUNCTIONAL_ANY_INVOCABLE_H_
|
||||
1719
TMessagesProj/jni/voip/webrtc/absl/functional/any_invocable_test.cc
Normal file
1719
TMessagesProj/jni/voip/webrtc/absl/functional/any_invocable_test.cc
Normal file
File diff suppressed because it is too large
Load diff
194
TMessagesProj/jni/voip/webrtc/absl/functional/bind_front.h
Normal file
194
TMessagesProj/jni/voip/webrtc/absl/functional/bind_front.h
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
// Copyright 2018 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// File: bind_front.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// `absl::bind_front()` returns a functor by binding a number of arguments to
|
||||
// the front of a provided (usually more generic) functor. Unlike `std::bind`,
|
||||
// it does not require the use of argument placeholders. The simpler syntax of
|
||||
// `absl::bind_front()` allows you to avoid known misuses with `std::bind()`.
|
||||
//
|
||||
// `absl::bind_front()` is meant as a drop-in replacement for C++20's upcoming
|
||||
// `std::bind_front()`, which similarly resolves these issues with
|
||||
// `std::bind()`. Both `bind_front()` alternatives, unlike `std::bind()`, allow
|
||||
// partial function application. (See
|
||||
// https://en.wikipedia.org/wiki/Partial_application).
|
||||
|
||||
#ifndef ABSL_FUNCTIONAL_BIND_FRONT_H_
|
||||
#define ABSL_FUNCTIONAL_BIND_FRONT_H_
|
||||
|
||||
#if defined(__cpp_lib_bind_front) && __cpp_lib_bind_front >= 201907L
|
||||
#include <functional> // For std::bind_front.
|
||||
#endif // defined(__cpp_lib_bind_front) && __cpp_lib_bind_front >= 201907L
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "absl/functional/internal/front_binder.h"
|
||||
#include "absl/utility/utility.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
// bind_front()
|
||||
//
|
||||
// Binds the first N arguments of an invocable object and stores them by value.
|
||||
//
|
||||
// Like `std::bind()`, `absl::bind_front()` is implicitly convertible to
|
||||
// `std::function`. In particular, it may be used as a simpler replacement for
|
||||
// `std::bind()` in most cases, as it does not require placeholders to be
|
||||
// specified. More importantly, it provides more reliable correctness guarantees
|
||||
// than `std::bind()`; while `std::bind()` will silently ignore passing more
|
||||
// parameters than expected, for example, `absl::bind_front()` will report such
|
||||
// mis-uses as errors. In C++20, `absl::bind_front` is replaced by
|
||||
// `std::bind_front`.
|
||||
//
|
||||
// absl::bind_front(a...) can be seen as storing the results of
|
||||
// std::make_tuple(a...).
|
||||
//
|
||||
// Example: Binding a free function.
|
||||
//
|
||||
// int Minus(int a, int b) { return a - b; }
|
||||
//
|
||||
// assert(absl::bind_front(Minus)(3, 2) == 3 - 2);
|
||||
// assert(absl::bind_front(Minus, 3)(2) == 3 - 2);
|
||||
// assert(absl::bind_front(Minus, 3, 2)() == 3 - 2);
|
||||
//
|
||||
// Example: Binding a member function.
|
||||
//
|
||||
// struct Math {
|
||||
// int Double(int a) const { return 2 * a; }
|
||||
// };
|
||||
//
|
||||
// Math math;
|
||||
//
|
||||
// assert(absl::bind_front(&Math::Double)(&math, 3) == 2 * 3);
|
||||
// // Stores a pointer to math inside the functor.
|
||||
// assert(absl::bind_front(&Math::Double, &math)(3) == 2 * 3);
|
||||
// // Stores a copy of math inside the functor.
|
||||
// assert(absl::bind_front(&Math::Double, math)(3) == 2 * 3);
|
||||
// // Stores std::unique_ptr<Math> inside the functor.
|
||||
// assert(absl::bind_front(&Math::Double,
|
||||
// std::unique_ptr<Math>(new Math))(3) == 2 * 3);
|
||||
//
|
||||
// Example: Using `absl::bind_front()`, instead of `std::bind()`, with
|
||||
// `std::function`.
|
||||
//
|
||||
// class FileReader {
|
||||
// public:
|
||||
// void ReadFileAsync(const std::string& filename, std::string* content,
|
||||
// const std::function<void()>& done) {
|
||||
// // Calls Executor::Schedule(std::function<void()>).
|
||||
// Executor::DefaultExecutor()->Schedule(
|
||||
// absl::bind_front(&FileReader::BlockingRead, this,
|
||||
// filename, content, done));
|
||||
// }
|
||||
//
|
||||
// private:
|
||||
// void BlockingRead(const std::string& filename, std::string* content,
|
||||
// const std::function<void()>& done) {
|
||||
// CHECK_OK(file::GetContents(filename, content, {}));
|
||||
// done();
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// `absl::bind_front()` stores bound arguments explicitly using the type passed
|
||||
// rather than implicitly based on the type accepted by its functor.
|
||||
//
|
||||
// Example: Binding arguments explicitly.
|
||||
//
|
||||
// void LogStringView(absl::string_view sv) {
|
||||
// LOG(INFO) << sv;
|
||||
// }
|
||||
//
|
||||
// Executor* e = Executor::DefaultExecutor();
|
||||
// std::string s = "hello";
|
||||
// absl::string_view sv = s;
|
||||
//
|
||||
// // absl::bind_front(LogStringView, arg) makes a copy of arg and stores it.
|
||||
// e->Schedule(absl::bind_front(LogStringView, sv)); // ERROR: dangling
|
||||
// // string_view.
|
||||
//
|
||||
// e->Schedule(absl::bind_front(LogStringView, s)); // OK: stores a copy of
|
||||
// // s.
|
||||
//
|
||||
// To store some of the arguments passed to `absl::bind_front()` by reference,
|
||||
// use std::ref()` and `std::cref()`.
|
||||
//
|
||||
// Example: Storing some of the bound arguments by reference.
|
||||
//
|
||||
// class Service {
|
||||
// public:
|
||||
// void Serve(const Request& req, std::function<void()>* done) {
|
||||
// // The request protocol buffer won't be deleted until done is called.
|
||||
// // It's safe to store a reference to it inside the functor.
|
||||
// Executor::DefaultExecutor()->Schedule(
|
||||
// absl::bind_front(&Service::BlockingServe, this, std::cref(req),
|
||||
// done));
|
||||
// }
|
||||
//
|
||||
// private:
|
||||
// void BlockingServe(const Request& req, std::function<void()>* done);
|
||||
// };
|
||||
//
|
||||
// Example: Storing bound arguments by reference.
|
||||
//
|
||||
// void Print(const std::string& a, const std::string& b) {
|
||||
// std::cerr << a << b;
|
||||
// }
|
||||
//
|
||||
// std::string hi = "Hello, ";
|
||||
// std::vector<std::string> names = {"Chuk", "Gek"};
|
||||
// // Doesn't copy hi.
|
||||
// for_each(names.begin(), names.end(),
|
||||
// absl::bind_front(Print, std::ref(hi)));
|
||||
//
|
||||
// // DO NOT DO THIS: the functor may outlive "hi", resulting in
|
||||
// // dangling references.
|
||||
// foo->DoInFuture(absl::bind_front(Print, std::ref(hi), "Guest")); // BAD!
|
||||
// auto f = absl::bind_front(Print, std::ref(hi), "Guest"); // BAD!
|
||||
//
|
||||
// Example: Storing reference-like types.
|
||||
//
|
||||
// void Print(absl::string_view a, const std::string& b) {
|
||||
// std::cerr << a << b;
|
||||
// }
|
||||
//
|
||||
// std::string hi = "Hello, ";
|
||||
// // Copies "hi".
|
||||
// absl::bind_front(Print, hi)("Chuk");
|
||||
//
|
||||
// // Compile error: std::reference_wrapper<const string> is not implicitly
|
||||
// // convertible to string_view.
|
||||
// // absl::bind_front(Print, std::cref(hi))("Chuk");
|
||||
//
|
||||
// // Doesn't copy "hi".
|
||||
// absl::bind_front(Print, absl::string_view(hi))("Chuk");
|
||||
//
|
||||
#if defined(__cpp_lib_bind_front) && __cpp_lib_bind_front >= 201907L
|
||||
using std::bind_front;
|
||||
#else // defined(__cpp_lib_bind_front) && __cpp_lib_bind_front >= 201907L
|
||||
template <class F, class... BoundArgs>
|
||||
constexpr functional_internal::bind_front_t<F, BoundArgs...> bind_front(
|
||||
F&& func, BoundArgs&&... args) {
|
||||
return functional_internal::bind_front_t<F, BoundArgs...>(
|
||||
absl::in_place, std::forward<F>(func), std::forward<BoundArgs>(args)...);
|
||||
}
|
||||
#endif // defined(__cpp_lib_bind_front) && __cpp_lib_bind_front >= 201907L
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_FUNCTIONAL_BIND_FRONT_H_
|
||||
227
TMessagesProj/jni/voip/webrtc/absl/functional/bind_front_test.cc
Normal file
227
TMessagesProj/jni/voip/webrtc/absl/functional/bind_front_test.cc
Normal file
|
|
@ -0,0 +1,227 @@
|
|||
// Copyright 2018 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/functional/bind_front.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/memory/memory.h"
|
||||
|
||||
namespace {
|
||||
|
||||
char CharAt(const char* s, size_t index) { return s[index]; }
|
||||
|
||||
TEST(BindTest, Basics) {
|
||||
EXPECT_EQ('C', absl::bind_front(CharAt)("ABC", 2));
|
||||
EXPECT_EQ('C', absl::bind_front(CharAt, "ABC")(2));
|
||||
EXPECT_EQ('C', absl::bind_front(CharAt, "ABC", 2)());
|
||||
}
|
||||
|
||||
TEST(BindTest, Lambda) {
|
||||
auto lambda = [](int x, int y, int z) { return x + y + z; };
|
||||
EXPECT_EQ(6, absl::bind_front(lambda)(1, 2, 3));
|
||||
EXPECT_EQ(6, absl::bind_front(lambda, 1)(2, 3));
|
||||
EXPECT_EQ(6, absl::bind_front(lambda, 1, 2)(3));
|
||||
EXPECT_EQ(6, absl::bind_front(lambda, 1, 2, 3)());
|
||||
}
|
||||
|
||||
struct Functor {
|
||||
std::string operator()() & { return "&"; }
|
||||
std::string operator()() const& { return "const&"; }
|
||||
std::string operator()() && { return "&&"; }
|
||||
std::string operator()() const&& { return "const&&"; }
|
||||
};
|
||||
|
||||
TEST(BindTest, PerfectForwardingOfBoundArgs) {
|
||||
auto f = absl::bind_front(Functor());
|
||||
const auto& cf = f;
|
||||
EXPECT_EQ("&", f());
|
||||
EXPECT_EQ("const&", cf());
|
||||
EXPECT_EQ("&&", std::move(f)());
|
||||
EXPECT_EQ("const&&", std::move(cf)());
|
||||
}
|
||||
|
||||
struct ArgDescribe {
|
||||
std::string operator()(int&) const { return "&"; } // NOLINT
|
||||
std::string operator()(const int&) const { return "const&"; } // NOLINT
|
||||
std::string operator()(int&&) const { return "&&"; }
|
||||
std::string operator()(const int&&) const { return "const&&"; }
|
||||
};
|
||||
|
||||
TEST(BindTest, PerfectForwardingOfFreeArgs) {
|
||||
ArgDescribe f;
|
||||
int i;
|
||||
EXPECT_EQ("&", absl::bind_front(f)(static_cast<int&>(i)));
|
||||
EXPECT_EQ("const&", absl::bind_front(f)(static_cast<const int&>(i)));
|
||||
EXPECT_EQ("&&", absl::bind_front(f)(static_cast<int&&>(i)));
|
||||
EXPECT_EQ("const&&", absl::bind_front(f)(static_cast<const int&&>(i)));
|
||||
}
|
||||
|
||||
struct NonCopyableFunctor {
|
||||
NonCopyableFunctor() = default;
|
||||
NonCopyableFunctor(const NonCopyableFunctor&) = delete;
|
||||
NonCopyableFunctor& operator=(const NonCopyableFunctor&) = delete;
|
||||
const NonCopyableFunctor* operator()() const { return this; }
|
||||
};
|
||||
|
||||
TEST(BindTest, RefToFunctor) {
|
||||
// It won't copy/move the functor and use the original object.
|
||||
NonCopyableFunctor ncf;
|
||||
auto bound_ncf = absl::bind_front(std::ref(ncf));
|
||||
auto bound_ncf_copy = bound_ncf;
|
||||
EXPECT_EQ(&ncf, bound_ncf_copy());
|
||||
}
|
||||
|
||||
struct Struct {
|
||||
std::string value;
|
||||
};
|
||||
|
||||
TEST(BindTest, StoreByCopy) {
|
||||
Struct s = {"hello"};
|
||||
auto f = absl::bind_front(&Struct::value, s);
|
||||
auto g = f;
|
||||
EXPECT_EQ("hello", f());
|
||||
EXPECT_EQ("hello", g());
|
||||
EXPECT_NE(&s.value, &f());
|
||||
EXPECT_NE(&s.value, &g());
|
||||
EXPECT_NE(&g(), &f());
|
||||
}
|
||||
|
||||
struct NonCopyable {
|
||||
explicit NonCopyable(const std::string& s) : value(s) {}
|
||||
NonCopyable(const NonCopyable&) = delete;
|
||||
NonCopyable& operator=(const NonCopyable&) = delete;
|
||||
|
||||
std::string value;
|
||||
};
|
||||
|
||||
const std::string& GetNonCopyableValue(const NonCopyable& n) { return n.value; }
|
||||
|
||||
TEST(BindTest, StoreByRef) {
|
||||
NonCopyable s("hello");
|
||||
auto f = absl::bind_front(&GetNonCopyableValue, std::ref(s));
|
||||
EXPECT_EQ("hello", f());
|
||||
EXPECT_EQ(&s.value, &f());
|
||||
auto g = std::move(f); // NOLINT
|
||||
EXPECT_EQ("hello", g());
|
||||
EXPECT_EQ(&s.value, &g());
|
||||
s.value = "goodbye";
|
||||
EXPECT_EQ("goodbye", g());
|
||||
}
|
||||
|
||||
TEST(BindTest, StoreByCRef) {
|
||||
NonCopyable s("hello");
|
||||
auto f = absl::bind_front(&GetNonCopyableValue, std::cref(s));
|
||||
EXPECT_EQ("hello", f());
|
||||
EXPECT_EQ(&s.value, &f());
|
||||
auto g = std::move(f); // NOLINT
|
||||
EXPECT_EQ("hello", g());
|
||||
EXPECT_EQ(&s.value, &g());
|
||||
s.value = "goodbye";
|
||||
EXPECT_EQ("goodbye", g());
|
||||
}
|
||||
|
||||
const std::string& GetNonCopyableValueByWrapper(
|
||||
std::reference_wrapper<NonCopyable> n) {
|
||||
return n.get().value;
|
||||
}
|
||||
|
||||
TEST(BindTest, StoreByRefInvokeByWrapper) {
|
||||
NonCopyable s("hello");
|
||||
auto f = absl::bind_front(GetNonCopyableValueByWrapper, std::ref(s));
|
||||
EXPECT_EQ("hello", f());
|
||||
EXPECT_EQ(&s.value, &f());
|
||||
auto g = std::move(f);
|
||||
EXPECT_EQ("hello", g());
|
||||
EXPECT_EQ(&s.value, &g());
|
||||
s.value = "goodbye";
|
||||
EXPECT_EQ("goodbye", g());
|
||||
}
|
||||
|
||||
TEST(BindTest, StoreByPointer) {
|
||||
NonCopyable s("hello");
|
||||
auto f = absl::bind_front(&NonCopyable::value, &s);
|
||||
EXPECT_EQ("hello", f());
|
||||
EXPECT_EQ(&s.value, &f());
|
||||
auto g = std::move(f);
|
||||
EXPECT_EQ("hello", g());
|
||||
EXPECT_EQ(&s.value, &g());
|
||||
}
|
||||
|
||||
int Sink(std::unique_ptr<int> p) {
|
||||
return *p;
|
||||
}
|
||||
|
||||
std::unique_ptr<int> Factory(int n) { return absl::make_unique<int>(n); }
|
||||
|
||||
TEST(BindTest, NonCopyableArg) {
|
||||
EXPECT_EQ(42, absl::bind_front(Sink)(absl::make_unique<int>(42)));
|
||||
EXPECT_EQ(42, absl::bind_front(Sink, absl::make_unique<int>(42))());
|
||||
}
|
||||
|
||||
TEST(BindTest, NonCopyableResult) {
|
||||
EXPECT_THAT(absl::bind_front(Factory)(42), ::testing::Pointee(42));
|
||||
EXPECT_THAT(absl::bind_front(Factory, 42)(), ::testing::Pointee(42));
|
||||
}
|
||||
|
||||
// is_copy_constructible<FalseCopyable<unique_ptr<T>> is true but an attempt to
|
||||
// instantiate the copy constructor leads to a compile error. This is similar
|
||||
// to how standard containers behave.
|
||||
template <class T>
|
||||
struct FalseCopyable {
|
||||
FalseCopyable() {}
|
||||
FalseCopyable(const FalseCopyable& other) : m(other.m) {}
|
||||
FalseCopyable(FalseCopyable&& other) : m(std::move(other.m)) {}
|
||||
T m;
|
||||
};
|
||||
|
||||
int GetMember(FalseCopyable<std::unique_ptr<int>> x) { return *x.m; }
|
||||
|
||||
TEST(BindTest, WrappedMoveOnly) {
|
||||
FalseCopyable<std::unique_ptr<int>> x;
|
||||
x.m = absl::make_unique<int>(42);
|
||||
auto f = absl::bind_front(&GetMember, std::move(x));
|
||||
EXPECT_EQ(42, std::move(f)());
|
||||
}
|
||||
|
||||
int Plus(int a, int b) { return a + b; }
|
||||
|
||||
TEST(BindTest, ConstExpr) {
|
||||
constexpr auto f = absl::bind_front(CharAt);
|
||||
EXPECT_EQ(f("ABC", 1), 'B');
|
||||
static constexpr int five = 5;
|
||||
constexpr auto plus5 = absl::bind_front(Plus, five);
|
||||
EXPECT_EQ(plus5(1), 6);
|
||||
|
||||
static constexpr char data[] = "DEF";
|
||||
constexpr auto g = absl::bind_front(CharAt, data);
|
||||
EXPECT_EQ(g(1), 'E');
|
||||
}
|
||||
|
||||
struct ManglingCall {
|
||||
int operator()(int, double, std::string) const { return 0; }
|
||||
};
|
||||
|
||||
TEST(BindTest, Mangling) {
|
||||
// We just want to generate a particular instantiation to see its mangling.
|
||||
absl::bind_front(ManglingCall{}, 1, 3.3)("A");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
151
TMessagesProj/jni/voip/webrtc/absl/functional/function_ref.h
Normal file
151
TMessagesProj/jni/voip/webrtc/absl/functional/function_ref.h
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
// Copyright 2019 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// File: function_ref.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// This header file defines the `absl::FunctionRef` type for holding a
|
||||
// non-owning reference to an object of any invocable type. This function
|
||||
// reference is typically most useful as a type-erased argument type for
|
||||
// accepting function types that neither take ownership nor copy the type; using
|
||||
// the reference type in this case avoids a copy and an allocation. Best
|
||||
// practices of other non-owning reference-like objects (such as
|
||||
// `absl::string_view`) apply here.
|
||||
//
|
||||
// An `absl::FunctionRef` is similar in usage to a `std::function` but has the
|
||||
// following differences:
|
||||
//
|
||||
// * It doesn't own the underlying object.
|
||||
// * It doesn't have a null or empty state.
|
||||
// * It never performs deep copies or allocations.
|
||||
// * It's much faster and cheaper to construct.
|
||||
// * It's trivially copyable and destructable.
|
||||
//
|
||||
// Generally, `absl::FunctionRef` should not be used as a return value, data
|
||||
// member, or to initialize a `std::function`. Such usages will often lead to
|
||||
// problematic lifetime issues. Once you convert something to an
|
||||
// `absl::FunctionRef` you cannot make a deep copy later.
|
||||
//
|
||||
// This class is suitable for use wherever a "const std::function<>&"
|
||||
// would be used without making a copy. ForEach functions and other versions of
|
||||
// the visitor pattern are a good example of when this class should be used.
|
||||
//
|
||||
// This class is trivial to copy and should be passed by value.
|
||||
#ifndef ABSL_FUNCTIONAL_FUNCTION_REF_H_
|
||||
#define ABSL_FUNCTIONAL_FUNCTION_REF_H_
|
||||
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/functional/internal/function_ref.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
// FunctionRef
|
||||
//
|
||||
// Dummy class declaration to allow the partial specialization based on function
|
||||
// types below.
|
||||
template <typename T>
|
||||
class FunctionRef;
|
||||
|
||||
// FunctionRef
|
||||
//
|
||||
// An `absl::FunctionRef` is a lightweight wrapper to any invocable object with
|
||||
// a compatible signature. Generally, an `absl::FunctionRef` should only be used
|
||||
// as an argument type and should be preferred as an argument over a const
|
||||
// reference to a `std::function`. `absl::FunctionRef` itself does not allocate,
|
||||
// although the wrapped invocable may.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // The following function takes a function callback by const reference
|
||||
// bool Visitor(const std::function<void(my_proto&,
|
||||
// absl::string_view)>& callback);
|
||||
//
|
||||
// // Assuming that the function is not stored or otherwise copied, it can be
|
||||
// // replaced by an `absl::FunctionRef`:
|
||||
// bool Visitor(absl::FunctionRef<void(my_proto&, absl::string_view)>
|
||||
// callback);
|
||||
//
|
||||
// Note: the assignment operator within an `absl::FunctionRef` is intentionally
|
||||
// deleted to prevent misuse; because the `absl::FunctionRef` does not own the
|
||||
// underlying type, assignment likely indicates misuse.
|
||||
template <typename R, typename... Args>
|
||||
class FunctionRef<R(Args...)> {
|
||||
private:
|
||||
// Used to disable constructors for objects that are not compatible with the
|
||||
// signature of this FunctionRef.
|
||||
template <typename F,
|
||||
typename FR = absl::base_internal::invoke_result_t<F, Args&&...>>
|
||||
using EnableIfCompatible =
|
||||
typename std::enable_if<std::is_void<R>::value ||
|
||||
std::is_convertible<FR, R>::value>::type;
|
||||
|
||||
public:
|
||||
// Constructs a FunctionRef from any invocable type.
|
||||
template <typename F, typename = EnableIfCompatible<const F&>>
|
||||
// NOLINTNEXTLINE(runtime/explicit)
|
||||
FunctionRef(const F& f ABSL_ATTRIBUTE_LIFETIME_BOUND)
|
||||
: invoker_(&absl::functional_internal::InvokeObject<F, R, Args...>) {
|
||||
absl::functional_internal::AssertNonNull(f);
|
||||
ptr_.obj = &f;
|
||||
}
|
||||
|
||||
// Overload for function pointers. This eliminates a level of indirection that
|
||||
// would happen if the above overload was used (it lets us store the pointer
|
||||
// instead of a pointer to a pointer).
|
||||
//
|
||||
// This overload is also used for references to functions, since references to
|
||||
// functions can decay to function pointers implicitly.
|
||||
template <
|
||||
typename F, typename = EnableIfCompatible<F*>,
|
||||
absl::functional_internal::EnableIf<absl::is_function<F>::value> = 0>
|
||||
FunctionRef(F* f) // NOLINT(runtime/explicit)
|
||||
: invoker_(&absl::functional_internal::InvokeFunction<F*, R, Args...>) {
|
||||
assert(f != nullptr);
|
||||
ptr_.fun = reinterpret_cast<decltype(ptr_.fun)>(f);
|
||||
}
|
||||
|
||||
// To help prevent subtle lifetime bugs, FunctionRef is not assignable.
|
||||
// Typically, it should only be used as an argument type.
|
||||
FunctionRef& operator=(const FunctionRef& rhs) = delete;
|
||||
FunctionRef(const FunctionRef& rhs) = default;
|
||||
|
||||
// Call the underlying object.
|
||||
R operator()(Args... args) const {
|
||||
return invoker_(ptr_, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
private:
|
||||
absl::functional_internal::VoidPtr ptr_;
|
||||
absl::functional_internal::Invoker<R, Args...> invoker_;
|
||||
};
|
||||
|
||||
// Allow const qualified function signatures. Since FunctionRef requires
|
||||
// constness anyway we can just make this a no-op.
|
||||
template <typename R, typename... Args>
|
||||
class FunctionRef<R(Args...) const> : public FunctionRef<R(Args...)> {
|
||||
public:
|
||||
using FunctionRef<R(Args...)>::FunctionRef;
|
||||
};
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_FUNCTIONAL_FUNCTION_REF_H_
|
||||
|
|
@ -0,0 +1,293 @@
|
|||
// Copyright 2019 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/functional/function_ref.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/container/internal/test_instance_tracker.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/memory/memory.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace {
|
||||
|
||||
void RunFun(FunctionRef<void()> f) { f(); }
|
||||
|
||||
TEST(FunctionRefTest, Lambda) {
|
||||
bool ran = false;
|
||||
RunFun([&] { ran = true; });
|
||||
EXPECT_TRUE(ran);
|
||||
}
|
||||
|
||||
int Function() { return 1337; }
|
||||
|
||||
TEST(FunctionRefTest, Function1) {
|
||||
FunctionRef<int()> ref(&Function);
|
||||
EXPECT_EQ(1337, ref());
|
||||
}
|
||||
|
||||
TEST(FunctionRefTest, Function2) {
|
||||
FunctionRef<int()> ref(Function);
|
||||
EXPECT_EQ(1337, ref());
|
||||
}
|
||||
|
||||
TEST(FunctionRefTest, ConstFunction) {
|
||||
FunctionRef<int() const> ref(Function);
|
||||
EXPECT_EQ(1337, ref());
|
||||
}
|
||||
|
||||
int NoExceptFunction() noexcept { return 1337; }
|
||||
|
||||
// TODO(jdennett): Add a test for noexcept member functions.
|
||||
TEST(FunctionRefTest, NoExceptFunction) {
|
||||
FunctionRef<int()> ref(NoExceptFunction);
|
||||
EXPECT_EQ(1337, ref());
|
||||
}
|
||||
|
||||
TEST(FunctionRefTest, ForwardsArgs) {
|
||||
auto l = [](std::unique_ptr<int> i) { return *i; };
|
||||
FunctionRef<int(std::unique_ptr<int>)> ref(l);
|
||||
EXPECT_EQ(42, ref(absl::make_unique<int>(42)));
|
||||
}
|
||||
|
||||
TEST(FunctionRef, ReturnMoveOnly) {
|
||||
auto l = [] { return absl::make_unique<int>(29); };
|
||||
FunctionRef<std::unique_ptr<int>()> ref(l);
|
||||
EXPECT_EQ(29, *ref());
|
||||
}
|
||||
|
||||
TEST(FunctionRef, ManyArgs) {
|
||||
auto l = [](int a, int b, int c) { return a + b + c; };
|
||||
FunctionRef<int(int, int, int)> ref(l);
|
||||
EXPECT_EQ(6, ref(1, 2, 3));
|
||||
}
|
||||
|
||||
TEST(FunctionRef, VoidResultFromNonVoidFunctor) {
|
||||
bool ran = false;
|
||||
auto l = [&]() -> int {
|
||||
ran = true;
|
||||
return 2;
|
||||
};
|
||||
FunctionRef<void()> ref(l);
|
||||
ref();
|
||||
EXPECT_TRUE(ran);
|
||||
}
|
||||
|
||||
TEST(FunctionRef, CastFromDerived) {
|
||||
struct Base {};
|
||||
struct Derived : public Base {};
|
||||
|
||||
Derived d;
|
||||
auto l1 = [&](Base* b) { EXPECT_EQ(&d, b); };
|
||||
FunctionRef<void(Derived*)> ref1(l1);
|
||||
ref1(&d);
|
||||
|
||||
auto l2 = [&]() -> Derived* { return &d; };
|
||||
FunctionRef<Base*()> ref2(l2);
|
||||
EXPECT_EQ(&d, ref2());
|
||||
}
|
||||
|
||||
TEST(FunctionRef, VoidResultFromNonVoidFuncton) {
|
||||
FunctionRef<void()> ref(Function);
|
||||
ref();
|
||||
}
|
||||
|
||||
TEST(FunctionRef, MemberPtr) {
|
||||
struct S {
|
||||
int i;
|
||||
};
|
||||
|
||||
S s{1100111};
|
||||
auto mem_ptr = &S::i;
|
||||
FunctionRef<int(const S& s)> ref(mem_ptr);
|
||||
EXPECT_EQ(1100111, ref(s));
|
||||
}
|
||||
|
||||
TEST(FunctionRef, MemberFun) {
|
||||
struct S {
|
||||
int i;
|
||||
int get_i() const { return i; }
|
||||
};
|
||||
|
||||
S s{22};
|
||||
auto mem_fun_ptr = &S::get_i;
|
||||
FunctionRef<int(const S& s)> ref(mem_fun_ptr);
|
||||
EXPECT_EQ(22, ref(s));
|
||||
}
|
||||
|
||||
TEST(FunctionRef, MemberFunRefqualified) {
|
||||
struct S {
|
||||
int i;
|
||||
int get_i() && { return i; }
|
||||
};
|
||||
auto mem_fun_ptr = &S::get_i;
|
||||
S s{22};
|
||||
FunctionRef<int(S && s)> ref(mem_fun_ptr);
|
||||
EXPECT_EQ(22, ref(std::move(s)));
|
||||
}
|
||||
|
||||
#if !defined(_WIN32) && defined(GTEST_HAS_DEATH_TEST)
|
||||
|
||||
TEST(FunctionRef, MemberFunRefqualifiedNull) {
|
||||
struct S {
|
||||
int i;
|
||||
int get_i() && { return i; }
|
||||
};
|
||||
auto mem_fun_ptr = &S::get_i;
|
||||
mem_fun_ptr = nullptr;
|
||||
EXPECT_DEBUG_DEATH({ FunctionRef<int(S && s)> ref(mem_fun_ptr); }, "");
|
||||
}
|
||||
|
||||
TEST(FunctionRef, NullMemberPtrAssertFails) {
|
||||
struct S {
|
||||
int i;
|
||||
};
|
||||
using MemberPtr = int S::*;
|
||||
MemberPtr mem_ptr = nullptr;
|
||||
EXPECT_DEBUG_DEATH({ FunctionRef<int(const S& s)> ref(mem_ptr); }, "");
|
||||
}
|
||||
|
||||
TEST(FunctionRef, NullStdFunctionAssertPasses) {
|
||||
std::function<void()> function = []() {};
|
||||
FunctionRef<void()> ref(function);
|
||||
}
|
||||
|
||||
TEST(FunctionRef, NullStdFunctionAssertFails) {
|
||||
std::function<void()> function = nullptr;
|
||||
EXPECT_DEBUG_DEATH({ FunctionRef<void()> ref(function); }, "");
|
||||
}
|
||||
|
||||
TEST(FunctionRef, NullAnyInvocableAssertPasses) {
|
||||
AnyInvocable<void() const> invocable = []() {};
|
||||
FunctionRef<void()> ref(invocable);
|
||||
}
|
||||
TEST(FunctionRef, NullAnyInvocableAssertFails) {
|
||||
AnyInvocable<void() const> invocable = nullptr;
|
||||
EXPECT_DEBUG_DEATH({ FunctionRef<void()> ref(invocable); }, "");
|
||||
}
|
||||
|
||||
#endif // GTEST_HAS_DEATH_TEST
|
||||
|
||||
TEST(FunctionRef, CopiesAndMovesPerPassByValue) {
|
||||
absl::test_internal::InstanceTracker tracker;
|
||||
absl::test_internal::CopyableMovableInstance instance(0);
|
||||
auto l = [](absl::test_internal::CopyableMovableInstance) {};
|
||||
FunctionRef<void(absl::test_internal::CopyableMovableInstance)> ref(l);
|
||||
ref(instance);
|
||||
EXPECT_EQ(tracker.copies(), 1);
|
||||
EXPECT_EQ(tracker.moves(), 1);
|
||||
}
|
||||
|
||||
TEST(FunctionRef, CopiesAndMovesPerPassByRef) {
|
||||
absl::test_internal::InstanceTracker tracker;
|
||||
absl::test_internal::CopyableMovableInstance instance(0);
|
||||
auto l = [](const absl::test_internal::CopyableMovableInstance&) {};
|
||||
FunctionRef<void(const absl::test_internal::CopyableMovableInstance&)> ref(l);
|
||||
ref(instance);
|
||||
EXPECT_EQ(tracker.copies(), 0);
|
||||
EXPECT_EQ(tracker.moves(), 0);
|
||||
}
|
||||
|
||||
TEST(FunctionRef, CopiesAndMovesPerPassByValueCallByMove) {
|
||||
absl::test_internal::InstanceTracker tracker;
|
||||
absl::test_internal::CopyableMovableInstance instance(0);
|
||||
auto l = [](absl::test_internal::CopyableMovableInstance) {};
|
||||
FunctionRef<void(absl::test_internal::CopyableMovableInstance)> ref(l);
|
||||
ref(std::move(instance));
|
||||
EXPECT_EQ(tracker.copies(), 0);
|
||||
EXPECT_EQ(tracker.moves(), 2);
|
||||
}
|
||||
|
||||
TEST(FunctionRef, CopiesAndMovesPerPassByValueToRef) {
|
||||
absl::test_internal::InstanceTracker tracker;
|
||||
absl::test_internal::CopyableMovableInstance instance(0);
|
||||
auto l = [](const absl::test_internal::CopyableMovableInstance&) {};
|
||||
FunctionRef<void(absl::test_internal::CopyableMovableInstance)> ref(l);
|
||||
ref(std::move(instance));
|
||||
EXPECT_EQ(tracker.copies(), 0);
|
||||
EXPECT_EQ(tracker.moves(), 1);
|
||||
}
|
||||
|
||||
TEST(FunctionRef, PassByValueTypes) {
|
||||
using absl::functional_internal::Invoker;
|
||||
using absl::functional_internal::VoidPtr;
|
||||
using absl::test_internal::CopyableMovableInstance;
|
||||
struct Trivial {
|
||||
void* p[2];
|
||||
};
|
||||
struct LargeTrivial {
|
||||
void* p[3];
|
||||
};
|
||||
|
||||
static_assert(std::is_same<Invoker<void, int>, void (*)(VoidPtr, int)>::value,
|
||||
"Scalar types should be passed by value");
|
||||
static_assert(
|
||||
std::is_same<Invoker<void, Trivial>, void (*)(VoidPtr, Trivial)>::value,
|
||||
"Small trivial types should be passed by value");
|
||||
static_assert(std::is_same<Invoker<void, LargeTrivial>,
|
||||
void (*)(VoidPtr, LargeTrivial &&)>::value,
|
||||
"Large trivial types should be passed by rvalue reference");
|
||||
static_assert(
|
||||
std::is_same<Invoker<void, CopyableMovableInstance>,
|
||||
void (*)(VoidPtr, CopyableMovableInstance &&)>::value,
|
||||
"Types with copy/move ctor should be passed by rvalue reference");
|
||||
|
||||
// References are passed as references.
|
||||
static_assert(
|
||||
std::is_same<Invoker<void, int&>, void (*)(VoidPtr, int&)>::value,
|
||||
"Reference types should be preserved");
|
||||
static_assert(
|
||||
std::is_same<Invoker<void, CopyableMovableInstance&>,
|
||||
void (*)(VoidPtr, CopyableMovableInstance&)>::value,
|
||||
"Reference types should be preserved");
|
||||
static_assert(
|
||||
std::is_same<Invoker<void, CopyableMovableInstance&&>,
|
||||
void (*)(VoidPtr, CopyableMovableInstance &&)>::value,
|
||||
"Reference types should be preserved");
|
||||
|
||||
// Make sure the address of an object received by reference is the same as the
|
||||
// address of the object passed by the caller.
|
||||
{
|
||||
LargeTrivial obj;
|
||||
auto test = [&obj](LargeTrivial& input) { ASSERT_EQ(&input, &obj); };
|
||||
absl::FunctionRef<void(LargeTrivial&)> ref(test);
|
||||
ref(obj);
|
||||
}
|
||||
|
||||
{
|
||||
Trivial obj;
|
||||
auto test = [&obj](Trivial& input) { ASSERT_EQ(&input, &obj); };
|
||||
absl::FunctionRef<void(Trivial&)> ref(test);
|
||||
ref(obj);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(FunctionRef, ReferenceToIncompleteType) {
|
||||
struct IncompleteType;
|
||||
auto test = [](IncompleteType&) {};
|
||||
absl::FunctionRef<void(IncompleteType&)> ref(test);
|
||||
|
||||
struct IncompleteType {};
|
||||
IncompleteType obj;
|
||||
ref(obj);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
|
@ -0,0 +1,176 @@
|
|||
// Copyright 2022 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "benchmark/benchmark.h"
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/functional/function_ref.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace {
|
||||
|
||||
int dummy = 0;
|
||||
|
||||
void FreeFunction() { benchmark::DoNotOptimize(dummy); }
|
||||
|
||||
struct TrivialFunctor {
|
||||
void operator()() const { benchmark::DoNotOptimize(dummy); }
|
||||
};
|
||||
|
||||
struct LargeFunctor {
|
||||
void operator()() const { benchmark::DoNotOptimize(this); }
|
||||
std::string a, b, c;
|
||||
};
|
||||
|
||||
template <typename Function, typename... Args>
|
||||
void ABSL_ATTRIBUTE_NOINLINE CallFunction(Function f, Args&&... args) {
|
||||
f(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename Function, typename Callable, typename... Args>
|
||||
void ConstructAndCallFunctionBenchmark(benchmark::State& state,
|
||||
const Callable& c, Args&&... args) {
|
||||
for (auto _ : state) {
|
||||
CallFunction<Function>(c, std::forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
void BM_TrivialStdFunction(benchmark::State& state) {
|
||||
ConstructAndCallFunctionBenchmark<std::function<void()>>(state,
|
||||
TrivialFunctor{});
|
||||
}
|
||||
BENCHMARK(BM_TrivialStdFunction);
|
||||
|
||||
void BM_TrivialFunctionRef(benchmark::State& state) {
|
||||
ConstructAndCallFunctionBenchmark<FunctionRef<void()>>(state,
|
||||
TrivialFunctor{});
|
||||
}
|
||||
BENCHMARK(BM_TrivialFunctionRef);
|
||||
|
||||
void BM_TrivialAnyInvocable(benchmark::State& state) {
|
||||
ConstructAndCallFunctionBenchmark<AnyInvocable<void()>>(state,
|
||||
TrivialFunctor{});
|
||||
}
|
||||
BENCHMARK(BM_TrivialAnyInvocable);
|
||||
|
||||
void BM_LargeStdFunction(benchmark::State& state) {
|
||||
ConstructAndCallFunctionBenchmark<std::function<void()>>(state,
|
||||
LargeFunctor{});
|
||||
}
|
||||
BENCHMARK(BM_LargeStdFunction);
|
||||
|
||||
void BM_LargeFunctionRef(benchmark::State& state) {
|
||||
ConstructAndCallFunctionBenchmark<FunctionRef<void()>>(state, LargeFunctor{});
|
||||
}
|
||||
BENCHMARK(BM_LargeFunctionRef);
|
||||
|
||||
|
||||
void BM_LargeAnyInvocable(benchmark::State& state) {
|
||||
ConstructAndCallFunctionBenchmark<AnyInvocable<void()>>(state,
|
||||
LargeFunctor{});
|
||||
}
|
||||
BENCHMARK(BM_LargeAnyInvocable);
|
||||
|
||||
void BM_FunPtrStdFunction(benchmark::State& state) {
|
||||
ConstructAndCallFunctionBenchmark<std::function<void()>>(state, FreeFunction);
|
||||
}
|
||||
BENCHMARK(BM_FunPtrStdFunction);
|
||||
|
||||
void BM_FunPtrFunctionRef(benchmark::State& state) {
|
||||
ConstructAndCallFunctionBenchmark<FunctionRef<void()>>(state, FreeFunction);
|
||||
}
|
||||
BENCHMARK(BM_FunPtrFunctionRef);
|
||||
|
||||
void BM_FunPtrAnyInvocable(benchmark::State& state) {
|
||||
ConstructAndCallFunctionBenchmark<AnyInvocable<void()>>(state, FreeFunction);
|
||||
}
|
||||
BENCHMARK(BM_FunPtrAnyInvocable);
|
||||
|
||||
// Doesn't include construction or copy overhead in the loop.
|
||||
template <typename Function, typename Callable, typename... Args>
|
||||
void CallFunctionBenchmark(benchmark::State& state, const Callable& c,
|
||||
Args... args) {
|
||||
Function f = c;
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(&f);
|
||||
f(args...);
|
||||
}
|
||||
}
|
||||
|
||||
struct FunctorWithTrivialArgs {
|
||||
void operator()(int a, int b, int c) const {
|
||||
benchmark::DoNotOptimize(a);
|
||||
benchmark::DoNotOptimize(b);
|
||||
benchmark::DoNotOptimize(c);
|
||||
}
|
||||
};
|
||||
|
||||
void BM_TrivialArgsStdFunction(benchmark::State& state) {
|
||||
CallFunctionBenchmark<std::function<void(int, int, int)>>(
|
||||
state, FunctorWithTrivialArgs{}, 1, 2, 3);
|
||||
}
|
||||
BENCHMARK(BM_TrivialArgsStdFunction);
|
||||
|
||||
void BM_TrivialArgsFunctionRef(benchmark::State& state) {
|
||||
CallFunctionBenchmark<FunctionRef<void(int, int, int)>>(
|
||||
state, FunctorWithTrivialArgs{}, 1, 2, 3);
|
||||
}
|
||||
BENCHMARK(BM_TrivialArgsFunctionRef);
|
||||
|
||||
void BM_TrivialArgsAnyInvocable(benchmark::State& state) {
|
||||
CallFunctionBenchmark<AnyInvocable<void(int, int, int)>>(
|
||||
state, FunctorWithTrivialArgs{}, 1, 2, 3);
|
||||
}
|
||||
BENCHMARK(BM_TrivialArgsAnyInvocable);
|
||||
|
||||
struct FunctorWithNonTrivialArgs {
|
||||
void operator()(std::string a, std::string b, std::string c) const {
|
||||
benchmark::DoNotOptimize(&a);
|
||||
benchmark::DoNotOptimize(&b);
|
||||
benchmark::DoNotOptimize(&c);
|
||||
}
|
||||
};
|
||||
|
||||
void BM_NonTrivialArgsStdFunction(benchmark::State& state) {
|
||||
std::string a, b, c;
|
||||
CallFunctionBenchmark<
|
||||
std::function<void(std::string, std::string, std::string)>>(
|
||||
state, FunctorWithNonTrivialArgs{}, a, b, c);
|
||||
}
|
||||
BENCHMARK(BM_NonTrivialArgsStdFunction);
|
||||
|
||||
void BM_NonTrivialArgsFunctionRef(benchmark::State& state) {
|
||||
std::string a, b, c;
|
||||
CallFunctionBenchmark<
|
||||
FunctionRef<void(std::string, std::string, std::string)>>(
|
||||
state, FunctorWithNonTrivialArgs{}, a, b, c);
|
||||
}
|
||||
BENCHMARK(BM_NonTrivialArgsFunctionRef);
|
||||
|
||||
void BM_NonTrivialArgsAnyInvocable(benchmark::State& state) {
|
||||
std::string a, b, c;
|
||||
CallFunctionBenchmark<
|
||||
AnyInvocable<void(std::string, std::string, std::string)>>(
|
||||
state, FunctorWithNonTrivialArgs{}, a, b, c);
|
||||
}
|
||||
BENCHMARK(BM_NonTrivialArgsAnyInvocable);
|
||||
|
||||
} // namespace
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
|
@ -0,0 +1,855 @@
|
|||
// Copyright 2022 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// Implementation details for `absl::AnyInvocable`
|
||||
|
||||
#ifndef ABSL_FUNCTIONAL_INTERNAL_ANY_INVOCABLE_H_
|
||||
#define ABSL_FUNCTIONAL_INTERNAL_ANY_INVOCABLE_H_
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// This implementation chooses between local storage and remote storage for //
|
||||
// the contained target object based on the target object's size, alignment //
|
||||
// requirements, and whether or not it has a nothrow move constructor. //
|
||||
// Additional optimizations are performed when the object is a trivially //
|
||||
// copyable type [basic.types]. //
|
||||
// //
|
||||
// There are three datamembers per `AnyInvocable` instance //
|
||||
// //
|
||||
// 1) A union containing either //
|
||||
// - A pointer to the target object referred to via a void*, or //
|
||||
// - the target object, emplaced into a raw char buffer //
|
||||
// //
|
||||
// 2) A function pointer to a "manager" function operation that takes a //
|
||||
// discriminator and logically branches to either perform a move operation //
|
||||
// or destroy operation based on that discriminator. //
|
||||
// //
|
||||
// 3) A function pointer to an "invoker" function operation that invokes the //
|
||||
// target object, directly returning the result. //
|
||||
// //
|
||||
// When in the logically empty state, the manager function is an empty //
|
||||
// function and the invoker function is one that would be undefined behavior //
|
||||
// to call. //
|
||||
// //
|
||||
// An additional optimization is performed when converting from one //
|
||||
// AnyInvocable to another where only the noexcept specification and/or the //
|
||||
// cv/ref qualifiers of the function type differ. In these cases, the //
|
||||
// conversion works by "moving the guts", similar to if they were the same //
|
||||
// exact type, as opposed to having to perform an additional layer of //
|
||||
// wrapping through remote storage. //
|
||||
// //
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// IWYU pragma: private, include "absl/functional/any_invocable.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/internal/invoke.h"
|
||||
#include "absl/base/macros.h"
|
||||
#include "absl/base/optimization.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
#include "absl/utility/utility.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
// Helper macro used to prevent spelling `noexcept` in language versions older
|
||||
// than C++17, where it is not part of the type system, in order to avoid
|
||||
// compilation failures and internal compiler errors.
|
||||
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
||||
#define ABSL_INTERNAL_NOEXCEPT_SPEC(noex) noexcept(noex)
|
||||
#else
|
||||
#define ABSL_INTERNAL_NOEXCEPT_SPEC(noex)
|
||||
#endif
|
||||
|
||||
// Defined in functional/any_invocable.h
|
||||
template <class Sig>
|
||||
class AnyInvocable;
|
||||
|
||||
namespace internal_any_invocable {
|
||||
|
||||
// Constants relating to the small-object-storage for AnyInvocable
|
||||
enum StorageProperty : std::size_t {
|
||||
kAlignment = alignof(std::max_align_t), // The alignment of the storage
|
||||
kStorageSize = sizeof(void*) * 2 // The size of the storage
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// A metafunction for checking if a type is an AnyInvocable instantiation.
|
||||
// This is used during conversion operations.
|
||||
template <class T>
|
||||
struct IsAnyInvocable : std::false_type {};
|
||||
|
||||
template <class Sig>
|
||||
struct IsAnyInvocable<AnyInvocable<Sig>> : std::true_type {};
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// A metafunction that tells us whether or not a target function type should be
|
||||
// stored locally in the small object optimization storage
|
||||
template <class T>
|
||||
constexpr bool IsStoredLocally() {
|
||||
if constexpr (sizeof(T) <= kStorageSize && alignof(T) <= kAlignment &&
|
||||
kAlignment % alignof(T) == 0) {
|
||||
return std::is_nothrow_move_constructible<T>::value;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// An implementation of std::remove_cvref_t of C++20.
|
||||
template <class T>
|
||||
using RemoveCVRef =
|
||||
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// An implementation of the C++ standard INVOKE<R> pseudo-macro, operation is
|
||||
// equivalent to std::invoke except that it forces an implicit conversion to the
|
||||
// specified return type. If "R" is void, the function is executed and the
|
||||
// return value is simply ignored.
|
||||
template <class ReturnType, class F, class... P,
|
||||
typename = absl::enable_if_t<std::is_void<ReturnType>::value>>
|
||||
void InvokeR(F&& f, P&&... args) {
|
||||
absl::base_internal::invoke(std::forward<F>(f), std::forward<P>(args)...);
|
||||
}
|
||||
|
||||
template <class ReturnType, class F, class... P,
|
||||
absl::enable_if_t<!std::is_void<ReturnType>::value, int> = 0>
|
||||
ReturnType InvokeR(F&& f, P&&... args) {
|
||||
// GCC 12 has a false-positive -Wmaybe-uninitialized warning here.
|
||||
#if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(12, 0)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
||||
#endif
|
||||
return absl::base_internal::invoke(std::forward<F>(f),
|
||||
std::forward<P>(args)...);
|
||||
#if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(12, 0)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
}
|
||||
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
///
|
||||
// A metafunction that takes a "T" corresponding to a parameter type of the
|
||||
// user's specified function type, and yields the parameter type to use for the
|
||||
// type-erased invoker. In order to prevent observable moves, this must be
|
||||
// either a reference or, if the type is trivial, the original parameter type
|
||||
// itself. Since the parameter type may be incomplete at the point that this
|
||||
// metafunction is used, we can only do this optimization for scalar types
|
||||
// rather than for any trivial type.
|
||||
template <typename T>
|
||||
T ForwardImpl(std::true_type);
|
||||
|
||||
template <typename T>
|
||||
T&& ForwardImpl(std::false_type);
|
||||
|
||||
// NOTE: We deliberately use an intermediate struct instead of a direct alias,
|
||||
// as a workaround for b/206991861 on MSVC versions < 1924.
|
||||
template <class T>
|
||||
struct ForwardedParameter {
|
||||
using type = decltype((
|
||||
ForwardImpl<T>)(std::integral_constant<bool,
|
||||
std::is_scalar<T>::value>()));
|
||||
};
|
||||
|
||||
template <class T>
|
||||
using ForwardedParameterType = typename ForwardedParameter<T>::type;
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// A discriminator when calling the "manager" function that describes operation
|
||||
// type-erased operation should be invoked.
|
||||
//
|
||||
// "relocate_from_to" specifies that the manager should perform a move.
|
||||
//
|
||||
// "dispose" specifies that the manager should perform a destroy.
|
||||
enum class FunctionToCall : bool { relocate_from_to, dispose };
|
||||
|
||||
// The portion of `AnyInvocable` state that contains either a pointer to the
|
||||
// target object or the object itself in local storage
|
||||
union TypeErasedState {
|
||||
struct {
|
||||
// A pointer to the type-erased object when remotely stored
|
||||
void* target;
|
||||
// The size of the object for `RemoteManagerTrivial`
|
||||
std::size_t size;
|
||||
} remote;
|
||||
|
||||
// Local-storage for the type-erased object when small and trivial enough
|
||||
alignas(kAlignment) char storage[kStorageSize];
|
||||
};
|
||||
|
||||
// A typed accessor for the object in `TypeErasedState` storage
|
||||
template <class T>
|
||||
T& ObjectInLocalStorage(TypeErasedState* const state) {
|
||||
// We launder here because the storage may be reused with the same type.
|
||||
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
|
||||
return *std::launder(reinterpret_cast<T*>(&state->storage));
|
||||
#elif ABSL_HAVE_BUILTIN(__builtin_launder)
|
||||
return *__builtin_launder(reinterpret_cast<T*>(&state->storage));
|
||||
#else
|
||||
|
||||
// When `std::launder` or equivalent are not available, we rely on undefined
|
||||
// behavior, which works as intended on Abseil's officially supported
|
||||
// platforms as of Q2 2022.
|
||||
#if !defined(__clang__) && defined(__GNUC__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
||||
#endif
|
||||
return *reinterpret_cast<T*>(&state->storage);
|
||||
#if !defined(__clang__) && defined(__GNUC__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
// The type for functions issuing lifetime-related operations: move and dispose
|
||||
// A pointer to such a function is contained in each `AnyInvocable` instance.
|
||||
// NOTE: When specifying `FunctionToCall::`dispose, the same state must be
|
||||
// passed as both "from" and "to".
|
||||
using ManagerType = void(FunctionToCall /*operation*/,
|
||||
TypeErasedState* /*from*/, TypeErasedState* /*to*/)
|
||||
ABSL_INTERNAL_NOEXCEPT_SPEC(true);
|
||||
|
||||
// The type for functions issuing the actual invocation of the object
|
||||
// A pointer to such a function is contained in each AnyInvocable instance.
|
||||
template <bool SigIsNoexcept, class ReturnType, class... P>
|
||||
using InvokerType = ReturnType(TypeErasedState*, ForwardedParameterType<P>...)
|
||||
ABSL_INTERNAL_NOEXCEPT_SPEC(SigIsNoexcept);
|
||||
|
||||
// The manager that is used when AnyInvocable is empty
|
||||
inline void EmptyManager(FunctionToCall /*operation*/,
|
||||
TypeErasedState* /*from*/,
|
||||
TypeErasedState* /*to*/) noexcept {}
|
||||
|
||||
// The manager that is used when a target function is in local storage and is
|
||||
// a trivially copyable type.
|
||||
inline void LocalManagerTrivial(FunctionToCall /*operation*/,
|
||||
TypeErasedState* const from,
|
||||
TypeErasedState* const to) noexcept {
|
||||
// This single statement without branching handles both possible operations.
|
||||
//
|
||||
// For FunctionToCall::dispose, "from" and "to" point to the same state, and
|
||||
// so this assignment logically would do nothing.
|
||||
//
|
||||
// Note: Correctness here relies on http://wg21.link/p0593, which has only
|
||||
// become standard in C++20, though implementations do not break it in
|
||||
// practice for earlier versions of C++.
|
||||
//
|
||||
// The correct way to do this without that paper is to first placement-new a
|
||||
// default-constructed T in "to->storage" prior to the memmove, but doing so
|
||||
// requires a different function to be created for each T that is stored
|
||||
// locally, which can cause unnecessary bloat and be less cache friendly.
|
||||
*to = *from;
|
||||
|
||||
// Note: Because the type is trivially copyable, the destructor does not need
|
||||
// to be called ("trivially copyable" requires a trivial destructor).
|
||||
}
|
||||
|
||||
// The manager that is used when a target function is in local storage and is
|
||||
// not a trivially copyable type.
|
||||
template <class T>
|
||||
void LocalManagerNontrivial(FunctionToCall operation,
|
||||
TypeErasedState* const from,
|
||||
TypeErasedState* const to) noexcept {
|
||||
static_assert(IsStoredLocally<T>(),
|
||||
"Local storage must only be used for supported types.");
|
||||
static_assert(!std::is_trivially_copyable<T>::value,
|
||||
"Locally stored types must be trivially copyable.");
|
||||
|
||||
T& from_object = (ObjectInLocalStorage<T>)(from);
|
||||
|
||||
switch (operation) {
|
||||
case FunctionToCall::relocate_from_to:
|
||||
// NOTE: Requires that the left-hand operand is already empty.
|
||||
::new (static_cast<void*>(&to->storage)) T(std::move(from_object));
|
||||
ABSL_FALLTHROUGH_INTENDED;
|
||||
case FunctionToCall::dispose:
|
||||
from_object.~T(); // Must not throw. // NOLINT
|
||||
return;
|
||||
}
|
||||
ABSL_UNREACHABLE();
|
||||
}
|
||||
|
||||
// The invoker that is used when a target function is in local storage
|
||||
// Note: QualTRef here is the target function type along with cv and reference
|
||||
// qualifiers that must be used when calling the function.
|
||||
template <bool SigIsNoexcept, class ReturnType, class QualTRef, class... P>
|
||||
ReturnType LocalInvoker(
|
||||
TypeErasedState* const state,
|
||||
ForwardedParameterType<P>... args) noexcept(SigIsNoexcept) {
|
||||
using RawT = RemoveCVRef<QualTRef>;
|
||||
static_assert(
|
||||
IsStoredLocally<RawT>(),
|
||||
"Target object must be in local storage in order to be invoked from it.");
|
||||
|
||||
auto& f = (ObjectInLocalStorage<RawT>)(state);
|
||||
return (InvokeR<ReturnType>)(static_cast<QualTRef>(f),
|
||||
static_cast<ForwardedParameterType<P>>(args)...);
|
||||
}
|
||||
|
||||
// The manager that is used when a target function is in remote storage and it
|
||||
// has a trivial destructor
|
||||
inline void RemoteManagerTrivial(FunctionToCall operation,
|
||||
TypeErasedState* const from,
|
||||
TypeErasedState* const to) noexcept {
|
||||
switch (operation) {
|
||||
case FunctionToCall::relocate_from_to:
|
||||
// NOTE: Requires that the left-hand operand is already empty.
|
||||
to->remote = from->remote;
|
||||
return;
|
||||
case FunctionToCall::dispose:
|
||||
#if defined(__cpp_sized_deallocation)
|
||||
::operator delete(from->remote.target, from->remote.size);
|
||||
#else // __cpp_sized_deallocation
|
||||
::operator delete(from->remote.target);
|
||||
#endif // __cpp_sized_deallocation
|
||||
return;
|
||||
}
|
||||
ABSL_UNREACHABLE();
|
||||
}
|
||||
|
||||
// The manager that is used when a target function is in remote storage and the
|
||||
// destructor of the type is not trivial
|
||||
template <class T>
|
||||
void RemoteManagerNontrivial(FunctionToCall operation,
|
||||
TypeErasedState* const from,
|
||||
TypeErasedState* const to) noexcept {
|
||||
static_assert(!IsStoredLocally<T>(),
|
||||
"Remote storage must only be used for types that do not "
|
||||
"qualify for local storage.");
|
||||
|
||||
switch (operation) {
|
||||
case FunctionToCall::relocate_from_to:
|
||||
// NOTE: Requires that the left-hand operand is already empty.
|
||||
to->remote.target = from->remote.target;
|
||||
return;
|
||||
case FunctionToCall::dispose:
|
||||
::delete static_cast<T*>(from->remote.target); // Must not throw.
|
||||
return;
|
||||
}
|
||||
ABSL_UNREACHABLE();
|
||||
}
|
||||
|
||||
// The invoker that is used when a target function is in remote storage
|
||||
template <bool SigIsNoexcept, class ReturnType, class QualTRef, class... P>
|
||||
ReturnType RemoteInvoker(
|
||||
TypeErasedState* const state,
|
||||
ForwardedParameterType<P>... args) noexcept(SigIsNoexcept) {
|
||||
using RawT = RemoveCVRef<QualTRef>;
|
||||
static_assert(!IsStoredLocally<RawT>(),
|
||||
"Target object must be in remote storage in order to be "
|
||||
"invoked from it.");
|
||||
|
||||
auto& f = *static_cast<RawT*>(state->remote.target);
|
||||
return (InvokeR<ReturnType>)(static_cast<QualTRef>(f),
|
||||
static_cast<ForwardedParameterType<P>>(args)...);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// A metafunction that checks if a type T is an instantiation of
|
||||
// absl::in_place_type_t (needed for constructor constraints of AnyInvocable).
|
||||
template <class T>
|
||||
struct IsInPlaceType : std::false_type {};
|
||||
|
||||
template <class T>
|
||||
struct IsInPlaceType<absl::in_place_type_t<T>> : std::true_type {};
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// A constructor name-tag used with CoreImpl (below) to request the
|
||||
// conversion-constructor. QualDecayedTRef is the decayed-type of the object to
|
||||
// wrap, along with the cv and reference qualifiers that must be applied when
|
||||
// performing an invocation of the wrapped object.
|
||||
template <class QualDecayedTRef>
|
||||
struct TypedConversionConstruct {};
|
||||
|
||||
// A helper base class for all core operations of AnyInvocable. Most notably,
|
||||
// this class creates the function call operator and constraint-checkers so that
|
||||
// the top-level class does not have to be a series of partial specializations.
|
||||
//
|
||||
// Note: This definition exists (as opposed to being a declaration) so that if
|
||||
// the user of the top-level template accidentally passes a template argument
|
||||
// that is not a function type, they will get a static_assert in AnyInvocable's
|
||||
// class body rather than an error stating that Impl is not defined.
|
||||
template <class Sig>
|
||||
class Impl {}; // Note: This is partially-specialized later.
|
||||
|
||||
// A std::unique_ptr deleter that deletes memory allocated via ::operator new.
|
||||
#if defined(__cpp_sized_deallocation)
|
||||
class TrivialDeleter {
|
||||
public:
|
||||
explicit TrivialDeleter(std::size_t size) : size_(size) {}
|
||||
|
||||
void operator()(void* target) const {
|
||||
::operator delete(target, size_);
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t size_;
|
||||
};
|
||||
#else // __cpp_sized_deallocation
|
||||
class TrivialDeleter {
|
||||
public:
|
||||
explicit TrivialDeleter(std::size_t) {}
|
||||
|
||||
void operator()(void* target) const { ::operator delete(target); }
|
||||
};
|
||||
#endif // __cpp_sized_deallocation
|
||||
|
||||
template <bool SigIsNoexcept, class ReturnType, class... P>
|
||||
class CoreImpl;
|
||||
|
||||
constexpr bool IsCompatibleConversion(void*, void*) { return false; }
|
||||
template <bool NoExceptSrc, bool NoExceptDest, class... T>
|
||||
constexpr bool IsCompatibleConversion(CoreImpl<NoExceptSrc, T...>*,
|
||||
CoreImpl<NoExceptDest, T...>*) {
|
||||
return !NoExceptDest || NoExceptSrc;
|
||||
}
|
||||
|
||||
// A helper base class for all core operations of AnyInvocable that do not
|
||||
// depend on the cv/ref qualifiers of the function type.
|
||||
template <bool SigIsNoexcept, class ReturnType, class... P>
|
||||
class CoreImpl {
|
||||
public:
|
||||
using result_type = ReturnType;
|
||||
|
||||
CoreImpl() noexcept : manager_(EmptyManager), invoker_(nullptr) {}
|
||||
|
||||
// Note: QualDecayedTRef here includes the cv-ref qualifiers associated with
|
||||
// the invocation of the Invocable. The unqualified type is the target object
|
||||
// type to be stored.
|
||||
template <class QualDecayedTRef, class F>
|
||||
explicit CoreImpl(TypedConversionConstruct<QualDecayedTRef>, F&& f) {
|
||||
using DecayedT = RemoveCVRef<QualDecayedTRef>;
|
||||
|
||||
if constexpr (std::is_pointer<DecayedT>::value ||
|
||||
std::is_member_pointer<DecayedT>::value) {
|
||||
// This condition handles types that decay into pointers, which includes
|
||||
// function references. Since function references cannot be null, GCC
|
||||
// warns against comparing their decayed form with nullptr. Since this is
|
||||
// template-heavy code, we prefer to disable these warnings locally
|
||||
// instead of adding yet another overload of this function.
|
||||
//
|
||||
// TODO(b/290784225): Avoid warnings using constexpr programming instead.
|
||||
#if !defined(__clang__) && defined(__GNUC__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wpragmas"
|
||||
#pragma GCC diagnostic ignored "-Waddress"
|
||||
#pragma GCC diagnostic ignored "-Wnonnull-compare"
|
||||
#endif
|
||||
if (static_cast<DecayedT>(f) == nullptr) {
|
||||
#if !defined(__clang__) && defined(__GNUC__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
manager_ = EmptyManager;
|
||||
invoker_ = nullptr;
|
||||
} else {
|
||||
InitializeStorage<QualDecayedTRef>(std::forward<F>(f));
|
||||
}
|
||||
} else if constexpr (IsCompatibleAnyInvocable<DecayedT>::value) {
|
||||
// In this case we can "steal the guts" of the other AnyInvocable.
|
||||
f.manager_(FunctionToCall::relocate_from_to, &f.state_, &state_);
|
||||
manager_ = f.manager_;
|
||||
invoker_ = f.invoker_;
|
||||
|
||||
f.manager_ = EmptyManager;
|
||||
f.invoker_ = nullptr;
|
||||
} else if constexpr (IsAnyInvocable<DecayedT>::value) {
|
||||
if (f.HasValue()) {
|
||||
InitializeStorage<QualDecayedTRef>(std::forward<F>(f));
|
||||
} else {
|
||||
manager_ = EmptyManager;
|
||||
invoker_ = nullptr;
|
||||
}
|
||||
} else {
|
||||
InitializeStorage<QualDecayedTRef>(std::forward<F>(f));
|
||||
}
|
||||
}
|
||||
|
||||
// Note: QualTRef here includes the cv-ref qualifiers associated with the
|
||||
// invocation of the Invocable. The unqualified type is the target object
|
||||
// type to be stored.
|
||||
template <class QualTRef, class... Args>
|
||||
explicit CoreImpl(absl::in_place_type_t<QualTRef>, Args&&... args) {
|
||||
InitializeStorage<QualTRef>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
CoreImpl(CoreImpl&& other) noexcept {
|
||||
other.manager_(FunctionToCall::relocate_from_to, &other.state_, &state_);
|
||||
manager_ = other.manager_;
|
||||
invoker_ = other.invoker_;
|
||||
other.manager_ = EmptyManager;
|
||||
other.invoker_ = nullptr;
|
||||
}
|
||||
|
||||
CoreImpl& operator=(CoreImpl&& other) noexcept {
|
||||
// Put the left-hand operand in an empty state.
|
||||
//
|
||||
// Note: A full reset that leaves us with an object that has its invariants
|
||||
// intact is necessary in order to handle self-move. This is required by
|
||||
// types that are used with certain operations of the standard library, such
|
||||
// as the default definition of std::swap when both operands target the same
|
||||
// object.
|
||||
Clear();
|
||||
|
||||
// Perform the actual move/destroy operation on the target function.
|
||||
other.manager_(FunctionToCall::relocate_from_to, &other.state_, &state_);
|
||||
manager_ = other.manager_;
|
||||
invoker_ = other.invoker_;
|
||||
other.manager_ = EmptyManager;
|
||||
other.invoker_ = nullptr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
~CoreImpl() { manager_(FunctionToCall::dispose, &state_, &state_); }
|
||||
|
||||
// Check whether or not the AnyInvocable is in the empty state.
|
||||
bool HasValue() const { return invoker_ != nullptr; }
|
||||
|
||||
// Effects: Puts the object into its empty state.
|
||||
void Clear() {
|
||||
manager_(FunctionToCall::dispose, &state_, &state_);
|
||||
manager_ = EmptyManager;
|
||||
invoker_ = nullptr;
|
||||
}
|
||||
|
||||
// Use local (inline) storage for applicable target object types.
|
||||
template <class QualTRef, class... Args>
|
||||
void InitializeStorage(Args&&... args) {
|
||||
if constexpr (IsStoredLocally<RemoveCVRef<QualTRef>>()) {
|
||||
using RawT = RemoveCVRef<QualTRef>;
|
||||
::new (static_cast<void*>(&state_.storage))
|
||||
RawT(std::forward<Args>(args)...);
|
||||
|
||||
invoker_ = LocalInvoker<SigIsNoexcept, ReturnType, QualTRef, P...>;
|
||||
// We can simplify our manager if we know the type is trivially copyable.
|
||||
InitializeLocalManager<RawT>();
|
||||
} else {
|
||||
InitializeRemoteManager<RemoveCVRef<QualTRef>>(
|
||||
std::forward<Args>(args)...);
|
||||
// This is set after everything else in case an exception is thrown in an
|
||||
// earlier step of the initialization.
|
||||
invoker_ = RemoteInvoker<SigIsNoexcept, ReturnType, QualTRef, P...>;
|
||||
}
|
||||
}
|
||||
|
||||
template <class T,
|
||||
typename = absl::enable_if_t<std::is_trivially_copyable<T>::value>>
|
||||
void InitializeLocalManager() {
|
||||
manager_ = LocalManagerTrivial;
|
||||
}
|
||||
|
||||
template <class T,
|
||||
absl::enable_if_t<!std::is_trivially_copyable<T>::value, int> = 0>
|
||||
void InitializeLocalManager() {
|
||||
manager_ = LocalManagerNontrivial<T>;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
using HasTrivialRemoteStorage =
|
||||
std::integral_constant<bool, std::is_trivially_destructible<T>::value &&
|
||||
alignof(T) <=
|
||||
ABSL_INTERNAL_DEFAULT_NEW_ALIGNMENT>;
|
||||
|
||||
template <class T, class... Args,
|
||||
typename = absl::enable_if_t<HasTrivialRemoteStorage<T>::value>>
|
||||
void InitializeRemoteManager(Args&&... args) {
|
||||
// unique_ptr is used for exception-safety in case construction throws.
|
||||
std::unique_ptr<void, TrivialDeleter> uninitialized_target(
|
||||
::operator new(sizeof(T)), TrivialDeleter(sizeof(T)));
|
||||
::new (uninitialized_target.get()) T(std::forward<Args>(args)...);
|
||||
state_.remote.target = uninitialized_target.release();
|
||||
state_.remote.size = sizeof(T);
|
||||
manager_ = RemoteManagerTrivial;
|
||||
}
|
||||
|
||||
template <class T, class... Args,
|
||||
absl::enable_if_t<!HasTrivialRemoteStorage<T>::value, int> = 0>
|
||||
void InitializeRemoteManager(Args&&... args) {
|
||||
state_.remote.target = ::new T(std::forward<Args>(args)...);
|
||||
manager_ = RemoteManagerNontrivial<T>;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Type trait to determine if the template argument is an AnyInvocable whose
|
||||
// function type is compatible enough with ours such that we can
|
||||
// "move the guts" out of it when moving, rather than having to place a new
|
||||
// object into remote storage.
|
||||
|
||||
template <typename Other>
|
||||
struct IsCompatibleAnyInvocable {
|
||||
static constexpr bool value = false;
|
||||
};
|
||||
|
||||
template <typename Sig>
|
||||
struct IsCompatibleAnyInvocable<AnyInvocable<Sig>> {
|
||||
static constexpr bool value =
|
||||
(IsCompatibleConversion)(static_cast<
|
||||
typename AnyInvocable<Sig>::CoreImpl*>(
|
||||
nullptr),
|
||||
static_cast<CoreImpl*>(nullptr));
|
||||
};
|
||||
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TypeErasedState state_;
|
||||
ManagerType* manager_;
|
||||
InvokerType<SigIsNoexcept, ReturnType, P...>* invoker_;
|
||||
};
|
||||
|
||||
// A constructor name-tag used with Impl to request the
|
||||
// conversion-constructor
|
||||
struct ConversionConstruct {};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// A metafunction that is normally an identity metafunction except that when
|
||||
// given a std::reference_wrapper<T>, it yields T&. This is necessary because
|
||||
// currently std::reference_wrapper's operator() is not conditionally noexcept,
|
||||
// so when checking if such an Invocable is nothrow-invocable, we must pull out
|
||||
// the underlying type.
|
||||
template <class T>
|
||||
struct UnwrapStdReferenceWrapperImpl {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct UnwrapStdReferenceWrapperImpl<std::reference_wrapper<T>> {
|
||||
using type = T&;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
using UnwrapStdReferenceWrapper =
|
||||
typename UnwrapStdReferenceWrapperImpl<T>::type;
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// An alias that always yields std::true_type (used with constraints) where
|
||||
// substitution failures happen when forming the template arguments.
|
||||
template <class... T>
|
||||
using TrueAlias =
|
||||
std::integral_constant<bool, sizeof(absl::void_t<T...>*) != 0>;
|
||||
|
||||
/*SFINAE constraints for the conversion-constructor.*/
|
||||
template <class Sig, class F,
|
||||
class = absl::enable_if_t<
|
||||
!std::is_same<RemoveCVRef<F>, AnyInvocable<Sig>>::value>>
|
||||
using CanConvert = TrueAlias<
|
||||
absl::enable_if_t<!IsInPlaceType<RemoveCVRef<F>>::value>,
|
||||
absl::enable_if_t<Impl<Sig>::template CallIsValid<F>::value>,
|
||||
absl::enable_if_t<
|
||||
Impl<Sig>::template CallIsNoexceptIfSigIsNoexcept<F>::value>,
|
||||
absl::enable_if_t<std::is_constructible<absl::decay_t<F>, F>::value>>;
|
||||
|
||||
/*SFINAE constraints for the std::in_place constructors.*/
|
||||
template <class Sig, class F, class... Args>
|
||||
using CanEmplace = TrueAlias<
|
||||
absl::enable_if_t<Impl<Sig>::template CallIsValid<F>::value>,
|
||||
absl::enable_if_t<
|
||||
Impl<Sig>::template CallIsNoexceptIfSigIsNoexcept<F>::value>,
|
||||
absl::enable_if_t<std::is_constructible<absl::decay_t<F>, Args...>::value>>;
|
||||
|
||||
/*SFINAE constraints for the conversion-assign operator.*/
|
||||
template <class Sig, class F,
|
||||
class = absl::enable_if_t<
|
||||
!std::is_same<RemoveCVRef<F>, AnyInvocable<Sig>>::value>>
|
||||
using CanAssign = TrueAlias<
|
||||
absl::enable_if_t<Impl<Sig>::template CallIsValid<F>::value>,
|
||||
absl::enable_if_t<
|
||||
Impl<Sig>::template CallIsNoexceptIfSigIsNoexcept<F>::value>,
|
||||
absl::enable_if_t<std::is_constructible<absl::decay_t<F>, F>::value>>;
|
||||
|
||||
/*SFINAE constraints for the reference-wrapper conversion-assign operator.*/
|
||||
template <class Sig, class F>
|
||||
using CanAssignReferenceWrapper = TrueAlias<
|
||||
absl::enable_if_t<
|
||||
Impl<Sig>::template CallIsValid<std::reference_wrapper<F>>::value>,
|
||||
absl::enable_if_t<Impl<Sig>::template CallIsNoexceptIfSigIsNoexcept<
|
||||
std::reference_wrapper<F>>::value>>;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// The constraint for checking whether or not a call meets the noexcept
|
||||
// callability requirements. This is a preprocessor macro because specifying it
|
||||
// this way as opposed to a disjunction/branch can improve the user-side error
|
||||
// messages and avoids an instantiation of std::is_nothrow_invocable_r in the
|
||||
// cases where the user did not specify a noexcept function type.
|
||||
//
|
||||
#define ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT(inv_quals, noex) \
|
||||
ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT_##noex(inv_quals)
|
||||
|
||||
// The disjunction below is because we can't rely on std::is_nothrow_invocable_r
|
||||
// to give the right result when ReturnType is non-moveable in toolchains that
|
||||
// don't treat non-moveable result types correctly. For example this was the
|
||||
// case in libc++ before commit c3a24882 (2022-05).
|
||||
#define ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT_true(inv_quals) \
|
||||
absl::enable_if_t<absl::disjunction< \
|
||||
std::is_nothrow_invocable_r< \
|
||||
ReturnType, UnwrapStdReferenceWrapper<absl::decay_t<F>> inv_quals, \
|
||||
P...>, \
|
||||
std::conjunction< \
|
||||
std::is_nothrow_invocable< \
|
||||
UnwrapStdReferenceWrapper<absl::decay_t<F>> inv_quals, P...>, \
|
||||
std::is_same< \
|
||||
ReturnType, \
|
||||
absl::base_internal::invoke_result_t< \
|
||||
UnwrapStdReferenceWrapper<absl::decay_t<F>> inv_quals, \
|
||||
P...>>>>::value>
|
||||
|
||||
#define ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT_false(inv_quals)
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// A macro to generate partial specializations of Impl with the different
|
||||
// combinations of supported cv/reference qualifiers and noexcept specifier.
|
||||
//
|
||||
// Here, `cv` are the cv-qualifiers if any, `ref` is the ref-qualifier if any,
|
||||
// inv_quals is the reference type to be used when invoking the target, and
|
||||
// noex is "true" if the function type is noexcept, or false if it is not.
|
||||
//
|
||||
// The CallIsValid condition is more complicated than simply using
|
||||
// absl::base_internal::is_invocable_r because we can't rely on it to give the
|
||||
// right result when ReturnType is non-moveable in toolchains that don't treat
|
||||
// non-moveable result types correctly. For example this was the case in libc++
|
||||
// before commit c3a24882 (2022-05).
|
||||
#define ABSL_INTERNAL_ANY_INVOCABLE_IMPL_(cv, ref, inv_quals, noex) \
|
||||
template <class ReturnType, class... P> \
|
||||
class Impl<ReturnType(P...) cv ref ABSL_INTERNAL_NOEXCEPT_SPEC(noex)> \
|
||||
: public CoreImpl<noex, ReturnType, P...> { \
|
||||
public: \
|
||||
/*The base class, which contains the datamembers and core operations*/ \
|
||||
using Core = CoreImpl<noex, ReturnType, P...>; \
|
||||
\
|
||||
/*SFINAE constraint to check if F is invocable with the proper signature*/ \
|
||||
template <class F> \
|
||||
using CallIsValid = TrueAlias<absl::enable_if_t<absl::disjunction< \
|
||||
absl::base_internal::is_invocable_r<ReturnType, \
|
||||
absl::decay_t<F> inv_quals, P...>, \
|
||||
std::is_same<ReturnType, \
|
||||
absl::base_internal::invoke_result_t< \
|
||||
absl::decay_t<F> inv_quals, P...>>>::value>>; \
|
||||
\
|
||||
/*SFINAE constraint to check if F is nothrow-invocable when necessary*/ \
|
||||
template <class F> \
|
||||
using CallIsNoexceptIfSigIsNoexcept = \
|
||||
TrueAlias<ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT(inv_quals, \
|
||||
noex)>; \
|
||||
\
|
||||
/*Put the AnyInvocable into an empty state.*/ \
|
||||
Impl() = default; \
|
||||
\
|
||||
/*The implementation of a conversion-constructor from "f*/ \
|
||||
/*This forwards to Core, attaching inv_quals so that the base class*/ \
|
||||
/*knows how to properly type-erase the invocation.*/ \
|
||||
template <class F> \
|
||||
explicit Impl(ConversionConstruct, F&& f) \
|
||||
: Core(TypedConversionConstruct< \
|
||||
typename std::decay<F>::type inv_quals>(), \
|
||||
std::forward<F>(f)) {} \
|
||||
\
|
||||
/*Forward along the in-place construction parameters.*/ \
|
||||
template <class T, class... Args> \
|
||||
explicit Impl(absl::in_place_type_t<T>, Args&&... args) \
|
||||
: Core(absl::in_place_type<absl::decay_t<T> inv_quals>, \
|
||||
std::forward<Args>(args)...) {} \
|
||||
\
|
||||
/*Raises a fatal error when the AnyInvocable is invoked after a move*/ \
|
||||
static ReturnType InvokedAfterMove( \
|
||||
TypeErasedState*, \
|
||||
ForwardedParameterType<P>...) noexcept(noex) { \
|
||||
ABSL_HARDENING_ASSERT(false && "AnyInvocable use-after-move"); \
|
||||
std::terminate(); \
|
||||
} \
|
||||
\
|
||||
InvokerType<noex, ReturnType, P...>* ExtractInvoker() cv { \
|
||||
using QualifiedTestType = int cv ref; \
|
||||
auto* invoker = this->invoker_; \
|
||||
if (!std::is_const<QualifiedTestType>::value && \
|
||||
std::is_rvalue_reference<QualifiedTestType>::value) { \
|
||||
ABSL_ASSERT([this]() { \
|
||||
/* We checked that this isn't const above, so const_cast is safe */ \
|
||||
const_cast<Impl*>(this)->invoker_ = InvokedAfterMove; \
|
||||
return this->HasValue(); \
|
||||
}()); \
|
||||
} \
|
||||
return invoker; \
|
||||
} \
|
||||
\
|
||||
/*The actual invocation operation with the proper signature*/ \
|
||||
ReturnType operator()(P... args) cv ref noexcept(noex) { \
|
||||
assert(this->invoker_ != nullptr); \
|
||||
return this->ExtractInvoker()( \
|
||||
const_cast<TypeErasedState*>(&this->state_), \
|
||||
static_cast<ForwardedParameterType<P>>(args)...); \
|
||||
} \
|
||||
}
|
||||
|
||||
// Define the `noexcept(true)` specialization only for C++17 and beyond, when
|
||||
// `noexcept` is part of the type system.
|
||||
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
||||
// A convenience macro that defines specializations for the noexcept(true) and
|
||||
// noexcept(false) forms, given the other properties.
|
||||
#define ABSL_INTERNAL_ANY_INVOCABLE_IMPL(cv, ref, inv_quals) \
|
||||
ABSL_INTERNAL_ANY_INVOCABLE_IMPL_(cv, ref, inv_quals, false); \
|
||||
ABSL_INTERNAL_ANY_INVOCABLE_IMPL_(cv, ref, inv_quals, true)
|
||||
#else
|
||||
#define ABSL_INTERNAL_ANY_INVOCABLE_IMPL(cv, ref, inv_quals) \
|
||||
ABSL_INTERNAL_ANY_INVOCABLE_IMPL_(cv, ref, inv_quals, false)
|
||||
#endif
|
||||
|
||||
// Non-ref-qualified partial specializations
|
||||
ABSL_INTERNAL_ANY_INVOCABLE_IMPL(, , &);
|
||||
ABSL_INTERNAL_ANY_INVOCABLE_IMPL(const, , const&);
|
||||
|
||||
// Lvalue-ref-qualified partial specializations
|
||||
ABSL_INTERNAL_ANY_INVOCABLE_IMPL(, &, &);
|
||||
ABSL_INTERNAL_ANY_INVOCABLE_IMPL(const, &, const&);
|
||||
|
||||
// Rvalue-ref-qualified partial specializations
|
||||
ABSL_INTERNAL_ANY_INVOCABLE_IMPL(, &&, &&);
|
||||
ABSL_INTERNAL_ANY_INVOCABLE_IMPL(const, &&, const&&);
|
||||
|
||||
// Undef the detail-only macros.
|
||||
#undef ABSL_INTERNAL_ANY_INVOCABLE_IMPL
|
||||
#undef ABSL_INTERNAL_ANY_INVOCABLE_IMPL_
|
||||
#undef ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT_false
|
||||
#undef ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT_true
|
||||
#undef ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT
|
||||
#undef ABSL_INTERNAL_NOEXCEPT_SPEC
|
||||
|
||||
} // namespace internal_any_invocable
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_FUNCTIONAL_INTERNAL_ANY_INVOCABLE_H_
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
// Copyright 2018 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Implementation details for `absl::bind_front()`.
|
||||
|
||||
#ifndef ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_
|
||||
#define ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/internal/invoke.h"
|
||||
#include "absl/container/internal/compressed_tuple.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
#include "absl/utility/utility.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace functional_internal {
|
||||
|
||||
// Invoke the method, expanding the tuple of bound arguments.
|
||||
template <class R, class Tuple, size_t... Idx, class... Args>
|
||||
R Apply(Tuple&& bound, absl::index_sequence<Idx...>, Args&&... free) {
|
||||
return base_internal::invoke(
|
||||
std::forward<Tuple>(bound).template get<Idx>()...,
|
||||
std::forward<Args>(free)...);
|
||||
}
|
||||
|
||||
template <class F, class... BoundArgs>
|
||||
class FrontBinder {
|
||||
using BoundArgsT = absl::container_internal::CompressedTuple<F, BoundArgs...>;
|
||||
using Idx = absl::make_index_sequence<sizeof...(BoundArgs) + 1>;
|
||||
|
||||
BoundArgsT bound_args_;
|
||||
|
||||
public:
|
||||
template <class... Ts>
|
||||
constexpr explicit FrontBinder(absl::in_place_t, Ts&&... ts)
|
||||
: bound_args_(std::forward<Ts>(ts)...) {}
|
||||
|
||||
template <class... FreeArgs, class R = base_internal::invoke_result_t<
|
||||
F&, BoundArgs&..., FreeArgs&&...>>
|
||||
R operator()(FreeArgs&&... free_args) & {
|
||||
return functional_internal::Apply<R>(bound_args_, Idx(),
|
||||
std::forward<FreeArgs>(free_args)...);
|
||||
}
|
||||
|
||||
template <class... FreeArgs,
|
||||
class R = base_internal::invoke_result_t<
|
||||
const F&, const BoundArgs&..., FreeArgs&&...>>
|
||||
R operator()(FreeArgs&&... free_args) const& {
|
||||
return functional_internal::Apply<R>(bound_args_, Idx(),
|
||||
std::forward<FreeArgs>(free_args)...);
|
||||
}
|
||||
|
||||
template <class... FreeArgs, class R = base_internal::invoke_result_t<
|
||||
F&&, BoundArgs&&..., FreeArgs&&...>>
|
||||
R operator()(FreeArgs&&... free_args) && {
|
||||
// This overload is called when *this is an rvalue. If some of the bound
|
||||
// arguments are stored by value or rvalue reference, we move them.
|
||||
return functional_internal::Apply<R>(std::move(bound_args_), Idx(),
|
||||
std::forward<FreeArgs>(free_args)...);
|
||||
}
|
||||
|
||||
template <class... FreeArgs,
|
||||
class R = base_internal::invoke_result_t<
|
||||
const F&&, const BoundArgs&&..., FreeArgs&&...>>
|
||||
R operator()(FreeArgs&&... free_args) const&& {
|
||||
// This overload is called when *this is an rvalue. If some of the bound
|
||||
// arguments are stored by value or rvalue reference, we move them.
|
||||
return functional_internal::Apply<R>(std::move(bound_args_), Idx(),
|
||||
std::forward<FreeArgs>(free_args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <class F, class... BoundArgs>
|
||||
using bind_front_t = FrontBinder<decay_t<F>, absl::decay_t<BoundArgs>...>;
|
||||
|
||||
} // namespace functional_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_
|
||||
|
|
@ -0,0 +1,116 @@
|
|||
// Copyright 2019 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_
|
||||
#define ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_
|
||||
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
#include "absl/base/internal/invoke.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace functional_internal {
|
||||
|
||||
// Like a void* that can handle function pointers as well. The standard does not
|
||||
// allow function pointers to round-trip through void*, but void(*)() is fine.
|
||||
//
|
||||
// Note: It's important that this class remains trivial and is the same size as
|
||||
// a pointer, since this allows the compiler to perform tail-call optimizations
|
||||
// when the underlying function is a callable object with a matching signature.
|
||||
union VoidPtr {
|
||||
const void* obj;
|
||||
void (*fun)();
|
||||
};
|
||||
|
||||
// Chooses the best type for passing T as an argument.
|
||||
// Attempt to be close to SystemV AMD64 ABI. Objects with trivial copy ctor are
|
||||
// passed by value.
|
||||
template <typename T,
|
||||
bool IsLValueReference = std::is_lvalue_reference<T>::value>
|
||||
struct PassByValue : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct PassByValue<T, /*IsLValueReference=*/false>
|
||||
: std::integral_constant<bool,
|
||||
absl::is_trivially_copy_constructible<T>::value &&
|
||||
absl::is_trivially_copy_assignable<
|
||||
typename std::remove_cv<T>::type>::value &&
|
||||
std::is_trivially_destructible<T>::value &&
|
||||
sizeof(T) <= 2 * sizeof(void*)> {};
|
||||
|
||||
template <typename T>
|
||||
struct ForwardT : std::conditional<PassByValue<T>::value, T, T&&> {};
|
||||
|
||||
// An Invoker takes a pointer to the type-erased invokable object, followed by
|
||||
// the arguments that the invokable object expects.
|
||||
//
|
||||
// Note: The order of arguments here is an optimization, since member functions
|
||||
// have an implicit "this" pointer as their first argument, putting VoidPtr
|
||||
// first allows the compiler to perform tail-call optimization in many cases.
|
||||
template <typename R, typename... Args>
|
||||
using Invoker = R (*)(VoidPtr, typename ForwardT<Args>::type...);
|
||||
|
||||
//
|
||||
// InvokeObject and InvokeFunction provide static "Invoke" functions that can be
|
||||
// used as Invokers for objects or functions respectively.
|
||||
//
|
||||
// static_cast<R> handles the case the return type is void.
|
||||
template <typename Obj, typename R, typename... Args>
|
||||
R InvokeObject(VoidPtr ptr, typename ForwardT<Args>::type... args) {
|
||||
auto o = static_cast<const Obj*>(ptr.obj);
|
||||
return static_cast<R>(
|
||||
absl::base_internal::invoke(*o, std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
template <typename Fun, typename R, typename... Args>
|
||||
R InvokeFunction(VoidPtr ptr, typename ForwardT<Args>::type... args) {
|
||||
auto f = reinterpret_cast<Fun>(ptr.fun);
|
||||
return static_cast<R>(
|
||||
absl::base_internal::invoke(f, std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
template <typename Sig>
|
||||
void AssertNonNull(const std::function<Sig>& f) {
|
||||
assert(f != nullptr);
|
||||
(void)f;
|
||||
}
|
||||
|
||||
template <typename Sig>
|
||||
void AssertNonNull(const AnyInvocable<Sig>& f) {
|
||||
assert(f != nullptr);
|
||||
(void)f;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
void AssertNonNull(const F&) {}
|
||||
|
||||
template <typename F, typename C>
|
||||
void AssertNonNull(F C::*f) {
|
||||
assert(f != nullptr);
|
||||
(void)f;
|
||||
}
|
||||
|
||||
template <bool C>
|
||||
using EnableIf = typename ::std::enable_if<C, int>::type;
|
||||
|
||||
} // namespace functional_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_FUNCTIONAL_INTERNAL_FUNCTION_REF_H_
|
||||
92
TMessagesProj/jni/voip/webrtc/absl/functional/overload.h
Normal file
92
TMessagesProj/jni/voip/webrtc/absl/functional/overload.h
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
// Copyright 2023 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// File: overload.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// `absl::Overload` is a functor that provides overloads based on the functors
|
||||
// with which it is created. This can, for example, be used to locally define an
|
||||
// anonymous visitor type for `std::visit` inside a function using lambdas.
|
||||
//
|
||||
// Before using this function, consider whether named function overloads would
|
||||
// be a better design.
|
||||
//
|
||||
// Note: absl::Overload requires C++17.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// std::variant<std::string, int32_t, int64_t> v(int32_t{1});
|
||||
// const size_t result =
|
||||
// std::visit(absl::Overload{
|
||||
// [](const std::string& s) { return s.size(); },
|
||||
// [](const auto& s) { return sizeof(s); },
|
||||
// },
|
||||
// v);
|
||||
// assert(result == 4);
|
||||
//
|
||||
|
||||
#ifndef ABSL_FUNCTIONAL_OVERLOAD_H_
|
||||
#define ABSL_FUNCTIONAL_OVERLOAD_H_
|
||||
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
#if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
||||
ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
||||
|
||||
template <typename... T>
|
||||
struct Overload final : T... {
|
||||
using T::operator()...;
|
||||
|
||||
// For historical reasons we want to support use that looks like a function
|
||||
// call:
|
||||
//
|
||||
// absl::Overload(lambda_1, lambda_2)
|
||||
//
|
||||
// This works automatically in C++20 because we have support for parenthesized
|
||||
// aggregate initialization. Before then we must provide a constructor that
|
||||
// makes this work.
|
||||
//
|
||||
constexpr explicit Overload(T... ts) : T(std::move(ts))... {}
|
||||
};
|
||||
|
||||
// Before C++20, which added support for CTAD for aggregate types, we must also
|
||||
// teach the compiler how to deduce the template arguments for Overload.
|
||||
//
|
||||
template <typename... T>
|
||||
Overload(T...) -> Overload<T...>;
|
||||
|
||||
#else
|
||||
|
||||
namespace functional_internal {
|
||||
template <typename T>
|
||||
constexpr bool kDependentFalse = false;
|
||||
}
|
||||
|
||||
template <typename Dependent = int, typename... T>
|
||||
auto Overload(T&&...) {
|
||||
static_assert(functional_internal::kDependentFalse<Dependent>,
|
||||
"Overload is only usable with C++17 or above.");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_FUNCTIONAL_OVERLOAD_H_
|
||||
213
TMessagesProj/jni/voip/webrtc/absl/functional/overload_test.cc
Normal file
213
TMessagesProj/jni/voip/webrtc/absl/functional/overload_test.cc
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
// Copyright 2023 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/functional/overload.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/variant.h"
|
||||
|
||||
#if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
|
||||
ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(OverloadTest, DispatchConsidersTypeWithAutoFallback) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](int v) { return absl::StrCat("int ", v); },
|
||||
[](double v) { return absl::StrCat("double ", v); },
|
||||
[](const char* v) { return absl::StrCat("const char* ", v); },
|
||||
[](auto v) { return absl::StrCat("auto ", v); },
|
||||
};
|
||||
|
||||
EXPECT_EQ("int 1", overloaded(1));
|
||||
EXPECT_EQ("double 2.5", overloaded(2.5));
|
||||
EXPECT_EQ("const char* hello", overloaded("hello"));
|
||||
EXPECT_EQ("auto 1.5", overloaded(1.5f));
|
||||
}
|
||||
|
||||
TEST(OverloadTest, DispatchConsidersNumberOfArguments) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](int a) { return a + 1; },
|
||||
[](int a, int b) { return a * b; },
|
||||
[]() -> absl::string_view { return "none"; },
|
||||
};
|
||||
|
||||
EXPECT_EQ(3, overloaded(2));
|
||||
EXPECT_EQ(21, overloaded(3, 7));
|
||||
EXPECT_EQ("none", overloaded());
|
||||
}
|
||||
|
||||
TEST(OverloadTest, SupportsConstantEvaluation) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](int a) { return a + 1; },
|
||||
[](int a, int b) { return a * b; },
|
||||
[]() -> absl::string_view { return "none"; },
|
||||
};
|
||||
|
||||
static_assert(overloaded() == "none");
|
||||
static_assert(overloaded(2) == 3);
|
||||
static_assert(overloaded(3, 7) == 21);
|
||||
}
|
||||
|
||||
TEST(OverloadTest, PropogatesDefaults) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](int a, int b = 5) { return a * b; },
|
||||
[](double c) { return c; },
|
||||
};
|
||||
|
||||
EXPECT_EQ(21, overloaded(3, 7));
|
||||
EXPECT_EQ(35, overloaded(7));
|
||||
EXPECT_EQ(2.5, overloaded(2.5));
|
||||
}
|
||||
|
||||
TEST(OverloadTest, AmbiguousWithDefaultsNotInvocable) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](int a, int b = 5) { return a * b; },
|
||||
[](int c) { return c; },
|
||||
};
|
||||
|
||||
static_assert(!std::is_invocable_v<decltype(overloaded), int>);
|
||||
static_assert(std::is_invocable_v<decltype(overloaded), int, int>);
|
||||
}
|
||||
|
||||
TEST(OverloadTest, AmbiguousDuplicatesNotInvocable) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](int a) { return a; },
|
||||
[](int c) { return c; },
|
||||
};
|
||||
|
||||
static_assert(!std::is_invocable_v<decltype(overloaded), int>);
|
||||
}
|
||||
|
||||
TEST(OverloadTest, AmbiguousConversionNotInvocable) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](uint16_t a) { return a; },
|
||||
[](uint64_t c) { return c; },
|
||||
};
|
||||
|
||||
static_assert(!std::is_invocable_v<decltype(overloaded), int>);
|
||||
}
|
||||
|
||||
TEST(OverloadTest, AmbiguousConversionWithAutoNotInvocable) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](auto a) { return a; },
|
||||
[](auto c) { return c; },
|
||||
};
|
||||
|
||||
static_assert(!std::is_invocable_v<decltype(overloaded), int>);
|
||||
}
|
||||
|
||||
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
|
||||
|
||||
TEST(OverloadTest, AmbiguousConversionWithAutoAndTemplateNotInvocable) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](auto a) { return a; },
|
||||
[]<class T>(T c) { return c; },
|
||||
};
|
||||
|
||||
static_assert(!std::is_invocable_v<decltype(overloaded), int>);
|
||||
}
|
||||
|
||||
TEST(OverloadTest, DispatchConsidersTypeWithTemplateFallback) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](int a) { return a; },
|
||||
[]<class T>(T c) { return c * 2; },
|
||||
};
|
||||
|
||||
EXPECT_EQ(7, overloaded(7));
|
||||
EXPECT_EQ(14.0, overloaded(7.0));
|
||||
}
|
||||
|
||||
#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
|
||||
|
||||
TEST(OverloadTest, DispatchConsidersSfinae) {
|
||||
auto overloaded = absl::Overload{
|
||||
[](auto a) -> decltype(a + 1) { return a + 1; },
|
||||
};
|
||||
|
||||
static_assert(std::is_invocable_v<decltype(overloaded), int>);
|
||||
static_assert(!std::is_invocable_v<decltype(overloaded), std::string>);
|
||||
}
|
||||
|
||||
TEST(OverloadTest, VariantVisitDispatchesCorrectly) {
|
||||
absl::variant<int, double, std::string> v(1);
|
||||
auto overloaded = absl::Overload{
|
||||
[](int) -> absl::string_view { return "int"; },
|
||||
[](double) -> absl::string_view { return "double"; },
|
||||
[](const std::string&) -> absl::string_view { return "string"; },
|
||||
};
|
||||
|
||||
EXPECT_EQ("int", absl::visit(overloaded, v));
|
||||
v = 1.1;
|
||||
EXPECT_EQ("double", absl::visit(overloaded, v));
|
||||
v = "hello";
|
||||
EXPECT_EQ("string", absl::visit(overloaded, v));
|
||||
}
|
||||
|
||||
TEST(OverloadTest, VariantVisitWithAutoFallbackDispatchesCorrectly) {
|
||||
absl::variant<std::string, int32_t, int64_t> v(int32_t{1});
|
||||
auto overloaded = absl::Overload{
|
||||
[](const std::string& s) { return s.size(); },
|
||||
[](const auto& s) { return sizeof(s); },
|
||||
};
|
||||
|
||||
EXPECT_EQ(4, absl::visit(overloaded, v));
|
||||
v = int64_t{1};
|
||||
EXPECT_EQ(8, absl::visit(overloaded, v));
|
||||
v = std::string("hello");
|
||||
EXPECT_EQ(5, absl::visit(overloaded, v));
|
||||
}
|
||||
|
||||
// This API used to be exported as a function, so it should also work fine to
|
||||
// use parantheses when initializing it.
|
||||
TEST(OverloadTest, UseWithParentheses) {
|
||||
const auto overloaded =
|
||||
absl::Overload([](const std::string& s) { return s.size(); },
|
||||
[](const auto& s) { return sizeof(s); });
|
||||
|
||||
absl::variant<std::string, int32_t, int64_t> v(int32_t{1});
|
||||
EXPECT_EQ(4, absl::visit(overloaded, v));
|
||||
|
||||
v = int64_t{1};
|
||||
EXPECT_EQ(8, absl::visit(overloaded, v));
|
||||
|
||||
v = std::string("hello");
|
||||
EXPECT_EQ(5, absl::visit(overloaded, v));
|
||||
}
|
||||
|
||||
TEST(OverloadTest, HasConstexprConstructor) {
|
||||
constexpr auto overloaded = absl::Overload{
|
||||
[](int v) { return absl::StrCat("int ", v); },
|
||||
[](double v) { return absl::StrCat("double ", v); },
|
||||
[](const char* v) { return absl::StrCat("const char* ", v); },
|
||||
[](auto v) { return absl::StrCat("auto ", v); },
|
||||
};
|
||||
|
||||
EXPECT_EQ("int 1", overloaded(1));
|
||||
EXPECT_EQ("double 2.5", overloaded(2.5));
|
||||
EXPECT_EQ("const char* hello", overloaded("hello"));
|
||||
EXPECT_EQ("auto 1.5", overloaded(1.5f));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue