2 * Copyright 2012 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "folly/ApplyTuple.h"
20 #include <gtest/gtest.h>
27 void func(int a, int b, double c) {
34 void func(int a, int b, double c) {
38 double retVal(int a, double b) {
43 Wat(Wat const&) = delete;
49 int func(int) { return 0; }
50 bool func(bool) { return true; }
54 int operator()() const {
61 CopyCount(CopyCount const&) {
62 std::cout << "copy count copy ctor\n";
66 void anotherFunc(CopyCount const&) {}
68 std::function<void (int, int, double)> makeFunc() {
73 GuardObjBase(GuardObjBase&&) {}
75 GuardObjBase(GuardObjBase const&) = delete;
76 GuardObjBase& operator=(GuardObjBase const&) = delete;
78 typedef GuardObjBase const& Guard;
80 template<class F, class Tuple>
81 struct GuardObj : GuardObjBase {
82 explicit GuardObj(F&& f, Tuple&& args)
84 , args_(std::move(args))
86 GuardObj(GuardObj&& g)
87 : GuardObjBase(std::move(g))
89 , args_(std::move(g.args_))
93 folly::applyTuple(f_, args_);
96 GuardObj(const GuardObj&) = delete;
97 GuardObj& operator=(const GuardObj&) = delete;
104 template<class F, class ...Args>
105 GuardObj<typename std::decay<F>::type,std::tuple<Args...>>
106 guard(F&& f, Args&&... args) {
107 return GuardObj<typename std::decay<F>::type,std::tuple<Args...>>(
109 std::tuple<Args...>(std::forward<Args>(args)...)
116 Mover(const Mover&) = delete;
117 Mover& operator=(const Mover&) = delete;
120 void move_only_func(Mover&&) {}
124 TEST(ApplyTuple, Test) {
125 auto argsTuple = std::make_tuple(1, 2, 3.0);
127 folly::applyTuple(func2, argsTuple);
128 folly::applyTuple(func, argsTuple);
129 folly::applyTuple(func, std::make_tuple(1, 2, 3.0));
130 folly::applyTuple(makeFunc(), std::make_tuple(1, 2, 3.0));
131 folly::applyTuple(makeFunc(), argsTuple);
133 std::unique_ptr<Wat> wat(new Wat);
134 folly::applyTuple(&Wat::func, std::make_tuple(wat.get(), 1, 2, 3.0));
135 auto argsTuple2 = std::make_tuple(wat.get(), 1, 2, 3.0);
136 folly::applyTuple(&Wat::func, argsTuple2);
139 folly::applyTuple(&Wat::retVal,
140 std::make_tuple(wat.get(), 1, 9.0)));
142 auto test = guard(func, 1, 2, 3.0);
144 auto test2 = guard(anotherFunc, cpy);
145 auto test3 = guard(anotherFunc, std::cref(cpy));
150 static_cast<int (Overloaded::*)(int)>(&Overloaded::func),
151 std::make_tuple(&ovl, 12)));
154 static_cast<bool (Overloaded::*)(bool)>(&Overloaded::func),
155 std::make_tuple(&ovl, false)));
157 int x = folly::applyTuple(std::plus<int>(), std::make_tuple(12, 12));
161 folly::applyTuple(move_only_func,
162 std::forward_as_tuple(std::forward<Mover>(Mover())));