1*14675a02SAndroid Build Coastguard Worker /*
2*14675a02SAndroid Build Coastguard Worker * Copyright 2018 Google LLC
3*14675a02SAndroid Build Coastguard Worker *
4*14675a02SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*14675a02SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*14675a02SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*14675a02SAndroid Build Coastguard Worker *
8*14675a02SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*14675a02SAndroid Build Coastguard Worker *
10*14675a02SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*14675a02SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*14675a02SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*14675a02SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*14675a02SAndroid Build Coastguard Worker * limitations under the License.
15*14675a02SAndroid Build Coastguard Worker */
16*14675a02SAndroid Build Coastguard Worker
17*14675a02SAndroid Build Coastguard Worker #ifndef FCP_BASE_MOVE_TO_LAMBDA_H_
18*14675a02SAndroid Build Coastguard Worker #define FCP_BASE_MOVE_TO_LAMBDA_H_
19*14675a02SAndroid Build Coastguard Worker
20*14675a02SAndroid Build Coastguard Worker #include <utility>
21*14675a02SAndroid Build Coastguard Worker
22*14675a02SAndroid Build Coastguard Worker namespace fcp {
23*14675a02SAndroid Build Coastguard Worker
24*14675a02SAndroid Build Coastguard Worker /**
25*14675a02SAndroid Build Coastguard Worker * Copyable wrapper for a move-only value. See MoveToLambda.
26*14675a02SAndroid Build Coastguard Worker * The value is accessible with the * and -> operators.
27*14675a02SAndroid Build Coastguard Worker *
28*14675a02SAndroid Build Coastguard Worker * You must be careful to avoid accidnetal copies of this type. Copies are
29*14675a02SAndroid Build Coastguard Worker * destructive (by design), so accidental copies might lead to using a
30*14675a02SAndroid Build Coastguard Worker */
31*14675a02SAndroid Build Coastguard Worker template <typename T>
32*14675a02SAndroid Build Coastguard Worker class MoveToLambdaWrapper {
33*14675a02SAndroid Build Coastguard Worker public:
MoveToLambdaWrapper(T t)34*14675a02SAndroid Build Coastguard Worker explicit MoveToLambdaWrapper(T t) : value_(std::move(t)) {}
35*14675a02SAndroid Build Coastguard Worker
36*14675a02SAndroid Build Coastguard Worker // The copy and move constructors are intentionally non-const.
37*14675a02SAndroid Build Coastguard Worker
MoveToLambdaWrapper(MoveToLambdaWrapper const & other)38*14675a02SAndroid Build Coastguard Worker MoveToLambdaWrapper(MoveToLambdaWrapper const& other)
39*14675a02SAndroid Build Coastguard Worker : value_(std::move(other.value_)) {}
40*14675a02SAndroid Build Coastguard Worker
41*14675a02SAndroid Build Coastguard Worker MoveToLambdaWrapper& operator=(MoveToLambdaWrapper const& other) {
42*14675a02SAndroid Build Coastguard Worker value_ = std::move(other.value_);
43*14675a02SAndroid Build Coastguard Worker return *this;
44*14675a02SAndroid Build Coastguard Worker }
45*14675a02SAndroid Build Coastguard Worker
46*14675a02SAndroid Build Coastguard Worker // We respect const-ness of the wrapper when dereferencing, so that 'mutable'
47*14675a02SAndroid Build Coastguard Worker // is required on the lambda depending on usage of the value; changes
48*14675a02SAndroid Build Coastguard Worker // to a captured value persist across calls to the lambda, which is rarely
49*14675a02SAndroid Build Coastguard Worker // desired.
50*14675a02SAndroid Build Coastguard Worker
51*14675a02SAndroid Build Coastguard Worker T const& operator*() const & {
52*14675a02SAndroid Build Coastguard Worker return value_;
53*14675a02SAndroid Build Coastguard Worker }
54*14675a02SAndroid Build Coastguard Worker
55*14675a02SAndroid Build Coastguard Worker T const* operator->() const & {
56*14675a02SAndroid Build Coastguard Worker return &value_;
57*14675a02SAndroid Build Coastguard Worker }
58*14675a02SAndroid Build Coastguard Worker
59*14675a02SAndroid Build Coastguard Worker T& operator*() & {
60*14675a02SAndroid Build Coastguard Worker return value_;
61*14675a02SAndroid Build Coastguard Worker }
62*14675a02SAndroid Build Coastguard Worker
63*14675a02SAndroid Build Coastguard Worker T* operator->() & {
64*14675a02SAndroid Build Coastguard Worker return &value_;
65*14675a02SAndroid Build Coastguard Worker }
66*14675a02SAndroid Build Coastguard Worker
67*14675a02SAndroid Build Coastguard Worker private:
68*14675a02SAndroid Build Coastguard Worker mutable T value_;
69*14675a02SAndroid Build Coastguard Worker };
70*14675a02SAndroid Build Coastguard Worker
71*14675a02SAndroid Build Coastguard Worker /**
72*14675a02SAndroid Build Coastguard Worker * Allows capturing a value into a lambda 'by move', before C++14. This is
73*14675a02SAndroid Build Coastguard Worker * implemented by a copyable wrapper, which actually moves its value.
74*14675a02SAndroid Build Coastguard Worker *
75*14675a02SAndroid Build Coastguard Worker * auto moving = MoveToLambda(value);
76*14675a02SAndroid Build Coastguard Worker * DoSometing([moving]{ V const& v = *moving; ... });
77*14675a02SAndroid Build Coastguard Worker */
78*14675a02SAndroid Build Coastguard Worker template <typename T>
MoveToLambda(T && value)79*14675a02SAndroid Build Coastguard Worker MoveToLambdaWrapper<std::remove_reference_t<T>> MoveToLambda(T&& value) {
80*14675a02SAndroid Build Coastguard Worker static_assert(
81*14675a02SAndroid Build Coastguard Worker std::is_rvalue_reference<T&&>::value,
82*14675a02SAndroid Build Coastguard Worker "Expected an rvalue: If the value is copied anyway (to this function), "
83*14675a02SAndroid Build Coastguard Worker "you might as well put it in the lambda-capture list directly.");
84*14675a02SAndroid Build Coastguard Worker return MoveToLambdaWrapper<std::remove_reference_t<T>>(
85*14675a02SAndroid Build Coastguard Worker std::forward<T>(value));
86*14675a02SAndroid Build Coastguard Worker }
87*14675a02SAndroid Build Coastguard Worker
88*14675a02SAndroid Build Coastguard Worker } // namespace fcp
89*14675a02SAndroid Build Coastguard Worker
90*14675a02SAndroid Build Coastguard Worker #endif // FCP_BASE_MOVE_TO_LAMBDA_H_
91