blob: 0c4f0bb93882274a30b0cec8219007daa9bcd213 [file] [log] [blame]
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "flutter/fml/memory/ref_counted.h"
#include "flutter/fml/memory/ref_ptr.h"
namespace fml {
namespace internal {
template <typename T>
class CopyableLambda {
explicit CopyableLambda(T func)
: impl_(MakeRefCounted<Impl>(std::move(func))) {}
template <typename... ArgType>
auto operator()(ArgType&&... args) const {
return impl_->func_(std::forward<ArgType>(args)...);
class Impl : public RefCountedThreadSafe<Impl> {
explicit Impl(T func) : func_(std::move(func)) {}
T func_;
RefPtr<Impl> impl_;
} // namespace internal
// Provides a wrapper for a move-only lambda that is implictly convertable to an
// std::function.
// std::function is copyable, but if a lambda captures an argument with a
// move-only type, the lambda itself is not copyable. In order to use the lambda
// in places that accept std::functions, we provide a copyable object that wraps
// the lambda and is implicitly convertable to an std::function.
// std::unique_ptr<Foo> foo = ...
// std::function<int()> func =
// fml::MakeCopyable([bar = std::move(foo)]() { return bar->count(); });
// Notice that the return type of MakeCopyable is rarely used directly. Instead,
// callers typically erase the type by implicitly converting the return value
// to an std::function.
template <typename T>
internal::CopyableLambda<T> MakeCopyable(T lambda) {
return internal::CopyableLambda<T>(std::move(lambda));
} // namespace fml